Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bitbucket sha fix for merged pr #225

Merged
merged 4 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion scm/driver/bitbucket/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 6 additions & 1 deletion scm/driver/bitbucket/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -782,14 +782,19 @@ 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,
PullRequest: scm.PullRequest{
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,
Expand Down
14 changes: 13 additions & 1 deletion scm/driver/stash/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion scm/driver/stash/testdata/webhooks/pr_merged.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down