|
| 1 | +import * as core from '@actions/core'; |
| 2 | +import {context} from '@actions/github'; |
| 3 | +import {Octokit, RestEndpointMethodTypes} from '@octokit/rest'; |
| 4 | +import {allLabels, Label} from '../../../ng-dev/pr/common/labels.js'; |
| 5 | +import {getAuthTokenFor, ANGULAR_ROBOT, revokeActiveInstallationToken} from '../../utils.js'; |
| 6 | + |
| 7 | +/** The type for a Github label returned from the Github API. */ |
| 8 | +type GithubLabel = |
| 9 | + RestEndpointMethodTypes['issues']['listLabelsForRepo']['response']['data'][number]; |
| 10 | + |
| 11 | +/** Synchronize the provided managed labels with the given repository. */ |
| 12 | +async function syncLabelsInRepo(github: Octokit, repoName: string, managedLabels: Label[]) { |
| 13 | + core.startGroup(`Syncing ${repoName}`); |
| 14 | + /** The current repository name and owner for usage in the Github API. */ |
| 15 | + const repo = {repo: repoName, owner: context.repo.owner}; |
| 16 | + |
| 17 | + core.debug(`Requesting labels`); |
| 18 | + /** The list of current labels from Github for the repository. */ |
| 19 | + const repoLabels = await github.paginate(github.issues.listLabelsForRepo, context.repo); |
| 20 | + core.debug(`Retrieved ${repoLabels.length} from Github`); |
| 21 | + |
| 22 | + // For each label in the list of managed labels, ensure that it exists and is in sync. |
| 23 | + // NOTE: Not all labels in repositories are managed. Labels which are not included or managed in |
| 24 | + // our tooling definitions and configurations are ignored entirely by tooling. |
| 25 | + for (const {description, label} of managedLabels) { |
| 26 | + await core.group(`Syncing label: ${label}`, async () => { |
| 27 | + /** The label from Github if a match is found. */ |
| 28 | + const matchedLabel = repoLabels.find(({name}: GithubLabel) => name === label); |
| 29 | + |
| 30 | + // When no matched label is found, Github doesn't currently have the label we intend to sync, |
| 31 | + // we create the label via the API directly. |
| 32 | + if (matchedLabel === undefined) { |
| 33 | + core.info('Adding label to repository'); |
| 34 | + await github.issues.createLabel({...repo, name: label, description: description}); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + // If a description and name of the label are defined for the managed label, and they match |
| 39 | + // the current name and description of the label from Github, everything is in sync. |
| 40 | + if ( |
| 41 | + (description === undefined || description === matchedLabel.description) && |
| 42 | + (label === undefined || label === matchedLabel.name) |
| 43 | + ) { |
| 44 | + core.info('Skipping, label already in sync'); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + // Since the synced and new label cases have been handled, the only remaining action would be |
| 49 | + // to update the label to bring the name and description in sync without expectations. |
| 50 | + core.info('Updating label in repository'); |
| 51 | + await github.issues.updateLabel({ |
| 52 | + ...repo, |
| 53 | + new_name: label, |
| 54 | + name: matchedLabel.name, |
| 55 | + description: description, |
| 56 | + }); |
| 57 | + }); |
| 58 | + } |
| 59 | + core.endGroup(); |
| 60 | +} |
| 61 | + |
| 62 | +async function main() { |
| 63 | + /** The Github API instance to use for requests. */ |
| 64 | + const github = new Octokit({auth: await getAuthTokenFor(ANGULAR_ROBOT)}); |
| 65 | + |
| 66 | + try { |
| 67 | + /** The list of managed labels. */ |
| 68 | + const labels = [...Object.values(allLabels)]; |
| 69 | + /** The repositories to sync the labels in, from the provided config. */ |
| 70 | + const repos = core.getMultilineInput('repos', {required: true, trimWhitespace: true}); |
| 71 | + |
| 72 | + await core.group('Repos being synced:', async () => |
| 73 | + repos.forEach((repo) => core.info(`- ${repo}`)), |
| 74 | + ); |
| 75 | + for (const repo of repos) { |
| 76 | + await syncLabelsInRepo(github, repo, labels); |
| 77 | + } |
| 78 | + } finally { |
| 79 | + await revokeActiveInstallationToken(github); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +main().catch((err) => { |
| 84 | + console.error(err); |
| 85 | + core.setFailed('Failed with the above error'); |
| 86 | +}); |
0 commit comments