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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
use clap::{Args, Subcommand};
use crate::cli::common::normalize_str_vec;
use crate::domain::review_comment::CommentSelection;
use super::json_output::JsonOutput;
mod checkout;
mod git;
mod read;
mod review_write;
mod target;
mod worktree;
mod write;
#[derive(Args)]
#[command(after_long_help = "See docs/usage/pull-requests.md for supported PR workflows.")]
pub struct PrArgs {
#[command(subcommand)]
pub command: PrCommand,
}
#[derive(Subcommand)]
pub enum PrCommand {
/// List pull requests
#[command(
after_long_help = "JSON verifies canonical details for every PR. See docs/reference/automation.md."
)]
List {
/// Filter by state (open, closed, all)
#[arg(
long,
short,
default_value = "open",
value_parser = ["open", "closed", "all"],
ignore_case = true
)]
state: String,
#[command(flatten)]
json: JsonOutput,
/// Do not use a pager
#[arg(long)]
no_pager: bool,
},
/// View a pull request
#[command(
after_long_help = "With --comments, the default is --kind all. JSON adds comment_coverage."
)]
View {
/// PR number
number: u64,
/// Show comments
#[arg(long, short)]
comments: bool,
/// Select discussion, review, or all comments (default all). Review uses Web HTML with sign-in only if needed
#[arg(long = "kind", requires = "comments")]
kind: Option<CommentSelection>,
/// Open in browser
#[arg(long, short, conflicts_with = "json_fields")]
web: bool,
#[command(flatten)]
json: JsonOutput,
/// Do not use a pager
#[arg(long)]
no_pager: bool,
},
/// Create a pull request
#[command(
after_long_help = "Omitted --base uses the target default branch. An inferred head must match the published commit. See docs/usage/pull-requests.md."
)]
Create {
/// PR title (prompts when omitted)
#[arg(long, short)]
title: Option<String>,
/// PR body (optional without a terminal)
#[arg(long, short)]
body: Option<String>,
/// Read UTF-8 body from a file, or - for stdin
#[arg(long, conflicts_with = "body", value_name = "FILE|-")]
body_file: Option<std::path::PathBuf>,
/// Compose body in GB_EDITOR, VISUAL, EDITOR or the default editor (terminal only)
#[arg(long, short = 'e', conflicts_with_all = ["body", "body_file"])]
editor: bool,
/// Published head branch (default: verify and use current branch). Cross-repo: OWNER:BRANCH or --head-owner.
#[arg(long)]
head: Option<String>,
/// Owner for the head branch; sends the head as OWNER:BRANCH
#[arg(long = "head-owner")]
head_owner: Option<String>,
/// Base branch (defaults to the target repository default branch)
#[arg(long, short = 'B')]
base: Option<String>,
/// Output the created pull request as JSON
#[arg(long)]
json: bool,
/// Return an existing open PR for the same head/base instead of creating a duplicate
#[arg(long)]
detect_existing: bool,
},
/// Edit a pull request
#[command(after_long_help = "--web permits assignee changes. Title, body and state use REST.")]
Edit {
/// PR number
number: u64,
/// New PR title
#[arg(long, short)]
title: Option<String>,
/// New PR body
#[arg(long, short)]
body: Option<String>,
/// Read UTF-8 body from a file, or - for stdin
#[arg(long, conflicts_with = "body", value_name = "FILE|-")]
body_file: Option<std::path::PathBuf>,
/// Compose body in GB_EDITOR, VISUAL, EDITOR or the default editor (terminal only)
#[arg(long, short = 'e', conflicts_with_all = ["body", "body_file"])]
editor: bool,
/// Add assignee username (repeatable or comma-separated)
#[arg(long = "add-assignee", value_delimiter = ',')]
add_assignee: Vec<String>,
/// Remove assignee username (repeatable or comma-separated)
#[arg(long = "remove-assignee", value_delimiter = ',')]
remove_assignee: Vec<String>,
/// Update PR state (open or closed)
#[arg(long, value_parser = ["open", "closed"], ignore_case = true)]
state: Option<String>,
/// Permit GitBucket Web UI assignee changes (title/body/state use REST)
#[arg(long)]
web: bool,
},
/// Close a pull request
Close {
/// PR number
number: u64,
},
/// Reopen a closed, unmerged pull request
Reopen {
/// PR number
number: u64,
},
/// Merge a pull request
#[command(
after_long_help = "Head checks are client-side and non-atomic. Use --match-head-commit to pin the reviewed head. See docs/reference/merge.md."
)]
Merge {
/// PR number
number: u64,
/// Merge commit message
#[arg(long, short)]
message: Option<String>,
/// Full reviewed head SHA (client checks only; not an atomic server guard)
#[arg(long, value_parser = crate::domain::pull_request::CommitId::parse)]
match_head_commit: Option<crate::domain::pull_request::CommitId>,
/// Print the result and explicit client-check/atomic=false metadata as JSON
#[arg(long)]
json: bool,
},
/// Checkout a pull request branch locally
#[command(
after_long_help = "Requires a clean worktree and checks out the saved PR head. See docs/reference/pull-request-snapshots.md."
)]
Checkout {
/// PR number
number: u64,
},
/// View the diff of a pull request
#[command(
after_long_help = "Requires a local Git repository. Compares the saved base and head. See docs/reference/pull-request-snapshots.md."
)]
Diff {
/// PR number
number: u64,
/// Do not use a pager
#[arg(long)]
no_pager: bool,
},
/// Add, edit, reply to, delete, or list comments on a pull request
Comment(PrCommentArgs),
}
#[derive(Args)]
#[command(
args_conflicts_with_subcommands = true,
subcommand_negates_reqs = true,
after_long_help = "Inline writes require --web and local Git. For an uncertain create or reply, keep the original retry receipt, actor and --request-id. See docs/reference/review-writes.md."
)]
pub struct PrCommentArgs {
#[command(subcommand)]
pub command: Option<PrCommentCommand>,
/// PR number
pub number: Option<u64>,
/// Comment body (prompts when omitted)
#[arg(long, short)]
pub body: Option<String>,
/// Read UTF-8 body from a file, or - for stdin
#[arg(long, conflicts_with = "body")]
pub body_file: Option<std::path::PathBuf>,
/// Compose body in GB_EDITOR, VISUAL, EDITOR or the default editor (terminal only)
#[arg(long, short = 'e', conflicts_with_all = ["body", "body_file"])]
pub editor: bool,
/// Exact path in the saved PR diff (destination path for renames)
#[arg(long, requires_all = ["line", "side", "web"], conflicts_with = "edit_last")]
pub file: Option<String>,
/// Old/new file line number, not a patch offset
#[arg(long, requires = "file")]
pub line: Option<u64>,
/// Side of the saved diff (old or new)
#[arg(long, requires = "file")]
pub side: Option<crate::domain::review_write::Side>,
/// Expected full saved PR head SHA; defaults to the head read at start
#[arg(long, requires = "file")]
pub commit: Option<String>,
/// Explicitly permit Cookie-authenticated inline Web writes
#[arg(long, requires = "file")]
pub web: bool,
/// Stable retry key; a new key permits an intentional identical submission
#[arg(long, requires = "file")]
pub request_id: Option<String>,
/// Edit your last ordinary discussion comment instead of adding a new one
#[arg(long)]
pub edit_last: bool,
/// Output the comment as JSON
#[arg(long)]
pub json: bool,
}
#[derive(Subcommand)]
pub enum PrCommentCommand {
/// Edit your current inline comment
Edit {
#[command(flatten)]
target: ReviewTargetArgs,
#[arg(long, short, conflicts_with = "body_file")]
body: Option<String>,
/// Read UTF-8 body from a file, or - for stdin
#[arg(long, conflicts_with = "body", value_name = "FILE|-")]
body_file: Option<std::path::PathBuf>,
/// Compose body in GB_EDITOR, VISUAL, EDITOR or the default editor (terminal only)
#[arg(long, short = 'e', conflicts_with_all = ["body", "body_file"])]
editor: bool,
},
/// Reply in an inline position group
Reply {
#[command(flatten)]
target: ReviewTargetArgs,
#[arg(long, short, conflicts_with = "body_file")]
body: Option<String>,
/// Read UTF-8 body from a file, or - for stdin
#[arg(long, conflicts_with = "body", value_name = "FILE|-")]
body_file: Option<std::path::PathBuf>,
/// Compose body in GB_EDITOR, VISUAL, EDITOR or the default editor (terminal only)
#[arg(long, short = 'e', conflicts_with_all = ["body", "body_file"])]
editor: bool,
#[arg(long)]
request_id: Option<String>,
},
/// Delete your current inline comment
Delete(ReviewTargetArgs),
/// List comments on a pull request
#[command(
after_long_help = "The default shows discussion and visible reviews. JSON reports coverage for both sources. For private reviews, use gb auth web-login or set GB_PASSWORD."
)]
List {
/// PR number
number: u64,
/// Select discussion, review, or all comments. Review uses Web HTML with sign-in only if needed
#[arg(long, default_value = "all")]
kind: CommentSelection,
#[command(flatten)]
json: JsonOutput,
/// Do not use a pager
#[arg(long)]
no_pager: bool,
},
}
#[derive(Args)]
pub struct ReviewTargetArgs {
pub number: u64,
/// Storage kind; ordinary discussion IDs are never accepted here
#[arg(long, required = true, value_parser = ["inline"])]
pub kind: String,
#[arg(long)]
pub id: u64,
/// Expected full saved PR head SHA
#[arg(long)]
pub commit: Option<String>,
/// Permit Cookie-authenticated Web changes to this CommitComment record
#[arg(long, required = true)]
pub web: bool,
#[arg(long)]
pub json: bool,
}
pub async fn run(
args: PrArgs,
cli_hostname: &Option<String>,
cli_repo: &Option<String>,
cli_profile: &Option<String>,
) -> crate::error::Result<()> {
match args.command {
PrCommand::List {
state,
json,
no_pager,
} => read::list(cli_hostname, cli_repo, cli_profile, &state, json, no_pager).await,
PrCommand::View {
number,
comments,
kind,
web,
json,
no_pager,
} => {
read::view(
cli_hostname,
cli_repo,
cli_profile,
read::ViewOptions {
number,
show_comments: comments,
selection: kind.unwrap_or_default(),
web,
json,
no_pager,
},
)
.await
}
PrCommand::Create {
title,
body,
body_file,
editor,
head,
head_owner,
base,
json,
detect_existing,
} => {
write::create(
cli_hostname,
cli_repo,
cli_profile,
write::CreateRequest {
title,
body: super::input::body(body, body_file, editor).await?,
head,
head_owner,
base,
json,
detect_existing,
},
)
.await
}
PrCommand::Edit {
number,
title,
body,
body_file,
editor,
add_assignee,
remove_assignee,
state,
web,
} => {
write::edit(
cli_hostname,
cli_repo,
cli_profile,
write::EditRequest {
number,
title,
body: super::input::body(body, body_file, editor).await?,
add_assignees: normalize_str_vec(add_assignee),
remove_assignees: normalize_str_vec(remove_assignee),
state,
web,
},
)
.await
}
PrCommand::Close { number } => {
write::close(cli_hostname, cli_repo, cli_profile, number).await
}
PrCommand::Reopen { number } => {
write::reopen(cli_hostname, cli_repo, cli_profile, number).await
}
PrCommand::Merge {
number,
message,
match_head_commit,
json,
} => {
write::merge(
cli_hostname,
cli_repo,
cli_profile,
number,
message,
match_head_commit,
json,
)
.await
}
PrCommand::Checkout { number } => {
checkout::checkout(cli_hostname, cli_repo, cli_profile, number).await
}
PrCommand::Diff { number, no_pager } => {
worktree::diff(cli_hostname, cli_repo, cli_profile, number, no_pager).await
}
PrCommand::Comment(args) => match args.command {
Some(PrCommentCommand::Edit {
target,
body,
body_file,
editor,
}) => {
review_write::run(
cli_hostname,
cli_repo,
cli_profile,
review_write::Request::identified(
target,
review_write::Action::Edit,
body,
body_file,
editor,
None,
),
)
.await
}
Some(PrCommentCommand::Reply {
target,
body,
body_file,
editor,
request_id,
}) => {
review_write::run(
cli_hostname,
cli_repo,
cli_profile,
review_write::Request::identified(
target,
review_write::Action::Reply,
body,
body_file,
editor,
request_id,
),
)
.await
}
Some(PrCommentCommand::Delete(target)) => {
review_write::run(
cli_hostname,
cli_repo,
cli_profile,
review_write::Request::identified(
target,
review_write::Action::Delete,
None,
None,
false,
None,
),
)
.await
}
Some(PrCommentCommand::List {
number,
kind,
json,
no_pager,
}) => {
read::list_comments(
cli_hostname,
cli_repo,
cli_profile,
number,
kind,
json,
no_pager,
)
.await
}
None => {
let number = args.number.ok_or_else(|| {
crate::error::GbError::Other(
"PR number is required. Use `gb pr comment <NUMBER>` or `gb pr comment list <NUMBER>`.".into(),
)
})?;
if args.file.is_some() {
return review_write::run(
cli_hostname,
cli_repo,
cli_profile,
review_write::Request::create(number, args)?,
)
.await;
}
let body = super::input::body(args.body, args.body_file, args.editor).await?;
write::comment(
cli_hostname,
cli_repo,
cli_profile,
number,
body,
args.edit_last,
args.json,
)
.await
}
},
}
}