diff --git a/src/hooks/issuePrRef.js b/src/hooks/issuePrRef.js new file mode 100644 index 0000000..1dcf95b --- /dev/null +++ b/src/hooks/issuePrRef.js @@ -0,0 +1,30 @@ +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 } = req.body + log(`Try referencing ref ${pull_request.head.ref} to issue`) + + const regex = /refs\/heads\/(feature|bugfix)\/ISS-(\d+)-/ + const issueId = regex.exec(ref)[2] + + let body = pull_request.body + if(body.includes('> ')) { + body = body.replaceAll(/> .+<\/code>/g, `> issue #${issueId}`) + }else { + body += '\n\n' + body += `> issue #${issueId}` + } + + const path = `/repos/${repository.full_name}/pulls/${pull_request.number}` + + gitea.patch(path, { body }) + } +} \ No newline at end of file