forked from go-gitea/tea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull.go
79 lines (68 loc) · 1.8 KB
/
pull.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2018 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 cmd
import (
"code.gitea.io/sdk/gitea"
"fmt"
"log"
"strconv"
"github.com/urfave/cli"
)
// CmdPull represents to login a gitea server.
var CmdPull = cli.Command{
Name: "pull",
Usage: "Operate with pull of the repository",
Description: `Operate with pull of the repository and returns the pull request's body message`,
Action: runPull,
ArgsUsage: "[<pull request index>]",
Flags: []cli.Flag{
cli.StringFlag{
Name: "login, l",
Usage: "Indicate one login, optional when inside a gitea repository",
},
cli.StringFlag{
Name: "repo, r",
Usage: "Indicate one repository, optional when inside a gitea repository",
},
cli.BoolFlag{
Name: "merge, mg",
Usage: "Merge a certain pull request",
},
},
}
func runPull(ctx *cli.Context) error {
args := ctx.Args()
if args.Present() {
return runPullDetail(ctx, args.First())
}
return runPulls(ctx)
}
func runPullDetail(ctx *cli.Context, index string) error {
login, owner, repo := initCommand(ctx)
idx, err := strconv.ParseInt(index, 10, 64)
if err != nil {
return err
}
pr, _, err := login.Client().GetPullRequest(owner, repo, idx)
if err != nil {
log.Fatal(err)
}
if pr == nil {
fmt.Println("Pull request not found")
return nil
}
if ctx.Bool("merge") {
_, _, err := login.Client().MergePullRequest(owner, repo, idx, gitea.MergePullRequestOption{Style: gitea.MergeStyleRebase})
if err != nil {
log.Fatal(err)
}
} else {
name := pr.Poster.FullName
if len(name) == 0 {
name = pr.Poster.UserName
}
fmt.Printf("#%d\t%s\t%s\t%s\t%s\n", pr.Index, name, pr.Updated.Format("2006-01-02 15:04:05"), pr.Title, pr.Body)
}
return nil
}