status-github-bot/bot_scripts/manage-pr-checklist.js

165 lines
4.9 KiB
JavaScript
Raw Normal View History

// 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"
//
// Author:
// virneo
const fetch = require('node-fetch')
const getConfig = require('probot-config')
const defaultConfig = require('../lib/config')
const gitHubHelpers = require('../lib/github-helpers')
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 = {}
const currentStatus = await gitHubHelpers.getPullRequestCurrentStatusForContext(context, 'PRChecklist')
2018-10-10 09:08:39 +00:00
const { isChecklistComplete, firstCheck } = await verifyChecklist(context, settings)
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'
if (found) {
2018-10-10 09:08:39 +00:00
targetUrl = comment.url
}
await context.github.repos.createStatus(context.repo({
sha: context.payload.pull_request.head.sha,
state: newStatus,
target_url: targetUrl,
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'
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)
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 }
}
async function getCheckListComment (context) {
try {
const owner = context.payload.repository.owner.login
const repo = context.payload.repository.name
const number = context.payload.pull_request.number
const comments = await context.github.paginate(context.github.issues.listComments({ owner, repo, number }), res => res.data)
for (const comment of comments) {
2018-10-10 09:08:39 +00:00
const { found } = checkPRChecklist(comment.body)
if (found) {
2018-10-10 09:08:39 +00:00
return { found, comment }
}
}
return false
} catch (e) {
return true
}
}
async function getCheckListBody (context) {
2018-10-10 09:08:39 +00:00
const { found, comment } = await getCheckListComment(context)
if (found) {
2018-10-10 09:08:39 +00:00
const { body } = checkPRChecklist(comment.body)
2018-10-10 09:08:39 +00:00
return { found, body }
}
return false
}
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)
if (res && res.length > 0) {
found = true
body = res
}
2018-10-10 09:08:39 +00:00
return { found, body }
}