Skip to content

Commit b78ba64

Browse files
committed
Revert on-path-change on pr merge in bitbucket
This reverts commit f9fd3f3. Reason for Revert: The previous fix for SRVKP-7432 introduced an unexpected bug (SRVKP-8616) where the `{{ revision }}` dynamic variable in Pipelines as Code no longer fetches the correct, newest merge commit in Bitbucket Data Center. Instead, it was fetching the last commit of the source branch. Customer feedback indicates this change breaks their CI/CD workflow, as their teams rely on the merge-commit ID and do not wish to alter their git strategies. This is a critical blocker for them, preventing them from upgrading to versions 1.19.x and accessing other important fixes, such as the one for CAP-724. This revert restores the previous behavior, ensuring the `{{ revision }}` variable correctly references the merge commit. A long-term solution to address both issues (SRVKP-7432 and SRVKP-8616) will be developed in separate, newly-created tickets (SRVKP-8619 and SRVKP-8620). Fixes: SRVKP-8616 Related: SRVKP-7432, SRVKP-8619, SRVKP-8620 Signed-off-by: Chmouel Boudjnah <[email protected]>
1 parent 9c2edcc commit b78ba64

File tree

7 files changed

+18
-164
lines changed

7 files changed

+18
-164
lines changed

pkg/provider/bitbucketdatacenter/parse_payload.go

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func sanitizeOwner(owner string) string {
8888
}
8989

