From 9abd9472b1ceeb7ae15915c7f7bd27c6d20a473d Mon Sep 17 00:00:00 2001 From: Jaz White Date: Wed, 2 Aug 2023 15:35:02 -0400 Subject: [PATCH] feat: only allow one env key in configuration (#41) Now that we don't add env configuration info in the comment, limit to one env key Other changes - don't fetch entire flag config for environment when we only need site link - refactor usage of flags/flagKeys --- README.md | 2 +- action.yml | 2 +- comments/comments.go | 30 ++++++++++++++---------------- comments/comments_test.go | 13 ++++++------- config/config.go | 4 ++-- diff/diff_test.go | 2 +- main.go | 30 +++++++++--------------------- search/search.go | 9 +++++++-- 8 files changed, 41 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index f1a33f04..d4392fd0 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ jobs: | repo-token | Token to use to authorize comments on PR. Typically the GITHUB_TOKEN secret. | `true` | | | access-token | LaunchDarkly access token | `true` | | | project-key | LaunchDarkly Project | `false` | default | -| environment-key | LaunchDarkly environment for setting flag links. Can be a comma-separated list of environment keys. | `false` | production | +| environment-key | LaunchDarkly environment for creating flag links | `false` | production | | placeholder-comment | Comment even if no flags are found. If flags are found in later commits this comment will be updated. | `false` | false | | max-flags | Maximum number of flags to find per PR | `false` | 5 | | base-uri | The base URI for the LaunchDarkly server. Most users should use the default value. | `false` | https://app.launchdarkly.com | diff --git a/action.yml b/action.yml index b743b235..724af730 100644 --- a/action.yml +++ b/action.yml @@ -22,7 +22,7 @@ inputs: required: false default: 'default' environment-key: - description: 'LaunchDarkly environment for setting flag links. Can be a comma-separated list of environment keys.' + description: 'LaunchDarkly environment for creating flag links' required: false default: 'production' placeholder-comment: diff --git a/comments/comments.go b/comments/comments.go index 4b46688c..984c2bbc 100644 --- a/comments/comments.go +++ b/comments/comments.go @@ -22,12 +22,11 @@ import ( ) type Comment struct { - Flag ldapi.FeatureFlag - Aliases []string - ChangeType string - Primary ldapi.FeatureFlagConfig - Environments map[string]ldapi.FeatureFlagConfig - LDInstance string + Flag ldapi.FeatureFlag + Aliases []string + ChangeType string + Primary ldapi.FeatureFlagConfig + LDInstance string } func isNil(a interface{}) bool { @@ -37,11 +36,10 @@ func isNil(a interface{}) bool { func githubFlagComment(flag ldapi.FeatureFlag, aliases []string, config *config.Config) (string, error) { commentTemplate := Comment{ - Flag: flag, - Aliases: aliases, - Primary: flag.Environments[config.LdEnvironment[0]], - Environments: flag.Environments, - LDInstance: config.LdInstance, + Flag: flag, + Aliases: aliases, + Primary: flag.Environments[config.LdEnvironment], + LDInstance: config.LdInstance, } var commentBody bytes.Buffer // All whitespace for template is required to be there or it will not render properly nested. @@ -112,7 +110,7 @@ func BuildFlagComment(buildComment FlagComments, flagsRef lflags.FlagsRef, exist return postedComments } -func ProcessFlags(flagsRef lflags.FlagsRef, flags ldapi.FeatureFlags, config *lcr.Config) FlagComments { +func ProcessFlags(flagsRef lflags.FlagsRef, flags []ldapi.FeatureFlag, config *lcr.Config) FlagComments { buildComment := FlagComments{} addedKeys := make([]string, 0, len(flagsRef.FlagsAdded)) for key := range flagsRef.FlagsAdded { @@ -124,8 +122,8 @@ func ProcessFlags(flagsRef lflags.FlagsRef, flags ldapi.FeatureFlags, config *lc // If flag is in both added and removed then it is being modified delete(flagsRef.FlagsRemoved, flagKey) flagAliases := uniqueAliases(flagsRef.FlagsAdded[flagKey]) - idx, _ := find(flags.Items, flagKey) - createComment, err := githubFlagComment(flags.Items[idx], flagAliases, config) + idx, _ := find(flags, flagKey) + createComment, err := githubFlagComment(flags[idx], flagAliases, config) buildComment.CommentsAdded = append(buildComment.CommentsAdded, createComment) if err != nil { log.Println(err) @@ -138,8 +136,8 @@ func ProcessFlags(flagsRef lflags.FlagsRef, flags ldapi.FeatureFlags, config *lc sort.Strings(removedKeys) for _, flagKey := range removedKeys { flagAliases := uniqueAliases(flagsRef.FlagsRemoved[flagKey]) - idx, _ := find(flags.Items, flagKey) - removedComment, err := githubFlagComment(flags.Items[idx], flagAliases, config) + idx, _ := find(flags, flagKey) + removedComment, err := githubFlagComment(flags[idx], flagAliases, config) buildComment.CommentsRemoved = append(buildComment.CommentsRemoved, removedComment) if err != nil { log.Println(err) diff --git a/comments/comments_test.go b/comments/comments_test.go index fcf7fc11..942a8698 100644 --- a/comments/comments_test.go +++ b/comments/comments_test.go @@ -22,7 +22,7 @@ func newTestAccEnv() *testFlagEnv { flag := createFlag("example-flag") config := config.Config{ - LdEnvironment: []string{"production"}, + LdEnvironment: "production", LdInstance: "https://example.com/", } return &testFlagEnv{ @@ -79,15 +79,14 @@ func newCommentBuilderAccEnv() *testCommentBuilder { } type testProcessor struct { - Flags ldapi.FeatureFlags + Flags []ldapi.FeatureFlag FlagsRef lflags.FlagsRef Config config.Config } func newProcessFlagAccEnv() *testProcessor { flag := createFlag("example-flag") - flags := ldapi.FeatureFlags{} - flags.Items = append(flags.Items, flag) + flags := []ldapi.FeatureFlag{flag} flagsAdded := make(lflags.FlagAliasMap) flagsRemoved := make(lflags.FlagAliasMap) flagsRef := lflags.FlagsRef{ @@ -96,7 +95,7 @@ func newProcessFlagAccEnv() *testProcessor { } config := config.Config{ - LdEnvironment: []string{"production"}, + LdEnvironment: "production", LdInstance: "https://example.com/", } return &testProcessor{ @@ -109,7 +108,7 @@ func newProcessFlagAccEnv() *testProcessor { func newProcessMultipleFlagsFlagAccEnv() *testProcessor { flag := createFlag("example-flag") flag2 := createFlag("second-flag") - flags := ldapi.FeatureFlags{Items: []ldapi.FeatureFlag{flag, flag2}} + flags := []ldapi.FeatureFlag{flag, flag2} flagsAdded := make(lflags.FlagAliasMap) flagsRemoved := make(lflags.FlagAliasMap) flagsRef := lflags.FlagsRef{ @@ -118,7 +117,7 @@ func newProcessMultipleFlagsFlagAccEnv() *testProcessor { } config := config.Config{ - LdEnvironment: []string{"production"}, + LdEnvironment: "production", LdInstance: "https://example.com/", } return &testProcessor{ diff --git a/config/config.go b/config/config.go index 9df251d1..a2ca58a6 100644 --- a/config/config.go +++ b/config/config.go @@ -14,7 +14,7 @@ import ( type Config struct { LdProject string - LdEnvironment []string + LdEnvironment string LdInstance string Owner string Repo string @@ -40,7 +40,7 @@ func ValidateInputandParse(ctx context.Context) (*Config, error) { if config.LdProject == "" { return nil, errors.New("`project-key` is required") } - config.LdEnvironment = strings.Split(os.Getenv("INPUT_ENVIRONMENT-KEY"), ",") + config.LdEnvironment = os.Getenv("INPUT_ENVIRONMENT-KEY") if len(config.LdEnvironment) == 0 { return nil, errors.New("`environment-key` is required") } diff --git a/diff/diff_test.go b/diff/diff_test.go index 1f4b0ffc..6304bbff 100644 --- a/diff/diff_test.go +++ b/diff/diff_test.go @@ -68,7 +68,7 @@ func newProcessFlagAccEnv() *testProcessor { } config := config.Config{ - LdEnvironment: []string{"production"}, + LdEnvironment: "production", LdInstance: "https://example.com/", } return &testProcessor{ diff --git a/main.go b/main.go index 656ecb31..78860d58 100644 --- a/main.go +++ b/main.go @@ -37,10 +37,10 @@ func main() { } // Query for flags - flags, flagKeys, err := getFlags(config) + flags, err := getFlags(config) failExit(err) - if len(flags.Items) == 0 { + if len(flags) == 0 { log.Println("No flags found") os.Exit(0) } @@ -48,7 +48,7 @@ func main() { opts, err := getOptions(config) failExit(err) - matcher, err := search.GetMatcher(config, opts, flagKeys) + matcher, err := search.GetMatcher(config, opts, flags) failExit(err) multiFiles, err := getDiffs(ctx, config, *event.PullRequest.Number) @@ -87,40 +87,28 @@ func main() { failExit(err) } -func getFlags(config *lcr.Config) (ldapi.FeatureFlags, []string, error) { - var envString string - for idx, env := range config.LdEnvironment { - envString = envString + fmt.Sprintf("env=%s", env) - if idx != (len(config.LdEnvironment) - 1) { - envString = envString + "&" - } - } - url := config.LdInstance + "/api/v2/flags/" + config.LdProject + "?" + envString + "&summary=0" +func getFlags(config *lcr.Config) ([]ldapi.FeatureFlag, error) { + url := fmt.Sprintf("%s/api/v2/flags/%s?env=%s", config.LdInstance, config.LdProject, config.LdEnvironment) client := &http.Client{} req, err := http.NewRequest("GET", url, nil) if err != nil { - return ldapi.FeatureFlags{}, []string{}, err + return []ldapi.FeatureFlag{}, err } req.Header.Add("Authorization", config.ApiToken) resp, err := client.Do(req) if err != nil { - return ldapi.FeatureFlags{}, []string{}, err + return []ldapi.FeatureFlag{}, err } - defer resp.Body.Close() flags := ldapi.FeatureFlags{} err = json.NewDecoder(resp.Body).Decode(&flags) if err != nil { - return ldapi.FeatureFlags{}, []string{}, err + return []ldapi.FeatureFlag{}, err } - flagKeys := make([]string, 0, len(flags.Items)) - for _, flag := range flags.Items { - flagKeys = append(flagKeys, flag.Key) - } - return flags, flagKeys, nil + return flags.Items, nil } func checkExistingComments(event *github.PullRequestEvent, config *lcr.Config, ctx context.Context) *github.IssueComment { diff --git a/search/search.go b/search/search.go index 7ad5b35e..00302d47 100644 --- a/search/search.go +++ b/search/search.go @@ -4,6 +4,7 @@ import ( "log" "strings" + ldapi "github.com/launchdarkly/api-client-go/v7" "github.com/launchdarkly/ld-find-code-refs/v2/aliases" "github.com/launchdarkly/ld-find-code-refs/v2/options" lsearch "github.com/launchdarkly/ld-find-code-refs/v2/search" @@ -12,8 +13,11 @@ import ( "github.com/spf13/viper" ) -func GetMatcher(config *lcr.Config, opts options.Options, flagKeys []string) (matcher lsearch.Matcher, err error) { - elements := []lsearch.ElementMatcher{} +func GetMatcher(config *lcr.Config, opts options.Options, flags []ldapi.FeatureFlag) (matcher lsearch.Matcher, err error) { + flagKeys := make([]string, 0, len(flags)) + for _, flag := range flags { + flagKeys = append(flagKeys, flag.Key) + } aliasesByFlagKey, err := aliases.GenerateAliases(flagKeys, opts.Aliases, config.Workspace) if err != nil { @@ -21,6 +25,7 @@ func GetMatcher(config *lcr.Config, opts options.Options, flagKeys []string) (ma } delimiters := strings.Join(Dedupe(getDelimiters(opts)), "") + elements := []lsearch.ElementMatcher{} elements = append(elements, lsearch.NewElementMatcher(config.LdProject, "", delimiters, flagKeys, aliasesByFlagKey)) matcher = lsearch.Matcher{ Elements: elements,