removeCollaboratorsFromRepos - GitHub Documentation

Code

const axios = require('axios');
const path = require('path');
const { readPropertiesFile, replacePlaceholders } = require('../essential');

async function removeCollaboratorsFromRepos(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.delete(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 = removeCollaboratorsFromRepos;

Usage

/*
    @param org = String
    @param repos = Array
    @param collaborators = Array
    @param token = String
*/
const { removeCollaboratorsFromRepos } = require('jwz/github');

const org = 'your-org-name';
const repos = ['your-repoA', 'your-repoB'];
const collaborators = [['collaboratorA'], ['collaboratorB']];
const token = 'your-token';

const res = await removeCollaboratorsFromRepos(org, repos, collaborators, token);
console.log(res);

Explanation

The removeCollaboratorsFromRepos function removes collaborators from specified GitHub repositories. It uses Axios to make HTTP DELETE requests to the GitHub API and handles multiple repositories and their respective collaborators.