Skip to content

Commit

Permalink
add support for bitbucketcloud
Browse files Browse the repository at this point in the history
TBC on Monday the christmas FML

Signed-off-by: Chmouel Boudjnah <[email protected]>
  • Loading branch information
chmouel committed Dec 20, 2024
1 parent 3495b53 commit f086178
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 19 deletions.
15 changes: 13 additions & 2 deletions pkg/provider/bitbucketcloud/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/provider/bitbucketcloud/parse_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions pkg/provider/bitbucketcloud/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type PullRequest struct {
ID int `json:"id"`
Links Links
Title string `json:"title"`
State string `json:"state"`
}

type PullRequestEvent struct {
Expand Down
65 changes: 61 additions & 4 deletions test/bitbucket_cloud_pullrequest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -27,21 +31,74 @@ 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,
SHA: hash,
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:
Expand Down
12 changes: 4 additions & 8 deletions test/pkg/bitbucketcloud/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := "[email protected]"

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{
Expand Down

0 comments on commit f086178

Please sign in to comment.