Skip to content

Commit

Permalink
Fix panic in PAC controller on delete tag event in gitlab
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
zakisk committed Oct 18, 2024
1 parent 5ab1fc7 commit 77624f4
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 4 deletions.
10 changes: 10 additions & 0 deletions pkg/provider/gitlab/parse_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ func (v *Provider) ParsePayload(_ context.Context, _ *params.Run, request *http.
processedEvent.TargetProjectID = gitEvent.Project.ID
processedEvent.EventType = strings.ReplaceAll(event, " Hook", "")
case *gitlab.TagEvent:
// GitLab sends same event for both Tag creation and deletion i.e. "Tag Push Hook".
// if gitEvent.After is containing all zeros and gitEvent.CheckoutSHA is empty
// it is Delete "Tag Push Hook".
if isZeroSHA(gitEvent.After) && gitEvent.CheckoutSHA == "" {
return nil, fmt.Errorf("event Delete %s is not supported", event)
}
lastCommitIdx := len(gitEvent.Commits) - 1
processedEvent.Sender = gitEvent.UserUsername
processedEvent.DefaultBranch = gitEvent.Project.DefaultBranch
Expand Down Expand Up @@ -136,3 +142,7 @@ func (v *Provider) ParsePayload(_ context.Context, _ *params.Run, request *http.
v.repoURL = processedEvent.URL
return processedEvent, nil
}

func isZeroSHA(sha string) bool {
return sha == "0000000000000000000000000000000000000000"
}
55 changes: 55 additions & 0 deletions test/gitlab_delete_tag_event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//go:build e2e
// +build e2e

package test

import (
"context"
"net/http"
"regexp"
"testing"

"github.com/openshift-pipelines/pipelines-as-code/test/pkg/cctx"
tgitlab "github.com/openshift-pipelines/pipelines-as-code/test/pkg/gitlab"
twait "github.com/openshift-pipelines/pipelines-as-code/test/pkg/wait"
"github.com/tektoncd/pipeline/pkg/names"
"github.com/xanzy/go-gitlab"
"gotest.tools/v3/assert"
)

func TestGitlabDeleteTagEvent(t *testing.T) {
targetNS := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-ns")
ctx := context.Background()
runcnx, opts, glprovider, err := tgitlab.Setup(ctx)
assert.NilError(t, err)
ctx, err = cctx.GetControllerCtxInfo(ctx, runcnx)
assert.NilError(t, err)
runcnx.Clients.Log.Info("Testing with Gitlab")

projectinfo, resp, err := glprovider.Client.Projects.GetProject(opts.ProjectID, nil)
assert.NilError(t, err)
if resp != nil && resp.StatusCode == http.StatusNotFound {
t.Errorf("Repository %s not found in %s", opts.Organization, opts.Repo)
}
defer tgitlab.TearDown(ctx, t, runcnx, glprovider, -1, "", targetNS, opts.ProjectID)

err = tgitlab.CreateCRD(ctx, projectinfo, runcnx, targetNS, nil)
assert.NilError(t, err)

tagName := "v1.0-test"
err = tgitlab.CreateTag(glprovider.Client, projectinfo.ID, tagName)
assert.NilError(t, err)
runcnx.Clients.Log.Infof("Created Tag %s in %s repository", tagName, projectinfo.Name)

err = tgitlab.DeleteTag(glprovider.Client, projectinfo.ID, tagName)
assert.NilError(t, err)
runcnx.Clients.Log.Infof("Deleted Tag %s in %s repository", tagName, projectinfo.Name)

reg := regexp.MustCompile("event Delete Tag Push Hook is not supported*")
err = twait.RegexpMatchingInControllerLog(ctx, runcnx, *reg, 10, "controller", gitlab.Ptr(int64(20)))
assert.NilError(t, err)
}

// Local Variables:
// compile-command: "go test -tags=e2e -v -run TestGitlabDeleteTagEvent$ ."
// End:
11 changes: 7 additions & 4 deletions test/pkg/gitlab/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,19 @@ func TearDown(ctx context.Context, t *testing.T, runcnx *params.Run, glprovider
runcnx.Clients.Log.Infof("Not cleaning up and closing PR since TEST_NOCLEANUP is set")
return
}
runcnx.Clients.Log.Infof("Closing PR %d", mrNumber)

if mrNumber != -1 {
runcnx.Clients.Log.Infof("Closing PR %d", mrNumber)
_, _, err := glprovider.Client.MergeRequests.UpdateMergeRequest(projectid, mrNumber,
&gitlab2.UpdateMergeRequestOptions{StateEvent: gitlab2.Ptr("close")})
if err != nil {
t.Fatal(err)
}
}
repository.NSTearDown(ctx, t, runcnx, targetNS)
runcnx.Clients.Log.Infof("Deleting Ref %s", ref)
_, err := glprovider.Client.Branches.DeleteBranch(projectid, ref)
assert.NilError(t, err)
if ref != "" {
runcnx.Clients.Log.Infof("Deleting Ref %s", ref)
_, err := glprovider.Client.Branches.DeleteBranch(projectid, ref)
assert.NilError(t, err)
}
}
32 changes: 32 additions & 0 deletions test/pkg/gitlab/test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package gitlab

import (
"fmt"
"net/http"

ghlib "github.com/xanzy/go-gitlab"
)

Expand All @@ -15,3 +18,32 @@ func CreateMR(client *ghlib.Client, pid int, sourceBranch, targetBranch, title s
}
return mr.IID, nil
}

func CreateTag(client *ghlib.Client, pid int, tagName string) error {
_, resp, err := client.Tags.CreateTag(pid, &ghlib.CreateTagOptions{
TagName: ghlib.Ptr(tagName),
Ref: ghlib.Ptr("main"),
})
if err != nil {
return err
}

if resp.StatusCode != http.StatusCreated {
return fmt.Errorf("failed to create tag : %d", resp.StatusCode)
}

return nil
}

func DeleteTag(client *ghlib.Client, pid int, tagName string) error {
resp, err := client.Tags.DeleteTag(pid, tagName)
if err != nil {
return err
}

if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("failed to delete tag : %d", resp.StatusCode)
}

return nil
}

0 comments on commit 77624f4

Please sign in to comment.