|
| 1 | +/** |
| 2 | + * Copyright Zendesk, Inc. |
| 3 | + * |
| 4 | + * Use of this source code is governed under the Apache License, Version 2.0 |
| 5 | + * found at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +import { repository as getRepository, token as getToken } from '../index.js'; |
| 9 | +import { handleErrorMessage, handleSuccessMessage } from '../../utils/index.js'; |
| 10 | +import { Command } from 'commander'; |
| 11 | +import { Octokit } from '@octokit/rest'; |
| 12 | +import { Ora } from 'ora'; |
| 13 | + |
| 14 | +interface IGitHubMembershipArgs { |
| 15 | + org?: string; |
| 16 | + users?: string[]; |
| 17 | + path?: string; |
| 18 | + token?: string; |
| 19 | + spinner?: Ora; |
| 20 | +} |
| 21 | + |
| 22 | +type RETVAL = { |
| 23 | + success: string[]; |
| 24 | + failure: string[]; |
| 25 | +}; |
| 26 | + |
| 27 | +/** |
| 28 | + * Execute the `github-membership` command. |
| 29 | + * |
| 30 | + * @param {string} [args.org] GitHub organization name. |
| 31 | + * @param {string[]} [args.users] GitHub user names to remove from membership. |
| 32 | + * @param {string} [args.path] Path to a git directory. |
| 33 | + * @param {string} [args.token] GitHub personal access token. |
| 34 | + * @param {string} [args.spinner] Terminal spinner. |
| 35 | + * |
| 36 | + * @returns {Promise<object>} The success and failure results of the membership command. |
| 37 | + */ |
| 38 | +export const execute = async (args: IGitHubMembershipArgs): Promise<RETVAL> => { |
| 39 | + const retVal: RETVAL = { success: [], failure: [] }; |
| 40 | + |
| 41 | + try { |
| 42 | + const auth = args.token || (await getToken(args.spinner)); |
| 43 | + const github = new Octokit({ auth }); |
| 44 | + const org = (args.org || (await getRepository(args.path, args.spinner))?.owner)!; |
| 45 | + |
| 46 | + if (args.users) { |
| 47 | + const requests = []; |
| 48 | + |
| 49 | + for (const user of args.users) { |
| 50 | + /* https://octokit.github.io/rest.js/v21/#orgs-remove-member */ |
| 51 | + const request = github.orgs.removeMember({ org, username: user }); |
| 52 | + |
| 53 | + requests.push(request); |
| 54 | + } |
| 55 | + |
| 56 | + await Promise.allSettled(requests).then(results => { |
| 57 | + results.forEach(result => { |
| 58 | + if (result.status === 'fulfilled') { |
| 59 | + const user = result.value.url.split('/').pop(); |
| 60 | + |
| 61 | + retVal.success.push(user!); |
| 62 | + } else { |
| 63 | + const user = result.reason.response.url.split('/').pop(); |
| 64 | + |
| 65 | + if (typeof user === 'string') { |
| 66 | + retVal.failure.push(user); |
| 67 | + } |
| 68 | + |
| 69 | + handleErrorMessage(result.reason, 'github-membership', args.spinner); |
| 70 | + } |
| 71 | + }); |
| 72 | + }); |
| 73 | + } else { |
| 74 | + /* https://octokit.github.io/rest.js/v21/#orgs-list-members */ |
| 75 | + const members = await github.paginate(github.orgs.listMembers, { org, per_page: 100 }); |
| 76 | + |
| 77 | + retVal.success = members.map(member => member.login).sort(); |
| 78 | + } |
| 79 | + } catch (error: unknown) { |
| 80 | + handleErrorMessage(error, 'github-membership', args.spinner); |
| 81 | + |
| 82 | + throw error; |
| 83 | + } |
| 84 | + |
| 85 | + return retVal; |
| 86 | +}; |
| 87 | + |
| 88 | +export default (spinner: Ora): Command => { |
| 89 | + const command = new Command('github-membership'); |
| 90 | + |
| 91 | + return command |
| 92 | + .description('manage GitHub organization membership') |
| 93 | + .option('-o, --org [org]', 'GitHub organization name; defaults to repository owner') |
| 94 | + .option('-d --delete <users...>', 'remove members') |
| 95 | + .option('-l --list', 'list members') |
| 96 | + .option('-p, --path <path>', 'git directory') |
| 97 | + .option('-t, --token <token>', 'access token') |
| 98 | + .action(async () => { |
| 99 | + try { |
| 100 | + spinner.start(); |
| 101 | + |
| 102 | + const options = command.opts(); |
| 103 | + const results = await execute({ |
| 104 | + org: options.org, |
| 105 | + users: options.delete, |
| 106 | + path: options.path, |
| 107 | + token: options.token, |
| 108 | + spinner |
| 109 | + }); |
| 110 | + const toMessage = (members: string[]): string => { |
| 111 | + let retVal: string; |
| 112 | + |
| 113 | + if (options.list) { |
| 114 | + retVal = members.join(', '); |
| 115 | + } else { |
| 116 | + const length = members.length; |
| 117 | + |
| 118 | + retVal = `${length} ${length === 1 ? 'member' : 'members'}`; |
| 119 | + } |
| 120 | + |
| 121 | + return retVal; |
| 122 | + }; |
| 123 | + |
| 124 | + if (results.success.length > 0) { |
| 125 | + let message = toMessage(results.success); |
| 126 | + |
| 127 | + if (options.delete) { |
| 128 | + message = `Removed: ${message}`; |
| 129 | + } |
| 130 | + |
| 131 | + handleSuccessMessage(message, spinner); |
| 132 | + } |
| 133 | + |
| 134 | + if (results.failure.length > 0) { |
| 135 | + let message = toMessage(results.failure); |
| 136 | + |
| 137 | + if (options.delete) { |
| 138 | + message = `Failed: ${message}`; |
| 139 | + } |
| 140 | + |
| 141 | + handleErrorMessage(message, 'github-membership', spinner); |
| 142 | + } |
| 143 | + } catch { |
| 144 | + spinner.fail('GitHub membership not found'); |
| 145 | + process.exitCode = 1; |
| 146 | + } finally { |
| 147 | + spinner.stop(); |
| 148 | + } |
| 149 | + }); |
| 150 | +}; |
0 commit comments