Skip to content

Commit

Permalink
Fix bug when rerunning the GitHub action
Browse files Browse the repository at this point in the history
Prior to this commit rerunning the GitHub action would always lead
to an error.  This was because the GITHUB_REF environment variable
exposed by the Github Action does not contain the pull request number
when rerunning a job (see [1]).

Fixed by now retrieving the pull request number from the Github event
JSON (instead of via the GITHUB_REF), via the GITHUB_EVENT_PATH
environment variable.

[1] actions/checkout#58 (comment)
  • Loading branch information
johnboyes committed May 7, 2020
1 parent d0d7988 commit fd03ae4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
18 changes: 12 additions & 6 deletions internal/github/pullrequest/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package pullrequest

import (
"encoding/json"
"io/ioutil"
"os"
"strconv"
"path/filepath"
"strings"
)

Expand All @@ -19,11 +20,16 @@ func (action action) repositoryName() string {
}

func (action action) pullRequestNumber() int {
s := strings.Split(os.Getenv("GITHUB_REF"), "/")[2]
pullRequestNumber, err := strconv.Atoi(s)
panicIfError((err))

return pullRequestNumber
event := struct {
PullRequestNumber int `json:"number"`
}{}
githubEventJSONFile, err := os.Open(filepath.Clean(os.Getenv("GITHUB_EVENT_PATH")))
panicIfError(err)
defer githubEventJSONFile.Close() //nolint
byteValue, _ := ioutil.ReadAll(githubEventJSONFile)
panicIfError(json.Unmarshal(byteValue, &event))

return event.PullRequestNumber
}

func (action action) token() string {
Expand Down
20 changes: 16 additions & 4 deletions magefile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@ package test
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/magefile/mage/mage"
)

const (
EnvGitHubRepository = "GITHUB_REPOSITORY"
EnvGitHubRef = "GITHUB_REF"
EnvGitHubEventPath = "GITHUB_EVENT_PATH"
EnvGitHubActionInputLabels = "GITHUB_ACTION_INPUT_LABELS"
GitHubTestRepo = "agilepathway/test-label-checker-consumer"
PRWithNoLabels = 1 // https://github.com/agilepathway/test-label-checker-consumer/pull/1
PRWithOneSpecifiedLabel = 2 // https://github.com/agilepathway/test-label-checker-consumer/pull/2
PRWithTwoSpecifiedLabels = 3 // https://github.com/agilepathway/test-label-checker-consumer/pull/3
GitHubEventJSONDir = "testdata"
GitHubEventJSONFilename = "github_event.json"
)

func TestPullRequestWithOneSpecifiedLabelShouldSucceed(t *testing.T) {
Expand Down Expand Up @@ -49,15 +53,18 @@ func TestPullRequestWithTwoSpecifiedLabelsShouldFail(t *testing.T) {
}

func TestMain(m *testing.M) {
os.Setenv(EnvGitHubRepository, GitHubTestRepo) //nolint
os.Mkdir(GitHubEventJSONDir, os.ModePerm) //nolint
os.Setenv(EnvGitHubRepository, GitHubTestRepo) //nolint
os.Setenv(EnvGitHubEventPath, gitHubEventFullPath()) //nolint
os.Exit(testMainWrapper(m))
}

func testMainWrapper(m *testing.M) int {
//nolint
defer func() {
os.RemoveAll(GitHubEventJSONDir)
os.Unsetenv(EnvGitHubRepository)
os.Unsetenv(EnvGitHubRef)
os.Unsetenv(EnvGitHubEventPath)
os.Unsetenv(EnvGitHubActionInputLabels)
}()

Expand All @@ -73,7 +80,8 @@ func checkLabels() (int, *bytes.Buffer, *bytes.Buffer) {
}

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

func specifySemVerLabels() {
Expand All @@ -99,3 +107,7 @@ func expectError(exitCode int, t *testing.T, stderr fmt.Stringer, expectedStdErr
t.Fatalf("expected %q but got %q", expectedStdErr, actual)
}
}

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

0 comments on commit fd03ae4

Please sign in to comment.