From c5ccf9f1d1ef595d0b7ef0e18aacfbbde4f03eba Mon Sep 17 00:00:00 2001 From: matt Date: Sun, 7 Jul 2024 23:56:14 +0000 Subject: [PATCH] reduce frequency of label presence checks --- src/handlers/index.ts | 2 ++ src/handlers/installation.ts | 66 ++++++++++++++++++++++++++++++++++++ src/index.ts | 43 ----------------------- 3 files changed, 68 insertions(+), 43 deletions(-) create mode 100644 src/handlers/installation.ts diff --git a/src/handlers/index.ts b/src/handlers/index.ts index 0ab7c4f..5d4c923 100644 --- a/src/handlers/index.ts +++ b/src/handlers/index.ts @@ -1,11 +1,13 @@ import { Probot } from 'probot'; import CheckSuite from './checkSuite.js'; +import Installation from './installation.js'; import PullRequest from './pullRequest.js'; import PullRequestReview from './pullRequestReview.js'; export default (app: Probot) => { CheckSuite(app); + Installation(app); PullRequest(app); PullRequestReview(app); }; diff --git a/src/handlers/installation.ts b/src/handlers/installation.ts new file mode 100644 index 0000000..986631b --- /dev/null +++ b/src/handlers/installation.ts @@ -0,0 +1,66 @@ +import { Probot, ProbotOctokit } from 'probot'; + +import { labels } from '../labels.js'; + +const ensureLabelsExist = async ( + owner: string, + repo: string, + octokit: ProbotOctokit, +) => { + const existingLabels = ( + await octokit.issues.listLabelsForRepo({ + owner, + repo, + }) + ).data; + + for (const label of Object.values(labels)) { + const existing = existingLabels.find(l => l.name === label.name); + const color = label.color.substring(1).toUpperCase(); + + if (!existing) { + await octokit.issues.createLabel({ + owner, + repo, + name: label.name, + color, + }); + + continue; + } + + if (existing.color.toUpperCase() !== color) { + await octokit.issues.updateLabel({ + owner, + repo, + name: label.name, + color, + }); + + continue; + } + } +}; + +export default (app: Probot) => { + app.on( + [ + 'installation.created', + 'installation.new_permissions_accepted', + 'installation.unsuspend', + 'installation_repositories.added', + ], + async context => { + const installationId = context.payload.installation.id; + + const repos = + await context.octokit.apps.listReposAccessibleToInstallation({ + installation_id: installationId, + }); + + for (const r of repos.data.repositories) { + await ensureLabelsExist(r.owner.login, r.name, context.octokit); + } + }, + ); +}; diff --git a/src/index.ts b/src/index.ts index d293b76..17d81c5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,6 @@ import { Probot } from 'probot'; import Handlers from './handlers/index.js'; -import { labels } from './labels.js'; export default (app: Probot) => { /* @@ -17,48 +16,6 @@ export default (app: Probot) => { }); */ - app.on( - ['pull_request', 'pull_request_review', 'check_suite'], - async context => { - const owner = context.payload.repository.owner.login; - const repo = context.payload.repository.name; - - const existingLabels = ( - await context.octokit.issues.listLabelsForRepo({ - owner, - repo, - }) - ).data; - - for (const label of Object.values(labels)) { - const existing = existingLabels.find(l => l.name === label.name); - const color = label.color.substring(1).toUpperCase(); - - if (!existing) { - await context.octokit.issues.createLabel({ - owner, - repo, - name: label.name, - color, - }); - - continue; - } - - if (existing.color.toUpperCase() !== color) { - await context.octokit.issues.updateLabel({ - owner, - repo, - name: label.name, - color, - }); - - continue; - } - } - }, - ); - app.log.info('probot loaded'); Handlers(app); };