Skip to content

Commit a002ba8

Browse files
committed
combine thread and issue labeled handler functions
1 parent 827f7e2 commit a002ba8

File tree

3 files changed

+66
-72
lines changed

3 files changed

+66
-72
lines changed

index.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const analytics = require('./src/analytics')
55

66
const threadLabeled = require('./src/thread/labeled')
77
const threadOpened = require('./src/thread/opened')
8-
const issueLabeled = require('./src/issue/labeled')
98
const pullLabeled = require('./src/pull/labeled')
109
const pullMerged = require('./src/pull/merged')
1110
const pullSynchronized = require('./src/pull/synchronized')
@@ -100,15 +99,14 @@ module.exports = async ({ app, getRouter }) => {
10099
}
101100

102101
// Listeners
103-
// TODO combine first 2 listeners
104102
app.on(
105103
// All pull requests are issues in GitHub REST V3
106104
['issues.labeled', 'issues.unlabeled', 'pull_request.labeled', 'pull_request.unlabeled'],
107-
wrapPaymentCheck(threadLabeled(queue))
105+
wrapPaymentCheck(threadLabeled.close(queue))
108106
)
109107
app.on(
110108
['issues.labeled', 'issues.unlabeled', 'pull_request.labeled', 'pull_request.unlabeled'],
111-
wrapPaymentCheck(issueLabeled(queue))
109+
wrapPaymentCheck(threadLabeled.comment(queue))
112110
)
113111

114112
app.on(['issues.opened', 'pull_request.opened'], wrapPaymentCheck(threadOpened(queue)))

src/issue/labeled.js

-66
This file was deleted.

src/thread/labeled.js

+64-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,73 @@ const ms = require('ms')
66
const { getId, executeAction } = require('../util')
77
const getConfig = require('../config')
88
const { COMMENT, CLOSE } = require('../constants')
9-
const { timeToNumber, getLabelConfig, labelToAction } = require('./util')
9+
const {
10+
timeToNumber,
11+
getLabelConfig,
12+
getEffectiveLabel,
13+
labelToAction,
14+
labelsByAction,
15+
} = require('./util')
1016
const analytics = require('../analytics')
1117
const { closeIssue } = require('../api')
1218

13-
module.exports = (queue) => async (context) => {
19+
module.exports.close = (queue) => async (context) => {
20+
const thread = context.payload.pull_request || context.payload.issue
21+
22+
if (thread.state === 'closed') {
23+
return
24+
}
25+
26+
const ID = getId(context, { action: CLOSE })
27+
28+
const config = await getConfig(context)
29+
30+
const withClosableLabels = thread.labels.filter(labelsByAction(config, CLOSE))
31+
32+
if (withClosableLabels.length) {
33+
const { label, time } = getEffectiveLabel(config, withClosableLabels)
34+
35+
const jobExists = await queue.getJob(ID)
36+
if (!jobExists) {
37+
const { comment } = getLabelConfig(config, label.name, CLOSE)
38+
39+
if (comment && comment.trim() !== 'false') {
40+
const body = comment
41+
.replace('$DELAY', time == null ? '' : ms(time, { long: true }))
42+
.replace('$LABEL', label.name)
43+
.replace('$AUTHOR', thread.user.login)
44+
context.octokit.issues.createComment(context.issue({ body }))
45+
}
46+
}
47+
48+
if (time >= 0) {
49+
return queue
50+
.createJob({
51+
...context.issue({ installation_id: context.payload.installation.id }),
52+
action: CLOSE,
53+
})
54+
.setId(ID)
55+
.delayUntil(Date.now() + time)
56+
.save()
57+
.then((job) => {
58+
analytics.track(() => ({
59+
userId: context.payload.installation.id,
60+
event: `Close job created`,
61+
properties: {
62+
...job.data,
63+
id: job.id,
64+
},
65+
}))
66+
return job
67+
})
68+
}
69+
}
70+
71+
// If closable labels are removed, delete job for this issue
72+
return queue.removeJob(ID)
73+
}
74+
75+
module.exports.comment = (queue) => async (context) => {
1476
const thread = context.payload.pull_request || context.payload.issue
1577

1678
const config = await getConfig(context)

0 commit comments

Comments
 (0)