From 56a84a41b0475ec06f708c3bf3ceb1cc041950fd Mon Sep 17 00:00:00 2001 From: Jaz Date: Fri, 3 Nov 2023 15:40:35 -0400 Subject: [PATCH 01/11] add a reference builder to reduce logic in a few places --- comments/comments.go | 16 +------ comments/comments_test.go | 16 +++---- diff/diff.go | 39 +++++++--------- diff/diff_test.go | 41 ++++++++--------- flags/builder.go | 95 +++++++++++++++++++++++++++++++++++++++ flags/types.go | 4 +- main.go | 4 +- 7 files changed, 144 insertions(+), 71 deletions(-) create mode 100644 flags/builder.go diff --git a/comments/comments.go b/comments/comments.go index 8213046c..2cef2107 100644 --- a/comments/comments.go +++ b/comments/comments.go @@ -131,9 +131,7 @@ func ProcessFlags(flagsRef lflags.FlagsRef, flags []ldapi.FeatureFlag, config *l // sort keys so hashing can work for checking if comment already exists sort.Strings(addedKeys) for _, flagKey := range addedKeys { - // If flag is in both added and removed then it is being modified - delete(flagsRef.FlagsRemoved, flagKey) - flagAliases := uniqueAliases(flagsRef.FlagsAdded[flagKey]) + flagAliases := flagsRef.FlagsAdded[flagKey] idx, _ := find(flags, flagKey) createComment, err := githubFlagComment(flags[idx], flagAliases, true, config) buildComment.CommentsAdded = append(buildComment.CommentsAdded, createComment) @@ -147,7 +145,7 @@ func ProcessFlags(flagsRef lflags.FlagsRef, flags []ldapi.FeatureFlag, config *l } sort.Strings(removedKeys) for _, flagKey := range removedKeys { - flagAliases := uniqueAliases(flagsRef.FlagsRemoved[flagKey]) + flagAliases := flagsRef.FlagsRemoved[flagKey] idx, _ := find(flags, flagKey) removedComment, err := githubFlagComment(flags[idx], flagAliases, false, config) buildComment.CommentsRemoved = append(buildComment.CommentsRemoved, removedComment) @@ -184,16 +182,6 @@ func uniqueFlagKeys(a, b lflags.FlagAliasMap) []string { return allKeys } -func uniqueAliases(aliases lflags.AliasSet) []string { - flagAliases := make([]string, 0, len(aliases)) - for alias := range aliases { - if len(strings.TrimSpace(alias)) > 0 { - flagAliases = append(flagAliases, alias) - } - } - return flagAliases -} - func pluralize(str string, strLength int) string { tmpl := "%d %s" if strLength != 1 { diff --git a/comments/comments_test.go b/comments/comments_test.go index 1e808af0..580aa886 100644 --- a/comments/comments_test.go +++ b/comments/comments_test.go @@ -196,7 +196,7 @@ func (e *testFlagEnv) ArchivedRemoved(t *testing.T) { } func (e *testCommentBuilder) AddedOnly(t *testing.T) { - e.FlagsRef.FlagsAdded["example-flag"] = lflags.AliasSet{} + e.FlagsRef.FlagsAdded["example-flag"] = []string{} e.Comments.CommentsAdded = []string{"comment1", "comment2"} comment := BuildFlagComment(e.Comments, e.FlagsRef, nil) @@ -205,8 +205,8 @@ func (e *testCommentBuilder) AddedOnly(t *testing.T) { } func (e *testCommentBuilder) RemovedOnly(t *testing.T) { - e.FlagsRef.FlagsRemoved["example-flag"] = lflags.AliasSet{} - e.FlagsRef.FlagsRemoved["sample-flag"] = lflags.AliasSet{} + e.FlagsRef.FlagsRemoved["example-flag"] = []string{} + e.FlagsRef.FlagsRemoved["sample-flag"] = []string{} e.Comments.CommentsRemoved = []string{"comment1", "comment2"} comment := BuildFlagComment(e.Comments, e.FlagsRef, nil) @@ -215,8 +215,8 @@ func (e *testCommentBuilder) RemovedOnly(t *testing.T) { } func (e *testCommentBuilder) AddedAndRemoved(t *testing.T) { - e.FlagsRef.FlagsAdded["example-flag"] = lflags.AliasSet{} - e.FlagsRef.FlagsRemoved["example-flag"] = lflags.AliasSet{} + e.FlagsRef.FlagsAdded["example-flag"] = []string{} + e.FlagsRef.FlagsRemoved["example-flag"] = []string{} e.Comments.CommentsAdded = []string{"comment1", "comment2"} e.Comments.CommentsRemoved = []string{"comment1", "comment2"} comment := BuildFlagComment(e.Comments, e.FlagsRef, nil) @@ -228,7 +228,7 @@ func (e *testCommentBuilder) AddedAndRemoved(t *testing.T) { } func (e *testProcessor) Basic(t *testing.T) { - e.FlagsRef.FlagsAdded["example-flag"] = lflags.AliasSet{"": true} + e.FlagsRef.FlagsAdded["example-flag"] = []string{""} processor := ProcessFlags(e.FlagsRef, e.Flags, &e.Config) expected := FlagComments{ CommentsAdded: []string{"| [example flag](https://example.com/test) | `example-flag` | |"}, @@ -237,8 +237,8 @@ func (e *testProcessor) Basic(t *testing.T) { } func (e *testProcessor) Multi(t *testing.T) { - e.FlagsRef.FlagsAdded["example-flag"] = lflags.AliasSet{"": true} - e.FlagsRef.FlagsAdded["second-flag"] = lflags.AliasSet{"": true} + e.FlagsRef.FlagsAdded["example-flag"] = []string{""} + e.FlagsRef.FlagsAdded["second-flag"] = []string{""} processor := ProcessFlags(e.FlagsRef, e.Flags, &e.Config) expected := FlagComments{ CommentsAdded: []string{ diff --git a/diff/diff.go b/diff/diff.go index 0a7a69b8..0723e33a 100644 --- a/diff/diff.go +++ b/diff/diff.go @@ -50,17 +50,9 @@ func CheckDiff(parsedDiff *diff.FileDiff, workspace string) *DiffPaths { return &diffPaths } -func ProcessDiffs(matcher lsearch.Matcher, hunk *diff.Hunk, flagsRef lflags.FlagsRef, maxFlags int) { - flagMap := map[Operation]lflags.FlagAliasMap{ - Add: flagsRef.FlagsAdded, - Delete: flagsRef.FlagsRemoved, - } +func ProcessDiffs(matcher lsearch.Matcher, hunk *diff.Hunk, builder *lflags.ReferenceBuilder) { diffLines := strings.Split(string(hunk.Body), "\n") for _, line := range diffLines { - if flagsRef.Count() >= maxFlags { - break - } - op := operation(line) if op == Equal { continue @@ -69,21 +61,13 @@ func ProcessDiffs(matcher lsearch.Matcher, hunk *diff.Hunk, flagsRef lflags.Flag // only one for now elementMatcher := matcher.Elements[0] for _, flagKey := range elementMatcher.FindMatches(line) { - if _, ok := flagMap[op][flagKey]; !ok { - flagMap[op][flagKey] = make(lflags.AliasSet) - } - if aliasMatches := matcher.FindAliases(line, flagKey); len(aliasMatches) > 0 { - if _, ok := flagMap[op][flagKey]; !ok { - flagMap[op][flagKey] = make(lflags.AliasSet) - } - for _, alias := range aliasMatches { - flagMap[op][flagKey][alias] = true - } - } + aliasMatches := matcher.FindAliases(line, flagKey) + builder.AddReference(flagKey, op.String(), aliasMatches) + } + if builder.MaxReferences() { + break } } - flagsRef.FlagsAdded = flagMap[Add] - flagsRef.FlagsRemoved = flagMap[Delete] } // Operation defines the operation of a diff item. @@ -108,3 +92,14 @@ func operation(row string) Operation { return Equal } + +func (o Operation) String() string { + switch o { + case Add: + return "+" + case Delete: + return "-" + } + + return "" +} diff --git a/diff/diff_test.go b/diff/diff_test.go index 49024eb1..315e0219 100644 --- a/diff/diff_test.go +++ b/diff/diff_test.go @@ -36,9 +36,9 @@ func createFlag(key string) ldapi.FeatureFlag { } type testProcessor struct { - Flags ldapi.FeatureFlags - FlagsRef lflags.FlagsRef - Config config.Config + Flags ldapi.FeatureFlags + Config config.Config + Builder *lflags.ReferenceBuilder } func (t testProcessor) flagKeys() []string { @@ -55,21 +55,15 @@ func newProcessFlagAccEnv() *testProcessor { flags := ldapi.FeatureFlags{} flags.Items = append(flags.Items, flag) flags.Items = append(flags.Items, flag2) - flagsAdded := make(lflags.FlagAliasMap) - flagsRemoved := make(lflags.FlagAliasMap) - flagsRef := lflags.FlagsRef{ - FlagsAdded: flagsAdded, - FlagsRemoved: flagsRemoved, - } - + builder := lflags.NewReferenceBuilder(5) config := config.Config{ LdEnvironment: "production", LdInstance: "https://example.com/", } return &testProcessor{ - Flags: flags, - FlagsRef: flagsRef, - Config: config, + Flags: flags, + Config: config, + Builder: builder, } } @@ -128,7 +122,7 @@ func TestProcessDiffs(t *testing.T) { { name: "add flag", expected: lflags.FlagsRef{ - FlagsAdded: lflags.FlagAliasMap{"example-flag": lflags.AliasSet{}}, + FlagsAdded: lflags.FlagAliasMap{"example-flag": []string{}}, FlagsRemoved: lflags.FlagAliasMap{}, }, aliases: map[string][]string{}, @@ -145,7 +139,7 @@ func TestProcessDiffs(t *testing.T) { name: "remove flag", expected: lflags.FlagsRef{ FlagsAdded: lflags.FlagAliasMap{}, - FlagsRemoved: lflags.FlagAliasMap{"example-flag": lflags.AliasSet{}}, + FlagsRemoved: lflags.FlagAliasMap{"example-flag": []string{}}, }, aliases: map[string][]string{}, sampleBody: ` @@ -160,8 +154,8 @@ func TestProcessDiffs(t *testing.T) { { name: "add and remove flag", expected: lflags.FlagsRef{ - FlagsAdded: lflags.FlagAliasMap{"sample-flag": lflags.AliasSet{}}, - FlagsRemoved: lflags.FlagAliasMap{"example-flag": lflags.AliasSet{}}, + FlagsAdded: lflags.FlagAliasMap{"sample-flag": []string{}}, + FlagsRemoved: lflags.FlagAliasMap{"example-flag": []string{}}, }, aliases: map[string][]string{}, sampleBody: ` @@ -177,8 +171,8 @@ func TestProcessDiffs(t *testing.T) { { name: "modified flag", expected: lflags.FlagsRef{ - FlagsAdded: lflags.FlagAliasMap{"example-flag": lflags.AliasSet{}}, - FlagsRemoved: lflags.FlagAliasMap{"example-flag": lflags.AliasSet{}}, + FlagsAdded: lflags.FlagAliasMap{"example-flag": []string{}}, + FlagsRemoved: lflags.FlagAliasMap{"example-flag": []string{}}, }, aliases: map[string][]string{}, sampleBody: ` @@ -195,7 +189,7 @@ func TestProcessDiffs(t *testing.T) { { name: "alias flag", expected: lflags.FlagsRef{ - FlagsAdded: lflags.FlagAliasMap{"example-flag": lflags.AliasSet{"exampleFlag": true}}, + FlagsAdded: lflags.FlagAliasMap{"example-flag": []string{"exampleFlag"}}, FlagsRemoved: lflags.FlagAliasMap{}, }, aliases: map[string][]string{"example-flag": {"exampleFlag"}}, @@ -225,7 +219,7 @@ func TestProcessDiffs(t *testing.T) { { name: "require delimiters - match", expected: lflags.FlagsRef{ - FlagsAdded: lflags.FlagAliasMap{"example-flag": lflags.AliasSet{}}, + FlagsAdded: lflags.FlagAliasMap{"example-flag": []string{}}, FlagsRemoved: lflags.FlagAliasMap{}, }, delimiters: "'\"", @@ -255,8 +249,9 @@ func TestProcessDiffs(t *testing.T) { matcher := lsearch.Matcher{ Elements: elements, } - ProcessDiffs(matcher, hunk, processor.FlagsRef, 5) - assert.Equal(t, tc.expected, processor.FlagsRef) + ProcessDiffs(matcher, hunk, processor.Builder) + flagsRef := processor.Builder.Build() + assert.Equal(t, tc.expected, flagsRef) }) } } diff --git a/flags/builder.go b/flags/builder.go new file mode 100644 index 00000000..6457a93f --- /dev/null +++ b/flags/builder.go @@ -0,0 +1,95 @@ +package flags + +import ( + "sort" + "strings" +) + +type ReferenceBuilder struct { + max int // maximum number of flags to find + flagsAdded map[string][]string + flagsRemoved map[string][]string + foundFlags map[string]struct{} +} + +func NewReferenceBuilder(max int) *ReferenceBuilder { + return &ReferenceBuilder{ + flagsAdded: make(map[string][]string), + flagsRemoved: make(map[string][]string), + foundFlags: make(map[string]struct{}), + } +} + +func (b *ReferenceBuilder) MaxReferences() bool { + return len(b.foundFlags) >= b.max +} + +func (b *ReferenceBuilder) AddReference(flagKey string, op string, aliases []string) { + if op == "+" { + b.AddedFlag(flagKey, aliases) + } else if op == "-" { + b.RemovedFlag(flagKey, aliases) + } + // ignore +} + +func (b *ReferenceBuilder) AddedFlag(flagKey string, aliases []string) { + b.foundFlags[flagKey] = struct{}{} + if _, ok := b.flagsAdded[flagKey]; !ok { + b.flagsAdded[flagKey] = make([]string, 0, len(aliases)) + } + b.flagsAdded[flagKey] = append(b.flagsAdded[flagKey], aliases...) +} + +func (b *ReferenceBuilder) RemovedFlag(flagKey string, aliases []string) { + b.foundFlags[flagKey] = struct{}{} + if _, ok := b.flagsRemoved[flagKey]; !ok { + b.flagsRemoved[flagKey] = make([]string, 0, len(aliases)) + } + b.flagsRemoved[flagKey] = append(b.flagsRemoved[flagKey], aliases...) +} + +func (b *ReferenceBuilder) Build() FlagsRef { + added := make(map[string][]string, len(b.flagsAdded)) + removed := make(map[string][]string, len(b.flagsRemoved)) + + for flagKey := range b.foundFlags { + if aliases, ok := b.flagsAdded[flagKey]; ok { + // if there are any removed aliases, we should add them + aliases := append(aliases, b.flagsRemoved[flagKey]...) + aliases = uniqueStrs(aliases) + sort.Strings(aliases) + added[flagKey] = aliases + } else if aliases, ok := b.flagsRemoved[flagKey]; ok { + // only add to removed if it wasn't also added + aliases := uniqueStrs(aliases) + sort.Strings(aliases) + removed[flagKey] = aliases + } + } + + return FlagsRef{ + FlagsAdded: added, + FlagsRemoved: removed, + } +} + +// get slice with unique, non-empty strings +func uniqueStrs(s []string) []string { + if len(s) <= 1 { + return s + } + keys := make(map[string]struct{}, len(s)) + ret := make([]string, 0, len(s)) + for _, elem := range s { + trimmed := strings.TrimSpace(elem) + if len(trimmed) == 0 { + continue + } + if _, ok := keys[trimmed]; !ok { + keys[trimmed] = struct{}{} + ret = append(ret, trimmed) + } + } + return ret +} diff --git a/flags/types.go b/flags/types.go index 0bfc7575..1a6bf28e 100644 --- a/flags/types.go +++ b/flags/types.go @@ -1,8 +1,6 @@ package flags -type FlagAliasMap = map[string]AliasSet - -type AliasSet = map[string]bool +type FlagAliasMap = map[string][]string type FlagsRef struct { FlagsAdded FlagAliasMap diff --git a/main.go b/main.go index fda38edd..ba714848 100644 --- a/main.go +++ b/main.go @@ -59,13 +59,15 @@ func main() { FlagsRemoved: make(lflags.FlagAliasMap), } + builder := lflags.NewReferenceBuilder(config.MaxFlags) + for _, parsedDiff := range multiFiles { getPath := ldiff.CheckDiff(parsedDiff, config.Workspace) if getPath.Skip { continue } for _, hunk := range parsedDiff.Hunks { - ldiff.ProcessDiffs(matcher, hunk, flagsRef, config.MaxFlags) + ldiff.ProcessDiffs(matcher, hunk, builder) } } From 77bf42907676cf72cabc10cc3df0daee869736d8 Mon Sep 17 00:00:00 2001 From: Jaz Date: Fri, 3 Nov 2023 15:53:08 -0400 Subject: [PATCH 02/11] use element matcher --- diff/diff.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diff/diff.go b/diff/diff.go index 0723e33a..57f2da60 100644 --- a/diff/diff.go +++ b/diff/diff.go @@ -61,7 +61,7 @@ func ProcessDiffs(matcher lsearch.Matcher, hunk *diff.Hunk, builder *lflags.Refe // only one for now elementMatcher := matcher.Elements[0] for _, flagKey := range elementMatcher.FindMatches(line) { - aliasMatches := matcher.FindAliases(line, flagKey) + aliasMatches := elementMatcher.FindAliases(line, flagKey) builder.AddReference(flagKey, op.String(), aliasMatches) } if builder.MaxReferences() { From 2e4f104c95c6146ddcc448f9600445294c2ee8ba Mon Sep 17 00:00:00 2001 From: Jaz Date: Fri, 3 Nov 2023 15:53:13 -0400 Subject: [PATCH 03/11] update test --- comments/comments_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/comments/comments_test.go b/comments/comments_test.go index 580aa886..999ab094 100644 --- a/comments/comments_test.go +++ b/comments/comments_test.go @@ -228,7 +228,7 @@ func (e *testCommentBuilder) AddedAndRemoved(t *testing.T) { } func (e *testProcessor) Basic(t *testing.T) { - e.FlagsRef.FlagsAdded["example-flag"] = []string{""} + e.FlagsRef.FlagsAdded["example-flag"] = []string{} processor := ProcessFlags(e.FlagsRef, e.Flags, &e.Config) expected := FlagComments{ CommentsAdded: []string{"| [example flag](https://example.com/test) | `example-flag` | |"}, @@ -237,8 +237,8 @@ func (e *testProcessor) Basic(t *testing.T) { } func (e *testProcessor) Multi(t *testing.T) { - e.FlagsRef.FlagsAdded["example-flag"] = []string{""} - e.FlagsRef.FlagsAdded["second-flag"] = []string{""} + e.FlagsRef.FlagsAdded["example-flag"] = []string{} + e.FlagsRef.FlagsAdded["second-flag"] = []string{} processor := ProcessFlags(e.FlagsRef, e.Flags, &e.Config) expected := FlagComments{ CommentsAdded: []string{ From 59cc1109bfa18cf4b261eee05969e72163af549f Mon Sep 17 00:00:00 2001 From: Jaz Date: Fri, 3 Nov 2023 16:00:43 -0400 Subject: [PATCH 04/11] fix --- diff/diff_test.go | 4 ++-- flags/builder.go | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/diff/diff_test.go b/diff/diff_test.go index 315e0219..59f84e58 100644 --- a/diff/diff_test.go +++ b/diff/diff_test.go @@ -111,7 +111,7 @@ func TestCheckDiff(t *testing.T) { } } -func TestProcessDiffs(t *testing.T) { +func TestProcessDiffs_BuildReferences(t *testing.T) { cases := []struct { name string sampleBody string @@ -172,7 +172,7 @@ func TestProcessDiffs(t *testing.T) { name: "modified flag", expected: lflags.FlagsRef{ FlagsAdded: lflags.FlagAliasMap{"example-flag": []string{}}, - FlagsRemoved: lflags.FlagAliasMap{"example-flag": []string{}}, + FlagsRemoved: lflags.FlagAliasMap{}, }, aliases: map[string][]string{}, sampleBody: ` diff --git a/flags/builder.go b/flags/builder.go index 6457a93f..b718415f 100644 --- a/flags/builder.go +++ b/flags/builder.go @@ -17,6 +17,7 @@ func NewReferenceBuilder(max int) *ReferenceBuilder { flagsAdded: make(map[string][]string), flagsRemoved: make(map[string][]string), foundFlags: make(map[string]struct{}), + max: max, } } From 7522025a3d244e4d6d0cc2114151105e0af1d470 Mon Sep 17 00:00:00 2001 From: Jaz Date: Fri, 3 Nov 2023 16:11:44 -0400 Subject: [PATCH 05/11] use build --- main.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/main.go b/main.go index ba714848..89924d92 100644 --- a/main.go +++ b/main.go @@ -54,13 +54,7 @@ func main() { multiFiles, err := getDiffs(ctx, config, *event.PullRequest.Number) failExit(err) - flagsRef := lflags.FlagsRef{ - FlagsAdded: make(lflags.FlagAliasMap), - FlagsRemoved: make(lflags.FlagAliasMap), - } - builder := lflags.NewReferenceBuilder(config.MaxFlags) - for _, parsedDiff := range multiFiles { getPath := ldiff.CheckDiff(parsedDiff, config.Workspace) if getPath.Skip { @@ -70,6 +64,7 @@ func main() { ldiff.ProcessDiffs(matcher, hunk, builder) } } + flagsRef := builder.Build() // Add comment existingComment := checkExistingComments(event, config, ctx) From 3d82cf8a7c04c660642dc26850d98edda5111702 Mon Sep 17 00:00:00 2001 From: Jaz Date: Fri, 3 Nov 2023 16:12:13 -0400 Subject: [PATCH 06/11] test data --- testdata/test | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/testdata/test b/testdata/test index 6daf878f..05115364 100644 --- a/testdata/test +++ b/testdata/test @@ -1,6 +1,7 @@ show-widgets showWidgets betaUi +betaUi show_widgets beta_ui show-widgets @@ -8,4 +9,4 @@ old-pricing-banner show_widgets betaUi oldPricingBanner -betaUi +oldPricingBanner From 3d5b93d02f9a3fea6e80e4c2e4e8a573b449e8c1 Mon Sep 17 00:00:00 2001 From: Jaz Date: Fri, 3 Nov 2023 16:14:02 -0400 Subject: [PATCH 07/11] remove test data --- testdata/test | 1 - 1 file changed, 1 deletion(-) diff --git a/testdata/test b/testdata/test index 05115364..2ea99814 100644 --- a/testdata/test +++ b/testdata/test @@ -6,7 +6,6 @@ show_widgets beta_ui show-widgets old-pricing-banner -show_widgets betaUi oldPricingBanner oldPricingBanner From ae03e702cd0625acf7b852b9366b2f906f581389 Mon Sep 17 00:00:00 2001 From: Jaz Date: Fri, 3 Nov 2023 16:17:43 -0400 Subject: [PATCH 08/11] up --- diff/diff.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/diff/diff.go b/diff/diff.go index 57f2da60..e3320eae 100644 --- a/diff/diff.go +++ b/diff/diff.go @@ -83,10 +83,10 @@ const ( ) func operation(row string) Operation { - if strings.HasPrefix(row, "+") { + if strings.HasPrefix(row, Add.String()) { return Add } - if strings.HasPrefix(row, "-") { + if strings.HasPrefix(row, Delete.String()) { return Delete } From dd0cafb8d5b2738f5ee64557f2474a62562f9009 Mon Sep 17 00:00:00 2001 From: Jaz Date: Fri, 3 Nov 2023 16:26:41 -0400 Subject: [PATCH 09/11] handle equal --- diff/diff.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/diff/diff.go b/diff/diff.go index e3320eae..c8eb8f3c 100644 --- a/diff/diff.go +++ b/diff/diff.go @@ -99,6 +99,8 @@ func (o Operation) String() string { return "+" case Delete: return "-" + case Equal: + return "" } return "" From 15ba86578f5b2f435546671be86020a4b15393ee Mon Sep 17 00:00:00 2001 From: Jaz Date: Fri, 3 Nov 2023 16:32:49 -0400 Subject: [PATCH 10/11] move operation util to be used elsewhere --- diff/diff.go | 43 ++++-------------------------------- flags/builder.go | 15 +++++++++---- internal/utils/diff_utils.go | 39 ++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 43 deletions(-) create mode 100644 internal/utils/diff_utils.go diff --git a/diff/diff.go b/diff/diff.go index c8eb8f3c..f5be8056 100644 --- a/diff/diff.go +++ b/diff/diff.go @@ -7,6 +7,7 @@ import ( lflags "github.com/launchdarkly/find-code-references-in-pull-request/flags" "github.com/launchdarkly/find-code-references-in-pull-request/ignore" + "github.com/launchdarkly/find-code-references-in-pull-request/internal/utils" lsearch "github.com/launchdarkly/ld-find-code-refs/v2/search" "github.com/sourcegraph/go-diff/diff" ) @@ -53,8 +54,8 @@ func CheckDiff(parsedDiff *diff.FileDiff, workspace string) *DiffPaths { func ProcessDiffs(matcher lsearch.Matcher, hunk *diff.Hunk, builder *lflags.ReferenceBuilder) { diffLines := strings.Split(string(hunk.Body), "\n") for _, line := range diffLines { - op := operation(line) - if op == Equal { + op := utils.LineOperation(line) + if op == utils.OperationEqual { continue } @@ -62,46 +63,10 @@ func ProcessDiffs(matcher lsearch.Matcher, hunk *diff.Hunk, builder *lflags.Refe elementMatcher := matcher.Elements[0] for _, flagKey := range elementMatcher.FindMatches(line) { aliasMatches := elementMatcher.FindAliases(line, flagKey) - builder.AddReference(flagKey, op.String(), aliasMatches) + builder.AddReference(flagKey, op, aliasMatches) } if builder.MaxReferences() { break } } } - -// Operation defines the operation of a diff item. -type Operation int - -const ( - // Equal item represents an equals diff. - Equal Operation = iota - // Add item represents an insert diff. - Add - // Delete item represents a delete diff. - Delete -) - -func operation(row string) Operation { - if strings.HasPrefix(row, Add.String()) { - return Add - } - if strings.HasPrefix(row, Delete.String()) { - return Delete - } - - return Equal -} - -func (o Operation) String() string { - switch o { - case Add: - return "+" - case Delete: - return "-" - case Equal: - return "" - } - - return "" -} diff --git a/flags/builder.go b/flags/builder.go index b718415f..aedc203d 100644 --- a/flags/builder.go +++ b/flags/builder.go @@ -1,8 +1,11 @@ package flags import ( + "fmt" "sort" "strings" + + "github.com/launchdarkly/find-code-references-in-pull-request/internal/utils" ) type ReferenceBuilder struct { @@ -25,13 +28,17 @@ func (b *ReferenceBuilder) MaxReferences() bool { return len(b.foundFlags) >= b.max } -func (b *ReferenceBuilder) AddReference(flagKey string, op string, aliases []string) { - if op == "+" { +func (b *ReferenceBuilder) AddReference(flagKey string, op utils.Operation, aliases []string) error { + switch op { + case utils.OperationAdd: b.AddedFlag(flagKey, aliases) - } else if op == "-" { + case utils.OperationDelete: b.RemovedFlag(flagKey, aliases) + default: + return fmt.Errorf("invalid operation=%s", op.String()) } - // ignore + + return nil } func (b *ReferenceBuilder) AddedFlag(flagKey string, aliases []string) { diff --git a/internal/utils/diff_utils.go b/internal/utils/diff_utils.go new file mode 100644 index 00000000..dce70cc8 --- /dev/null +++ b/internal/utils/diff_utils.go @@ -0,0 +1,39 @@ +package utils + +import "strings" + +// Operation defines the operation of a diff item. +type Operation int + +const ( + // OperationEqual item represents an equals diff. + OperationEqual Operation = iota + // OperationAdd item represents an insert diff. + OperationAdd + // OperationDelete item represents a delete diff. + OperationDelete +) + +func LineOperation(line string) Operation { + if strings.HasPrefix(line, OperationAdd.String()) { + return OperationAdd + } + if strings.HasPrefix(line, OperationDelete.String()) { + return OperationDelete + } + + return OperationEqual +} + +func (o Operation) String() string { + switch o { + case OperationAdd: + return "+" + case OperationDelete: + return "-" + case OperationEqual: + return "" + } + + return "" +} From 3c9ce4ee1c408e003a2f5c5d5deea4e0b4769f42 Mon Sep 17 00:00:00 2001 From: Jaz Date: Fri, 3 Nov 2023 16:39:44 -0400 Subject: [PATCH 11/11] rename --- diff/diff.go | 6 +++--- flags/builder.go | 8 ++++---- .../{utils/diff_utils.go => util/diff_util/operation.go} | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) rename internal/{utils/diff_utils.go => util/diff_util/operation.go} (97%) diff --git a/diff/diff.go b/diff/diff.go index f5be8056..01bd9269 100644 --- a/diff/diff.go +++ b/diff/diff.go @@ -7,7 +7,7 @@ import ( lflags "github.com/launchdarkly/find-code-references-in-pull-request/flags" "github.com/launchdarkly/find-code-references-in-pull-request/ignore" - "github.com/launchdarkly/find-code-references-in-pull-request/internal/utils" + diff_util "github.com/launchdarkly/find-code-references-in-pull-request/internal/util/diff_util" lsearch "github.com/launchdarkly/ld-find-code-refs/v2/search" "github.com/sourcegraph/go-diff/diff" ) @@ -54,8 +54,8 @@ func CheckDiff(parsedDiff *diff.FileDiff, workspace string) *DiffPaths { func ProcessDiffs(matcher lsearch.Matcher, hunk *diff.Hunk, builder *lflags.ReferenceBuilder) { diffLines := strings.Split(string(hunk.Body), "\n") for _, line := range diffLines { - op := utils.LineOperation(line) - if op == utils.OperationEqual { + op := diff_util.LineOperation(line) + if op == diff_util.OperationEqual { continue } diff --git a/flags/builder.go b/flags/builder.go index aedc203d..4e311885 100644 --- a/flags/builder.go +++ b/flags/builder.go @@ -5,7 +5,7 @@ import ( "sort" "strings" - "github.com/launchdarkly/find-code-references-in-pull-request/internal/utils" + diff_util "github.com/launchdarkly/find-code-references-in-pull-request/internal/util/diff_util" ) type ReferenceBuilder struct { @@ -28,11 +28,11 @@ func (b *ReferenceBuilder) MaxReferences() bool { return len(b.foundFlags) >= b.max } -func (b *ReferenceBuilder) AddReference(flagKey string, op utils.Operation, aliases []string) error { +func (b *ReferenceBuilder) AddReference(flagKey string, op diff_util.Operation, aliases []string) error { switch op { - case utils.OperationAdd: + case diff_util.OperationAdd: b.AddedFlag(flagKey, aliases) - case utils.OperationDelete: + case diff_util.OperationDelete: b.RemovedFlag(flagKey, aliases) default: return fmt.Errorf("invalid operation=%s", op.String()) diff --git a/internal/utils/diff_utils.go b/internal/util/diff_util/operation.go similarity index 97% rename from internal/utils/diff_utils.go rename to internal/util/diff_util/operation.go index dce70cc8..1375f066 100644 --- a/internal/utils/diff_utils.go +++ b/internal/util/diff_util/operation.go @@ -1,4 +1,4 @@ -package utils +package diff_util import "strings"