Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add log lines #91

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .ldignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
testDir/*.js
vendor
8 changes: 4 additions & 4 deletions comments/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"html"
"html/template"
"log"
"reflect"
"sort"
"strings"
Expand All @@ -18,6 +17,7 @@ import (
"github.com/google/go-github/github"
ldapi "github.com/launchdarkly/api-client-go/v13"
lcr "github.com/launchdarkly/find-code-references-in-pull-request/config"
gha "github.com/launchdarkly/find-code-references-in-pull-request/internal/github_actions"
refs "github.com/launchdarkly/find-code-references-in-pull-request/internal/references"
)

Expand Down Expand Up @@ -113,7 +113,7 @@ func BuildFlagComment(buildComment FlagComments, flagsRef refs.ReferenceSummary,

hash := md5.Sum([]byte(postedComments))
if existingComment != nil && strings.Contains(*existingComment.Body, hex.EncodeToString(hash[:])) {
log.Println("comment already exists")
gha.Log("comment already exists")
return ""
}

Expand All @@ -129,7 +129,7 @@ func ProcessFlags(flagsRef refs.ReferenceSummary, flags []ldapi.FeatureFlag, con
idx, _ := find(flags, flagKey)
createComment, err := githubFlagComment(flags[idx], flagAliases, true, false, config)
if err != nil {
log.Println(err)
gha.LogError(err)
}
buildComment.CommentsAdded = append(buildComment.CommentsAdded, createComment)
}
Expand All @@ -144,7 +144,7 @@ func ProcessFlags(flagsRef refs.ReferenceSummary, flags []ldapi.FeatureFlag, con
}
removedComment, err := githubFlagComment(flags[idx], flagAliases, false, extinct, config)
if err != nil {
log.Println(err)
gha.LogError(err)
}
buildComment.CommentsRemoved = append(buildComment.CommentsRemoved, removedComment)
}
Expand Down
7 changes: 4 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package config
import (
"context"
"errors"
"fmt"
"os"
"strconv"
"strings"

"github.com/google/go-github/github"
"golang.org/x/oauth2"

gha "github.com/launchdarkly/find-code-references-in-pull-request/internal/github_actions"
)

type Config struct {
Expand All @@ -30,10 +31,10 @@ type Config struct {
func ValidateInputandParse(ctx context.Context) (*Config, error) {
// mask tokens
if accessToken := os.Getenv("INPUT_ACCESS-TOKEN"); len(accessToken) > 0 {
fmt.Printf("::add-mask::%s\n", accessToken)
gha.MaskInput(accessToken)
}
if repoToken := os.Getenv("INPUT_REPO-TOKEN"); len(repoToken) > 0 {
fmt.Printf("::add-mask::%s\n", repoToken)
gha.MaskInput(repoToken)
}

// init config with defaults
Expand Down
2 changes: 2 additions & 0 deletions diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

i "github.com/launchdarkly/find-code-references-in-pull-request/ignore"
gha "github.com/launchdarkly/find-code-references-in-pull-request/internal/github_actions"
refs "github.com/launchdarkly/find-code-references-in-pull-request/internal/references"
diff_util "github.com/launchdarkly/find-code-references-in-pull-request/internal/utils/diff_util"
"github.com/launchdarkly/ld-find-code-refs/v2/aliases"
Expand Down Expand Up @@ -79,6 +80,7 @@ func ProcessDiffs(matcher lsearch.Matcher, contents []byte, builder *refs.Refere
elementMatcher := matcher.Elements[0]
for _, flagKey := range elementMatcher.FindMatches(line) {
aliasMatches := elementMatcher.FindAliases(line, flagKey)
gha.Debug("Found (%s) reference to flag %s with aliases %v", op, flagKey, aliasMatches)
builder.AddReference(flagKey, op, aliasMatches)
}
if builder.MaxReferences() {
Expand Down
24 changes: 15 additions & 9 deletions internal/aliases/aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,35 @@ import (
"github.com/launchdarkly/ld-find-code-refs/v2/aliases"
"github.com/launchdarkly/ld-find-code-refs/v2/options"

gha "github.com/launchdarkly/find-code-references-in-pull-request/internal/github_actions"
"github.com/launchdarkly/find-code-references-in-pull-request/internal/utils"
)

// Generate aliases, making sure to identify aliases in the removed diff contents
func GenerateAliases(opts options.Options, flagKeys []string, diffContents aliases.FileContentsMap) (map[string][]string, error) {
gha.Log("Generating aliases...")
aliasesByFlagKey, err := aliases.GenerateAliases(flagKeys, opts.Aliases, opts.Dir)
if err != nil {
return nil, err
}

filePatternAliases := getFilepatternAliases(opts.Aliases)
for _, flag := range flagKeys {
for _, alias := range filePatternAliases {
aliases, err := aliases.GenerateAliasesFromFilePattern(alias, flag, opts.Dir, diffContents)
if err != nil {
// skip aliases that fail to generate
continue
if len(diffContents) > 0 {
gha.Debug("Generating aliases for removed files...")
filePatternAliases := getFilepatternAliases(opts.Aliases)
for _, flag := range flagKeys {
for _, alias := range filePatternAliases {
aliases, err := aliases.GenerateAliasesFromFilePattern(alias, flag, opts.Dir, diffContents)
if err != nil {
// skip aliases that fail to generate
continue
}
aliasesByFlagKey[flag] = append(aliasesByFlagKey[flag], aliases...)
}
aliasesByFlagKey[flag] = append(aliasesByFlagKey[flag], aliases...)
aliasesByFlagKey[flag] = utils.Dedupe(aliasesByFlagKey[flag])
}
aliasesByFlagKey[flag] = utils.Dedupe(aliasesByFlagKey[flag])
}

gha.Log("Finished generating aliases...")
return aliasesByFlagKey, nil
}

Expand Down
12 changes: 10 additions & 2 deletions internal/extinctions/extinctions.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
package extinctions

import (
gha "github.com/launchdarkly/find-code-references-in-pull-request/internal/github_actions"
refs "github.com/launchdarkly/find-code-references-in-pull-request/internal/references"
"github.com/launchdarkly/find-code-references-in-pull-request/search"
"github.com/launchdarkly/ld-find-code-refs/v2/options"
ld_search "github.com/launchdarkly/ld-find-code-refs/v2/search"
)

func CheckExtinctions(opts options.Options, builder *refs.ReferenceSummaryBuilder) error {
flagKeys := make([]string, 0, len(builder.RemovedFlagKeys()))
flagKeys := builder.RemovedFlagKeys()
if len(flagKeys) == 0 {
return nil
}
gha.StartLogGroup("Checking for extinctions...")
defer gha.EndLogGroup()

matcher, err := search.GetMatcher(opts, flagKeys, nil)
if err != nil {
return err
}

gha.Debug("Searching for any remaining references to %d removed flags...", len(flagKeys))
references, err := ld_search.SearchForRefs(opts.Dir, matcher)
if err != nil {
return err
}
gha.Debug("Found %d references to removed flags", len(references))

for _, ref := range references {
for _, hunk := range ref.Hunks {
gha.Debug("Flag '%s' is not extinct", hunk.FlagKey)
builder.AddHeadFlag(hunk.FlagKey)
}
}

return nil
}
40 changes: 33 additions & 7 deletions internal/github_actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ package github_actions

import (
"fmt"
"log"
"os"
)

func SetOutputOrLogError(name, value string) {
if err := SetOutput(name, value); err != nil {
LogError("Failed to set outputs.%s\n", name)
func SetOutput(name, value string) {
if err := setOutput(name, value); err != nil {
SetError("Failed to set outputs.%s\n", name)
}
}

func SetOutput(name, value string) error {
func setOutput(name, value string) error {
Debug("setting output %s=%s", name, value)
output := os.Getenv("GITHUB_OUTPUT")

f, err := os.OpenFile(output, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
Expand All @@ -23,14 +25,38 @@ func SetOutput(name, value string) error {
return err
}

func LogNotice(format string, a ...any) {
func MaskInput(input string) {
fmt.Printf("::add-mask::%s\n", input)
}

func Log(format string, a ...any) {
log.Println(fmt.Sprintf(format, a...))
}

func LogError(err error) {
log.Println(err)
}

func SetNotice(format string, a ...any) {
fmt.Printf("::notice::%s\n", fmt.Sprintf(format, a...))
}

func LogWarning(format string, a ...any) {
func SetWarning(format string, a ...any) {
fmt.Printf("::warning::%s\n", fmt.Sprintf(format, a...))
}

func LogError(format string, a ...any) {
func SetError(format string, a ...any) {
fmt.Printf("::error::%s\n", fmt.Sprintf(format, a...))
}

func Debug(format string, a ...any) {
fmt.Printf("::debug::%s\n", fmt.Sprintf(format, a...))
}

func StartLogGroup(format string, a ...any) {
fmt.Printf("::group::%s\n", fmt.Sprintf(format, a...))
}

func EndLogGroup() {
fmt.Println("::endgroup::")
}
3 changes: 3 additions & 0 deletions internal/ldclient/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (

ldapi "github.com/launchdarkly/api-client-go/v13"
lcr "github.com/launchdarkly/find-code-references-in-pull-request/config"
gha "github.com/launchdarkly/find-code-references-in-pull-request/internal/github_actions"
"github.com/launchdarkly/find-code-references-in-pull-request/internal/version"
"github.com/pkg/errors"
)

func GetAllFlags(config *lcr.Config) ([]ldapi.FeatureFlag, error) {
gha.Debug("Fetching all flags for project")
params := url.Values{}
params.Add("env", config.LdEnvironment)
activeFlags, err := getFlags(config, params)
Expand All @@ -32,6 +34,7 @@ func GetAllFlags(config *lcr.Config) ([]ldapi.FeatureFlag, error) {
flags = append(flags, archivedFlags...)
}

gha.Debug("Fetched %d flags", len(flags))
return flags, nil
}

Expand Down
66 changes: 66 additions & 0 deletions internal/references/builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package flags

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestBuilder_Build(t *testing.T) {
ref := ReferenceSummaryBuilder{
flagsAdded: map[string][]string{
"flag1": {"alias1"},
"flag2": {"alias2"},
},
flagsRemoved: map[string][]string{
"flag2": {},
"flag3": {"alias3"},
"flag4": {"alias4"},
},
flagsFoundAtHead: map[string]struct{}{
"flag3": {},
},
foundFlags: map[string]struct{}{
"flag1": {},
"flag2": {},
"flag3": {},
"flag4": {},
},
includeExtinctions: true,
}

built := ref.Build()

assert.Len(t, built.FlagsAdded, 2)
assert.Len(t, built.FlagsRemoved, 2)
assert.Len(t, built.ExtinctFlags, 1)
}

func TestBuilder_RemovedFlagKeys(t *testing.T) {
ref := ReferenceSummaryBuilder{
flagsAdded: map[string][]string{
"flag1": {"alias1"},
"flag2": {"alias2"},
},
flagsRemoved: map[string][]string{
"flag2": {},
"flag3": {"alias3"},
"flag4": {"alias4"},
},
flagsFoundAtHead: map[string]struct{}{
"flag3": {},
},
foundFlags: map[string]struct{}{
"flag1": {},
"flag2": {},
"flag3": {},
"flag4": {},
},
includeExtinctions: true,
}

keys := ref.RemovedFlagKeys()

assert.Len(t, keys, 3)
assert.ElementsMatch(t, keys, []string{"flag2", "flag3", "flag4"})
}
Loading