Skip to content

Commit f19b85d

Browse files
authored
feat: add support for merge strategy (#353)
1 parent 59ae150 commit f19b85d

File tree

3 files changed

+100
-18
lines changed

3 files changed

+100
-18
lines changed

bitbucket.go

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -299,25 +299,37 @@ type RepositoryTagTarget struct {
299299
Hash string `json:"hash"`
300300
}
301301

302+
type PullRequestsMergeStrategy string
303+
304+
const (
305+
MergeCommit PullRequestsMergeStrategy = "merge_commit"
306+
Squash PullRequestsMergeStrategy = "squash"
307+
FastForward PullRequestsMergeStrategy = "fast_forward"
308+
SquashFastForward PullRequestsMergeStrategy = "squash_fast_forward"
309+
RebaseFastForward PullRequestsMergeStrategy = "rebase_fast_forward"
310+
RebaseMerge PullRequestsMergeStrategy = "rebase_merge"
311+
)
312+
302313
type PullRequestsOptions struct {
303-
ID string `json:"id"`
304-
CommentID string `json:"comment_id"`
305-
Owner string `json:"owner"`
306-
RepoSlug string `json:"repo_slug"`
307-
Title string `json:"title"`
308-
Description string `json:"description"`
309-
CloseSourceBranch bool `json:"close_source_branch"`
310-
SourceBranch string `json:"source_branch"`
311-
SourceRepository string `json:"source_repository"`
312-
DestinationBranch string `json:"destination_branch"`
313-
DestinationCommit string `json:"destination_repository"`
314-
Message string `json:"message"`
315-
Reviewers []string `json:"reviewers"`
316-
States []string `json:"states"`
317-
Query string `json:"query"`
318-
Sort string `json:"sort"`
319-
Draft bool `json:"draft"`
320-
Commit string `json:"commit"`
314+
ID string `json:"id"`
315+
CommentID string `json:"comment_id"`
316+
Owner string `json:"owner"`
317+
RepoSlug string `json:"repo_slug"`
318+
Title string `json:"title"`
319+
Description string `json:"description"`
320+
CloseSourceBranch bool `json:"close_source_branch"`
321+
SourceBranch string `json:"source_branch"`
322+
SourceRepository string `json:"source_repository"`
323+
DestinationBranch string `json:"destination_branch"`
324+
DestinationCommit string `json:"destination_repository"`
325+
MergeStrategy PullRequestsMergeStrategy `json:"merge_strategy"`
326+
Message string `json:"message"`
327+
Reviewers []string `json:"reviewers"`
328+
States []string `json:"states"`
329+
Query string `json:"query"`
330+
Sort string `json:"sort"`
331+
Draft bool `json:"draft"`
332+
Commit string `json:"commit"`
321333
ctx context.Context
322334
}
323335

pullrequests.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ func (p *PullRequests) buildPullRequestBody(po *PullRequestsOptions) (string, er
265265
body["close_source_branch"] = po.CloseSourceBranch
266266
}
267267

268+
if po.MergeStrategy != "" {
269+
body["merge_strategy"] = po.MergeStrategy
270+
}
271+
268272
if po.Draft {
269273
body["draft"] = true
270274
}

pullrequests_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,3 +937,69 @@ func TestPullRequestsDecline_NotFound(t *testing.T) {
937937
assert.Error(t, err)
938938
assert.Nil(t, result)
939939
}
940+
func TestBuildPullRequestBody_MergeStrategy(t *testing.T) {
941+
t.Parallel()
942+
943+
tests := map[string]struct {
944+
strategy PullRequestsMergeStrategy
945+
expected string
946+
}{
947+
"MergeCommit": {MergeCommit, "merge_commit"},
948+
"Squash": {Squash, "squash"},
949+
"FastForward": {FastForward, "fast_forward"},
950+
"SquashFastForward": {SquashFastForward, "squash_fast_forward"},
951+
"RebaseFastForward": {RebaseFastForward, "rebase_fast_forward"},
952+
"RebaseMerge": {RebaseMerge, "rebase_merge"},
953+
}
954+
955+
for name, tc := range tests {
956+
t.Run(name, func(t *testing.T) {
957+
t.Parallel()
958+
pr := &PullRequests{}
959+
opts := &PullRequestsOptions{
960+
Title: "Merge Strategy",
961+
MergeStrategy: tc.strategy,
962+
}
963+
964+
data, err := pr.buildPullRequestBody(opts)
965+
require.NoError(t, err)
966+
967+
var body map[string]interface{}
968+
require.NoError(t, json.Unmarshal([]byte(data), &body))
969+
970+
assert.Equal(t, tc.expected, body["merge_strategy"], "Strategy %s did not serialize correctly", name)
971+
})
972+
}
973+
}
974+
975+
func TestPullRequestsMerge_WithStrategy(t *testing.T) {
976+
t.Parallel()
977+
var receivedBody map[string]interface{}
978+
979+
client, server := setupMockServer(func(w http.ResponseWriter, r *http.Request) {
980+
bodyBytes, _ := io.ReadAll(r.Body)
981+
_ = json.Unmarshal(bodyBytes, &receivedBody)
982+
983+
mergedPR := realisticPRResponse()
984+
mergedPR["state"] = "MERGED"
985+
respondJSON(w, http.StatusOK, mergedPR)
986+
})
987+
defer server.Close()
988+
989+
opts := &PullRequestsOptions{
990+
Owner: "owner",
991+
RepoSlug: "repo",
992+
ID: "1",
993+
MergeStrategy: FastForward,
994+
Message: "Fast-forwarding this PR",
995+
}
996+
997+
result, err := client.Repositories.PullRequests.Merge(opts)
998+
999+
require.NoError(t, err)
1000+
assert.Equal(t, string(FastForward), receivedBody["merge_strategy"])
1001+
assert.Equal(t, "Fast-forwarding this PR", receivedBody["message"])
1002+
1003+
resultMap := result.(map[string]interface{})
1004+
assert.Equal(t, "MERGED", resultMap["state"])
1005+
}

0 commit comments

Comments
 (0)