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

fix(#429): Multi-project scans now ignore projects without valid flags #430

Merged
merged 1 commit into from
Feb 13, 2024
Merged
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
25 changes: 13 additions & 12 deletions flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package flags

import (
"fmt"
"os"

"github.com/launchdarkly/ld-find-code-refs/v2/internal/helpers"
"github.com/launchdarkly/ld-find-code-refs/v2/internal/ld"
Expand Down Expand Up @@ -32,18 +31,8 @@ func GetFlagKeys(opts options.Options, repoParams ld.RepoParams) map[string][]st
if err != nil {
helpers.FatalServiceError(fmt.Errorf("could not retrieve flag keys from LaunchDarkly for project `%s`: %w", proj.Key, err), ignoreServiceErrors)
}

filteredFlags, omittedFlags := filterShortFlagKeys(flags)
if len(filteredFlags) == 0 {
log.Info.Printf("no flag keys longer than the minimum flag key length (%v) were found for project: %s, exiting early",
minFlagKeyLen, proj.Key)
os.Exit(0)
} else if len(omittedFlags) > 0 {
log.Warning.Printf("omitting %d flags with keys less than minimum (%d) for project: %s", len(omittedFlags), minFlagKeyLen, proj.Key)
}
flagKeys[proj.Key] = filteredFlags
addFlagKeys(flagKeys, flags, proj.Key)
}

return flagKeys
}

Expand All @@ -62,6 +51,18 @@ func filterShortFlagKeys(flags []string) (filtered []string, omitted []string) {
return filteredFlags, omittedFlags
}

func addFlagKeys(flagKeys map[string][]string, flags []string, projKey string) {
filteredFlags, omittedFlags := filterShortFlagKeys(flags)
if len(filteredFlags) == 0 {
log.Warning.Printf("no flag keys longer than the minimum flag key length (%v) were found for project: %s. Skipping project",
minFlagKeyLen, projKey)
return
} else if len(omittedFlags) > 0 {
log.Warning.Printf("omitting %d flags with keys less than minimum (%d) for project: %s", len(omittedFlags), minFlagKeyLen, projKey)
}
flagKeys[projKey] = filteredFlags
}

func getFlags(ldApi ld.ApiClient, projKey string) ([]string, error) {
flags, err := ldApi.GetFlagKeyList(projKey)
if err != nil {
Expand Down
61 changes: 61 additions & 0 deletions flags/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,64 @@ func Test_filterShortFlags(t *testing.T) {
})
}
}

func Test_addFlagKeys(t *testing.T) {
// Note: these specs assume minFlagKeyLen is 3
tests := []struct {
name string
flagKeys map[string][]string
flags []string
projKey string
want map[string][]string
}{
{
name: "With a project that contains no flags",
flags: []string{},
projKey: "test_project_1",
flagKeys: map[string][]string{},
want: map[string][]string{},
},
{
name: "With a project that contains no flags and already existing keys",
flags: []string{},
projKey: "test_project_1",
flagKeys: map[string][]string{"test_project_2": {"test_key"}},
want: map[string][]string{"test_project_2": {"test_key"}},
},
{
name: "With a project that contains a flag and already existing keys",
flags: []string{"test_project_key"},
projKey: "test_project_1",
flagKeys: map[string][]string{"test_project_2": {"test_key"}},
want: map[string][]string{"test_project_2": {"test_key"}, "test_project_1": {"test_project_key"}},
},
{
name: "With a project that contains multiple flags and already existing keys",
flags: []string{"test_project_key", "test_project_key_2"},
projKey: "test_project_1",
flagKeys: map[string][]string{"test_project_2": {"test_key"}},
want: map[string][]string{"test_project_2": {"test_key"}, "test_project_1": {"test_project_key", "test_project_key_2"}},
},
{
name: "With a project that contains a short and log flags",
flags: []string{"test_project_key", "t"},
projKey: "test_project_1",
flagKeys: map[string][]string{"test_project_2": {"test_key"}},
want: map[string][]string{"test_project_2": {"test_key"}, "test_project_1": {"test_project_key"}},
},
{
name: "With a project that contains a short flag",
flags: []string{"k"},
projKey: "test_project_1",
flagKeys: map[string][]string{"test_project_2": {"test_key"}},
want: map[string][]string{"test_project_2": {"test_key"}},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
addFlagKeys(tt.flagKeys, tt.flags, tt.projKey)
require.Equal(t, tt.want, tt.flagKeys)
})
}
}