Skip to content

Commit

Permalink
Update deprecated set-output GitHub Action command (#326)
Browse files Browse the repository at this point in the history
As per the [GitHub blog article][1] about this.

Also deleted the integration tests relating to the [allow failure][2]
mode, as the recently added end-to-end tests offer much better test
coverage (the integration tests failed to catch our recent bug there,
whereas the end-to-end tests would have caught it).  So there was no
benefit in refactoring those integration tests as part of this commit,
better to delete them.

[1]: https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/
  • Loading branch information
johnboyes committed Jan 1, 2023
1 parent dc9cf15 commit e7993d1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 87 deletions.
17 changes: 13 additions & 4 deletions internal/github/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ func (a *Action) CheckLabels() error {
return a.handleFailure()
}

fmt.Println(a.resultStepOutput("success"))
a.outputResult("success")

return nil
}

func (a *Action) handleFailure() error {
fmt.Println(a.resultStepOutput("failure"))
a.outputResult("failure")
err := errors.New(a.trimTrailingNewLine(a.failMsg))

if a.allowFailure() {
Expand Down Expand Up @@ -105,8 +105,17 @@ func (a *Action) pullRequestNumber() int {
return event.PullRequestNumber
}

func (a *Action) resultStepOutput(result string) string {
return "::set-output name=label_check::" + result
func (a *Action) outputResult(result string) {
label_check_output := fmt.Sprintf("label_check=%s", result)
gitHubOutputFileName := filepath.Clean(os.Getenv("GITHUB_OUTPUT"))
githubOutputFile, err := os.OpenFile(gitHubOutputFileName, os.O_APPEND|os.O_WRONLY, 0644)
panic.IfError(err)
_, err = githubOutputFile.WriteString(label_check_output)
if err != nil {
githubOutputFile.Close()
panic.IfError(err)
}
githubOutputFile.Close()
}

func (a *Action) token() string {
Expand Down
94 changes: 11 additions & 83 deletions label_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -44,6 +43,7 @@ const (
EnvHTTPSProxy = "HTTPS_PROXY"
EnvGitHubEnterprise = "INPUT_GITHUB_ENTERPRISE_GRAPHQL_URL"
EnvAllowFailure = "INPUT_ALLOW_FAILURE"
EnvGitHubOutput = "GITHUB_OUTPUT"
GitHubEnterpriseCloudEndpoint = "https://api.github.com/graphql"
GitHubEnterpriseServerEndpoint = "https://example.com/api/graphql"
GitHubTestRepo = "agilepathway/test-label-checker-consumer"
Expand All @@ -53,6 +53,7 @@ const (
ThreeLabelsPR = 4 // https://github.com/agilepathway/test-label-checker-consumer/pull/4
GitHubEventJSONDir = "testdata/temp"
GitHubEventJSONFilename = "github_event.json"
GitHubOutputFilename = "github_output"
MagefileVerbose = "MAGEFILE_VERBOSE"
HoverflyProxyAddress = "127.0.0.1:8500"
NeedNoneGotNone = "Label check successful: required none of major, minor, patch, and found 0.\n"
Expand Down Expand Up @@ -83,8 +84,8 @@ func TestLabelChecks(t *testing.T) {
expectedStdout string
expectedStderr string
}{
"Need none, got none": {NoLabelsPR, checkNone, NeedNoneGotNone, ""},
"Need none, got one": {OneLabelPR, checkNone, "", NeedNoneGotOne},
"Need none, got none": {NoLabelsPR, checkNone, NeedNoneGotNone, ""},
"Need none, got one": {OneLabelPR, checkNone, "", NeedNoneGotOne},
"Need none, got two": {TwoLabelsPR, checkNone, "", NeedNoneGotTwo},
"Need one, got none": {NoLabelsPR, checkOne, "", NeedOneGotNone},
"Need one, got one": {OneLabelPR, checkOne, NeedOneGotOne, ""},
Expand Down Expand Up @@ -115,10 +116,7 @@ func TestLabelChecks(t *testing.T) {
t.Run(name, func(t *testing.T) {
tc.expectedStdout = "Checking GitHub labels ...\n" + tc.expectedStdout
if len(tc.expectedStderr) > 0 {
tc.expectedStdout += "::set-output name=label_check::failure\n"
tc.expectedStderr = "Error: " + tc.expectedStderr
} else {
tc.expectedStdout += "::set-output name=label_check::success\n"
}
setPullRequestNumber(tc.prNumber)
tc.specifyChecks()
Expand Down Expand Up @@ -149,83 +147,13 @@ func TestLabelChecks(t *testing.T) {
}
}

// nolint: lll, funlen, dupl
func TestLabelChecksWithFailureAllowed(t *testing.T) {
tests := map[string]struct {
prNumber int
specifyChecks specifyChecks
expectedStdout string
expectedStderr string
}{
"Need none, got none": {NoLabelsPR, checkNone, NeedNoneGotNone, ""},
"Need none, got one": {OneLabelPR, checkNone, "", NeedNoneGotOne},
"Need none, got two": {TwoLabelsPR, checkNone, "", NeedNoneGotTwo},
"Need one, got none": {NoLabelsPR, checkOne, "", NeedOneGotNone},
"Need one, got one": {OneLabelPR, checkOne, NeedOneGotOne, ""},
"Need one, got two": {TwoLabelsPR, checkOne, "", NeedOneGotTwo},
"Need all, got none": {NoLabelsPR, checkAll, "", NeedAllGotNone},
"Need all, got one": {OneLabelPR, checkAll, "", NeedAllGotOne},
"Need all, got two": {TwoLabelsPR, checkAll, "", NeedAllGotTwo},
"Need all, got all": {ThreeLabelsPR, checkAll, NeedAllGotAll, ""},
"Need any, got none": {NoLabelsPR, checkAny, "", NeedAnyGotNone},
"Need any, got one": {OneLabelPR, checkAny, NeedAnyGotOne, ""},
"Need any, got two": {TwoLabelsPR, checkAny, NeedAnyGotTwo, ""},
"Need any, got three": {ThreeLabelsPR, checkAny, NeedAnyGotThree, ""},
"Need [none, one], got none": {NoLabelsPR, checkNoneAndOne, NeedNoneGotNone, NeedOneGotNone},
"Need [none, one], got one": {OneLabelPR, checkNoneAndOne, NeedOneGotOne, NeedNoneGotOne},
"Need [none, one], got two": {TwoLabelsPR, checkNoneAndOne, "", NeedOneGotTwo + NeedNoneGotTwo},
"Need [none, one, all], got none": {NoLabelsPR, checkNoneAndOneAndAll, NeedNoneGotNone, NeedOneGotNone + NeedAllGotNone},
"Need [none, one, all], got one": {OneLabelPR, checkNoneAndOneAndAll, NeedOneGotOne, NeedNoneGotOne + NeedAllGotOne},
"Need [none, one, all], got two": {TwoLabelsPR, checkNoneAndOneAndAll, "", NeedOneGotTwo + NeedNoneGotTwo + NeedAllGotTwo},
"Need [none, one, all], got three": {ThreeLabelsPR, checkNoneAndOneAndAll, NeedAllGotAll, NeedOneGotThree + NeedNoneGotThree},
"Need [none, one, all, any], got none": {NoLabelsPR, checkNoneAndOneAndAllAndAny, NeedNoneGotNone, NeedOneGotNone + NeedAllGotNone + NeedAnyGotNone},
"Need [none, one, all, any], got one": {OneLabelPR, checkNoneAndOneAndAllAndAny, NeedOneGotOne + NeedAnyGotOne, NeedNoneGotOne + NeedAllGotOne},
"Need [none, one, all, any], got two": {TwoLabelsPR, checkNoneAndOneAndAllAndAny, NeedAnyGotTwo, NeedOneGotTwo + NeedNoneGotTwo + NeedAllGotTwo},
"Need [none, one, all, any], got three": {ThreeLabelsPR, checkNoneAndOneAndAllAndAny, NeedAllGotAll + NeedAnyGotThree, NeedOneGotThree + NeedNoneGotThree},
}
for name, tc := range tests {
allowFailure()

tc := tc

t.Run(name, func(t *testing.T) {
tc.expectedStdout = "Checking GitHub labels ...\n" + tc.expectedStdout
if len(tc.expectedStderr) > 0 {
tc.expectedStdout += "::set-output name=label_check::failure\n"
tc.expectedStderr = "Error: " + tc.expectedStderr
} else {
tc.expectedStdout += "::set-output name=label_check::success\n"
}
setPullRequestNumber(tc.prNumber)
tc.specifyChecks()

exitCode, stderr, stdout := checkLabels()

if exitCode != 0 {
t.Fatalf("expected exit code 0 but got %v", exitCode)
}

if actual := stdout.String(); actual != tc.expectedStdout {
t.Fatalf("expected %q but got %q", tc.expectedStdout, actual)
}

if actual := stderr.String(); actual != tc.expectedStderr {
t.Fatalf("expected %q but got %q", tc.expectedStderr, actual)
}

os.Unsetenv(EnvRequireNoneOf) //nolint
os.Unsetenv(EnvRequireOneOf) //nolint
os.Unsetenv(EnvRequireAllOf) //nolint
os.Unsetenv(EnvRequireAnyOf) //nolint
})
}
}

func TestMain(m *testing.M) {
flag.Parse()
os.Mkdir(GitHubEventJSONDir, os.ModePerm) //nolint
os.Create(gitHubOutputFullPath()) //nolint
os.Setenv(EnvGitHubRepository, GitHubTestRepo) //nolint
os.Setenv(EnvGitHubEventPath, gitHubEventFullPath()) //nolint
os.Setenv(EnvGitHubOutput, gitHubOutputFullPath()) //nolint
os.Setenv(MagefileVerbose, "1") //nolint
os.Setenv(EnvRequireOneOf, " ") //nolint
os.Setenv(EnvRequireNoneOf, " ") //nolint
Expand Down Expand Up @@ -309,7 +237,7 @@ func checkLabels() (int, *bytes.Buffer, *bytes.Buffer) {

func setPullRequestNumber(prNumber int) {
githubEventJSON := []byte(fmt.Sprintf(`{ "number": %d }`, prNumber))
ioutil.WriteFile(gitHubEventFullPath(), githubEventJSON, os.ModePerm) //nolint
os.WriteFile(gitHubEventFullPath(), githubEventJSON, os.ModePerm) //nolint
}

func checkOne() {
Expand Down Expand Up @@ -343,10 +271,10 @@ func checkNoneAndOneAndAllAndAny() {
checkAny()
}

func allowFailure() {
os.Setenv(EnvAllowFailure, "true") //nolint
}

func gitHubEventFullPath() string {
return filepath.Join(GitHubEventJSONDir, GitHubEventJSONFilename)
}

func gitHubOutputFullPath() string {
return filepath.Join(GitHubEventJSONDir, GitHubOutputFilename)
}

0 comments on commit e7993d1

Please sign in to comment.