Skip to content
This repository was archived by the owner on Jul 1, 2024. It is now read-only.

Commit 7004f8a

Browse files
committed
Combine dependent actions
1 parent 5bbc261 commit 7004f8a

File tree

2 files changed

+28
-42
lines changed

2 files changed

+28
-42
lines changed

src/compute-pr-actions.ts

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,22 @@ export const LabelNames = [
4949
] as const;
5050

5151
export interface Actions {
52-
targetColumn?: ColumnName;
52+
projectColumn?: ColumnName | "REMOVE";
5353
labels: LabelName[];
5454
responseComments: Comments.Comment[];
5555
shouldClose: boolean;
5656
shouldMerge: boolean;
5757
shouldUpdateLabels: boolean;
58-
shouldUpdateProjectColumn: boolean;
59-
shouldRemoveFromActiveColumns: boolean;
6058
}
6159

6260
function createDefaultActions(): Actions {
6361
return {
64-
targetColumn: "Other",
62+
projectColumn: "Other",
6563
labels: [],
6664
responseComments: [],
6765
shouldClose: false,
6866
shouldMerge: false,
6967
shouldUpdateLabels: true,
70-
shouldUpdateProjectColumn: true,
71-
shouldRemoveFromActiveColumns: false,
7268
};
7369
}
7470

@@ -79,8 +75,6 @@ function createEmptyActions(): Actions {
7975
shouldClose: false,
8076
shouldMerge: false,
8177
shouldUpdateLabels: false,
82-
shouldUpdateProjectColumn: false,
83-
shouldRemoveFromActiveColumns: false,
8478
};
8579
}
8680

