Skip to content

Instantly share code, notes, and snippets.

@elonmir
Forked from suTerminus/git-repo-migration.js
Created May 19, 2022 10:12
Show Gist options
  • Save elonmir/c16472bb3d87dcbc27af9ccc14637002 to your computer and use it in GitHub Desktop.
Save elonmir/c16472bb3d87dcbc27af9ccc14637002 to your computer and use it in GitHub Desktop.
const { Octokit } = require("@octokit/rest");
const config = require("./config.json");
// config = {
// "repositories": [
// {
// "slug": "owner/repository",
// "maintainers": [
// "TeamA",
// "TeamB"
// ]
// }
// ]
// };
async function main() {
const octokit = new Octokit({
auth: process.env.PERSONAL_GH_TOKEN,
});
// for each repository
for (r of config.repositories) {
const [owner, repo] = r.slug.split("/");
let failed = false;
try {
// delete all collaborators
const teamOwner = await octokit.rest.repos.listTeams({
owner,
repo,
});
// first teams
for (t of teamOwner.data) {
await octokit.rest.teams.removeRepoInOrg({
org: owner,
team_slug: t.name.replaceAll(" ", "-"),
owner,
repo,
})
}
// then users
const collabs = await octokit.rest.repos.listCollaborators({
owner,
repo,
affiliation: "direct"
});
for (c of collabs.data) {
await octokit.rest.repos.removeCollaborator({
owner,
repo,
username: c.login,
});
}
// now add developers with write permissions
await octokit.rest.teams.addOrUpdateRepoPermissionsInOrg({
org: owner,
team_slug: "developer",
owner,
repo,
permission: "push"
});
// and now all maintainers
for (m of r.maintainers) {
await octokit.rest.teams.addOrUpdateRepoPermissionsInOrg({
org: owner,
team_slug: m.replaceAll(" ", "-"),
owner,
repo,
permission: "maintain"
});
}
} catch (error) {
console.error(error);
} finally {
console.log(`job status for ${owner}/${repo}: ${failed ? "failed" : "success"}`);
}
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment