diff --git a/.github/workflows/kind-e2e-tests.yaml b/.github/workflows/kind-e2e-tests.yaml index 664229e78..70fafcd94 100644 --- a/.github/workflows/kind-e2e-tests.yaml +++ b/.github/workflows/kind-e2e-tests.yaml @@ -10,6 +10,9 @@ on: description: "Run the build with tmate debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate)" required: false default: false + push: + branches: + - SRVKP-6636-test pull_request_target: types: [opened, synchronize, reopened] paths: @@ -47,13 +50,6 @@ jobs: run: | nohup gosmee client --saveDir /tmp/gosmee-replay ${{ secrets.PYSMEE_URL }} "http://${CONTROLLER_DOMAIN_URL}" & - - name: Setup tmate session - uses: mxschmitt/action-tmate@v3 - if: ${{ github.event_name == 'workflow_dispatch' && inputs.debug_enabled }} - with: - detached: true - limit-access-to-actor: true - - name: Start installing cluster run: | export PAC_DIR=${PWD} @@ -118,4 +114,7 @@ jobs: status: ${{ job.status }} notify_when: "failure" env: - SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} + + - name: Setup tmate session + uses: mxschmitt/action-tmate@v3 diff --git a/hack/gh-workflow-ci.sh b/hack/gh-workflow-ci.sh index 459f53bc5..ba5133287 100755 --- a/hack/gh-workflow-ci.sh +++ b/hack/gh-workflow-ci.sh @@ -109,10 +109,10 @@ run_e2e_tests() { export TEST_GITHUB_SECOND_TOKEN="${test_github_second_token}" export TEST_GITLAB_API_URL="https://gitlab.com" - export TEST_GITLAB_PROJECT_ID="34405323" + export TEST_GITLAB_PROJECT_ID="62404501" export TEST_GITLAB_TOKEN=${gitlab_token} # https://gitlab.com/gitlab-com/alliances/ibm-red-hat/sandbox/openshift-pipelines/pac-e2e-tests - make test-e2e + go test -tags=e2e ./test/ -v -run ^TestGitlabDeleteTagEvent$ } collect_logs() { diff --git a/pkg/provider/gitlab/parse_payload.go b/pkg/provider/gitlab/parse_payload.go index c22328126..4c04b2e5e 100644 --- a/pkg/provider/gitlab/parse_payload.go +++ b/pkg/provider/gitlab/parse_payload.go @@ -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 @@ -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" +} diff --git a/test/bitbucket_cloud_pullrequest_test.go b/test/bitbucket_cloud_pullrequest_test.go index d51e4f025..502855e80 100644 --- a/test/bitbucket_cloud_pullrequest_test.go +++ b/test/bitbucket_cloud_pullrequest_test.go @@ -15,6 +15,7 @@ import ( ) func TestBitbucketCloudPullRequest(t *testing.T) { + t.Skip("Skipping for my test") targetNS := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-ns") ctx := context.Background() diff --git a/test/gitlab_delete_tag_event_test.go b/test/gitlab_delete_tag_event_test.go new file mode 100644 index 000000000..26d5cd578 --- /dev/null +++ b/test/gitlab_delete_tag_event_test.go @@ -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: diff --git a/test/pkg/gitlab/setup.go b/test/pkg/gitlab/setup.go index 85d8df209..297dd6a95 100644 --- a/test/pkg/gitlab/setup.go +++ b/test/pkg/gitlab/setup.go @@ -72,8 +72,9 @@ 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 { @@ -81,7 +82,9 @@ func TearDown(ctx context.Context, t *testing.T, runcnx *params.Run, glprovider } } 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) + } } diff --git a/test/pkg/gitlab/test.go b/test/pkg/gitlab/test.go index 4ffe7b436..ca65acf0f 100644 --- a/test/pkg/gitlab/test.go +++ b/test/pkg/gitlab/test.go @@ -1,6 +1,9 @@ package gitlab import ( + "fmt" + "net/http" + ghlib "github.com/xanzy/go-gitlab" ) @@ -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 +}