From f08617877107ceef9168d0dc92d04667c494c067 Mon Sep 17 00:00:00 2001 From: Chmouel Boudjnah Date: Fri, 20 Dec 2024 18:30:26 +0100 Subject: [PATCH] add support for bitbucketcloud TBC on Monday the christmas FML Signed-off-by: Chmouel Boudjnah --- pkg/provider/bitbucketcloud/detect.go | 15 ++++- pkg/provider/bitbucketcloud/parse_payload.go | 11 ++-- pkg/provider/bitbucketcloud/types/types.go | 1 + test/bitbucket_cloud_pullrequest_test.go | 65 ++++++++++++++++++-- test/pkg/bitbucketcloud/pr.go | 12 ++-- 5 files changed, 85 insertions(+), 19 deletions(-) diff --git a/pkg/provider/bitbucketcloud/detect.go b/pkg/provider/bitbucketcloud/detect.go index 7726ca745..2f96b8233 100644 --- a/pkg/provider/bitbucketcloud/detect.go +++ b/pkg/provider/bitbucketcloud/detect.go @@ -10,6 +10,12 @@ import ( "go.uber.org/zap" ) +var ( + pullRequestsClosed = []string{"pullrequest:closed", "pullrequest:fulfilled", "pullrequest:rejected"} + pullRequestsCreated = []string{"pullrequest:created", "pullrequest:updated"} + pullRequestsCommentCreated = []string{"pullrequest:comment_created"} +) + func (v *Provider) Detect(req *http.Request, payload string, logger *zap.SugaredLogger) (bool, bool, *zap.SugaredLogger, string, error) { isBitCloud := false reqHeader := req.Header @@ -37,10 +43,15 @@ func (v *Provider) Detect(req *http.Request, payload string, logger *zap.Sugared switch e := eventInt.(type) { case *types.PullRequestEvent: - if provider.Valid(event, []string{"pullrequest:created", "pullrequest:updated"}) { + if provider.Valid(event, pullRequestsClosed) { + return setLoggerAndProceed(true, "", nil) + } + + if provider.Valid(event, pullRequestsCreated) { return setLoggerAndProceed(true, "", nil) } - if provider.Valid(event, []string{"pullrequest:comment_created"}) { + + if provider.Valid(event, pullRequestsCommentCreated) { if provider.IsTestRetestComment(e.Comment.Content.Raw) { return setLoggerAndProceed(true, "", nil) } diff --git a/pkg/provider/bitbucketcloud/parse_payload.go b/pkg/provider/bitbucketcloud/parse_payload.go index df25a5858..c45a4e356 100644 --- a/pkg/provider/bitbucketcloud/parse_payload.go +++ b/pkg/provider/bitbucketcloud/parse_payload.go @@ -125,15 +125,16 @@ func (v *Provider) ParsePayload(ctx context.Context, run *params.Run, request *h } processedEvent.Event = eventInt - switch e := eventInt.(type) { case *types.PullRequestEvent: - if provider.Valid(event, []string{"pullrequest:created", "pullrequest:updated"}) { - processedEvent.TriggerTarget = triggertype.PullRequest + processedEvent.TriggerTarget = triggertype.PullRequest + switch { + case provider.Valid(event, pullRequestsCreated): processedEvent.EventType = triggertype.PullRequest.String() - } else if provider.Valid(event, []string{"pullrequest:comment_created"}) { - processedEvent.TriggerTarget = triggertype.PullRequest + case provider.Valid(event, pullRequestsCommentCreated): opscomments.SetEventTypeAndTargetPR(processedEvent, e.Comment.Content.Raw) + case provider.Valid(event, pullRequestsClosed): + processedEvent.EventType = string(triggertype.PullRequestClosed) } processedEvent.Organization = e.Repository.Workspace.Slug processedEvent.Repository = strings.Split(e.Repository.FullName, "/")[1] diff --git a/pkg/provider/bitbucketcloud/types/types.go b/pkg/provider/bitbucketcloud/types/types.go index 29a2354c0..dd34db076 100644 --- a/pkg/provider/bitbucketcloud/types/types.go +++ b/pkg/provider/bitbucketcloud/types/types.go @@ -55,6 +55,7 @@ type PullRequest struct { ID int `json:"id"` Links Links Title string `json:"title"` + State string `json:"state"` } type PullRequestEvent struct { diff --git a/test/bitbucket_cloud_pullrequest_test.go b/test/bitbucket_cloud_pullrequest_test.go index d51e4f025..d54094243 100644 --- a/test/bitbucket_cloud_pullrequest_test.go +++ b/test/bitbucket_cloud_pullrequest_test.go @@ -5,11 +5,15 @@ package test import ( "context" + "fmt" "testing" + "github.com/ktrysmt/go-bitbucket" "github.com/openshift-pipelines/pipelines-as-code/pkg/params/triggertype" tbb "github.com/openshift-pipelines/pipelines-as-code/test/pkg/bitbucketcloud" - "github.com/openshift-pipelines/pipelines-as-code/test/pkg/wait" + "github.com/openshift-pipelines/pipelines-as-code/test/pkg/options" + "github.com/openshift-pipelines/pipelines-as-code/test/pkg/payload" + twait "github.com/openshift-pipelines/pipelines-as-code/test/pkg/wait" "github.com/tektoncd/pipeline/pkg/names" "gotest.tools/v3/assert" ) @@ -27,13 +31,18 @@ func TestBitbucketCloudPullRequest(t *testing.T) { targetRefName := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-test") title := "TestPullRequest - " + targetRefName - pr, repobranch := tbb.MakePR(t, bprovider, runcnx, bcrepo, opts, title, targetNS, targetRefName) + entries, err := payload.GetEntries( + map[string]string{".tekton/pipelinerun.yaml": "testdata/pipelinerun.yaml"}, + targetNS, options.MainBranch, triggertype.PullRequest.String(), map[string]string{}) + assert.NilError(t, err) + + pr, repobranch := tbb.MakePR(t, bprovider, runcnx, bcrepo, opts, title, targetRefName, entries) defer tbb.TearDown(ctx, t, runcnx, bprovider, opts, pr.ID, targetRefName, targetNS) hash, ok := repobranch.Target["hash"].(string) assert.Assert(t, ok) - sopt := wait.SuccessOpt{ + sopt := twait.SuccessOpt{ TargetNS: targetNS, OnEvent: triggertype.PullRequest.String(), NumberofPRMatch: 1, @@ -41,7 +50,55 @@ func TestBitbucketCloudPullRequest(t *testing.T) { Title: title, MinNumberStatus: 1, } - wait.Succeeded(ctx, t, runcnx, opts, sopt) + twait.Succeeded(ctx, t, runcnx, opts, sopt) +} + +func TestBitbucketCloudPullRequestCancelInProgressMerged(t *testing.T) { + targetNS := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-ns") + ctx := context.Background() + + runcnx, opts, bprovider, err := tbb.Setup(ctx) + if err != nil { + t.Skip(err.Error()) + return + } + bcrepo := tbb.CreateCRD(ctx, t, bprovider, runcnx, opts, targetNS) + targetRefName := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-test") + title := "TestPullRequest - " + targetRefName + + entries, err := payload.GetEntries( + map[string]string{".tekton/pipelinerun-on-label.yaml": "testdata/pipelinerun-cancel-in-progress.yaml"}, + targetNS, options.MainBranch, triggertype.PullRequest.String(), map[string]string{}) + assert.NilError(t, err) + + pr, repobranch := tbb.MakePR(t, bprovider, runcnx, bcrepo, opts, title, targetRefName, entries) + defer tbb.TearDown(ctx, t, runcnx, bprovider, opts, pr.ID, targetRefName, targetNS) + + sha, ok := repobranch.Target["hash"].(string) + assert.Assert(t, ok) + + waitOpts := twait.Opts{ + RepoName: targetNS, + Namespace: targetNS, + MinNumberStatus: 1, + PollTimeout: twait.DefaultTimeout, + TargetSHA: sha, + } + err = twait.UntilPipelineRunCreated(ctx, runcnx.Clients, waitOpts) + assert.NilError(t, err) + + po := &bitbucket.PullRequestsOptions{ + RepoSlug: opts.Repo, + Owner: opts.Organization, + ID: fmt.Sprintf("%d", pr.ID), + } + _, err = bprovider.Client.Repositories.PullRequests.Decline(po) + assert.NilError(t, err) + + // _, _, err = glprovider.Client.MergeRequests.UpdateMergeRequest(opts.ProjectID, mrID, &clientGitlab.UpdateMergeRequestOptions{ + // StateEvent: clientGitlab.Ptr("close"), + // }) + // assert.NilError(t, err) } // Local Variables: diff --git a/test/pkg/bitbucketcloud/pr.go b/test/pkg/bitbucketcloud/pr.go index 3059d6f2d..8fc858ac4 100644 --- a/test/pkg/bitbucketcloud/pr.go +++ b/test/pkg/bitbucketcloud/pr.go @@ -7,27 +7,23 @@ import ( "github.com/ktrysmt/go-bitbucket" "github.com/mitchellh/mapstructure" "github.com/openshift-pipelines/pipelines-as-code/pkg/params" - "github.com/openshift-pipelines/pipelines-as-code/pkg/params/triggertype" "github.com/openshift-pipelines/pipelines-as-code/pkg/provider/bitbucketcloud" "github.com/openshift-pipelines/pipelines-as-code/pkg/provider/bitbucketcloud/types" "github.com/openshift-pipelines/pipelines-as-code/test/pkg/options" - "github.com/openshift-pipelines/pipelines-as-code/test/pkg/payload" "gotest.tools/v3/assert" "gotest.tools/v3/fs" ) -func MakePR(t *testing.T, bprovider bitbucketcloud.Provider, runcnx *params.Run, bcrepo *bitbucket.Repository, opts options.E2E, title, targetNS, targetRefName string) (*types.PullRequest, *bitbucket.RepositoryBranch) { +func MakePR(t *testing.T, bprovider bitbucketcloud.Provider, runcnx *params.Run, bcrepo *bitbucket.Repository, opts options.E2E, title, targetRefName string, + entries map[string]string, +) (*types.PullRequest, *bitbucket.RepositoryBranch) { commitAuthor := "OpenShift Pipelines E2E test" commitEmail := "e2e-pipelines@redhat.com" - entries, err := payload.GetEntries( - map[string]string{".tekton/pipelinerun.yaml": "testdata/pipelinerun.yaml"}, - targetNS, options.MainBranch, triggertype.PullRequest.String(), map[string]string{}) - assert.NilError(t, err) tmpfile := fs.NewFile(t, "pipelinerun", fs.WithContent(entries[".tekton/pipelinerun.yaml"])) defer tmpfile.Remove() - err = bprovider.Client.Workspaces.Repositories.Repository.WriteFileBlob(&bitbucket.RepositoryBlobWriteOptions{ + err := bprovider.Client.Workspaces.Repositories.Repository.WriteFileBlob(&bitbucket.RepositoryBlobWriteOptions{ Owner: opts.Organization, RepoSlug: opts.Repo, Files: []bitbucket.File{