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
use clap::{Args, Subcommand};
use crate::cli::common::normalize_str_vec;
mod git;
mod read;
mod worktree;
mod write;
#[derive(Args)]
pub struct PrArgs {
#[command(subcommand)]
pub command: PrCommand,
}
#[derive(Subcommand)]
pub enum PrCommand {
/// List pull requests
List {
/// Filter by state (open, closed, all)
#[arg(
long,
short,
default_value = "open",
value_parser = ["open", "closed", "all"],
ignore_case = true
)]
state: String,
/// Output as JSON
#[arg(long)]
json: bool,
/// Do not use a pager
#[arg(long)]
no_pager: bool,
},
/// View a pull request
View {
/// PR number
number: u64,
/// Show comments
#[arg(long, short)]
comments: bool,
/// Open in browser
#[arg(long, short)]
web: bool,
/// Output as JSON
#[arg(long)]
json: bool,
/// Do not use a pager
#[arg(long)]
no_pager: bool,
},
/// Create a pull request
Create {
/// PR title (prompts when omitted)
#[arg(long, short)]
title: Option<String>,
/// PR body (prompts when omitted)
#[arg(long, short)]
body: Option<String>,
/// Head branch (defaults to current branch). For cross-repo PRs, use OWNER:BRANCH or pass --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 (prompts with main as the default when omitted)
#[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
Edit {
/// PR number
number: u64,
/// New PR title
#[arg(long, short)]
title: Option<String>,
/// New PR body
#[arg(long, short)]
body: Option<String>,
/// 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>,
/// Allow GitBucket web UI fallback when REST PR edit is unavailable
#[arg(long)]
web: bool,
},
/// Close a pull request
Close {
/// PR number
number: u64,
},
/// Merge a pull request
Merge {
/// PR number
number: u64,
/// Merge commit message
#[arg(long, short)]
message: Option<String>,
},
/// Checkout a pull request branch locally
Checkout {
/// PR number
number: u64,
},
/// View the diff of a pull request
Diff {
/// PR number
number: u64,
/// Do not use a pager
#[arg(long)]
no_pager: bool,
},
/// Add, edit, or list comments on a pull request
Comment(PrCommentArgs),
}
#[derive(Args)]
#[command(args_conflicts_with_subcommands = true, subcommand_negates_reqs = true)]
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>,
/// Edit your last 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 {
/// List comments on a pull request
List {
/// PR number
number: u64,
/// Output comments as JSON
#[arg(long)]
json: bool,
/// Do not use a pager
#[arg(long)]
no_pager: 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,
web,
json,
no_pager,
} => {
read::view(
cli_hostname,
cli_repo,
cli_profile,
read::ViewOptions {
number,
show_comments: comments,
web,
json,
no_pager,
},
)
.await
}
PrCommand::Create {
title,
body,
head,
head_owner,
base,
json,
detect_existing,
} => {
write::create(
cli_hostname,
cli_repo,
cli_profile,
write::CreateRequest {
title,
body,
head,
head_owner,
base,
json,
detect_existing,
},
)
.await
}
PrCommand::Edit {
number,
title,
body,
add_assignee,
remove_assignee,
state,
web,
} => {
write::edit(
cli_hostname,
cli_repo,
cli_profile,
write::EditRequest {
number,
title,
body,
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::Merge { number, message } => {
write::merge(cli_hostname, cli_repo, cli_profile, number, message).await
}
PrCommand::Checkout { number } => {
worktree::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::List {
number,
json,
no_pager,
}) => {
read::list_comments(cli_hostname, cli_repo, cli_profile, number, 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(),
)
})?;
write::comment(
cli_hostname,
cli_repo,
cli_profile,
number,
args.body,
args.edit_last,
args.json,
)
.await
}
},
}
}