const axios = require('axios');
const path = require('path');
const { readPropertiesFile, replacePlaceholders } = require('../essential');
async function inviteCollaboratorsToRepos(org, repos, collaborators, token) {
const filePath = path.join(__dirname, 'properties', 'api.properties');
const config = readPropertiesFile(filePath);
if (!config.repocollaboratorurl) {
throw new Error("Collaborator URL is missing in the configuration.");
}
const results = await Promise.all(repos.map(async (repo, i) => {
const repoResults = [];
for (let j = 0; j < collaborators[i].length; j++) {
const replacements = { organization: org, repository: repo, collaborator: collaborators[i][j] };
try {
await axios.put(replacePlaceholders(config.repocollaboratorurl, replacements), {}, {
headers: {
Authorization: \`Bearer ${token}\`,
Accept: 'application/vnd.github.v3+json',
},
});
repoResults.push({ collaborator: collaborators[i][j], success: true });
} catch (error) {
repoResults.push({ collaborator: collaborators[i][j], success: false, error: error.message });
}
}
return { repo, results: repoResults };
}));
return results;
}
module.exports = inviteCollaboratorsToRepos;
/*
@param org = String
@param repos = Array
@param collaborators = Array
@param token = String
*/
const { inviteCollaboratorsToRepos } = require('jwz/github');
const org = 'your-org-name';
const repos = ['your-repoA', 'your-repoB'];
const collaborators = [['collaboratorA1', 'collaboratorA2'], ['collaboratorB1']];
const token = 'your-token';
const res = await inviteCollaboratorsToRepos(org, repos, collaborators, token);
console.log(res);
The inviteCollaboratorsToRepos
function invites collaborators to specific GitHub repositories within an organization. It reads configuration details from an external properties file and uses Axios for API requests. Here's a breakdown:
org
: The name of the GitHub organization.repos
: An array of repository names where collaborators will be invited.collaborators
: A nested array of collaborators to invite for each repository. Each sub-array corresponds to a repository in the repos
array.token
: A GitHub personal access token for authentication.repo
: The repository name.results
: An array of objects with each collaborator's success status and error details (if any).