From e416aaca431bbe6396c2de5ee87b5075c6e72a41 Mon Sep 17 00:00:00 2001 From: Niclas Thobaben Date: Sat, 9 Jul 2022 12:17:51 +0200 Subject: [PATCH] implemented PR issue ref --- src/hooks/issuePrRef.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/hooks/issuePrRef.js 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