|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {assertValidGithubConfig, getConfig} from '../../utils/config'; |
| 10 | +import {AuthenticatedGitClient} from '../../utils/git/authenticated-git-client'; |
| 11 | +import {setRepoMergeMode} from '../../utils/git/repository-merge-mode'; |
| 12 | +import {green, Log, red, bold} from '../../utils/logging'; |
| 13 | + |
| 14 | +export async function setMergeModeRelease() { |
| 15 | + try { |
| 16 | + await setRepoReleaserTeamToOnlyCurrentUser(); |
| 17 | + await setRepoMergeMode('release'); |
| 18 | + Log.info(green(' ✔ Repository is set for release')); |
| 19 | + } catch (err) { |
| 20 | + Log.error(' ✘ Failed to setup of repository for release'); |
| 21 | + if (err instanceof Error) { |
| 22 | + Log.info(err.message); |
| 23 | + Log.debug(err.stack); |
| 24 | + return; |
| 25 | + } |
| 26 | + Log.info(err); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +async function setRepoReleaserTeamToOnlyCurrentUser() { |
| 31 | + /** The authenticated GitClient instance. */ |
| 32 | + const git = await AuthenticatedGitClient.get(); |
| 33 | + /** Caretaker specific configuration. */ |
| 34 | + const config = await getConfig([assertValidGithubConfig]); |
| 35 | + /** The github team name for the group containing the repository releaser. */ |
| 36 | + const group = `${config.github.name}-releaser`; |
| 37 | + /** The user currently authenticated to github. */ |
| 38 | + const login = await git.github.users.getAuthenticated().then(({data}) => data.login); |
| 39 | + /** |
| 40 | + * The membership role, if any for the current user in the releaser group. A maintainer role |
| 41 | + * is required to be able to modify the membership of the group. |
| 42 | + */ |
| 43 | + const membership = await git.github.teams |
| 44 | + .getMembershipForUserInOrg({ |
| 45 | + org: git.remoteConfig.owner, |
| 46 | + team_slug: group, |
| 47 | + username: login, |
| 48 | + }) |
| 49 | + .then( |
| 50 | + ({data}) => data.role, |
| 51 | + () => undefined, |
| 52 | + ); |
| 53 | + |
| 54 | + if (membership !== 'maintainer') { |
| 55 | + Log.info(`Unable to update membership in ${bold(group)}`); |
| 56 | + Log.info(`Please reach out to dev-infra for assistance.`); |
| 57 | + throw ''; |
| 58 | + } |
| 59 | + |
| 60 | + /** A list of the current members of the releaser group */ |
| 61 | + const members = new Set( |
| 62 | + await git.github.teams |
| 63 | + .listMembersInOrg({ |
| 64 | + org: git.remoteConfig.owner, |
| 65 | + team_slug: group, |
| 66 | + }) |
| 67 | + .then(({data}) => data.map(({login}) => login)), |
| 68 | + ); |
| 69 | + |
| 70 | + Log.debug(`Current membership for ${group}:`); |
| 71 | + for (const member of members) { |
| 72 | + Log.debug(` - ${member}`); |
| 73 | + } |
| 74 | + |
| 75 | + // Do not remove the current user. |
| 76 | + members.delete(login); |
| 77 | + |
| 78 | + await Promise.all( |
| 79 | + Array.from(members).map(async (username: string) => { |
| 80 | + // This check should be unnecessary, but is put in place as a second guard |
| 81 | + // against accidently locking all non-admins out of updating the group. |
| 82 | + if (username === login) { |
| 83 | + return; |
| 84 | + } |
| 85 | + await git.github.teams.removeMembershipForUserInOrg({ |
| 86 | + org: git.remoteConfig.owner, |
| 87 | + team_slug: group, |
| 88 | + username, |
| 89 | + }); |
| 90 | + Log.debug(`Removed ${username} from ${group}.`); |
| 91 | + }), |
| 92 | + ); |
| 93 | +} |
0 commit comments