From b4696d5416335c44bf3608b80a2311f091bc387c Mon Sep 17 00:00:00 2001 From: 6543 <6543@noreply.gitea.io> Date: Wed, 20 May 2020 22:41:21 +0000 Subject: [PATCH] PullMerge: use enum for MergeStyle (#328) Its already Breaking ... make struct look nice Signed-off-by: 6543 <6543@obermui.de> PullMerge: use enum for MergeStyle Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/328 Reviewed-by: Andrew Thornton Reviewed-by: lafriks --- gitea/pull.go | 24 ++++++++++++++++++------ gitea/pull_test.go | 12 ++++++------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/gitea/pull.go b/gitea/pull.go index d3b19b9..a0c7d44 100644 --- a/gitea/pull.go +++ b/gitea/pull.go @@ -66,6 +66,20 @@ type ListPullRequestsOptions struct { Milestone int64 } +// MergeStyle is used specify how a pull is merged +type MergeStyle string + +const ( + // MergeStyleMerge merge pull as usual + MergeStyleMerge MergeStyle = "merge" + // MergeStyleRebase rebase pull + MergeStyleRebase MergeStyle = "rebase" + // MergeStyleRebaseMerge rebase and merge pull + MergeStyleRebaseMerge MergeStyle = "rebase-merge" + // MergeStyleSquash squash and merge pull + MergeStyleSquash MergeStyle = "squash" +) + // QueryEncode turns options into querystring argument func (opt *ListPullRequestsOptions) QueryEncode() string { query := opt.getURLQuery() @@ -146,16 +160,14 @@ func (c *Client) EditPullRequest(owner, repo string, index int64, opt EditPullRe // MergePullRequestOption options when merging a pull request type MergePullRequestOption struct { - // required: true - // enum: merge,rebase,rebase-merge,squash - Do string `json:"Do" binding:"Required;In(merge,rebase,rebase-merge,squash)"` - MergeTitleField string `json:"MergeTitleField"` - MergeMessageField string `json:"MergeMessageField"` + Style MergeStyle `json:"Do"` + Title string `json:"MergeTitleField"` + Message string `json:"MergeMessageField"` } // MergePullRequest merge a PR to repository by PR id func (c *Client) MergePullRequest(owner, repo string, index int64, opt MergePullRequestOption) (bool, error) { - if opt.Do == "squash" { + if opt.Style == MergeStyleSquash { if err := c.CheckServerVersionConstraint(">=1.11.5"); err != nil { return false, err } diff --git a/gitea/pull_test.go b/gitea/pull_test.go index 47dec08..1ce7dec 100644 --- a/gitea/pull_test.go +++ b/gitea/pull_test.go @@ -62,9 +62,9 @@ func TestPull(t *testing.T) { assert.False(t, pullUpdateFile.HasMerged) assert.True(t, pullUpdateFile.Mergeable) merged, err := c.MergePullRequest(user.UserName, repoName, pullUpdateFile.Index, MergePullRequestOption{ - Do: "squash", - MergeTitleField: pullUpdateFile.Title, - MergeMessageField: "squash: " + pullUpdateFile.Title, + Style: MergeStyleSquash, + Title: pullUpdateFile.Title, + Message: "squash: " + pullUpdateFile.Title, }) assert.NoError(t, err) assert.True(t, merged) @@ -85,9 +85,9 @@ func TestPull(t *testing.T) { assert.False(t, pullConflict.HasMerged) assert.False(t, pullConflict.Mergeable) merged, err = c.MergePullRequest(user.UserName, repoName, pullConflict.Index, MergePullRequestOption{ - Do: "merge", - MergeTitleField: "pullConflict", - MergeMessageField: "pullConflict Msg", + Style: MergeStyleMerge, + Title: "pullConflict", + Message: "pullConflict Msg", }) assert.NoError(t, err) assert.False(t, merged)