Skip to content

Instantly share code, notes, and snippets.

@AndrewLeedham
Created December 12, 2020 16:42
Show Gist options
  • Save AndrewLeedham/b0afe909f998abae220a0e6b52d3bcbb to your computer and use it in GitHub Desktop.
Save AndrewLeedham/b0afe909f998abae220a0e6b52d3bcbb to your computer and use it in GitHub Desktop.
CLI for bulk merging dependabot PRs
const path = require('path');
require("dotenv").config({path: path.join(__dirname, '.env')});
const yargs = require('yargs');
const prompts = require('prompts');
const chalk = require('chalk');
const ora = require('ora');
const { Octokit } = require("@octokit/rest");
const octokit = new Octokit({
auth: process.env.GITHUB_PERSONAL_ACCESS_TOKEN
});
const dependabotUsers = new Set(['dependabot', 'dependabot[bot]', 'dependabot-preview', 'dependabot-preview[bot]']);
const repos = [
{owner: '<owner>', name: '<repo>'},
];
function formatTitle(title) {
const formatted = title.replace(/chore\(deps(?:-dev)?\):\s/, '');
const split = formatted.split(' ');
split[1] = chalk.green(split[1]);
return split.join(' ');
}
yargs.command('merge', 'List all your open dependabot pull requests and merge selected', {}, async (args) => {
let allPRs = [];
for (const repo of repos) {
const {data: pulls} = await octokit.pulls.list({owner: repo.owner, repo: repo.name});
allPRs = [...allPRs, ...pulls.filter(({user}) => dependabotUsers.has(user.login))];
}
const longestRepo = repos.reduce((acc, cur) => Math.max(cur.name.length, acc), 0);
const choices = allPRs.map((value) => ({title: `${value.base.repo.name}${' '.repeat(longestRepo - value.base.repo.name.length)}${formatTitle(value.title)} (${value.html_url})`, value}));
const {selected, merge} = await prompts([{
type: 'multiselect',
name: 'selected',
message: 'Select PRs',
choices,
},
{
type: 'confirm',
name: 'merge',
message: 'Are you sure you want to merge all of these PRs?',
initial: false,
}]);
if (merge && selected.length > 0) {
for (const pr of selected) {
const spinner = ora(`Merging ${pr.title} (${pr.html_url})`).start();
try {
await octokit.pulls.merge({
owner: pr.base.user.login,
repo: pr.base.repo.name,
pull_number: pr.number
});
spinner.succeed();
} catch(e) {
spinner.fail();
console.group();
console.error(chalk.red(e.toString()));
console.groupEnd();
}
}
}
}).argv;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment