Skip to content

Commit

Permalink
feat: set number changed flags in github action outputs (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
jazanne authored Jun 28, 2023
1 parent ee2efec commit f8d6743
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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 }}"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vscode
cr-flags
.secrets
4 changes: 4 additions & 0 deletions .secrets.example.env
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 19 additions & 0 deletions internal/github_actions/actions.go
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 16 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions testdata/act/pull-request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"pull_request": {
"number": 20,
"head": {
"ref": "sample-head-ref"
},
"base": {
"ref": "sample-base-ref"
}
}
}
4 changes: 3 additions & 1 deletion testdata/test
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
show-widgets
beta-ui
showWidgets
betaUi
show_widgets
saver-goal
beta_ui

0 comments on commit f8d6743

Please sign in to comment.