From 32e680098bb1e96ae0d2a5d05378bc54db37d575 Mon Sep 17 00:00:00 2001 From: raghavharness Date: Tue, 13 Sep 2022 17:27:33 +0530 Subject: [PATCH 1/3] use merge commit sha if pull request is merged for bitbucket --- scm/driver/bitbucket/pr.go | 11 ++++++++++- .../testdata/webhooks/pr_fulfilled.json.golden | 2 +- scm/driver/bitbucket/webhook.go | 7 ++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/scm/driver/bitbucket/pr.go b/scm/driver/bitbucket/pr.go index 20dc31441..033860f85 100644 --- a/scm/driver/bitbucket/pr.go +++ b/scm/driver/bitbucket/pr.go @@ -108,6 +108,10 @@ type pr struct { HTML string `json:"html"` Type string `json:"type"` } `json:"summary"` + MergeCommit struct { + Type string `json:"type"` + Hash string `json:"hash"` + } `json:"merge_commit"` Source reference `json:"source"` State string `json:"state"` Author user `json:"author"` @@ -144,11 +148,16 @@ func convertPullRequests(from *prs) []*scm.PullRequest { } func convertPullRequest(from *pr) *scm.PullRequest { + sha := from.Source.Commit.Hash + // if pr is merged, use the merge commit + if from.MergeCommit.Hash != "" { + sha = from.MergeCommit.Hash + } return &scm.PullRequest{ Number: from.ID, Title: from.Title, Body: from.Description, - Sha: from.Source.Commit.Hash, + Sha: sha, Source: from.Source.Branch.Name, Target: from.Destination.Branch.Name, Fork: from.Source.Repository.FullName, diff --git a/scm/driver/bitbucket/testdata/webhooks/pr_fulfilled.json.golden b/scm/driver/bitbucket/testdata/webhooks/pr_fulfilled.json.golden index 1cc60a2f0..6b03b80fd 100644 --- a/scm/driver/bitbucket/testdata/webhooks/pr_fulfilled.json.golden +++ b/scm/driver/bitbucket/testdata/webhooks/pr_fulfilled.json.golden @@ -17,7 +17,7 @@ "Number": 1, "Title": "Awesome new feature", "Body": "made some changes", - "Sha": "0704fc5beccc", + "Sha": "4f8f6de9d0ff", "Ref": "refs/pull-requests/1/from", "Source": "develop", "Target": "master", diff --git a/scm/driver/bitbucket/webhook.go b/scm/driver/bitbucket/webhook.go index c567e565f..51f2c3f48 100644 --- a/scm/driver/bitbucket/webhook.go +++ b/scm/driver/bitbucket/webhook.go @@ -782,6 +782,11 @@ func convertTagDeleteHook(src *pushHook) *scm.TagHook { // func convertPullRequestHook(src *webhook) *scm.PullRequestHook { + sha := src.PullRequest.Source.Commit.Hash + // if pr is merged, use the merge commit + if src.PullRequest.MergeCommit.Hash != "" { + sha = src.PullRequest.MergeCommit.Hash + } namespace, name := scm.Split(src.Repository.FullName) return &scm.PullRequestHook{ Action: scm.ActionOpen, @@ -789,7 +794,7 @@ func convertPullRequestHook(src *webhook) *scm.PullRequestHook { Number: src.PullRequest.ID, Title: src.PullRequest.Title, Body: src.PullRequest.Description, - Sha: src.PullRequest.Source.Commit.Hash, + Sha: sha, Ref: fmt.Sprintf("refs/pull-requests/%d/from", src.PullRequest.ID), Source: src.PullRequest.Source.Branch.Name, Target: src.PullRequest.Destination.Branch.Name, From cfb5ca9937b25722f9a35832b66e463731a6f66d Mon Sep 17 00:00:00 2001 From: raghavharness Date: Wed, 14 Sep 2022 13:42:38 +0530 Subject: [PATCH 2/3] changes for stash --- scm/driver/stash/pr.go | 14 +++++++++++++- .../stash/testdata/webhooks/pr_merged.json.golden | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/scm/driver/stash/pr.go b/scm/driver/stash/pr.go index 836c5392b..e7607b859 100644 --- a/scm/driver/stash/pr.go +++ b/scm/driver/stash/pr.go @@ -173,6 +173,12 @@ type pr struct { Links struct { Self []link `json:"self"` } `json:"links"` + Properties struct { + MergeCommit struct { + ID string `json:"id"` + DisplayID string `json:"displayId"` + } `json:"mergeCommit"` + } `json:"properties"` } type prs struct { @@ -212,6 +218,12 @@ func convertPullRequests(from *prs) []*scm.PullRequest { } func convertPullRequest(from *pr) *scm.PullRequest { + sha := from.FromRef.LatestCommit + // if pr is merged, use the merge commit + if from.Properties.MergeCommit.ID != "" { + sha = from.Properties.MergeCommit.ID + } + fork := scm.Join( from.FromRef.Repository.Project.Key, from.FromRef.Repository.Slug, @@ -220,7 +232,7 @@ func convertPullRequest(from *pr) *scm.PullRequest { Number: from.ID, Title: from.Title, Body: from.Description, - Sha: from.FromRef.LatestCommit, + Sha: sha, Ref: fmt.Sprintf("refs/pull-requests/%d/from", from.ID), Source: from.FromRef.DisplayID, Target: from.ToRef.DisplayID, diff --git a/scm/driver/stash/testdata/webhooks/pr_merged.json.golden b/scm/driver/stash/testdata/webhooks/pr_merged.json.golden index 344032d2c..f4ab41c57 100644 --- a/scm/driver/stash/testdata/webhooks/pr_merged.json.golden +++ b/scm/driver/stash/testdata/webhooks/pr_merged.json.golden @@ -17,7 +17,7 @@ "Number": 3, "Title": "Develop", "Body": "* added LICENSE\r\n* add COPYING file", - "Sha": "b9eaed50a03c073b20dfa82e5e753d295e7f0e56", + "Sha": "83f836ca538dc4f43d29fda46a5b85ea49f1b8e7", "Ref": "refs/pull-requests/3/from", "Source": "develop", "Target": "master", From c7c7db684a217ff0514650a9d3562c6e64f3cfd4 Mon Sep 17 00:00:00 2001 From: raghavharness Date: Wed, 14 Sep 2022 18:39:13 +0530 Subject: [PATCH 3/3] use new merge field instead of exisiting sha field --- scm/driver/bitbucket/pr.go | 8 ++------ .../bitbucket/testdata/webhooks/pr_fulfilled.json.golden | 3 ++- scm/driver/bitbucket/webhook.go | 8 ++------ scm/driver/stash/pr.go | 9 ++------- scm/driver/stash/testdata/webhooks/pr_merged.json.golden | 3 ++- 5 files changed, 10 insertions(+), 21 deletions(-) diff --git a/scm/driver/bitbucket/pr.go b/scm/driver/bitbucket/pr.go index 033860f85..5cc3e5ba9 100644 --- a/scm/driver/bitbucket/pr.go +++ b/scm/driver/bitbucket/pr.go @@ -148,16 +148,12 @@ func convertPullRequests(from *prs) []*scm.PullRequest { } func convertPullRequest(from *pr) *scm.PullRequest { - sha := from.Source.Commit.Hash - // if pr is merged, use the merge commit - if from.MergeCommit.Hash != "" { - sha = from.MergeCommit.Hash - } return &scm.PullRequest{ Number: from.ID, Title: from.Title, Body: from.Description, - Sha: sha, + Sha: from.Source.Commit.Hash, + Merge: from.MergeCommit.Hash, Source: from.Source.Branch.Name, Target: from.Destination.Branch.Name, Fork: from.Source.Repository.FullName, diff --git a/scm/driver/bitbucket/testdata/webhooks/pr_fulfilled.json.golden b/scm/driver/bitbucket/testdata/webhooks/pr_fulfilled.json.golden index 6b03b80fd..b76c354af 100644 --- a/scm/driver/bitbucket/testdata/webhooks/pr_fulfilled.json.golden +++ b/scm/driver/bitbucket/testdata/webhooks/pr_fulfilled.json.golden @@ -17,7 +17,8 @@ "Number": 1, "Title": "Awesome new feature", "Body": "made some changes", - "Sha": "4f8f6de9d0ff", + "Sha": "0704fc5beccc", + "Merge": "4f8f6de9d0ff", "Ref": "refs/pull-requests/1/from", "Source": "develop", "Target": "master", diff --git a/scm/driver/bitbucket/webhook.go b/scm/driver/bitbucket/webhook.go index 51f2c3f48..b47675e6d 100644 --- a/scm/driver/bitbucket/webhook.go +++ b/scm/driver/bitbucket/webhook.go @@ -782,11 +782,6 @@ func convertTagDeleteHook(src *pushHook) *scm.TagHook { // func convertPullRequestHook(src *webhook) *scm.PullRequestHook { - sha := src.PullRequest.Source.Commit.Hash - // if pr is merged, use the merge commit - if src.PullRequest.MergeCommit.Hash != "" { - sha = src.PullRequest.MergeCommit.Hash - } namespace, name := scm.Split(src.Repository.FullName) return &scm.PullRequestHook{ Action: scm.ActionOpen, @@ -794,7 +789,8 @@ func convertPullRequestHook(src *webhook) *scm.PullRequestHook { Number: src.PullRequest.ID, Title: src.PullRequest.Title, Body: src.PullRequest.Description, - Sha: sha, + Sha: src.PullRequest.Source.Commit.Hash, + Merge: src.PullRequest.MergeCommit.Hash, Ref: fmt.Sprintf("refs/pull-requests/%d/from", src.PullRequest.ID), Source: src.PullRequest.Source.Branch.Name, Target: src.PullRequest.Destination.Branch.Name, diff --git a/scm/driver/stash/pr.go b/scm/driver/stash/pr.go index e7607b859..87cdb8fda 100644 --- a/scm/driver/stash/pr.go +++ b/scm/driver/stash/pr.go @@ -218,12 +218,6 @@ func convertPullRequests(from *prs) []*scm.PullRequest { } func convertPullRequest(from *pr) *scm.PullRequest { - sha := from.FromRef.LatestCommit - // if pr is merged, use the merge commit - if from.Properties.MergeCommit.ID != "" { - sha = from.Properties.MergeCommit.ID - } - fork := scm.Join( from.FromRef.Repository.Project.Key, from.FromRef.Repository.Slug, @@ -232,7 +226,8 @@ func convertPullRequest(from *pr) *scm.PullRequest { Number: from.ID, Title: from.Title, Body: from.Description, - Sha: sha, + Sha: from.FromRef.LatestCommit, + Merge: from.Properties.MergeCommit.ID, Ref: fmt.Sprintf("refs/pull-requests/%d/from", from.ID), Source: from.FromRef.DisplayID, Target: from.ToRef.DisplayID, diff --git a/scm/driver/stash/testdata/webhooks/pr_merged.json.golden b/scm/driver/stash/testdata/webhooks/pr_merged.json.golden index f4ab41c57..2b3367535 100644 --- a/scm/driver/stash/testdata/webhooks/pr_merged.json.golden +++ b/scm/driver/stash/testdata/webhooks/pr_merged.json.golden @@ -17,7 +17,8 @@ "Number": 3, "Title": "Develop", "Body": "* added LICENSE\r\n* add COPYING file", - "Sha": "83f836ca538dc4f43d29fda46a5b85ea49f1b8e7", + "Sha": "b9eaed50a03c073b20dfa82e5e753d295e7f0e56", + "Merge": "83f836ca538dc4f43d29fda46a5b85ea49f1b8e7", "Ref": "refs/pull-requests/3/from", "Source": "develop", "Target": "master",