Skip to content

Commit 77624f4

Browse files
committed
Fix panic in PAC controller on delete tag event in gitlab
fixed panic in PAC controller when a tag delete event occurs in a repository on gitlab. E2E test is added as well to confirm this behaviour. https://issues.redhat.com/browse/SRVKP-6636 Signed-off-by: Zaki Shaikh <[email protected]>
1 parent 5ab1fc7 commit 77624f4

File tree

4 files changed

+104
-4
lines changed

4 files changed

+104
-4
lines changed

pkg/provider/gitlab/parse_payload.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ func (v *Provider) ParsePayload(_ context.Context, _ *params.Run, request *http.
6161
processedEvent.TargetProjectID = gitEvent.Project.ID
6262
processedEvent.EventType = strings.ReplaceAll(event, " Hook", "")
6363
case *gitlab.TagEvent:
64+
// GitLab sends same event for both Tag creation and deletion i.e. "Tag Push Hook".
65+
// if gitEvent.After is containing all zeros and gitEvent.CheckoutSHA is empty
66+
// it is Delete "Tag Push Hook".
67+
if isZeroSHA(gitEvent.After) && gitEvent.CheckoutSHA == "" {
68+
return nil, fmt.Errorf("event Delete %s is not supported", event)
69+
}
6470
lastCommitIdx := len(gitEvent.Commits) - 1
6571
processedEvent.Sender = gitEvent.UserUsername
6672
processedEvent.DefaultBranch = gitEvent.Project.DefaultBranch
@@ -136,3 +142,7 @@ func (v *Provider) ParsePayload(_ context.Context, _ *params.Run, request *http.
136142
v.repoURL = processedEvent.URL
137143
return processedEvent, nil
138144
}
145+
146+
func isZeroSHA(sha string) bool {
147+
return sha == "0000000000000000000000000000000000000000"
148+
}

test/gitlab_delete_tag_event_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//go:build e2e
2+
// +build e2e
3+
4+
package test
5+
6+
import (
7+
"context"
8+
"net/http"
9+
"regexp"
10+
"testing"
11+
12+
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/cctx"
13+
tgitlab "github.com/openshift-pipelines/pipelines-as-code/test/pkg/gitlab"
14+
twait "github.com/openshift-pipelines/pipelines-as-code/test/pkg/wait"
15+
"github.com/tektoncd/pipeline/pkg/names"
16+
"github.com/xanzy/go-gitlab"
17+
"gotest.tools/v3/assert"
18+
)
19+
20+
func TestGitlabDeleteTagEvent(t *testing.T) {
21+
targetNS := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-ns")
22+
ctx := context.Background()
23+
runcnx, opts, glprovider, err := tgitlab.Setup(ctx)
24+
assert.NilError(t, err)
25+
ctx, err = cctx.GetControllerCtxInfo(ctx, runcnx)
26+
assert.NilError(t, err)
27+
runcnx.Clients.Log.Info("Testing with Gitlab")
28+
29+
projectinfo, resp, err := glprovider.Client.Projects.GetProject(opts.ProjectID, nil)
30+
assert.NilError(t, err)
31+
if resp != nil && resp.StatusCode == http.StatusNotFound {
32+
t.Errorf("Repository %s not found in %s", opts.Organization, opts.Repo)
33+
}
34+
defer tgitlab.TearDown(ctx, t, runcnx, glprovider, -1, "", targetNS, opts.ProjectID)
35+
36+
err = tgitlab.CreateCRD(ctx, projectinfo, runcnx, targetNS, nil)
37+
assert.NilError(t, err)
38+
39+
tagName := "v1.0-test"
40+
err = tgitlab.CreateTag(glprovider.Client, projectinfo.ID, tagName)
41+
assert.NilError(t, err)
42+
runcnx.Clients.Log.Infof("Created Tag %s in %s repository", tagName, projectinfo.Name)
43+
44+
err = tgitlab.DeleteTag(glprovider.Client, projectinfo.ID, tagName)
45+
assert.NilError(t, err)
46+
runcnx.Clients.Log.Infof("Deleted Tag %s in %s repository", tagName, projectinfo.Name)
47+
48+
reg := regexp.MustCompile("event Delete Tag Push Hook is not supported*")
49+
err = twait.RegexpMatchingInControllerLog(ctx, runcnx, *reg, 10, "controller", gitlab.Ptr(int64(20)))
50+
assert.NilError(t, err)
51+
}
52+
53+
// Local Variables:
54+
// compile-command: "go test -tags=e2e -v -run TestGitlabDeleteTagEvent$ ."
55+
// End:

test/pkg/gitlab/setup.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,19 @@ func TearDown(ctx context.Context, t *testing.T, runcnx *params.Run, glprovider
7272
runcnx.Clients.Log.Infof("Not cleaning up and closing PR since TEST_NOCLEANUP is set")
7373
return
7474
}
75-
runcnx.Clients.Log.Infof("Closing PR %d", mrNumber)
75+
7676
if mrNumber != -1 {
77+
runcnx.Clients.Log.Infof("Closing PR %d", mrNumber)
7778
_, _, err := glprovider.Client.MergeRequests.UpdateMergeRequest(projectid, mrNumber,
7879
&gitlab2.UpdateMergeRequestOptions{StateEvent: gitlab2.Ptr("close")})
7980
if err != nil {
8081
t.Fatal(err)
8182
}
8283
}
8384
repository.NSTearDown(ctx, t, runcnx, targetNS)
84-
runcnx.Clients.Log.Infof("Deleting Ref %s", ref)
85-
_, err := glprovider.Client.Branches.DeleteBranch(projectid, ref)
86-
assert.NilError(t, err)
85+
if ref != "" {
86+
runcnx.Clients.Log.Infof("Deleting Ref %s", ref)
87+
_, err := glprovider.Client.Branches.DeleteBranch(projectid, ref)
88+
assert.NilError(t, err)
89+
}
8790
}

test/pkg/gitlab/test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package gitlab
22

33
import (
4+
"fmt"
5+
"net/http"
6+
47
ghlib "github.com/xanzy/go-gitlab"
58
)
69

@@ -15,3 +18,32 @@ func CreateMR(client *ghlib.Client, pid int, sourceBranch, targetBranch, title s
1518
}
1619
return mr.IID, nil
1720
}
21+
22+
func CreateTag(client *ghlib.Client, pid int, tagName string) error {
23+
_, resp, err := client.Tags.CreateTag(pid, &ghlib.CreateTagOptions{
24+
TagName: ghlib.Ptr(tagName),
25+
Ref: ghlib.Ptr("main"),
26+
})
27+
if err != nil {
28+
return err
29+
}
30+
31+
if resp.StatusCode != http.StatusCreated {
32+
return fmt.Errorf("failed to create tag : %d", resp.StatusCode)
33+
}
34+
35+
return nil
36+
}
37+
38+
func DeleteTag(client *ghlib.Client, pid int, tagName string) error {
39+
resp, err := client.Tags.DeleteTag(pid, tagName)
40+
if err != nil {
41+
return err
42+
}
43+
44+
if resp.StatusCode != http.StatusNoContent {
45+
return fmt.Errorf("failed to delete tag : %d", resp.StatusCode)
46+
}
47+
48+
return nil
49+
}

0 commit comments

Comments
 (0)