From 0bf844018c812697647499c8d44b8f8917b74b05 Mon Sep 17 00:00:00 2001 From: Norwin Date: Thu, 18 Mar 2021 03:56:05 +0800 Subject: [PATCH] Add `tea pr merge` (#348) fixes #343 Co-authored-by: Norwin Roosen Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/tea/pulls/348 Reviewed-by: 6543 <6543@obermui.de> Reviewed-by: Andrew Thornton Co-authored-by: Norwin Co-committed-by: Norwin --- cmd/pulls.go | 1 + cmd/pulls/merge.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 cmd/pulls/merge.go diff --git a/cmd/pulls.go b/cmd/pulls.go index db789db..8cb3a85 100644 --- a/cmd/pulls.go +++ b/cmd/pulls.go @@ -43,6 +43,7 @@ var CmdPulls = cli.Command{ &pulls.CmdPullsReview, &pulls.CmdPullsApprove, &pulls.CmdPullsReject, + &pulls.CmdPullsMerge, }, } diff --git a/cmd/pulls/merge.go b/cmd/pulls/merge.go new file mode 100644 index 0000000..3213db3 --- /dev/null +++ b/cmd/pulls/merge.go @@ -0,0 +1,70 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package pulls + +import ( + "fmt" + + "code.gitea.io/sdk/gitea" + "code.gitea.io/tea/cmd/flags" + "code.gitea.io/tea/modules/context" + "code.gitea.io/tea/modules/utils" + + "github.com/urfave/cli/v2" +) + +// CmdPullsMerge merges a PR +var CmdPullsMerge = cli.Command{ + Name: "merge", + Aliases: []string{"m"}, + Usage: "Merge a pull request", + Description: "Merge a pull request", + ArgsUsage: "", + Flags: append([]cli.Flag{ + &cli.StringFlag{ + Name: "style", + Aliases: []string{"s"}, + Usage: "Kind of merge to perform: merge, rebase, squash, rebase-merge", + Value: "merge", + }, + &cli.StringFlag{ + Name: "title", + Aliases: []string{"t"}, + Usage: "Merge commit title", + }, + &cli.StringFlag{ + Name: "message", + Aliases: []string{"m"}, + Usage: "Merge commit message", + }, + }, flags.AllDefaultFlags...), + Action: func(cmd *cli.Context) error { + ctx := context.InitCommand(cmd) + ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) + + if ctx.Args().Len() != 1 { + return fmt.Errorf("Must specify a PR index") + } + + idx, err := utils.ArgToIndex(ctx.Args().First()) + if err != nil { + return err + } + + success, _, err := ctx.Login.Client().MergePullRequest(ctx.Owner, ctx.Repo, idx, gitea.MergePullRequestOption{ + Style: gitea.MergeStyle(ctx.String("style")), + Title: ctx.String("title"), + Message: ctx.String("message"), + }) + + if err != nil { + return err + } + if !success { + return fmt.Errorf("Failed to merge PR. Is it still open?") + } + return nil + }, +}