Skip to content

Instantly share code, notes, and snippets.

@msfjarvis
Last active June 15, 2020 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msfjarvis/c509730f8ffd23c8839101e5ab12ac82 to your computer and use it in GitHub Desktop.
Save msfjarvis/c509730f8ffd23c8839101e5ab12ac82 to your computer and use it in GitHub Desktop.
GitHub branch renamer powered by OctoKit
import { Octokit } from '@octokit/rest';
import { renameBranch } from 'octokit-plugin-rename-branch';
import { Endpoints } from '@octokit/types';
/*
* Here's the standard disclaimer about how this worked for me, may not work for you,
* and that I am not responsible in the slightest to make it work for you. There's no huge story
* to go with this, I just decided this is something I wanted to do so I did it.
*/
const PERSONAL_TOKEN = 't0t4lly-r34l-t0k3n';
const USERNAME = 'octocat';
const REPLACEMENT_BRANCH = 'develop';
type listUserReposResponse = Endpoints['GET /repos/:owner/:repo']['response'];
const MyOctokit = Octokit.plugin(renameBranch);
const octokit = new MyOctokit({
auth: PERSONAL_TOKEN,
});
octokit.repos
.listForUser({
username: USERNAME,
type: 'owner',
})
.then(({ data }) => {
data.forEach((element: listUserReposResponse['data']) => {
if (element.default_branch === 'master' && !element.fork && !element.archived) {
console.log(`Changing branch for ${element.name}`);
octokit.renameBranch({
owner: USERNAME,
repo: element.name,
current_name: element.default_branch,
name: REPLACEMENT_BRANCH,
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment