2018-07-23 05:31:43 +00:00
|
|
|
// Description:
|
|
|
|
// Script that listens to GitHub pull requests events
|
|
|
|
// and manages a checklist based on configuration in github-bot.yml
|
|
|
|
// It sets the mergeable status based checklist state
|
|
|
|
//
|
|
|
|
// Dependencies:
|
|
|
|
// github: "^13.1.0"
|
2019-01-31 15:07:54 +00:00
|
|
|
// probot-config: "^1.0.0"
|
2018-07-23 05:31:43 +00:00
|
|
|
//
|
|
|
|
// Author:
|
|
|
|
// virneo
|
|
|
|
|
|
|
|
const fetch = require('node-fetch')
|
|
|
|
const getConfig = require('probot-config')
|
|
|
|
const defaultConfig = require('../lib/config')
|
2019-01-10 12:57:39 +00:00
|
|
|
const gitHubHelpers = require('../lib/github-helpers')
|
2018-07-23 05:31:43 +00:00
|
|
|
const botName = 'manage-pr-checklist'
|
|
|
|
|
|
|
|
module.exports = (robot) => {
|
|
|
|
robot.on(
|
|
|
|
[
|
|
|
|
'pull_request.opened',
|
|
|
|
'pull_request.edited'
|
|
|
|
],
|
|
|
|
context => {
|
|
|
|
// Make sure we don't listen to our own messages
|
|
|
|
if (context.isBot) { return }
|
|
|
|
handlePullRequest(context, robot)
|
|
|
|
})
|
|
|
|
robot.on('issue_comment', context => {
|
|
|
|
// Make sure we don't listen to our own messages
|
|
|
|
if (context.isBot) { return }
|
|
|
|
handleIssue(context, robot)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handleIssue (context, robot) {
|
|
|
|
if (context.payload.issue.pull_request) {
|
|
|
|
const res = await fetch(context.payload.issue.pull_request.url)
|
|
|
|
const pr = await res.json()
|
|
|
|
context.payload.pull_request = pr
|
|
|
|
return handlePullRequest(context, robot)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handlePullRequest (context, robot) {
|
|
|
|
const config = await getConfig(context, 'github-bot.yml', defaultConfig(robot, '.github/github-bot.yml'))
|
|
|
|
const settings = config ? config['prchecklist'] : null
|
|
|
|
if (!settings) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (settings.title == null) settings.title = ''
|
|
|
|
if (settings.checklist == null) settings.checklist = {}
|
2019-01-10 12:57:39 +00:00
|
|
|
const currentStatus = await gitHubHelpers.getPullRequestCurrentStatusForContext(context, 'PRChecklist')
|
2018-10-10 09:08:39 +00:00
|
|
|
const { isChecklistComplete, firstCheck } = await verifyChecklist(context, settings)
|
2018-07-23 05:31:43 +00:00
|
|
|
const newStatus = isChecklistComplete ? 'success' : 'pending'
|
|
|
|
const hasChange = firstCheck || currentStatus !== newStatus
|
|
|
|
const logStatus = isChecklistComplete ? '✅' : '⏳'
|
|
|
|
const shortUrl = context.payload.pull_request.url
|
|
|
|
|
|
|
|
if (!hasChange) {
|
|
|
|
robot.log.info(`${botName} - 😐${logStatus} ${shortUrl}`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2018-10-10 09:08:39 +00:00
|
|
|
const { found, comment } = await getCheckListComment(context)
|
|
|
|
let targetUrl = 'https://github.com/status-im/status-github-bot.git'
|
2018-09-24 13:24:19 +00:00
|
|
|
if (found) {
|
2018-10-10 09:08:39 +00:00
|
|
|
targetUrl = comment.url
|
2018-09-24 13:24:19 +00:00
|
|
|
}
|
|
|
|
|
2018-07-23 05:31:43 +00:00
|
|
|
await context.github.repos.createStatus(context.repo({
|
|
|
|
sha: context.payload.pull_request.head.sha,
|
|
|
|
state: newStatus,
|
2018-09-24 13:24:19 +00:00
|
|
|
target_url: targetUrl,
|
2018-07-23 05:31:43 +00:00
|
|
|
description: isChecklistComplete ? 'ready for merge' : 'PR Checklist is incomplete',
|
|
|
|
context: 'PRChecklist'
|
|
|
|
}))
|
|
|
|
|
|
|
|
robot.log.info(`${botName} - 💾${logStatus} ${shortUrl}`)
|
|
|
|
} catch (err) {
|
|
|
|
robot.log.error(`${botName} - Couldn't create status for commits in the PR: ${err}`, context.payload.pull_request.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createOrEditChecklist (context, checkList, header) {
|
|
|
|
const owner = context.payload.repository.owner.login
|
|
|
|
const repo = context.payload.repository.name
|
|
|
|
const number = context.payload.pull_request.number
|
|
|
|
if (checkList && checkList.length > 0) {
|
2018-09-20 08:30:28 +00:00
|
|
|
let body = '<!--prchecklist--> \n### ' + header + '\n'
|
2018-07-23 05:31:43 +00:00
|
|
|
for (const key of checkList) {
|
|
|
|
body += '- [ ] ' + key + '\n'
|
|
|
|
}
|
|
|
|
await context.github.issues.createComment({ owner, repo, number, body })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function verifyChecklist (context, settings) {
|
|
|
|
let isChecklistComplete = true
|
|
|
|
let firstCheck = false
|
2018-10-10 09:08:39 +00:00
|
|
|
const { found, body } = await getCheckListBody(context)
|
2018-07-23 05:31:43 +00:00
|
|
|
if (found) {
|
|
|
|
if (body) {
|
|
|
|
for (const str of body) {
|
|
|
|
const res = str.match(/(-\s\[(\s)])(.*)/gm)
|
|
|
|
if (res != null) {
|
|
|
|
isChecklistComplete = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
isChecklistComplete = false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
await createOrEditChecklist(context, settings.checklist, settings.title)
|
|
|
|
isChecklistComplete = false
|
|
|
|
firstCheck = true
|
|
|
|
}
|
2018-10-10 09:08:39 +00:00
|
|
|
return { isChecklistComplete, firstCheck }
|
2018-07-23 05:31:43 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 13:24:19 +00:00
|
|
|
async function getCheckListComment (context) {
|
2018-07-23 05:31:43 +00:00
|
|
|
try {
|
|
|
|
const owner = context.payload.repository.owner.login
|
|
|
|
const repo = context.payload.repository.name
|
|
|
|
const number = context.payload.pull_request.number
|
2019-01-31 14:50:23 +00:00
|
|
|
const comments = await context.github.paginate(context.github.issues.listComments({ owner, repo, number }), res => res.data)
|
2018-07-23 05:31:43 +00:00
|
|
|
for (const comment of comments) {
|
2018-10-10 09:08:39 +00:00
|
|
|
const { found } = checkPRChecklist(comment.body)
|
2018-07-23 05:31:43 +00:00
|
|
|
if (found) {
|
2018-10-10 09:08:39 +00:00
|
|
|
return { found, comment }
|
2018-07-23 05:31:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
} catch (e) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 13:24:19 +00:00
|
|
|
async function getCheckListBody (context) {
|
2018-10-10 09:08:39 +00:00
|
|
|
const { found, comment } = await getCheckListComment(context)
|
2018-09-24 13:24:19 +00:00
|
|
|
if (found) {
|
2018-10-10 09:08:39 +00:00
|
|
|
const { body } = checkPRChecklist(comment.body)
|
2018-09-24 13:24:19 +00:00
|
|
|
|
2018-10-10 09:08:39 +00:00
|
|
|
return { found, body }
|
2018-09-24 13:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-07-23 05:31:43 +00:00
|
|
|
function checkPRChecklist (str) {
|
|
|
|
let found = false
|
|
|
|
let body = null
|
|
|
|
const isBotComment = str.match(/(<!--prchecklist-->)/g)
|
2018-10-10 09:08:39 +00:00
|
|
|
if (isBotComment == null) return { found, body }
|
|
|
|
const res = str.match(/(-\s\[(\s|x)])(.*)/gm)
|
2018-07-23 05:31:43 +00:00
|
|
|
if (res && res.length > 0) {
|
|
|
|
found = true
|
|
|
|
body = res
|
|
|
|
}
|
2018-10-10 09:08:39 +00:00
|
|
|
return { found, body }
|
2018-07-23 05:31:43 +00:00
|
|
|
}
|