From 6744e68ee29d3bcb6878f895d6ccb5f9f8e2c478 Mon Sep 17 00:00:00 2001 From: Markus Wolf Date: Thu, 9 Mar 2023 21:03:13 +0100 Subject: [PATCH] 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 --- pkg/model/github_context.go | 5 ++++- pkg/model/github_context_test.go | 20 +++++++++++++++----- pkg/runner/run_context_test.go | 2 +- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/pkg/model/github_context.go b/pkg/model/github_context.go index 9ce8d08a490..e4c31fcc048 100644 --- a/pkg/model/github_context.go +++ b/pkg/model/github_context.go @@ -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: @@ -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 == "" { diff --git a/pkg/model/github_context_test.go b/pkg/model/github_context_test.go index 29bb546a98f..ed08e2319cd 100644 --- a/pkg/model/github_context_test.go +++ b/pkg/model/github_context_test.go @@ -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", @@ -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", @@ -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", @@ -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", }, } @@ -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) }) } diff --git a/pkg/runner/run_context_test.go b/pkg/runner/run_context_test.go index e705946151e..3e26a02245c 100644 --- a/pkg/runner/run_context_test.go +++ b/pkg/runner/run_context_test.go @@ -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 {