This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
84 lines (76 loc) · 3.49 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
75
76
77
78
79
80
81
82
83
const command = require('commander');
const pkg = require('./package.json');
const GithubClient = require('./lib/github');
const GithubGraph = require('./lib/githubGraph');
const StaleRepos = require('./lib/stalerepos');
const notifiers = {
email: require('./lib/notifiers/email'),
github: require('./lib/notifiers/github'),
slack: require('./lib/notifiers/slack'),
console: require('./lib/notifiers/console'),
};
const notify = require('./lib/notify');
const filters = require('./lib/filters');
process.title = 'drillsergeant';
const coerceList = function(input) {
return input.split(',');
};
command
.version(pkg.version)
.option('-r, --repo <user/repository>', 'Define the [comma delimited] repositories to check PRs.', coerceList, [])
.option('-e, --email [email@address]', 'Set the [comma delimited] email address(es) to be notified.', null)
.option('-f, --replyto [Notifier Title <email@address>]', 'Set the reply to email address.', 'Drill Sergeant Notifier <no-reply@drillsergeant>')
.option('--subject-template [Some template (<%= date %>)', 'Set the email notification subject template. (date variable available in the template.)', 'Drill Sergeant Stale Pull Request Report (<%= date %>)')
.option('-l, --label', 'Should drill sergeant label the PR as stale?', false)
.option('-s, --staletime [number of hours]', 'Set the PR stale threshold. (default: 24)', 24)
.option('--include-labels [labels]', 'Define a [comma delimited] group of labels of which a PR must have at least one.', coerceList, [])
.option('--exclude-labels [labels]', 'Define a [comma delimited] group of labels of which a PR must NOT contain.', coerceList, [])
.option('--include-reviewed [count]', 'Filter PRs with at least [count] approved reviews.', 0)
.option('--exclude-reviewed [count]', 'Filter PRs without at least [count] approved reviews.', 0)
.option('--exclude-drafts', 'Should drill sergeant filter PRs in draft status?', false)
.option('--slack-webhook [url]', 'Slack webhook URL to post messages.')
.parse(process.argv);
if (!process.env.GITHUB_TOKEN) {
console.error('GITHUB_TOKEN is required.');
process.exit(1);
}
if (!command.repo.length) {
console.error('Repo argument must be provided.');
process.exit(1);
}
async function main() {
const githubGraph = new GithubGraph(process.env.GITHUB_TOKEN);
const githubClient = new GithubClient(process.env.GITHUB_TOKEN);
const notifier = new notify();
const stalerepos = new StaleRepos(githubGraph);
try {
const allPRs = await stalerepos.retrieve(command.repo, command.staletime);
const results = allPRs
.filter(filters.includeLabels.bind(null, command.includeLabels))
.filter(filters.excludeLabels.bind(null, command.excludeLabels))
.filter(repo => command.includeReviewed === 0 || filters.includeReviewed(command.includeReviewed, repo))
.filter(repo => command.excludeReviewed === 0 || filters.excludeReviewed(command.excludeReviewed, repo))
.filter(repo => !command.excludeDrafts || filters.excludeDrafts(repo));
if (!results.length) {
console.log('No stale pull requests to report.');
return;
}
if (command.email) {
notifier.add(new notifiers.email(command.email, command.replyto, command.subjectTemplate));
}
if (command.label) {
notifier.add(new notifiers.github(githubClient));
}
if (command.slackWebhook) {
notifier.add(new notifiers.slack(command.slackWebhook));
}
if (notifier.length() === 0) {
notifier.add(notifiers.console);
}
await notifier.notifyAll(results);
} catch (e) {
console.error(e);
process.exit(1);
}
}
main();