Skip to content

Commit

Permalink
combine thread and issue labeled handler functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mfix22 committed Feb 21, 2022
1 parent 827f7e2 commit a002ba8
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 72 deletions.
6 changes: 2 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const analytics = require('./src/analytics')

const threadLabeled = require('./src/thread/labeled')
const threadOpened = require('./src/thread/opened')
const issueLabeled = require('./src/issue/labeled')
const pullLabeled = require('./src/pull/labeled')
const pullMerged = require('./src/pull/merged')
const pullSynchronized = require('./src/pull/synchronized')
Expand Down Expand Up @@ -100,15 +99,14 @@ module.exports = async ({ app, getRouter }) => {
}

// Listeners
// TODO combine first 2 listeners
app.on(
// All pull requests are issues in GitHub REST V3
['issues.labeled', 'issues.unlabeled', 'pull_request.labeled', 'pull_request.unlabeled'],
wrapPaymentCheck(threadLabeled(queue))
wrapPaymentCheck(threadLabeled.close(queue))
)
app.on(
['issues.labeled', 'issues.unlabeled', 'pull_request.labeled', 'pull_request.unlabeled'],
wrapPaymentCheck(issueLabeled(queue))
wrapPaymentCheck(threadLabeled.comment(queue))
)

app.on(['issues.opened', 'pull_request.opened'], wrapPaymentCheck(threadOpened(queue)))
Expand Down
66 changes: 0 additions & 66 deletions src/issue/labeled.js

This file was deleted.

66 changes: 64 additions & 2 deletions src/thread/labeled.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,73 @@ const ms = require('ms')
const { getId, executeAction } = require('../util')
const getConfig = require('../config')
const { COMMENT, CLOSE } = require('../constants')
const { timeToNumber, getLabelConfig, labelToAction } = require('./util')
const {
timeToNumber,
getLabelConfig,
getEffectiveLabel,
labelToAction,
labelsByAction,
} = require('./util')
const analytics = require('../analytics')
const { closeIssue } = require('../api')

module.exports = (queue) => async (context) => {
module.exports.close = (queue) => async (context) => {
const thread = context.payload.pull_request || context.payload.issue

if (thread.state === 'closed') {
return
}

const ID = getId(context, { action: CLOSE })

const config = await getConfig(context)

const withClosableLabels = thread.labels.filter(labelsByAction(config, CLOSE))

if (withClosableLabels.length) {
const { label, time } = getEffectiveLabel(config, withClosableLabels)

const jobExists = await queue.getJob(ID)
if (!jobExists) {
const { comment } = getLabelConfig(config, label.name, CLOSE)

if (comment && comment.trim() !== 'false') {
const body = comment
.replace('$DELAY', time == null ? '' : ms(time, { long: true }))
.replace('$LABEL', label.name)
.replace('$AUTHOR', thread.user.login)
context.octokit.issues.createComment(context.issue({ body }))
}
}

if (time >= 0) {
return queue
.createJob({
...context.issue({ installation_id: context.payload.installation.id }),
action: CLOSE,
})
.setId(ID)
.delayUntil(Date.now() + time)
.save()
.then((job) => {
analytics.track(() => ({
userId: context.payload.installation.id,
event: `Close job created`,
properties: {
...job.data,
id: job.id,
},
}))
return job
})
}
}

// If closable labels are removed, delete job for this issue
return queue.removeJob(ID)
}

module.exports.comment = (queue) => async (context) => {
const thread = context.payload.pull_request || context.payload.issue

const config = await getConfig(context)
Expand Down

0 comments on commit a002ba8

Please sign in to comment.