Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduce frequency of label presence checks #14

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -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);
};
66 changes: 66 additions & 0 deletions src/handlers/installation.ts
Original file line number Diff line number Diff line change
@@ -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);
}
},
);
};
43 changes: 0 additions & 43 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Probot } from 'probot';

import Handlers from './handlers/index.js';
import { labels } from './labels.js';

export default (app: Probot) => {
/*
Expand All @@ -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);
};