Skip to content

Commit 6dca539

Browse files
authored
fix(#429): Multi-project scans now ignore projects without valid flags (#430)
### Description - Addresses issue #429. - Projects without invalid flags will be ignored. A log message is written when this occurs.
1 parent 3900e92 commit 6dca539

File tree

2 files changed

+74
-12
lines changed

2 files changed

+74
-12
lines changed

flags/flags.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package flags
22

33
import (
44
"fmt"
5-
"os"
65

76
"github.com/launchdarkly/ld-find-code-refs/v2/internal/helpers"
87
"github.com/launchdarkly/ld-find-code-refs/v2/internal/ld"
@@ -32,18 +31,8 @@ func GetFlagKeys(opts options.Options, repoParams ld.RepoParams) map[string][]st
3231
if err != nil {
3332
helpers.FatalServiceError(fmt.Errorf("could not retrieve flag keys from LaunchDarkly for project `%s`: %w", proj.Key, err), ignoreServiceErrors)
3433
}
35-
36-
filteredFlags, omittedFlags := filterShortFlagKeys(flags)
37-
if len(filteredFlags) == 0 {
38-
log.Info.Printf("no flag keys longer than the minimum flag key length (%v) were found for project: %s, exiting early",
39-
minFlagKeyLen, proj.Key)
40-
os.Exit(0)
41-
} else if len(omittedFlags) > 0 {
42-
log.Warning.Printf("omitting %d flags with keys less than minimum (%d) for project: %s", len(omittedFlags), minFlagKeyLen, proj.Key)
43-
}
44-
flagKeys[proj.Key] = filteredFlags
34+
addFlagKeys(flagKeys, flags, proj.Key)
4535
}
46-
4736
return flagKeys
4837
}
4938

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

54+
func addFlagKeys(flagKeys map[string][]string, flags []string, projKey string) {
55+
filteredFlags, omittedFlags := filterShortFlagKeys(flags)
56+
if len(filteredFlags) == 0 {
57+
log.Warning.Printf("no flag keys longer than the minimum flag key length (%v) were found for project: %s. Skipping project",
58+
minFlagKeyLen, projKey)
59+
return
60+
} else if len(omittedFlags) > 0 {
61+
log.Warning.Printf("omitting %d flags with keys less than minimum (%d) for project: %s", len(omittedFlags), minFlagKeyLen, projKey)
62+
}
63+
flagKeys[projKey] = filteredFlags
64+
}
65+
6566
func getFlags(ldApi ld.ApiClient, projKey string) ([]string, error) {
6667
flags, err := ldApi.GetFlagKeyList(projKey)
6768
if err != nil {

flags/flags_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,64 @@ func Test_filterShortFlags(t *testing.T) {
5353
})
5454
}
5555
}
56+
57+
func Test_addFlagKeys(t *testing.T) {
58+
// Note: these specs assume minFlagKeyLen is 3
59+
tests := []struct {
60+
name string
61+
flagKeys map[string][]string
62+
flags []string
63+
projKey string
64+
want map[string][]string
65+
}{
66+
{
67+
name: "With a project that contains no flags",
68+
flags: []string{},
69+
projKey: "test_project_1",
70+
flagKeys: map[string][]string{},
71+
want: map[string][]string{},
72+
},
73+
{
74+
name: "With a project that contains no flags and already existing keys",
75+
flags: []string{},
76+
projKey: "test_project_1",
77+
flagKeys: map[string][]string{"test_project_2": {"test_key"}},
78+
want: map[string][]string{"test_project_2": {"test_key"}},
79+
},
80+
{
81+
name: "With a project that contains a flag and already existing keys",
82+
flags: []string{"test_project_key"},
83+
projKey: "test_project_1",
84+
flagKeys: map[string][]string{"test_project_2": {"test_key"}},
85+
want: map[string][]string{"test_project_2": {"test_key"}, "test_project_1": {"test_project_key"}},
86+
},
87+
{
88+
name: "With a project that contains multiple flags and already existing keys",
89+
flags: []string{"test_project_key", "test_project_key_2"},
90+
projKey: "test_project_1",
91+
flagKeys: map[string][]string{"test_project_2": {"test_key"}},
92+
want: map[string][]string{"test_project_2": {"test_key"}, "test_project_1": {"test_project_key", "test_project_key_2"}},
93+
},
94+
{
95+
name: "With a project that contains a short and log flags",
96+
flags: []string{"test_project_key", "t"},
97+
projKey: "test_project_1",
98+
flagKeys: map[string][]string{"test_project_2": {"test_key"}},
99+
want: map[string][]string{"test_project_2": {"test_key"}, "test_project_1": {"test_project_key"}},
100+
},
101+
{
102+
name: "With a project that contains a short flag",
103+
flags: []string{"k"},
104+
projKey: "test_project_1",
105+
flagKeys: map[string][]string{"test_project_2": {"test_key"}},
106+
want: map[string][]string{"test_project_2": {"test_key"}},
107+
},
108+
}
109+
110+
for _, tt := range tests {
111+
t.Run(tt.name, func(t *testing.T) {
112+
addFlagKeys(tt.flagKeys, tt.flags, tt.projKey)
113+
require.Equal(t, tt.want, tt.flagKeys)
114+
})
115+
}
116+
}

0 commit comments

Comments
 (0)