Skip to content

Instantly share code, notes, and snippets.

@acbart
Created October 23, 2024 15:39
Show Gist options
  • Save acbart/e172eb805562de68461a54e0b0e9f201 to your computer and use it in GitHub Desktop.
Save acbart/e172eb805562de68461a54e0b0e9f201 to your computer and use it in GitHub Desktop.
Bulk Give Admin Permissions to Student Repositories on Github
(function(){
const token = "GITHUB_TOKEN"; // replace with your GitHub token
const BASE_URL = "https://api.github.com";
const ORG_NAME = "REPLACE_WITH_ORG_NAME";
const REPO_BASE_NAME = "REPLACE_WITH_REPO_STARTER_";
const repos = [
// Put your student names here, as a list of strings.
// You can easily get a list of submitted students from the Download->Download Grades button on github classroom.
];
// GitHub API version and token
const githubApiVersion = "2022-11-28";
// Fetch the list of collaborators and assign "admin" permissions
async function setAdminPermissions(studentName) {
const repoUrl = `${BASE_URL}/repos/${ORG_NAME}/${REPO_BASE_NAME}${studentName}`;
const collaboratorsUrl = `${repoUrl}/collaborators`;
try {
// Get the list of collaborators
const collaboratorsResponse = await fetch(collaboratorsUrl, {
headers: {
Authorization: `Bearer ${token}`,
"X-GitHub-Api-Version": githubApiVersion
}
});
if (!collaboratorsResponse.ok) {
throw new Error(`Error fetching collaborators for ${studentName}: ${collaboratorsResponse.statusText}`);
}
const collaborators = await collaboratorsResponse.json();
const collaboratorUsername = studentName;
const permissionUrl = `${collaboratorsUrl}/${collaboratorUsername}`;
// Set permission to 'admin'
const setPermissionResponse = await fetch(permissionUrl, {
method: 'PUT',
headers: {
Authorization: `Bearer ${token}`,
"X-GitHub-Api-Version": githubApiVersion,
'Content-Type': 'application/json'
},
body: JSON.stringify({
permission: 'admin'
})
});
if (!setPermissionResponse.ok) {
throw new Error(`Error setting admin permissions for ${collaboratorUsername} in ${studentName}: ${setPermissionResponse.statusText}`);
}
console.log(`Successfully set admin permission for ${collaboratorUsername} in ${studentName}`);
} catch (error) {
console.error(`Error processing ${repoUrl}: ${error.message}`);
}
}
// Iterate over all repositories and set admin permissions for collaborators
(async () => {
for (const studentName of repos) {
await setAdminPermissions(studentName);
}
})();
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment