This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
mr_close.go
68 lines (59 loc) · 1.67 KB
/
mr_close.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
package close
import (
"fmt"
"github.com/MakeNowJust/heredoc"
"github.com/profclems/glab/api"
"github.com/profclems/glab/commands/cmdutils"
"github.com/profclems/glab/commands/mr/mrutils"
"github.com/spf13/cobra"
"github.com/xanzy/go-gitlab"
)
func NewCmdClose(f *cmdutils.Factory) *cobra.Command {
var mrCloseCmd = &cobra.Command{
Use: "close [<id> | <branch>]",
Short: `Close merge requests`,
Long: ``,
Example: heredoc.Doc(`
$ glab mr close 1
$ glab mr close 1 2 3 4 # close multiple branches at once
$ glab mr close # use checked out branch
$ glab mr close branch
$ glab mr close username:branch
$ glab mr close branch -R another/repo
`),
RunE: func(cmd *cobra.Command, args []string) error {
var err error
c := f.IO.Color()
apiClient, err := f.HttpClient()
if err != nil {
return err
}
mrs, repo, err := mrutils.MRsFromArgs(f, args, "opened")
if err != nil {
return err
}
l := &gitlab.UpdateMergeRequestOptions{}
l.StateEvent = gitlab.String("close")
for _, mr := range mrs {
if err = mrutils.MRCheckErrors(mr, mrutils.MRCheckErrOptions{
Closed: true,
Merged: true,
}); err != nil {
return err
}
fmt.Fprintf(f.IO.StdOut, "- Closing Merge request...\n")
_, err := api.UpdateMR(apiClient, repo.FullName(), mr.IID, l)
if err != nil {
return err
}
// Update the value of the merge request to closed so that mrutils.DisplayMR
// prints it as red
mr.State = "closed"
fmt.Fprintf(f.IO.StdOut, "%s Closed Merge request !%d\n", c.RedCheck(), mr.IID)
fmt.Fprintln(f.IO.StdOut, mrutils.DisplayMR(c, mr))
}
return nil
},
}
return mrCloseCmd
}