Skip to content

Commit f82ac73

Browse files
committed
command: --label + github add labels
1 parent ec46afd commit f82ac73

3 files changed

Lines changed: 73 additions & 28 deletions

File tree

src/app/SyncGithub.tsx

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ async function run() {
2828
const master_branch = state.master_branch;
2929
const repo_path = state.repo_path;
3030
const sync_github = state.sync_github;
31+
const labels = argv.label ?? [];
3132

3233
invariant(branch_name, "branch_name must exist");
3334
invariant(commit_map, "commit_map must exist");
@@ -140,17 +141,17 @@ async function run() {
140141
const pr_url_list = all_pr_groups.map((g) => pr_url_by_group_id[g.id]);
141142

142143
// update PR body for all pr groups (not just push_group_list)
143-
const update_pr_body_tasks = [];
144+
const update_pr_tasks = [];
144145
for (let i = 0; i < all_pr_groups.length; i++) {
145146
const group = all_pr_groups[i];
146147

147148
const selected_url = pr_url_by_group_id[group.id];
148149

149-
const task = update_pr_body({ group, selected_url, pr_url_list });
150-
update_pr_body_tasks.push(task);
150+
const task = update_pr({ group, selected_url, pr_url_list, labels });
151+
update_pr_tasks.push(task);
151152
}
152153

153-
await Promise.all(update_pr_body_tasks);
154+
await Promise.all(update_pr_tasks);
154155

155156
actions.unregister_abort_handler();
156157

@@ -262,6 +263,7 @@ async function run() {
262263
title: group.title,
263264
body: DEFAULT_PR_BODY,
264265
draft: argv.draft,
266+
labels,
265267
});
266268

267269
if (!pr_url) {
@@ -273,10 +275,11 @@ async function run() {
273275
}
274276
}
275277

276-
async function update_pr_body(args: {
278+
async function update_pr(args: {
277279
group: CommitMetadataGroup;
278280
selected_url: string;
279281
pr_url_list: Array<string>;
282+
labels: Array<string>;
280283
}) {
281284
const { group, selected_url, pr_url_list } = args;
282285

@@ -292,17 +295,30 @@ async function run() {
292295

293296
const debug_meta = `${group.id} ${selected_url}`;
294297

295-
if (update_body === body) {
296-
actions.debug(`Skipping body update ${debug_meta}`);
297-
} else {
298-
actions.debug(`Update body ${debug_meta}`);
298+
const body_changed = update_body !== body;
299+
const needs_labels = args.labels.length > 0;
299300

300-
await github.pr_edit({
301-
branch: group.id,
302-
base: group.base,
303-
body: update_body,
304-
});
301+
if (!body_changed && !needs_labels) {
302+
actions.debug(`Skipping update ${debug_meta}`);
303+
return;
305304
}
305+
306+
actions.debug(`Update PR ${debug_meta}`);
307+
308+
const edit_args: Parameters<typeof github.pr_edit>[0] = {
309+
branch: group.id,
310+
base: group.base,
311+
};
312+
313+
if (body_changed) {
314+
edit_args.body = update_body;
315+
}
316+
317+
if (needs_labels) {
318+
edit_args.add_labels = args.labels;
319+
}
320+
321+
await github.pr_edit(edit_args);
306322
}
307323

308324
function is_pr_master_base(group: CommitMetadataGroup) {

src/command.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,24 @@ const DefaultOptions = {
122122
description: "Sync commit ranges to Github, disable with --no-sync",
123123
},
124124

125+
"label": {
126+
type: "array",
127+
alias: ["labels"],
128+
coerce: (label_input: Array<string | number>) => label_input.map((v) => String(v)),
129+
description: [
130+
// force line break
131+
"Apply labels to all PRs in the stack (repeatable)",
132+
"Example: --label backend --label needs-review",
133+
].join("\n"),
134+
},
135+
136+
"draft": {
137+
type: "boolean",
138+
alias: ["d"],
139+
default: false,
140+
description: "Open all PRs as drafts",
141+
},
142+
125143
"verify": {
126144
type: "boolean",
127145
default: true,
@@ -142,19 +160,6 @@ const DefaultOptions = {
142160
].join("\n"),
143161
},
144162

145-
"draft": {
146-
type: "boolean",
147-
alias: ["d"],
148-
default: false,
149-
description: "Open all PRs as drafts",
150-
},
151-
152-
"revise-sign": {
153-
type: "boolean",
154-
default: true,
155-
description: "Disable GPG signing for git revise with --no-revise-sign",
156-
},
157-
158163
"template": {
159164
type: "boolean",
160165
default: true,
@@ -164,6 +169,12 @@ const DefaultOptions = {
164169
"Disable with --no-template",
165170
].join("\n"),
166171
},
172+
173+
"revise-sign": {
174+
type: "boolean",
175+
default: true,
176+
description: "Disable GPG signing for git revise with --no-revise-sign",
177+
},
167178
} satisfies YargsOptions;
168179

169180
const FixupOptions = {

src/core/github.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ type CreatePullRequestArgs = {
121121
title: string;
122122
body: string;
123123
draft: boolean;
124+
labels?: Array<string>;
124125
};
125126

126127
export async function pr_create(args: CreatePullRequestArgs) {
@@ -147,6 +148,13 @@ export async function pr_create(args: CreatePullRequestArgs) {
147148
command_parts.push("--draft");
148149
}
149150

151+
if (args.labels && args.labels.length > 0) {
152+
for (const label of args.labels) {
153+
if (!label) continue;
154+
command_parts.push(`--label="${safe_quote(label)}"`);
155+
}
156+
}
157+
150158
const cli_result = await cli(command_parts);
151159

152160
if (cli_result.code !== 0) {
@@ -161,14 +169,17 @@ type EditPullRequestArgs = {
161169
branch: string;
162170
base?: string;
163171
body?: string;
172+
add_labels?: Array<string>;
164173
};
165174

166175
export async function pr_edit(args: EditPullRequestArgs) {
176+
// https://cli.github.com/manual/gh_pr_edit
177+
167178
// const state = Store.getState();
168179
// const actions = state.actions;
169180
// actions.debug(`github.pr_edit ${JSON.stringify(args)}`);
170181

171-
if (!args.base && !args.body) {
182+
if (!args.base && !args.body && !(args.add_labels && args.add_labels.length > 0)) {
172183
return;
173184
}
174185

@@ -186,6 +197,13 @@ export async function pr_edit(args: EditPullRequestArgs) {
186197
command_parts.push(`--body-file="${body_file}"`);
187198
}
188199

200+
if (args.add_labels && args.add_labels.length > 0) {
201+
for (const label of args.add_labels) {
202+
if (!label) continue;
203+
command_parts.push(`--add-label="${safe_quote(label)}"`);
204+
}
205+
}
206+
189207
const cli_result = await cli(command_parts);
190208

191209
if (cli_result.code !== 0) {

0 commit comments

Comments
 (0)