GitHub

removeCollaboratorsFromRepos

Code

const axios = require('axios');
const runCollaboratorRequests = require('./collaborators');

/*
    @param org = String
    @param repos = Array
    @param collaborators = Array
    @param token = String
*/
async function removeCollaboratorsFromRepos(org, repos, collaborators, token) {
    return runCollaboratorRequests(org, repos, collaborators, token,
        (url, headers) => axios.delete(url, { headers }));
}

module.exports = removeCollaboratorsFromRepos;

The endpoint guard, the iteration over repositories and collaborators, and the per collaborator result shape are shared with inviteCollaboratorsToRepos in src/github/collaborators.js. That file is internal machinery and is not part of the published surface, so it is not importable on its own:

const path = require('path');
const { readPropertiesFile, requireHttpsUrl, resolveSecureUrl } = require('../essential');

/*
    @param org = String
    @param repos = Array
    @param collaborators = Array
    @param token = String
    @param send = Function, sends one request given the resolved URL and the headers
*/
async function runCollaboratorRequests(org, repos, collaborators, token, send) {
    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.");
    }

    requireHttpsUrl(config.repocollaboratorurl, 'repocollaboratorurl');

    const headers = {
        Authorization: `Bearer ${token}`,
        Accept: 'application/vnd.github.v3+json',
    };

    const results = await Promise.all(repos.map(async (repo, i) => {
        const repoResults = [];

        for (let j = 0; j < collaborators[i].length; j++) {
            const collaborator = collaborators[i][j];
            const replacements = { organization: org, repository: repo, collaborator };

            try {
                await send(resolveSecureUrl(config.repocollaboratorurl, replacements, 'repocollaboratorurl'), headers);

                repoResults.push({ collaborator, success: true });
            } catch (error) {
                repoResults.push({ collaborator, success: false, error: error.message });
            }
        }

        return { repo, results: repoResults };
    }));

    return results;
}

module.exports = runCollaboratorRequests;

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.

  • Parameters:
    • org: The name of the GitHub organization.
    • repos: An array of repository names from which collaborators will be removed.
    • collaborators: A nested array where each sub-array contains the collaborators to be removed for the corresponding repository.
    • token: A GitHub personal access token for authentication.
  • Logic:
    • Reads the collaborator API endpoint URL from a properties file.
    • Checks the endpoint uses https, and throws before the token is sent if it does not.
    • Uses Axios to send DELETE requests for each collaborator in each repository.
    • Handles errors and tracks the success status for each collaborator.
  • Output:
    • An array of results where each result contains:
      • The repository name.
      • A list of collaborators with their success status and error messages (if any).