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-212460] add any changed output #52

Merged
merged 3 commits into from
Aug 10, 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
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ jobs:
echo "$flag was removed"
done
- name: Add label
if: steps.find-flags.outputs.any-modified == 'true' || steps.find-flags.outputs.any-removed == 'true'
if: steps.find-flags.outputs.any-changed == 'true'
run: gh pr edit $PR_NUMBER --add-label ld-flags
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
- name: Remove label
if: steps.find-flags.outputs.any-modified == 'false' && steps.find-flags.outputs.any-removed == 'false'
if: steps.find-flags.outputs.any-changed == 'false'
run: gh pr edit $PR_NUMBER --remove-label ld-flags
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ jobs:

# Add or remove labels on PRs if any flags have changed
- name: Add label
if: steps.find-flags.outputs.any-modified == 'true' || steps.find-flags.outputs.any-removed == 'true'
if: steps.find-flags.outputs.any-changed == 'true'
run: gh pr edit $PR_NUMBER --add-label ld-flags
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
- name: Remove label
if: steps.find-flags.outputs.any-modified == 'false' && steps.find-flags.outputs.any-removed == 'false'
if: steps.find-flags.outputs.any-changed == 'false'
run: gh pr edit $PR_NUMBER --remove-label ld-flags
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down Expand Up @@ -114,4 +114,7 @@ This action does not support monorepos or searching for flags across LaunchDarkl
| any-removed | Returns true if any flags have been removed in PR |
| removed-flags | Space-separated list of flags removed in PR |
| removed-flags-count | Number of flags removed in PR |
| any-Changed | Returns true if any flags have been changed in PR |
| changed-flags | Space-separated list of flags changed in PR |
| changed-flags-count | Number of flags changed in PR |
<!-- action-docs-outputs -->
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,9 @@ outputs:
description: Space-separated list of flags removed in PR
removed-flags-count:
description: Number of flags removed in PR
any-Changed:
description: Returns true if any flags have been changed in PR
changed-flags:
description: Space-separated list of flags changed in PR
changed-flags-count:
description: Number of flags changed in PR
6 changes: 6 additions & 0 deletions internal/github_actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import (
"os"
)

func SetOutputOrLogError(name, value string) {
if err := SetOutput(name, value); err != nil {
LogError("Failed to set outputs.%s\n", name)
}
}

func SetOutput(name, value string) error {
output := os.Getenv("GITHUB_OUTPUT")

Expand Down
47 changes: 19 additions & 28 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,40 +168,31 @@ func getOptions(config *lcr.Config) (options.Options, error) {
}

func setOutputs(flagsRef lflags.FlagsRef) {
flagsAddedCount := len(flagsRef.FlagsAdded)

if err := gha.SetOutput("any-modified", fmt.Sprintf("%t", flagsAddedCount > 0)); err != nil {
log.Println("Failed to set outputs.any-modified")
}
if err := gha.SetOutput("modified-flags-count", fmt.Sprintf("%d", flagsAddedCount)); err != nil {
log.Println("Failed to set outputs.modified-flags-count")
}
flagKeysAdded := make([]string, 0, len(flagsRef.FlagsAdded))
flagsModified := make([]string, 0, len(flagsRef.FlagsAdded))
for k := range flagsRef.FlagsAdded {
flagKeysAdded = append(flagKeysAdded, k)
flagsModified = append(flagsModified, k)
}
sort.Strings(flagKeysAdded)
if err := gha.SetOutput("modified-flags", strings.Join(flagKeysAdded, " ")); err != nil {
log.Println("Failed to set outputs.modified-flags")
setOutputsForChangedFlags("modified", flagsModified)

flagsRemoved := make([]string, 0, len(flagsRef.FlagsRemoved))
for k := range flagsRef.FlagsRemoved {
flagsRemoved = append(flagsRemoved, k)
}
setOutputsForChangedFlags("removed", flagsModified)

flagsRemovedCount := len(flagsRef.FlagsRemoved)
allChangedFlags := make([]string, 0, len(flagsModified)+len(flagsRemoved))
allChangedFlags = append(allChangedFlags, flagsModified...)
allChangedFlags = append(allChangedFlags, flagsRemoved...)
setOutputsForChangedFlags("changed", allChangedFlags)
}

if err := gha.SetOutput("any-removed", fmt.Sprintf("%t", flagsRemovedCount > 0)); err != nil {
log.Println("Failed to set outputs.any-removed")
}
if err := gha.SetOutput("removed-flags-count", fmt.Sprintf("%d", flagsRemovedCount)); err != nil {
log.Println("Failed to set outputs.removed-flags-count")
}
func setOutputsForChangedFlags(modifier string, changedFlags []string) {
count := len(changedFlags)
gha.SetOutputOrLogError(fmt.Sprintf("any-%s", modifier), fmt.Sprintf("%t", count > 0))
gha.SetOutputOrLogError(fmt.Sprintf("%s-flags-count", modifier), fmt.Sprintf("%d", count))

flagKeysRemoved := make([]string, 0, len(flagsRef.FlagsRemoved))
for k := range flagsRef.FlagsRemoved {
flagKeysRemoved = append(flagKeysRemoved, k)
}
sort.Strings(flagKeysRemoved)
if err := gha.SetOutput("removed-flags", strings.Join(flagKeysRemoved, " ")); err != nil {
log.Println("Failed to set outputs.removed-flags")
}
sort.Strings(changedFlags)
gha.SetOutputOrLogError(fmt.Sprintf("%s-flags", modifier), strings.Join(changedFlags, " "))
}

func failExit(err error) {
Expand Down