diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 765508ca..b710dd90 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,3 +1,6 @@ +# Run test file with command: +# act pull_request -e testdata/act/pull-request.json + name: Test Find Flags on: pull_request @@ -17,3 +20,8 @@ jobs: accessToken: ${{ secrets.LD_ACCESS_TOKEN }} githubToken: ${{ secrets.GITHUB_TOKEN }} baseUri: https://app.launchdarkly.com + - name: Find flags summary + run: | + echo "flags addded or modified ${{ steps.find_flags.outputs.flags_modified }}" + echo "flags removed ${{ steps.find_flags.outputs.flags_removed }}" + diff --git a/.gitignore b/.gitignore index d688c27c..677169f3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .vscode cr-flags +.secrets diff --git a/.secrets.example.env b/.secrets.example.env new file mode 100644 index 00000000..92f176fc --- /dev/null +++ b/.secrets.example.env @@ -0,0 +1,4 @@ +# Save as .secrets for testing with nektos/act + +LD_ACCESS_TOKEN= # LaunchDarkly access token +GITHUB_TOKEN= # GitHub PAT with repo access diff --git a/action.yml b/action.yml index 7ffc52bc..857acac7 100644 --- a/action.yml +++ b/action.yml @@ -38,3 +38,9 @@ inputs: description: 'Maximum number of flags to find per PR.' required: false default: '5' + +outputs: + flags_modified: + description: Number of flags added or modified in pull request + flags_removed: + description: Number of flags removed in pull request diff --git a/internal/github_actions/actions.go b/internal/github_actions/actions.go new file mode 100644 index 00000000..f36d4b09 --- /dev/null +++ b/internal/github_actions/actions.go @@ -0,0 +1,19 @@ +package github_actions + +import ( + "fmt" + "log" + "os" +) + +func SetOutput(name, value string) error { + output := os.Getenv("GITHUB_OUTPUT") + + f, err := os.OpenFile(output, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + log.Println(err) + } + defer f.Close() + _, err = fmt.Fprintf(f, "%s=%s\n", name, value) + return err +} diff --git a/main.go b/main.go index 28e9cb58..05c59ab5 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( ghc "github.com/launchdarkly/cr-flags/comments" lcr "github.com/launchdarkly/cr-flags/config" ldiff "github.com/launchdarkly/cr-flags/diff" + gha "github.com/launchdarkly/cr-flags/internal/github_actions" "github.com/launchdarkly/ld-find-code-refs/v2/aliases" "github.com/launchdarkly/ld-find-code-refs/v2/options" "github.com/sourcegraph/go-diff/diff" @@ -61,6 +62,10 @@ func main() { } } + // Set outputs + setOutputs(flagsRef) + + // Add comment existingComment := checkExistingComments(event, config, ctx) buildComment := ghc.ProcessFlags(flagsRef, flags, config) postedComments := ghc.BuildFlagComment(buildComment, flagsRef, existingComment) @@ -183,6 +188,17 @@ func getAliases(config *lcr.Config, flagKeys []string) (map[string][]string, err } +func setOutputs(flagsRef ghc.FlagsRef) { + err := gha.SetOutput("flags_modified", fmt.Sprintf("%d", len(flagsRef.FlagsAdded))) + if err != nil { + log.Println("Failed to set outputs.flags_modified") + } + err = gha.SetOutput("flags_removed", fmt.Sprintf("%d", len(flagsRef.FlagsRemoved))) + if err != nil { + log.Println("Failed to set outputs.flags_removed") + } +} + func failExit(err error) { if err != nil { log.Println(err) diff --git a/testdata/act/pull-request.json b/testdata/act/pull-request.json new file mode 100644 index 00000000..3dce926e --- /dev/null +++ b/testdata/act/pull-request.json @@ -0,0 +1,11 @@ +{ + "pull_request": { + "number": 20, + "head": { + "ref": "sample-head-ref" + }, + "base": { + "ref": "sample-base-ref" + } + } +} diff --git a/testdata/test b/testdata/test index 66c27936..1903ab5b 100644 --- a/testdata/test +++ b/testdata/test @@ -1,4 +1,6 @@ show-widgets +beta-ui showWidgets +betaUi show_widgets -saver-goal +beta_ui