Skip to content

Commit

Permalink
Bound number of flags (#11)
Browse files Browse the repository at this point in the history
* With no clean way to ignore whitespace only changes set a maximum number of flags that can be processed per PR.
  • Loading branch information
Dan O'Brien authored Apr 23, 2021
1 parent 01f76b2 commit c334411
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 4 deletions.
1 change: 0 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ jobs:
accessToken: ${{ secrets.LD_ACCESS_TOKEN }}
githubToken: ${{ secrets.GITHUB_TOKEN }}
baseUri: https://app.launchdarkly.com
referencePRonFlag: true
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ inputs:
description: 'Use LaunchDarkly Custom Properties to associate the repo name and flag.'
required: true
default: 'false'
maxFlags:
description: 'Maximum number of flags to find per PR.'
required: true
default: '5'
8 changes: 8 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Config struct {
Workspace string
GHClient *github.Client
ReferencePRonFlag bool
MaxFlags int
}

func ValidateInputandParse(ctx context.Context) (*Config, error) {
Expand All @@ -47,11 +48,18 @@ func ValidateInputandParse(ctx context.Context) (*Config, error) {
}

config.Workspace = os.Getenv("GITHUB_WORKSPACE")

ReferencePRonFlag, err := strconv.ParseBool(os.Getenv("INPUT_REFERENCEPRONFLAG"))
if err != nil {
return nil, err
}
config.ReferencePRonFlag = ReferencePRonFlag

MaxFlags, err := strconv.ParseInt(os.Getenv("INPUT_MAXFLAGS"), 10, 32)
if err != nil {
return nil, err
}
config.MaxFlags = int(MaxFlags)
config.GHClient = getGithubClient(ctx)
return &config, nil
}
Expand Down
6 changes: 5 additions & 1 deletion diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,13 @@ func CheckDiff(parsedDiff *diff.FileDiff, workspace string) *DiffPaths {
return &diffPaths
}

func ProcessDiffs(raw *diff.Hunk, flagsRef ghc.FlagsRef, flags ldapi.FeatureFlags, aliases map[string][]string) {
func ProcessDiffs(raw *diff.Hunk, flagsRef ghc.FlagsRef, flags ldapi.FeatureFlags, aliases map[string][]string, maxFlags int) {
diffRows := strings.Split(string(raw.Body), "\n")
for _, row := range diffRows {

if (len(flagsRef.FlagsAdded) + len(flagsRef.FlagsRemoved)) >= maxFlags {
break
}
if strings.HasPrefix(row, "+") {
for _, flag := range flags.Items {
if strings.Contains(row, flag.Key) {
Expand Down
2 changes: 1 addition & 1 deletion diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func TestProcessDiffs(t *testing.T) {
StartPosition: 1,
Body: []byte(tc.sampleBody),
}
ProcessDiffs(hunk, processor.FlagsRef, processor.Flags, tc.aliases)
ProcessDiffs(hunk, processor.FlagsRef, processor.Flags, tc.aliases, 5)
assert.Equal(t, tc.expected, processor.FlagsRef, "")
})
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func main() {
continue
}
for _, raw := range parsedDiff.Hunks {
ldiff.ProcessDiffs(raw, flagsRef, flags, aliases)
ldiff.ProcessDiffs(raw, flagsRef, flags, aliases, config.MaxFlags)
}
}
if err != nil {
Expand Down

0 comments on commit c334411

Please sign in to comment.