9090
// ParsePayload parses the payload from the event.
91-
func (v *Provider) ParsePayload(_ context.Context, run *params.Run, request *http.Request,
91+
func (v *Provider) ParsePayload(_ context.Context, _ *params.Run, request *http.Request,
9292
payload string,
9393
) (*info.Event, error) {
9494
processedEvent := info.NewEvent()
@@ -176,27 +176,6 @@ func (v *Provider) ParsePayload(_ context.Context, run *params.Run, request *htt
176176
}
177177

178178
processedEvent.SHA = e.Changes[0].ToHash
179-
180-
// In Bitbucket Data Center, when a pull request is merged, it creates two commits in the repository:
181-
// 1. A merge commit, which is represented by `changes[0].ToHash`.
182-
// 2. The actual commit containing the changes from the source branch.
183-
//
184-
// However, the merge commit often does not contain any file changes itself,
185-
// which can cause issues when determining whether file modifications should trigger PipelineRuns.
186-
//
187-
// Typically, a regular (non-merge) commit has a single parent, but a merge commit has two parents:
188-
// - The first parent is the previous HEAD of the destination branch (the branch into which the PR was merged).
189-
// - The second parent is the HEAD of the source branch (the branch being merged).
190-
//
191-
// To correctly identify the actual commit that contains the changes (i.e., the source branch's HEAD),
192-
// we inspect `e.Commits[0]`, and if it has more than one parent, we take the second parent.
193-
// This helps ensure we reference the correct commit.
194-
if len(e.Commits) > 1 && len(e.Commits[0].Parents) > 1 {
195-
processedEvent.SHA = e.Commits[0].Parents[1].ID
196-
run.Clients.Log.Infof("Detected a merge commit as HEAD; "+
197-
"using second parent commit SHA %s (source branch HEAD) to target push event", e.Commits[0].Parents[1].ID)
198-
}
199-
200179
processedEvent.URL = e.Repository.Links.Self[0].Href
201180
processedEvent.BaseBranch = e.Changes[0].RefID
202181
processedEvent.HeadBranch = e.Changes[0].RefID

pkg/provider/bitbucketdatacenter/parse_payload_test.go

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
1010
bbv1test "github.com/openshift-pipelines/pipelines-as-code/pkg/provider/bitbucketdatacenter/test"
1111
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/bitbucketdatacenter/types"
12-
"github.com/openshift-pipelines/pipelines-as-code/pkg/test/logger"
1312
"gotest.tools/v3/assert"
1413
rtesting "knative.dev/pkg/reconciler/testing"
1514
)
@@ -571,7 +570,6 @@ func TestParsePayload(t *testing.T) {
571570
payloadEvent any
572571
expEvent *info.Event
573572
eventType string
574-
wantSHA string
575573
wantErrSubstr string
576574
rawStr string
577575
targetPipelinerun string
@@ -609,14 +607,12 @@ func TestParsePayload(t *testing.T) {
609607
eventType: "pr:opened",
610608
payloadEvent: bbv1test.MakePREvent(ev1, ""),
611609
expEvent: ev1,
612-
wantSHA: "abcd",
613610
},
614611
{
615612
name: "good/push",
616613
eventType: "repo:refs_changed",
617614
payloadEvent: bbv1test.MakePushEvent(ev1, []types.PushRequestEventChange{{ToHash: ev1.SHA, RefID: "base"}}, []types.Commit{{ID: ev1.SHA}}),
618615
expEvent: ev1,
619-
wantSHA: "abcd",
620616
},
621617
{
622618
name: "bad/changes are empty in push",
@@ -632,82 +628,37 @@ func TestParsePayload(t *testing.T) {
632628
expEvent: ev1,
633629
wantErrSubstr: "push event contains no commits; cannot proceed",
634630
},
635-
{
636-
name: "good/changes are empty in push",
637-
eventType: "repo:refs_changed",
638-
payloadEvent: bbv1test.MakePushEvent(ev1, []types.PushRequestEventChange{
639-
{
640-
Ref: types.Ref{ID: "refs/heads/main"},
641-
ToHash: "abcd",
642-
},
643-
}, []types.Commit{
644-
{
645-
Parents: []struct {
646-
ID string `json:"id"`
647-
DisplayID string `json:"displayId"`
648-
}{
649-
{
650-
ID: "efghabcd",
651-
DisplayID: "efgh",
652-
},
653-
{
654-
ID: "abcdefgh",
655-
DisplayID: "abcd",
656-
},
657-
},
658-
},
659-
{
660-
ID: "abcdefgh",
661-
Parents: []struct {
662-
ID string `json:"id"`
663-
DisplayID string `json:"displayId"`
664-
}{
665-
{
666-
ID: "weroiusf",
667-
DisplayID: "wero",
668-
},
669-
},
670-
},
671-
}),
672-
expEvent: ev1,
673-
wantSHA: "abcdefgh",
674-
},
675631
{
676632
name: "good/comment ok-to-test",
677633
eventType: "pr:comment:added",
678634
payloadEvent: bbv1test.MakePREvent(ev1, "/ok-to-test"),
679635
expEvent: ev1,
680-
wantSHA: "abcd",
681636
},
682637
{
683638
name: "good/comment test",
684639
eventType: "pr:comment:added",
685640
payloadEvent: bbv1test.MakePREvent(ev1, "/test"),
686641
expEvent: ev1,
687-
wantSHA: "abcd",
688642
},
689643
{
690644
name: "good/comment retest a pr",
691645
eventType: "pr:comment:added",
692646
payloadEvent: bbv1test.MakePREvent(ev1, "/retest dummy"),
693647
expEvent: ev1,
694648
targetPipelinerun: "dummy",
695-
wantSHA: "abcd",
696649
},
697650
{
698651
name: "good/comment cancel a pr",
699652
eventType: "pr:comment:added",
700653
payloadEvent: bbv1test.MakePREvent(ev1, "/cancel dummy"),
701654
expEvent: ev1,
702655
canceltargetPipelinerun: "dummy",
703-
wantSHA: "abcd",
704656
},
705657
{
706658
name: "good/comment cancel all",
707659
eventType: "pr:comment:added",
708660
payloadEvent: bbv1test.MakePREvent(ev1, "/cancel"),
709661
expEvent: ev1,
710-
wantSHA: "abcd",
711662
},
712663
{
713664
name: "branch/deleted with zero hash",
@@ -735,7 +686,6 @@ func TestParsePayload(t *testing.T) {
735686
run := &params.Run{
736687
Info: info.Info{},
737688
}
738-
run.Clients.Log, _ = logger.GetLogger()
739689
_b, err := json.Marshal(tt.payloadEvent)
740690
assert.NilError(t, err)
741691
payload := string(_b)
@@ -757,7 +707,6 @@ func TestParsePayload(t *testing.T) {
757707
return
758708
}
759709
// assert SHA ID
760-
assert.Equal(t, tt.wantSHA, got.SHA)
761710
assert.Equal(t, got.AccountID, tt.expEvent.AccountID)
762711

763712
// test that we got slashed

test/bitbucket_datacenter_pull_request_test.go

Lines changed: 4 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,12 @@ import (
99
"os"
1010
"testing"
1111

12-
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
1312
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/triggertype"
1413
tbbdc "github.com/openshift-pipelines/pipelines-as-code/test/pkg/bitbucketdatacenter"
15-
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/options"
16-
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/payload"
1714
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/wait"
1815

19-
"github.com/jenkins-x/go-scm/scm"
2016
"github.com/tektoncd/pipeline/pkg/names"
2117
"gotest.tools/v3/assert"
22-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2318
)
2419

2520
func TestBitbucketDataCenterPullRequest(t *testing.T) {
@@ -40,11 +35,9 @@ func TestBitbucketDataCenterPullRequest(t *testing.T) {
4035
files[fmt.Sprintf(".tekton/pipelinerun-%d.yaml", i)] = "testdata/pipelinerun.yaml"
4136
}
4237

43-
files, err = payload.GetEntries(files, targetNS, options.MainBranch, triggertype.PullRequest.String(), map[string]string{})
44-
assert.NilError(t, err)
45-
4638
pr := tbbdc.CreatePR(ctx, t, client, runcnx, opts, repo, files, bitbucketWSOwner, targetNS)
47-
defer tbbdc.TearDown(ctx, t, runcnx, client, pr, bitbucketWSOwner, targetNS)
39+
runcnx.Clients.Log.Infof("Pull Request with title '%s' is created", pr.Title)
40+
defer tbbdc.TearDown(ctx, t, runcnx, client, pr.Number, bitbucketWSOwner, targetNS)
4841

4942
successOpts := wait.SuccessOpt{
5043
TargetNS: targetNS,
@@ -71,11 +64,9 @@ func TestBitbucketDataCenterCELPathChangeInPullRequest(t *testing.T) {
7164
".tekton/pipelinerun.yaml": "testdata/pipelinerun-cel-path-changed.yaml",
7265
}
7366

74-
files, err = payload.GetEntries(files, targetNS, options.MainBranch, triggertype.PullRequest.String(), map[string]string{})
75-
assert.NilError(t, err)
76-
7767
pr := tbbdc.CreatePR(ctx, t, client, runcnx, opts, repo, files, bitbucketWSOwner, targetNS)
78-
defer tbbdc.TearDown(ctx, t, runcnx, client, pr, bitbucketWSOwner, targetNS)
68+
runcnx.Clients.Log.Infof("Pull Request with title '%s' is created", pr.Title)
69+
defer tbbdc.TearDown(ctx, t, runcnx, client, pr.Number, bitbucketWSOwner, targetNS)
7970

8071
successOpts := wait.SuccessOpt{
8172
TargetNS: targetNS,
@@ -85,64 +76,3 @@ func TestBitbucketDataCenterCELPathChangeInPullRequest(t *testing.T) {
8576
}
8677
wait.Succeeded(ctx, t, runcnx, opts, successOpts)
8778
}
88-
89-
func TestBitbucketDataCenterOnPathChangeAnnotationOnPRMerge(t *testing.T) {
90-
targetNS := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-ns")
91-
// this would be a temporary base branch for the pull request we're going to raise
92-
// we need this because we're going to merge the pull request so that after test
93-
// we can delete the temporary base branch and our main branch should not be affected
94-
// by this merge because we run the E2E frequently.
95-
tempBaseBranch := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-ns")
96-
97-
ctx := context.Background()
98-
bitbucketWSOwner := os.Getenv("TEST_BITBUCKET_SERVER_E2E_REPOSITORY")
99-
100-
ctx, runcnx, opts, client, err := tbbdc.Setup(ctx)
101-
assert.NilError(t, err)
102-
103-
repo := tbbdc.CreateCRD(ctx, t, client, runcnx, bitbucketWSOwner, targetNS)
104-
runcnx.Clients.Log.Infof("Repository %s has been created", repo.Name)
105-
defer tbbdc.TearDownNs(ctx, t, runcnx, targetNS)
106-
107-
branch, resp, err := client.Git.CreateRef(ctx, bitbucketWSOwner, tempBaseBranch, repo.Branch)
108-
assert.NilError(t, err, "error creating branch: http status code: %d : %v", resp.Status, err)
109-
runcnx.Clients.Log.Infof("Base branch %s has been created", branch.Name)
110-
111-
opts.BaseBranch = branch.Name
112-
113-
if os.Getenv("TEST_NOCLEANUP") != "true" {
114-
defer func() {
115-
_, err := client.Git.DeleteRef(ctx, bitbucketWSOwner, tempBaseBranch)
116-
assert.NilError(t, err, "error deleting branch: http status code: %d : %v", resp.Status, err)
117-
}()
118-
}
119-
120-
files := map[string]string{
121-
".tekton/pr.yaml": "testdata/pipelinerun-on-path-change.yaml",
122-
"doc/foo/bar/README.md": "README.md",
123-
}
124-
125-
files, err = payload.GetEntries(files, targetNS, tempBaseBranch, triggertype.Push.String(), map[string]string{})
126-
assert.NilError(t, err)
127-
128-
pr := tbbdc.CreatePR(ctx, t, client, runcnx, opts, repo, files, bitbucketWSOwner, targetNS)
129-
defer tbbdc.TearDown(ctx, t, runcnx, client, nil, bitbucketWSOwner, targetNS)
130-
131-
// merge the pull request so that we can get push event.
132-
_, err = client.PullRequests.Merge(ctx, bitbucketWSOwner, pr.Number, &scm.PullRequestMergeOptions{})
133-
assert.NilError(t, err)
134-
135-
successOpts := wait.SuccessOpt{
136-
TargetNS: targetNS,
137-
OnEvent: triggertype.Push.String(),
138-
NumberofPRMatch: 1,
139-
MinNumberStatus: 1,
140-
}
141-
wait.Succeeded(ctx, t, runcnx, opts, successOpts)
142-
143-
pipelineRuns, err := runcnx.Clients.Tekton.TektonV1().PipelineRuns(targetNS).List(ctx, metav1.ListOptions{})
144-
assert.NilError(t, err)
145-
assert.Equal(t, len(pipelineRuns.Items), 1)
146-
// check that pipeline run contains on-path-change annotation.
147-
assert.Equal(t, pipelineRuns.Items[0].GetAnnotations()[keys.OnPathChange], "[doc/***.md]")
148-
}

test/bitbucket_datacenter_push_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestBitbucketDataCenterCELPathChangeOnPush(t *testing.T) {
3939
branch, resp, err := client.Git.CreateRef(ctx, bitbucketWSOwner, targetNS, mainBranchRef)
4040
assert.NilError(t, err, "error creating branch: http status code: %d : %v", resp.Status, err)
4141
runcnx.Clients.Log.Infof("Branch %s has been created", branch.Name)
42-
defer tbbs.TearDown(ctx, t, runcnx, client, nil, bitbucketWSOwner, branch.Name)
42+
defer tbbs.TearDown(ctx, t, runcnx, client, -1, bitbucketWSOwner, branch.Name)
4343

4444
files, err = payload.GetEntries(files, targetNS, branch.Name, triggertype.Push.String(), map[string]string{})
4545
assert.NilError(t, err)

test/pkg/bitbucketdatacenter/pr.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,31 @@ import (
66
"testing"
77

88
"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
9+
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/triggertype"
910
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/options"
11+
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/payload"
1012
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/scm"
1113

1214
goscm "github.com/jenkins-x/go-scm/scm"
1315
"gotest.tools/v3/assert"
1416
)
1517

1618
func CreatePR(ctx context.Context, t *testing.T, client *goscm.Client, runcnx *params.Run, opts options.E2E, repo *goscm.Repository, files map[string]string, orgAndRepo, targetNS string) *goscm.PullRequest {
17-
baseBranchRef := repo.Branch
18-
if opts.BaseBranch != "" {
19-
baseBranchRef = opts.BaseBranch
20-
}
21-
22-
branch, resp, err := client.Git.CreateRef(ctx, orgAndRepo, targetNS, baseBranchRef)
19+
mainBranchRef := "refs/heads/main"
20+
branch, resp, err := client.Git.CreateRef(ctx, orgAndRepo, targetNS, mainBranchRef)
2321
assert.NilError(t, err, "error creating branch: http status code: %d : %v", resp.Status, err)
2422
runcnx.Clients.Log.Infof("Branch %s has been created", branch.Name)
2523

24+
files, err = payload.GetEntries(files, targetNS, options.MainBranch, triggertype.PullRequest.String(), map[string]string{})
25+
assert.NilError(t, err)
2626
gitCloneURL, err := scm.MakeGitCloneURL(repo.Clone, opts.UserName, opts.Password)
2727
assert.NilError(t, err)
28-
2928
scmOpts := &scm.Opts{
3029
GitURL: gitCloneURL,
3130
Log: runcnx.Clients.Log,
3231
WebURL: repo.Clone,
3332
TargetRefName: targetNS,
34-
BaseRefName: baseBranchRef,
33+
BaseRefName: repo.Branch,
3534
CommitTitle: fmt.Sprintf("commit %s", targetNS),
3635
}
3736
scm.PushFilesToRefGit(t, scmOpts, files)
@@ -42,10 +41,9 @@ func CreatePR(ctx context.Context, t *testing.T, client *goscm.Client, runcnx *p
4241
Title: title,
4342
Body: "Test PAC on bitbucket data center",
4443
Head: targetNS,
45-
Base: baseBranchRef,
44+
Base: "main",
4645
}
4746
pr, resp, err := client.PullRequests.Create(ctx, orgAndRepo, prOpts)
4847
assert.NilError(t, err, "error creating pull request: http status code: %d : %v", resp.Status, err)
49-
runcnx.Clients.Log.Infof("Created Pull Request with Title '%s'. Head branch '%s' ⮕ Base Branch '%s'", pr.Title, pr.Head.Ref, pr.Base.Ref)
5048
return pr
5149
}

test/pkg/bitbucketdatacenter/setup.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,15 @@ func TearDownNs(ctx context.Context, t *testing.T, runcnx *params.Run, targetNS
8282
repository.NSTearDown(ctx, t, runcnx, targetNS)
8383
}
8484

85-
func TearDown(ctx context.Context, t *testing.T, runcnx *params.Run, client *scm.Client, pr *scm.PullRequest, orgAndRepo, ref string) {
85+
func TearDown(ctx context.Context, t *testing.T, runcnx *params.Run, client *scm.Client, prID int, orgAndRepo, ref string) {
8686
if os.Getenv("TEST_NOCLEANUP") == "true" {
8787
runcnx.Clients.Log.Infof("Not cleaning up and closing PR since TEST_NOCLEANUP is set")
8888
return
8989
}
9090

91-
// in Bitbucket Data Center, merged pull requests cannot be deleted.
92-
if pr != nil && !pr.Merged {
93-
runcnx.Clients.Log.Infof("Deleting PR #%d", pr.Number)
94-
_, err := client.PullRequests.DeletePullRequest(ctx, orgAndRepo, pr.Number)
91+
if prID != -1 {
92+
runcnx.Clients.Log.Infof("Deleting PR #%d", prID)
93+
_, err := client.PullRequests.DeletePullRequest(ctx, orgAndRepo, prID)
9594
assert.NilError(t, err)
9695
}
9796

test/pkg/options/options.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import "github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascod
44

55
type E2E struct {
66
Repo, Organization string
7-
BaseBranch string
87
DirectWebhook bool
98
ProjectID int
109
ControllerURL string

0 commit comments

Comments
 (0)