|
| 1 | +import { Probot, ProbotOctokit } from 'probot'; |
| 2 | + |
| 3 | +import { labels } from '../labels.js'; |
| 4 | + |
| 5 | +const ensureLabelsExist = async ( |
| 6 | + owner: string, |
| 7 | + repo: string, |
| 8 | + octokit: ProbotOctokit, |
| 9 | +) => { |
| 10 | + const existingLabels = ( |
| 11 | + await octokit.issues.listLabelsForRepo({ |
| 12 | + owner, |
| 13 | + repo, |
| 14 | + }) |
| 15 | + ).data; |
| 16 | + |
| 17 | + for (const label of Object.values(labels)) { |
| 18 | + const existing = existingLabels.find(l => l.name === label.name); |
| 19 | + const color = label.color.substring(1).toUpperCase(); |
| 20 | + |
| 21 | + if (!existing) { |
| 22 | + await octokit.issues.createLabel({ |
| 23 | + owner, |
| 24 | + repo, |
| 25 | + name: label.name, |
| 26 | + color, |
| 27 | + }); |
| 28 | + |
| 29 | + continue; |
| 30 | + } |
| 31 | + |
| 32 | + if (existing.color.toUpperCase() !== color) { |
| 33 | + await octokit.issues.updateLabel({ |
| 34 | + owner, |
| 35 | + repo, |
| 36 | + name: label.name, |
| 37 | + color, |
| 38 | + }); |
| 39 | + |
| 40 | + continue; |
| 41 | + } |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +export default (app: Probot) => { |
| 46 | + app.on( |
| 47 | + [ |
| 48 | + 'installation.created', |
| 49 | + 'installation.new_permissions_accepted', |
| 50 | + 'installation.unsuspend', |
| 51 | + 'installation_repositories.added', |
| 52 | + ], |
| 53 | + async context => { |
| 54 | + const installationId = context.payload.installation.id; |
| 55 | + |
| 56 | + const repos = |
| 57 | + await context.octokit.apps.listReposAccessibleToInstallation({ |
| 58 | + installation_id: installationId, |
| 59 | + }); |
| 60 | + |
| 61 | + for (const r of repos.data.repositories) { |
| 62 | + await ensureLabelsExist(r.owner.login, r.name, context.octokit); |
| 63 | + } |
| 64 | + }, |
| 65 | + ); |
| 66 | +}; |
0 commit comments