/
pr.go
264 lines (189 loc) · 5.91 KB
/
pr.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package commands
import (
"fmt"
"strconv"
"strings"
"github.com/github/hub/github"
"github.com/github/hub/ui"
"github.com/github/hub/utils"
)
var (
cmdPr = &Command{
Run: printHelp,
Usage: `
pr list [-s <STATE>] [-h <HEAD>] [-b <BASE>] [-o <SORT_KEY> [-^]] [-f <FORMAT>] [-L <LIMIT>]
pr checkout <PR-NUMBER> [<BRANCH>]
`,
Long: `Manage GitHub Pull Requests for the current repository.
## Commands:
* _list_:
List pull requests in the current repository.
* _checkout_:
Check out the head of a pull request in a new branch.
## Options:
-s, --state <STATE>
Filter pull requests by <STATE>. Supported values are: "open" (default),
"closed", "merged", or "all".
-h, --head <BRANCH>
Show pull requests started from the specified head <BRANCH>. The
"OWNER:BRANCH" format must be used for pull requests from forks.
-b, --base <BRANCH>
Show pull requests based off the specified <BRANCH>.
-f, --format <FORMAT>
Pretty print the list of pull requests using format <FORMAT> (default:
"%pC%>(8)%i%Creset %t% l%n"). See the "PRETTY FORMATS" section of
git-log(1) for some additional details on how placeholders are used in
format. The available placeholders are:
%I: pull request number
%i: pull request number prefixed with "#"
%U: the URL of this pull request
%S: state ("open" or "closed")
%pS: pull request state ("open", "draft", "merged", or "closed")
%sC: set color to red or green, depending on state
%pC: set color according to pull request state
%t: title
%l: colored labels
%L: raw, comma-separated labels
%b: body
%B: base branch
%sB: base commit SHA
%H: head branch
%sH: head commit SHA
%sm: merge commit SHA
%au: login name of author
%as: comma-separated list of assignees
%rs: comma-separated list of requested reviewers
%Mn: milestone number
%Mt: milestone title
%NC: number of comments
%Nc: number of comments wrapped in parentheses, or blank string if zero.
%cD: created date-only (no time of day)
%cr: created date, relative
%ct: created date, UNIX timestamp
%cI: created date, ISO 8601 format
%uD: updated date-only (no time of day)
%ur: updated date, relative
%ut: updated date, UNIX timestamp
%uI: updated date, ISO 8601 format
%mD: merged date-only (no time of day)
%mr: merged date, relative
%mt: merged date, UNIX timestamp
%mI: merged date, ISO 8601 format
%n: newline
%%: a literal %
--color[=<WHEN>]
Enable colored output even if stdout is not a terminal. <WHEN> can be one
of "always" (default for '--color'), "never", or "auto" (default).
-o, --sort <KEY>
Sort displayed issues by "created" (default), "updated", "popularity", or "long-running".
-^, --sort-ascending
Sort by ascending dates instead of descending.
-L, --limit <LIMIT>
Display only the first <LIMIT> issues.
## See also:
hub-issue(1), hub-pull-request(1), hub(1)
`,
}
cmdCheckoutPr = &Command{
Key: "checkout",
Run: checkoutPr,
KnownFlags: "\n",
}
cmdListPulls = &Command{
Key: "list",
Run: listPulls,
Long: cmdPr.Long,
}
)
func init() {
cmdPr.Use(cmdListPulls)
cmdPr.Use(cmdCheckoutPr)
CmdRunner.Use(cmdPr)
}
func printHelp(command *Command, args *Args) {
utils.Check(command.UsageError(""))
}
func listPulls(cmd *Command, args *Args) {
localRepo, err := github.LocalRepo()
utils.Check(err)
project, err := localRepo.MainProject()
utils.Check(err)
gh := github.NewClient(project.Host)
args.NoForward()
if args.Noop {
ui.Printf("Would request list of pull requests for %s\n", project)
return
}
filters := map[string]interface{}{}
if args.Flag.HasReceived("--state") {
filters["state"] = args.Flag.Value("--state")
}
if args.Flag.HasReceived("--sort") {
filters["sort"] = args.Flag.Value("--sort")
}
if args.Flag.HasReceived("--base") {
filters["base"] = args.Flag.Value("--base")
}
if args.Flag.HasReceived("--head") {
head := args.Flag.Value("--head")
if !strings.Contains(head, ":") {
head = fmt.Sprintf("%s:%s", project.Owner, head)
}
filters["head"] = head
}
if args.Flag.Bool("--sort-ascending") {
filters["direction"] = "asc"
} else {
filters["direction"] = "desc"
}
onlyMerged := false
if filters["state"] == "merged" {
filters["state"] = "closed"
onlyMerged = true
}
flagPullRequestLimit := args.Flag.Int("--limit")
flagPullRequestFormat := args.Flag.Value("--format")
if !args.Flag.HasReceived("--format") {
flagPullRequestFormat = "%pC%>(8)%i%Creset %t% l%n"
}
pulls, err := gh.FetchPullRequests(project, filters, flagPullRequestLimit, func(pr *github.PullRequest) bool {
return !(onlyMerged && pr.MergedAt.IsZero())
})
utils.Check(err)
colorize := colorizeOutput(args.Flag.HasReceived("--color"), args.Flag.Value("--color"))
for _, pr := range pulls {
ui.Print(formatPullRequest(pr, flagPullRequestFormat, colorize))
}
}
func checkoutPr(command *Command, args *Args) {
words := args.Words()
var newBranchName string
if len(words) == 0 {
utils.Check(fmt.Errorf("Error: No pull request number given"))
} else if len(words) > 1 {
newBranchName = words[1]
}
prNumberString := words[0]
_, err := strconv.Atoi(prNumberString)
utils.Check(err)
// Figure out the PR URL
localRepo, err := github.LocalRepo()
utils.Check(err)
baseProject, err := localRepo.MainProject()
utils.Check(err)
host, err := github.CurrentConfig().PromptForHost(baseProject.Host)
utils.Check(err)
client := github.NewClientWithHost(host)
pr, err := client.PullRequest(baseProject, prNumberString)
utils.Check(err)
newArgs, err := transformCheckoutArgs(args, pr, newBranchName)
utils.Check(err)
args.Replace(args.Executable, "checkout", newArgs...)
}
func formatPullRequest(pr github.PullRequest, format string, colorize bool) string {
placeholders := formatIssuePlaceholders(github.Issue(pr), colorize)
for key, value := range formatPullRequestPlaceholders(pr, colorize) {
placeholders[key] = value
}
return ui.Expand(format, placeholders, colorize)
}