Skip to content

Instantly share code, notes, and snippets.

@ruzz311
Created June 15, 2022 15:57
Show Gist options
  • Save ruzz311/33529b4b6c3ea5b85a11a799371ac216 to your computer and use it in GitHub Desktop.
Save ruzz311/33529b4b6c3ea5b85a11a799371ac216 to your computer and use it in GitHub Desktop.
Remove yourself from any assigned/review-requested PRs in a given project.

Assigned and Review-requested PR Cleanup

Found yourself swamped by a bunch of old PRs that are assigned/review-requested to your github user?
This script helps remove a given user (yourself) from all OPEN PRs that the user is assigned to or is in the review-requested.

How it works

Just a cli script that you need the project repo (full url, or github-like url ex: <org>/<project>) and a github username. The script will fetch all open PRs for the project and searches for returned PRs that involve the target user. It then edits the PR to remove the target user as an assignee and reviewer.

EXAMPLE USEAGE:

./gh-pr-cleanup.js facebook/react zucksucks
#!/usr/bin/env node
const util = require('util');
const exec = util.promisify(require('child_process').exec);
/**
* Retrieve any assigned PRs for the user/project combo.
*
* @param {string} ghRepo Repository using the [HOST/]OWNER/REPO format
* @param {string} ghUser
*/
async function fetchAssigned(ghUser, ghRepo) {
const fields = [
"id",
"number",
"author",
"title",
"reviewRequests",
"assignees",
"state",
"url",
];
const cmd = `gh pr list -s open -R ${ghRepo} --json ${fields.join(',')}`;
console.log(`runnning command: "${cmd}"`);
const {stdout, stderr} = await exec(cmd);
if (stderr) {
throw new Error(`Could not fetch PRs for user/repo: ${stderr}`);
}
const res = JSON.parse(stdout.trim());
// console.dir(res, {depth: 3})
const isGhUserReducer = (acc, val) => `${val.login || val.name}`.toUpperCase() === `${ghUser.toUpperCase()}` || acc;
return res.filter(x => {
const isReviewRequested = x.reviewRequests.reduce(isGhUserReducer, false);
const isAssignee = x.assignees.reduce(isGhUserReducer, false);
return isReviewRequested || isAssignee;
});
}
/**
*
* @param {string} ghUser
* @param {string} ghRepo Repository using the [HOST/]OWNER/REPO format
* @param {string} pr [<number> | <url> | <branch>]
*/
async function removeUserFromPr(ghUser, ghRepo, prNumber) {
const cmd = `gh pr edit ${prNumber} -R "${ghRepo}" --remove-assignee ${ghUser} --remove-reviewer ${ghUser}`
console.log(`runnning command: "${cmd}"`);
const {stdout, stderr} = await exec(cmd);
if (stderr) {
throw new Error(`Could not remove user from pr: ${stderr}`);
}
}
/**
* EXAMPLE USEAGE: `./gh-pr-cleanup.js facebook/react zucksucks`
*/
(async ({ghRepo, ghUser}) => {
let assignedPrs = [];
try {
console.log(`Retrieving all open PR's assigned to ${ghUser} in project ${ghRepo}`);
assignedPrs = await fetchAssigned(ghUser, ghRepo);
// console.log(JSON.stringify(assignedPrs, null, ''));
} catch(e) {
console.error(e);
}
console.log(`Removing ${ghUser} from ${assignedPrs.length} in repo ${ghRepo}`);
await Promise.all(
assignedPrs.map(async (pr) => {
try {
await removeUserFromPr(ghUser, ghRepo, pr.number);
} catch (e) {
console.warn(`Could not remove user "${ghUser}" from ${pr.url}! Reason: ${e}`);
}
})
);
})({
ghRepo: process.argv.length > 2 ? process.argv[2] : '<org>/<project>',
ghUser: process.argv.length > 3 ? process.argv[3] : '<gh-username>',
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment