teabot/src/hooks/issuePrRef.js

30 lines
839 B
JavaScript

const gitea = require('../gitea-api')
const log = (msg) => {
console.log('[issuePrRef]', msg)
}
module.exports = {
exec: (req) => {
if(req.headers['x-gitea-event'] !== 'pull_request') {
return
}
const { pull_request, repository } = req.body
log(`Try referencing ref ${pull_request.head.ref} to issue`)
const regex = /(feature|bugfix)\/ISS-(\d+)-/
const issueId = regex.exec(pull_request.head.ref)[2]
let body = pull_request.body
if(body.includes('> <code>')) {
body = body.replaceAll(/ISS #\d+/g, `ISS #${issueId}`)
}else {
body += '\n\n'
body += `**ISS #${issueId}**`
}
const path = `/repos/${repository.full_name}/pulls/${pull_request.number}`
gitea.patch(path, { body })
}
}