Created
May 19, 2022 09:52
-
-
Save suTerminus/59b10d03ec1e0ed6b96c8d7517b6baf5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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