fix: correct ref and ref_name (#1672)

* fix: correct ref and ref_name

The ref in the GitHub context is always full qualified
(e.g. refs/heads/branch, refs/tags/v1).
The ref_name is the ref with the strippep prefix.
In case of pull_requests, this is the merge commit ref
(e.g. refs/pull/123/merge -> 123/merge).

* test: update test data
This commit is contained in:
Markus Wolf 2023-03-09 21:03:13 +01:00 committed by GitHub
parent ac5dd8feb8
commit 6744e68ee2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 7 deletions

View file

@ -103,7 +103,7 @@ func (ghc *GithubContext) SetRef(ctx context.Context, defaultBranch string, repo
case "deployment", "deployment_status": case "deployment", "deployment_status":
ghc.Ref = asString(nestedMapLookup(ghc.Event, "deployment", "ref")) ghc.Ref = asString(nestedMapLookup(ghc.Event, "deployment", "ref"))
case "release": case "release":
ghc.Ref = asString(nestedMapLookup(ghc.Event, "release", "tag_name")) ghc.Ref = fmt.Sprintf("refs/tags/%s", asString(nestedMapLookup(ghc.Event, "release", "tag_name")))
case "push", "create", "workflow_dispatch": case "push", "create", "workflow_dispatch":
ghc.Ref = asString(ghc.Event["ref"]) ghc.Ref = asString(ghc.Event["ref"])
default: default:
@ -183,6 +183,9 @@ func (ghc *GithubContext) SetRefTypeAndName() {
} else if strings.HasPrefix(ghc.Ref, "refs/heads/") { } else if strings.HasPrefix(ghc.Ref, "refs/heads/") {
refType = "branch" refType = "branch"
refName = ghc.Ref[len("refs/heads/"):] refName = ghc.Ref[len("refs/heads/"):]
} else if strings.HasPrefix(ghc.Ref, "refs/pull/") {
refType = ""
refName = ghc.Ref[len("refs/pull/"):]
} }
if ghc.RefType == "" { if ghc.RefType == "" {

View file

@ -29,18 +29,21 @@ func TestSetRef(t *testing.T) {
eventName string eventName string
event map[string]interface{} event map[string]interface{}
ref string ref string
refName string
}{ }{
{ {
eventName: "pull_request_target", eventName: "pull_request_target",
event: map[string]interface{}{}, event: map[string]interface{}{},
ref: "refs/heads/master", ref: "refs/heads/master",
refName: "master",
}, },
{ {
eventName: "pull_request", eventName: "pull_request",
event: map[string]interface{}{ event: map[string]interface{}{
"number": 1234., "number": 1234.,
}, },
ref: "refs/pull/1234/merge", ref: "refs/pull/1234/merge",
refName: "1234/merge",
}, },
{ {
eventName: "deployment", eventName: "deployment",
@ -49,7 +52,8 @@ func TestSetRef(t *testing.T) {
"ref": "refs/heads/somebranch", "ref": "refs/heads/somebranch",
}, },
}, },
ref: "refs/heads/somebranch", ref: "refs/heads/somebranch",
refName: "somebranch",
}, },
{ {
eventName: "release", eventName: "release",
@ -58,14 +62,16 @@ func TestSetRef(t *testing.T) {
"tag_name": "v1.0.0", "tag_name": "v1.0.0",
}, },
}, },
ref: "v1.0.0", ref: "refs/tags/v1.0.0",
refName: "v1.0.0",
}, },
{ {
eventName: "push", eventName: "push",
event: map[string]interface{}{ event: map[string]interface{}{
"ref": "refs/heads/somebranch", "ref": "refs/heads/somebranch",
}, },
ref: "refs/heads/somebranch", ref: "refs/heads/somebranch",
refName: "somebranch",
}, },
{ {
eventName: "unknown", eventName: "unknown",
@ -74,12 +80,14 @@ func TestSetRef(t *testing.T) {
"default_branch": "main", "default_branch": "main",
}, },
}, },
ref: "refs/heads/main", ref: "refs/heads/main",
refName: "main",
}, },
{ {
eventName: "no-event", eventName: "no-event",
event: map[string]interface{}{}, event: map[string]interface{}{},
ref: "refs/heads/master", ref: "refs/heads/master",
refName: "master",
}, },
} }
@ -92,8 +100,10 @@ func TestSetRef(t *testing.T) {
} }
ghc.SetRef(context.Background(), "main", "/some/dir") ghc.SetRef(context.Background(), "main", "/some/dir")
ghc.SetRefTypeAndName()
assert.Equal(t, table.ref, ghc.Ref) assert.Equal(t, table.ref, ghc.Ref)
assert.Equal(t, table.refName, ghc.RefName)
}) })
} }

View file

@ -413,7 +413,7 @@ func TestGetGithubContextRef(t *testing.T) {
{event: "pull_request_target", json: `{"pull_request":{"base":{"ref": "main"}}}`, ref: "refs/heads/main"}, {event: "pull_request_target", json: `{"pull_request":{"base":{"ref": "main"}}}`, ref: "refs/heads/main"},
{event: "deployment", json: `{"deployment": {"ref": "tag-name"}}`, ref: "tag-name"}, {event: "deployment", json: `{"deployment": {"ref": "tag-name"}}`, ref: "tag-name"},
{event: "deployment_status", json: `{"deployment": {"ref": "tag-name"}}`, ref: "tag-name"}, {event: "deployment_status", json: `{"deployment": {"ref": "tag-name"}}`, ref: "tag-name"},
{event: "release", json: `{"release": {"tag_name": "tag-name"}}`, ref: "tag-name"}, {event: "release", json: `{"release": {"tag_name": "tag-name"}}`, ref: "refs/tags/tag-name"},
} }
for _, data := range table { for _, data := range table {