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

[sc-209335] feat: only allow one env key in configuration #41

Merged
merged 3 commits into from
Aug 2, 2023
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 14 additions & 16 deletions comments/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
Expand Down
13 changes: 6 additions & 7 deletions comments/comments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand All @@ -96,7 +95,7 @@ func newProcessFlagAccEnv() *testProcessor {
}

config := config.Config{
LdEnvironment: []string{"production"},
LdEnvironment: "production",
LdInstance: "https://example.com/",
}
return &testProcessor{
Expand All @@ -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{
Expand All @@ -118,7 +117,7 @@ func newProcessMultipleFlagsFlagAccEnv() *testProcessor {
}

config := config.Config{
LdEnvironment: []string{"production"},
LdEnvironment: "production",
LdInstance: "https://example.com/",
}
return &testProcessor{
Expand Down
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

type Config struct {
LdProject string
LdEnvironment []string
LdEnvironment string
LdInstance string
Owner string
Repo string
Expand All @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func newProcessFlagAccEnv() *testProcessor {
}

config := config.Config{
LdEnvironment: []string{"production"},
LdEnvironment: "production",
LdInstance: "https://example.com/",
}
return &testProcessor{
Expand Down
30 changes: 9 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ 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)
}

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)
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 7 additions & 2 deletions search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -12,15 +13,19 @@ 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 {
return lsearch.Matcher{}, err
}

delimiters := strings.Join(Dedupe(getDelimiters(opts)), "")
elements := []lsearch.ElementMatcher{}
elements = append(elements, lsearch.NewElementMatcher(config.LdProject, "", delimiters, flagKeys, aliasesByFlagKey))
matcher = lsearch.Matcher{
Elements: elements,
Expand Down