Skip to content

Commit

Permalink
fix: correct ref and ref_name (nektos#1672)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
KnisterPeter authored Mar 9, 2023
1 parent ac5dd8f commit 6744e68
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
5 changes: 4 additions & 1 deletion pkg/model/github_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (ghc *GithubContext) SetRef(ctx context.Context, defaultBranch string, repo
case "deployment", "deployment_status":
ghc.Ref = asString(nestedMapLookup(ghc.Event, "deployment", "ref"))
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":
ghc.Ref = asString(ghc.Event["ref"])
default:
Expand Down Expand Up @@ -183,6 +183,9 @@ func (ghc *GithubContext) SetRefTypeAndName() {
} else if strings.HasPrefix(ghc.Ref, "refs/heads/") {
refType = "branch"
refName = ghc.Ref[len("refs/heads/"):]
} else if strings.HasPrefix(ghc.Ref, "refs/pull/") {
refType = ""
refName = ghc.Ref[len("refs/pull/"):]
}

if ghc.RefType == "" {
Expand Down
20 changes: 15 additions & 5 deletions pkg/model/github_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,21 @@ func TestSetRef(t *testing.T) {
eventName string
event map[string]interface{}
ref string
refName string
}{
{
eventName: "pull_request_target",
event: map[string]interface{}{},
ref: "refs/heads/master",
refName: "master",
},
{
eventName: "pull_request",
event: map[string]interface{}{
"number": 1234.,
},
ref: "refs/pull/1234/merge",
ref: "refs/pull/1234/merge",
refName: "1234/merge",
},
{
eventName: "deployment",
Expand All @@ -49,7 +52,8 @@ func TestSetRef(t *testing.T) {
"ref": "refs/heads/somebranch",
},
},
ref: "refs/heads/somebranch",
ref: "refs/heads/somebranch",
refName: "somebranch",
},
{
eventName: "release",
Expand All @@ -58,14 +62,16 @@ func TestSetRef(t *testing.T) {
"tag_name": "v1.0.0",
},
},
ref: "v1.0.0",
ref: "refs/tags/v1.0.0",
refName: "v1.0.0",
},
{
eventName: "push",
event: map[string]interface{}{
"ref": "refs/heads/somebranch",
},
ref: "refs/heads/somebranch",
ref: "refs/heads/somebranch",
refName: "somebranch",
},
{
eventName: "unknown",
Expand All @@ -74,12 +80,14 @@ func TestSetRef(t *testing.T) {
"default_branch": "main",
},
},
ref: "refs/heads/main",
ref: "refs/heads/main",
refName: "main",
},
{
eventName: "no-event",
event: map[string]interface{}{},
ref: "refs/heads/master",
refName: "master",
},
}

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

ghc.SetRef(context.Background(), "main", "/some/dir")
ghc.SetRefTypeAndName()

assert.Equal(t, table.ref, ghc.Ref)
assert.Equal(t, table.refName, ghc.RefName)
})
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/runner/run_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ func TestGetGithubContextRef(t *testing.T) {
{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_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 {
Expand Down

0 comments on commit 6744e68

Please sign in to comment.