-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (63 loc) · 2.47 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* This is the main entrypoint to your Probot app
* @param {import('probot').Probot} app
*/
const { Octokit } = require("@octokit/core");
const { createProbotAuth } = require("octokit-auth-probot");
const { createAppAuth } = require("@octokit/auth-app");
const { config, composeConfigGet } = require("@probot/octokit-plugin-config");
const ProbotOctokit = Octokit.defaults({
authStrategy: createProbotAuth,
});
const processPull = async (pull, octokit, config, log) => {
if ((config.forkOnly == true) && (pull.head.repo.fork == false)) {
return;
}
log.info("Closed " + pull.base.repo.name + " " + pull.number)
await octokit.issues.createComment({
owner: pull.base.repo.owner.login,
repo: pull.base.repo.name,
issue_number: pull.number,
body: config.closureComment,
});
return await octokit.pulls.update({
owner: pull.base.repo.owner.login,
repo: pull.base.repo.name,
pull_number: pull.number,
state: "closed"
});
}
const processRepository = async (repository, octokit, config, log) => {
log.info("Processing " + repository.name)
pulls = await octokit.pulls.list({ owner: repository.owner.login, repo: repository.name})
pulls.data.forEach(async (pull) => { await processPull(pull, octokit, config, log) })
}
module.exports = async (app) => {
app.log.info("Started pr-auto-close bot");
const octokit = await app.auth(process.env.INSTALLATION_ID, app.log);
/*
* Pull the config from a known repo that is not the target as the target
* so that we can change the config independent of the target's release cycle.
*/
const cfg = await octokit.config.get({
owner: process.env.CONFIG_OWNER,
repo: process.env.CONFIG_REPO,
path: ".github/pr-auto-close.yml",
});
/*
* Go get any PRs that were opened while we were not running. Don't care about pagination
* because the number of open PRs are in the single digits.
*/
repositories = await octokit.apps.listReposAccessibleToInstallation();
repositories.data.repositories.forEach(async (repository) => { await processRepository(repository, octokit, cfg.config, app.log) });
/*
* Handle PRs as they come in.
*/
app.on(["pull_request.opened", "pull_request.reopened"], async (context) => {
return processPull(context.payload.pull_request, octokit, cfg.config, app.log);
});
// For more information on building apps:
// https://probot.github.io/docs/
// To get your app running against GitHub, see:
// https://probot.github.io/docs/development/
};