(group: string, members: string[])
| 106 | } |
| 107 | |
| 108 | async function setGithubTeam(group: string, members: string[]) { |
| 109 | /** The authenticated GitClient instance. */ |
| 110 | const git = await AuthenticatedGitClient.get(); |
| 111 | /** The full name of the group <org>/<group name>. */ |
| 112 | const fullSlug = `${git.remoteConfig.owner}/${group}`; |
| 113 | /** The list of current members of the group. */ |
| 114 | const current = (await getGroupMembers(group)) || []; |
| 115 | /** The list of users to be removed from the group. */ |
| 116 | const removed = current.filter((login) => !members.includes(login)); |
| 117 | /** Add a user to the group. */ |
| 118 | const add = async (username: string) => { |
| 119 | Log.debug(`Adding ${username} to ${fullSlug}.`); |
| 120 | await git.github.teams.addOrUpdateMembershipForUserInOrg({ |
| 121 | org: git.remoteConfig.owner, |
| 122 | team_slug: group, |
| 123 | username, |
| 124 | role: 'maintainer', |
| 125 | }); |
| 126 | }; |
| 127 | /** Remove a user from the group. */ |
| 128 | const remove = async (username: string) => { |
| 129 | Log.debug(`Removing ${username} from ${fullSlug}.`); |
| 130 | await git.github.teams.removeMembershipForUserInOrg({ |
| 131 | org: git.remoteConfig.owner, |
| 132 | team_slug: group, |
| 133 | username, |
| 134 | }); |
| 135 | }; |
| 136 | |
| 137 | Log.debug(`Github Team: ${fullSlug}`); |
| 138 | Log.debug(`Current Membership: ${current.join(', ')}`); |
| 139 | Log.debug(`New Membership: ${members.join(', ')}`); |
| 140 | Log.debug(`Removed: ${removed.join(', ')}`); |
| 141 | |
| 142 | // Add members before removing to prevent the account performing the action from removing their |
| 143 | // permissions to change the group membership early. |
| 144 | await Promise.all(members.map(add)); |
| 145 | await Promise.all(removed.map(remove)); |
| 146 | |
| 147 | Log.debug(green(`Successfully updated ${fullSlug}`)); |
| 148 | } |
no test coverage detected