@@ -233,25 +227,17 @@ function extendPrInfo(info: PrInfo): ExtendedPrInfo {
233227
export function process(prInfo: BotResult,
234228
extendedCallback: (info: ExtendedPrInfo) => void = _i => {}): Actions {
235229
if (prInfo.type === "remove") {
236-
if (prInfo.isDraft) {
237-
return {
238-
...createEmptyActions(),
239-
targetColumn: "Needs Author Action",
240-
shouldUpdateProjectColumn: true,
241-
};
242-
} else {
243-
return {
244-
...createEmptyActions(),
245-
shouldRemoveFromActiveColumns: true,
246-
};
247-
}
230+
return {
231+
...createEmptyActions(),
232+
projectColumn: prInfo.isDraft ? "Needs Author Action" : "REMOVE",
233+
};
248234
}
249235

250236
const actions = createDefaultActions();
251237
const post = (c: Comments.Comment) => actions.responseComments.push(c);
252238

253239
if (prInfo.type === "error") {
254-
actions.targetColumn = "Other";
240+
actions.projectColumn = "Other";
255241
actions.labels.push("Mergebot Error");
256242
post(Comments.HadError(prInfo.author, prInfo.message));
257243
return actions;
@@ -306,18 +292,18 @@ export function process(prInfo: BotResult,
306292
}
307293

308294
// Some step should override this
309-
actions.targetColumn = "Other";
295+
actions.projectColumn = "Other";
310296

311297
// Needs author attention (bad CI, merge conflicts)
312298
if (info.needsAuthorAction) {
313-
actions.targetColumn = "Needs Author Action";
299+
actions.projectColumn = "Needs Author Action";
314300
if (info.hasMergeConflict) post(Comments.MergeConflicted(headCommitAbbrOid, info.author));
315301
if (info.failedCI) post(Comments.CIFailed(headCommitAbbrOid, info.author, info.ciUrl!));
316302
if (info.hasChangereqs) post(Comments.ChangesRequest(headCommitAbbrOid, info.author));
317303
}
318304
// CI is running; default column is Waiting for Reviewers
319305
else if (info.ciResult === "unknown") {
320-
actions.targetColumn = "Waiting for Code Reviews";
306+
actions.projectColumn = "Waiting for Code Reviews";
321307
}
322308
// CI is missing
323309
else if (info.ciResult === "missing") {
@@ -327,13 +313,13 @@ export function process(prInfo: BotResult,
327313
if (dayjs(info.now).diff(info.lastPushDate, "minutes") >= 1) {
328314
label("Where is GH Actions?");
329315
} else {
330-
delete actions.targetColumn;
316+
delete actions.projectColumn;
331317
}
332318
}
333319
// CI is green
334320
else if (info.ciResult === "pass") {
335321
if (!info.canBeSelfMerged) {
336-
actions.targetColumn = info.reviewColumn;
322+
actions.projectColumn = info.reviewColumn;
337323
} else {
338324
label("Self Merge");
339325
// post even when merging, so it won't get deleted
@@ -342,9 +328,9 @@ export function process(prInfo: BotResult,
342328
headCommitAbbrOid));
343329
if (info.hasValidMergeRequest) {
344330
actions.shouldMerge = true;
345-
actions.targetColumn = "Recently Merged";
331+
actions.projectColumn = "Recently Merged";
346332
} else {
347-
actions.targetColumn = "Waiting for Author to Merge";
333+
actions.projectColumn = "Waiting for Author to Merge";
348334
}
349335
}
350336
// Ping stale reviewers if any
@@ -384,9 +370,9 @@ function makeStaleness(now: Date, author: string, otherOwners: string[]) { // cu
384370
if (state === "done") {
385371
if (doneColumn === "CLOSE") {
386372
actions.shouldClose = true;
387-
actions.shouldRemoveFromActiveColumns = true;
373+
actions.projectColumn = "REMOVE";
388374
} else {
389-
actions.targetColumn = doneColumn;
375+
actions.projectColumn = doneColumn;
390376
}
391377
}
392378
};

src/execute-pr-actions.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,20 @@ async function getMutationsForLabels(actions: Actions, pr: PR_repository_pullReq
4545
}
4646

4747
async function getMutationsForProjectChanges(actions: Actions, pr: PR_repository_pullRequest) {
48-
if (actions.shouldRemoveFromActiveColumns) {
49-
const card = pr.projectCards.nodes?.find(card => card?.project.number === ProjectBoardNumber);
50-
if (card?.column?.name === "Recently Merged") return [];
51-
return [createMutation<schema.DeleteProjectCardInput>("deleteProjectCard", { cardId: card!.id })];
48+
if (!actions.projectColumn) return [];
49+
const card = pr.projectCards.nodes?.find(card => card?.project.number === ProjectBoardNumber);
50+
if (actions.projectColumn === "REMOVE") {
51+
if (!card || card.column?.name === "Recently Merged") return [];
52+
return [createMutation<schema.DeleteProjectCardInput>("deleteProjectCard", { cardId: card.id })];
5253
}
53-
if (!(actions.shouldUpdateProjectColumn && actions.targetColumn)) return [];
54-
const existingCard = pr.projectCards.nodes?.find(n => !!n?.column && n.project.number === ProjectBoardNumber);
55-
const targetColumnId = await getProjectBoardColumnIdByName(actions.targetColumn);
56-
// No existing card => create a new one
57-
if (!existingCard) return [createMutation<schema.AddProjectCardInput>("addProjectCard", { contentId: pr.id, projectColumnId: targetColumnId })];
5854
// Existing card is ok => do nothing
59-
if (existingCard.column?.name === actions.targetColumn) return [];
60-
// Move existing card
61-
return [createMutation<schema.MoveProjectCardInput>("moveProjectCard", { cardId: existingCard.id, columnId: targetColumnId })];
55+
if (card?.column?.name === actions.projectColumn) return [];
56+
const columnId = await getProjectBoardColumnIdByName(actions.projectColumn);
57+
return [card
58+
// Move existing card
59+
? createMutation<schema.MoveProjectCardInput>("moveProjectCard", { cardId: card.id, columnId })
60+
// No existing card => create a new one
61+
: createMutation<schema.AddProjectCardInput>("addProjectCard", { contentId: pr.id, projectColumnId: columnId })];
6262
}
6363

6464
type ParsedComment = { id: string, body: string, tag: string, status: string };

0 commit comments

Comments
 (0)