fix: align github.ref to GitHub Action (#1362)
* fix: align github.ref to GitHub Action The `github.ref` value should be `refs/heads/branch` in most cases. There are a few exceptions handled by the code. This change prefixes the default case with `refs/heads` and adds tests for this and the excpetional cases. * fix: correct existing assertions
This commit is contained in:
@@ -95,7 +95,7 @@ func (ghc *GithubContext) SetRefAndSha(ctx context.Context, defaultBranch string
|
||||
// https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads
|
||||
switch ghc.EventName {
|
||||
case "pull_request_target":
|
||||
ghc.Ref = ghc.BaseRef
|
||||
ghc.Ref = fmt.Sprintf("refs/heads/%s", ghc.BaseRef)
|
||||
ghc.Sha = asString(nestedMapLookup(ghc.Event, "pull_request", "base", "sha"))
|
||||
case "pull_request", "pull_request_review", "pull_request_review_comment":
|
||||
ghc.Ref = fmt.Sprintf("refs/pull/%.0f/merge", ghc.Event["number"])
|
||||
@@ -110,7 +110,10 @@ func (ghc *GithubContext) SetRefAndSha(ctx context.Context, defaultBranch string
|
||||
ghc.Sha = asString(ghc.Event["after"])
|
||||
}
|
||||
default:
|
||||
ghc.Ref = asString(nestedMapLookup(ghc.Event, "repository", "default_branch"))
|
||||
defaultBranch := asString(nestedMapLookup(ghc.Event, "repository", "default_branch"))
|
||||
if defaultBranch != "" {
|
||||
ghc.Ref = fmt.Sprintf("refs/heads/%s", defaultBranch)
|
||||
}
|
||||
}
|
||||
|
||||
if ghc.Ref == "" {
|
||||
@@ -130,7 +133,7 @@ func (ghc *GithubContext) SetRefAndSha(ctx context.Context, defaultBranch string
|
||||
}
|
||||
|
||||
if ghc.Ref == "" {
|
||||
ghc.Ref = asString(nestedMapLookup(ghc.Event, "repository", "default_branch"))
|
||||
ghc.Ref = fmt.Sprintf("refs/heads/%s", asString(nestedMapLookup(ghc.Event, "repository", "default_branch")))
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -40,7 +40,7 @@ func TestSetRefAndSha(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
ref: "master",
|
||||
ref: "refs/heads/master",
|
||||
sha: "pr-base-sha",
|
||||
},
|
||||
{
|
||||
@@ -89,7 +89,7 @@ func TestSetRefAndSha(t *testing.T) {
|
||||
"default_branch": "main",
|
||||
},
|
||||
},
|
||||
ref: "main",
|
||||
ref: "refs/heads/main",
|
||||
sha: "1234fakesha",
|
||||
},
|
||||
{
|
||||
@@ -127,7 +127,7 @@ func TestSetRefAndSha(t *testing.T) {
|
||||
|
||||
ghc.SetRefAndSha(context.Background(), "", "/some/dir")
|
||||
|
||||
assert.Equal(t, "master", ghc.Ref)
|
||||
assert.Equal(t, "refs/heads/master", ghc.Ref)
|
||||
assert.Equal(t, "1234fakesha", ghc.Sha)
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user