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 f3d05bb commit f66f84b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
20 changes: 14 additions & 6 deletions internal/github/pullrequest/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ package pullrequest

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

type action struct {
}

type event struct {
PullRequestNumber int `json:"number"`
}

func (action action) repositoryOwner() string {
return strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")[0]
}
Expand All @@ -19,11 +24,14 @@ 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
var event event

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
17 changes: 13 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)
os.Setenv(EnvGitHubRepository, GitHubTestRepo) //nolint
os.Setenv(EnvGitHubEventPath, filepath.Join(GitHubEventJsonDir, GitHubEventJsonFilename)) //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,9 @@ 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))
gp := filepath.Join(GitHubEventJsonDir, GitHubEventJsonFilename)
ioutil.WriteFile(gp, githubEventJSON, os.ModePerm)
}

func specifySemVerLabels() {
Expand Down

0 comments on commit f66f84b

Please sign in to comment.