Skip to content

Commit c5ccf9f

Browse files
committed
reduce frequency of label presence checks
1 parent 9e5ff6a commit c5ccf9f

File tree

3 files changed

+68
-43
lines changed

3 files changed

+68
-43
lines changed

src/handlers/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { Probot } from 'probot';
22

33
import CheckSuite from './checkSuite.js';
4+
import Installation from './installation.js';
45
import PullRequest from './pullRequest.js';
56
import PullRequestReview from './pullRequestReview.js';
67

78
export default (app: Probot) => {
89
CheckSuite(app);
10+
Installation(app);
911
PullRequest(app);
1012
PullRequestReview(app);
1113
};

src/handlers/installation.ts

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
};

src/index.ts

-43
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Probot } from 'probot';
22

33
import Handlers from './handlers/index.js';
4-
import { labels } from './labels.js';
54

65
export default (app: Probot) => {
76
/*
@@ -17,48 +16,6 @@ export default (app: Probot) => {
1716
});
1817
*/
1918

20-
app.on(
21-
['pull_request', 'pull_request_review', 'check_suite'],
22-
async context => {
23-
const owner = context.payload.repository.owner.login;
24-
const repo = context.payload.repository.name;
25-
26-
const existingLabels = (
27-
await context.octokit.issues.listLabelsForRepo({
28-
owner,
29-
repo,
30-
})
31-
).data;
32-
33-
for (const label of Object.values(labels)) {
34-
const existing = existingLabels.find(l => l.name === label.name);
35-
const color = label.color.substring(1).toUpperCase();
36-
37-
if (!existing) {
38-
await context.octokit.issues.createLabel({
39-
owner,
40-
repo,
41-
name: label.name,
42-
color,
43-
});
44-
45-
continue;
46-
}
47-
48-
if (existing.color.toUpperCase() !== color) {
49-
await context.octokit.issues.updateLabel({
50-
owner,
51-
repo,
52-
name: label.name,
53-
color,
54-
});
55-
56-
continue;
57-
}
58-
}
59-
},
60-
);
61-
6219
app.log.info('probot loaded');
6320
Handlers(app);
6421
};

0 commit comments

Comments
 (0)