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 <art27@cantab.net>
Reviewed-by: lafriks <lafriks@noreply.gitea.io>
This commit is contained in:
6543 2020-05-20 22:41:21 +00:00 committed by lafriks
parent 2cc36f912f
commit b4696d5416
2 changed files with 24 additions and 12 deletions

View file

@ -66,6 +66,20 @@ type ListPullRequestsOptions struct {
Milestone int64 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 // QueryEncode turns options into querystring argument
func (opt *ListPullRequestsOptions) QueryEncode() string { func (opt *ListPullRequestsOptions) QueryEncode() string {
query := opt.getURLQuery() 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 // MergePullRequestOption options when merging a pull request
type MergePullRequestOption struct { type MergePullRequestOption struct {
// required: true Style MergeStyle `json:"Do"`
// enum: merge,rebase,rebase-merge,squash Title string `json:"MergeTitleField"`
Do string `json:"Do" binding:"Required;In(merge,rebase,rebase-merge,squash)"` Message string `json:"MergeMessageField"`
MergeTitleField string `json:"MergeTitleField"`
MergeMessageField string `json:"MergeMessageField"`
} }
// MergePullRequest merge a PR to repository by PR id // MergePullRequest merge a PR to repository by PR id
func (c *Client) MergePullRequest(owner, repo string, index int64, opt MergePullRequestOption) (bool, error) { 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 { if err := c.CheckServerVersionConstraint(">=1.11.5"); err != nil {
return false, err return false, err
} }

View file

@ -62,9 +62,9 @@ func TestPull(t *testing.T) {
assert.False(t, pullUpdateFile.HasMerged) assert.False(t, pullUpdateFile.HasMerged)
assert.True(t, pullUpdateFile.Mergeable) assert.True(t, pullUpdateFile.Mergeable)
merged, err := c.MergePullRequest(user.UserName, repoName, pullUpdateFile.Index, MergePullRequestOption{ merged, err := c.MergePullRequest(user.UserName, repoName, pullUpdateFile.Index, MergePullRequestOption{
Do: "squash", Style: MergeStyleSquash,
MergeTitleField: pullUpdateFile.Title, Title: pullUpdateFile.Title,
MergeMessageField: "squash: " + pullUpdateFile.Title, Message: "squash: " + pullUpdateFile.Title,
}) })
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, merged) assert.True(t, merged)
@ -85,9 +85,9 @@ func TestPull(t *testing.T) {
assert.False(t, pullConflict.HasMerged) assert.False(t, pullConflict.HasMerged)
assert.False(t, pullConflict.Mergeable) assert.False(t, pullConflict.Mergeable)
merged, err = c.MergePullRequest(user.UserName, repoName, pullConflict.Index, MergePullRequestOption{ merged, err = c.MergePullRequest(user.UserName, repoName, pullConflict.Index, MergePullRequestOption{
Do: "merge", Style: MergeStyleMerge,
MergeTitleField: "pullConflict", Title: "pullConflict",
MergeMessageField: "pullConflict Msg", Message: "pullConflict Msg",
}) })
assert.NoError(t, err) assert.NoError(t, err)
assert.False(t, merged) assert.False(t, merged)