Skip to content

Commit

Permalink
add ability to update PR by adding a label (#102)
Browse files Browse the repository at this point in the history
This feature allows a maintainer to update a PR's branch by adding the `giteabot/update-branch` label. It is being removed after the update.

Co-authored-by: silverwind <[email protected]>
  • Loading branch information
denyskon and silverwind authored Aug 2, 2023
1 parent e86156f commit 70a19bf
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 31 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ The script will also lock issues and pull requests that have been closed for 3
months. If the issue was commented on in the last two weeks, a comment will be
posted suggesting opening a new issue to continue the discussion.

### Maintainer commands

The script can execute some actions like updating a PR's branch if requested by
a maintainer through a `giteabot/*` label.

## Usage

Set the following environment variables:
Expand Down
3 changes: 2 additions & 1 deletion src/labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import { debounce } from "https://deno.land/[email protected]/async/mod.ts";
// manage backports or any other label that causes the bot to act on
// detection, such as reviewed/* or backport/*
export const isRelevantLabel = (label: string): boolean => {
return label.startsWith("reviewed/") || label.startsWith("backport/");
return label.startsWith("reviewed/") || label.startsWith("backport/") ||
label.startsWith("giteabot/");
};

const maintain = async () => {
Expand Down
36 changes: 6 additions & 30 deletions src/mergeQueue.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import {
addComment,
fetchPendingMerge,
needsUpdate,
removeLabel,
updatePr,
} from "./github.ts";
import { fetchPendingMerge, removeLabel } from "./github.ts";
import * as prActions from "./prActions.ts";
import { debounce } from "https://deno.land/[email protected]/async/mod.ts";

const updateBranch = async () => {
Expand All @@ -13,31 +8,12 @@ const updateBranch = async () => {

// update all PRs pending merge (only if they need an update)
for (const pr of pendingMerge.items) {
if (!await needsUpdate(pr.number)) continue;
const response = await updatePr(pr.number);
if (response.ok) {
console.info(`Synced PR #${pr.number} in merge queue`);
continue;
}
const err = await prActions.updateBranch(pr);

const body = await response.json();
if (body.message !== "merge conflict between base and head") {
console.error(`Failed to sync PR #${pr.number} in merge queue`);
console.error(JSON.stringify(body));
continue;
if (err?.message == "merge conflicts in PR") {
// if there is a merge conflict, we'll remove the reviewed/wait-merge label
await removeLabel(pr.number, "reviewed/wait-merge");
}

console.info(
`Merge conflict detected in PR #${pr.number} in merge queue`,
);
// if there is a merge conflict, we'll add a comment to fix the conflicts and remove the reviewed/wait-merge label
await Promise.all([
addComment(
pr.number,
`@${pr.user.login} please fix the merge conflicts. :tea:`,
),
removeLabel(pr.number, "reviewed/wait-merge"),
]);
}
};

Expand Down
45 changes: 45 additions & 0 deletions src/prActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { addComment, needsUpdate, removeLabel, updatePr } from "./github.ts";
import { debounce } from "https://deno.land/[email protected]/async/mod.ts";

const execute = async (
label: string,
pr: { number: number; user: { login: string } },
) => {
if (label === "giteabot/update-branch") {
await updateBranch(pr);
await removeLabel(pr.number, "giteabot/update-branch");
}
};

export const updateBranch = async (
pr: { number: number; user: { login: string } },
) => {
if (await needsUpdate(pr.number)) {
const response = await updatePr(pr.number);
if (response.ok) {
console.info(`Synced PR #${pr.number}`);
return;
}

const body = await response.json();
if (body.message !== "merge conflict between base and head") {
console.error(
`Failed to sync PR #${pr.number}`,
);
console.error(JSON.stringify(body));
}

console.info(
`Merge conflict detected in PR #${pr.number}`,
);
// if there is a merge conflict, we'll add a comment to fix the conflicts
await addComment(
pr.number,
`@${pr.user.login} please fix the merge conflicts. :tea:`,
);
return Error("merge conflicts in PR");
}
};

// make sure we don't trigger too often
export const run = debounce(execute, 8000);
2 changes: 2 additions & 0 deletions src/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as milestones from "./milestones.ts";
import * as lgtm from "./lgtm.ts";
import * as comments from "./comments.ts";
import * as lock from "./lock.ts";
import * as prActions from "./prActions.ts";

const secret = Deno.env.get("BACKPORTER_GITHUB_SECRET");

Expand Down Expand Up @@ -45,6 +46,7 @@ webhook.on(
if (labels.isRelevantLabel(payload.label.name)) {
labels.run();
mergeQueue.run();
prActions.run(payload.label.name, payload.pull_request);
}
},
);
Expand Down

0 comments on commit 70a19bf

Please sign in to comment.