Skip to content

Commit 6744e68

Browse files
authored
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
1 parent ac5dd8f commit 6744e68

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

Diff for: pkg/model/github_context.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (ghc *GithubContext) SetRef(ctx context.Context, defaultBranch string, repo
103103
case "deployment", "deployment_status":
104104
ghc.Ref = asString(nestedMapLookup(ghc.Event, "deployment", "ref"))
105105
case "release":
106-
ghc.Ref = asString(nestedMapLookup(ghc.Event, "release", "tag_name"))
106+
ghc.Ref = fmt.Sprintf("refs/tags/%s", asString(nestedMapLookup(ghc.Event, "release", "tag_name")))
107107
case "push", "create", "workflow_dispatch":
108108
ghc.Ref = asString(ghc.Event["ref"])
109109
default:
@@ -183,6 +183,9 @@ func (ghc *GithubContext) SetRefTypeAndName() {
183183
} else if strings.HasPrefix(ghc.Ref, "refs/heads/") {
184184
refType = "branch"
185185
refName = ghc.Ref[len("refs/heads/"):]
186+
} else if strings.HasPrefix(ghc.Ref, "refs/pull/") {
187+
refType = ""
188+
refName = ghc.Ref[len("refs/pull/"):]
186189
}
187190

188191
if ghc.RefType == "" {

Diff for: pkg/model/github_context_test.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,21 @@ func TestSetRef(t *testing.T) {
2929
eventName string
3030
event map[string]interface{}
3131
ref string
32+
refName string
3233
}{
3334
{
3435
eventName: "pull_request_target",
3536
event: map[string]interface{}{},
3637
ref: "refs/heads/master",
38+
refName: "master",
3739
},
3840
{
3941
eventName: "pull_request",
4042
event: map[string]interface{}{
4143
"number": 1234.,
4244
},
43-
ref: "refs/pull/1234/merge",
45+
ref: "refs/pull/1234/merge",
46+
refName: "1234/merge",
4447
},
4548
{
4649
eventName: "deployment",
@@ -49,7 +52,8 @@ func TestSetRef(t *testing.T) {
4952
"ref": "refs/heads/somebranch",
5053
},
5154
},
52-
ref: "refs/heads/somebranch",
55+
ref: "refs/heads/somebranch",
56+
refName: "somebranch",
5357
},
5458
{
5559
eventName: "release",
@@ -58,14 +62,16 @@ func TestSetRef(t *testing.T) {
5862
"tag_name": "v1.0.0",
5963
},
6064
},
61-
ref: "v1.0.0",
65+
ref: "refs/tags/v1.0.0",
66+
refName: "v1.0.0",
6267
},
6368
{
6469
eventName: "push",
6570
event: map[string]interface{}{
6671
"ref": "refs/heads/somebranch",
6772
},
68-
ref: "refs/heads/somebranch",
73+
ref: "refs/heads/somebranch",
74+
refName: "somebranch",
6975
},
7076
{
7177
eventName: "unknown",
@@ -74,12 +80,14 @@ func TestSetRef(t *testing.T) {
7480
"default_branch": "main",
7581
},
7682
},
77-
ref: "refs/heads/main",
83+
ref: "refs/heads/main",
84+
refName: "main",
7885
},
7986
{
8087
eventName: "no-event",
8188
event: map[string]interface{}{},
8289
ref: "refs/heads/master",
90+
refName: "master",
8391
},
8492
}
8593

@@ -92,8 +100,10 @@ func TestSetRef(t *testing.T) {
92100
}
93101

94102
ghc.SetRef(context.Background(), "main", "/some/dir")
103+
ghc.SetRefTypeAndName()
95104

96105
assert.Equal(t, table.ref, ghc.Ref)
106+
assert.Equal(t, table.refName, ghc.RefName)
97107
})
98108
}
99109

Diff for: pkg/runner/run_context_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ func TestGetGithubContextRef(t *testing.T) {
413413
{event: "pull_request_target", json: `{"pull_request":{"base":{"ref": "main"}}}`, ref: "refs/heads/main"},
414414
{event: "deployment", json: `{"deployment": {"ref": "tag-name"}}`, ref: "tag-name"},
415415
{event: "deployment_status", json: `{"deployment": {"ref": "tag-name"}}`, ref: "tag-name"},
416-
{event: "release", json: `{"release": {"tag_name": "tag-name"}}`, ref: "tag-name"},
416+
{event: "release", json: `{"release": {"tag_name": "tag-name"}}`, ref: "refs/tags/tag-name"},
417417
}
418418

419419
for _, data := range table {

0 commit comments

Comments
 (0)