-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (129 loc) · 3.14 KB
/
main.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strings"
)
func main() {
if len(os.Args) < 2 {
err := errors.New("[ERROR] you must provide a command")
fmt.Println(err)
printUsage()
os.Exit(1)
}
command := os.Args[1]
restOfArgs := os.Args[2:]
switch command {
case "--help", "-h":
printUsage()
case "complete":
completePRs(restOfArgs...)
case "abandon":
abandonPRs(restOfArgs...)
default:
printUsage()
}
}
func extractIDfromLink(link string) string {
uri := strings.Split(link, "?")[0]
parts := strings.Split(uri, "/")
id := parts[len(parts)-1]
return id
}
func executeComplete(id string, cliArgs ...string) error {
cmdList := []string{"repos", "pr", "update", "--id", id, "--status", "completed"}
if hasOption("--delete-source-branch", cliArgs) {
cmdList = append(cmdList, "--delete-source-branch")
}
if hasOption("--squash", cliArgs) {
cmdList = append(cmdList, "--squash")
}
cmd := exec.Command("az", cmdList...)
out, err := cmd.Output()
if err != nil {
fmt.Printf("[ERROR] could not execute command complete: %s error: %s\n", out, err)
return err
}
return nil
}
func executeAbandon(id string, cliArgs ...string) error {
cmdList := []string{"repos", "pr", "update", "--id", id, "--status", "abandoned"}
if hasOption("--delete-source-branch", cliArgs) {
cmdList = append(cmdList, "--delete-source-branch")
}
cmd := exec.Command("az", cmdList...)
out, err := cmd.Output()
if err != nil {
fmt.Printf("[ERROR] could not execute command abandon: %s\n", out)
return err
}
return nil
}
type PRHandler func(string) error
func onPRs(handler PRHandler) {
buf := new(bytes.Buffer)
io.Copy(buf, os.Stdin)
bufStr := buf.String()
prsLinks := strings.Split(bufStr, "\n")
for _, link := range prsLinks {
link = strings.TrimSpace(link)
if link == "" {
continue
}
if strings.HasPrefix(link, "--") {
continue
}
handler(extractIDfromLink(link))
}
}
func hasOption(option string, options []string) bool {
for _, opt := range options {
if opt == option {
return true
}
}
return false
}
func completePRs(restOfArgs ...string) {
onPRs(func(id string) error {
fmt.Printf("complete PR: %s\n", id)
err := executeComplete(id, restOfArgs...)
if err != nil {
fmt.Printf("[ERROR] could not complete PR: %s error: %s", id, err)
return err
}
return nil
})
}
func abandonPRs(restOfArgs ...string) {
onPRs(func(id string) error {
fmt.Printf("abandon PR: %s\n", id)
err := executeAbandon(id, restOfArgs...)
if err != nil {
fmt.Printf("[ERROR] could not abandon PR: %s error: %s", id, err)
return err
}
return nil
})
}
func printUsage() {
usage := `
Usage:
prs [command] [options]
Commands:
complete Complete the specified pull requests
abandon Abandon the specified pull requests
Options:
--help, -h Show this help message and exit
Complete Command Options:
--delete-source-branch Delete the source branch after completing the pull request
--squash Squash the commits when completing the pull request
Abandon Command Options:
--delete-source-branch Delete the source branch after abandoning the pull request
`
fmt.Println(usage)
}