37 lines
1.2 KiB
JavaScript
37 lines
1.2 KiB
JavaScript
const gitea = require('../gitea-api')
|
|
|
|
const log = (msg) => {
|
|
console.log('[issueBranchName]', msg)
|
|
}
|
|
|
|
module.exports = {
|
|
exec: (req) => {
|
|
if(req.headers['x-gitea-event'] !== 'issues') {
|
|
return
|
|
}
|
|
const { issue, repository } = req.body
|
|
const isBug = issue.labels.length && !!issue.labels.find(label => label.name === 'bug')
|
|
const prefix = isBug ? 'bugfix' : 'feature'
|
|
const branch = issue.title.replace(/[^a-z0-9\s]/gi, '').replaceAll(' ', '-')
|
|
const branchName = `${prefix}/${issue.number}-${branch}`
|
|
|
|
log(`Created branch name ${branchName} in ${repository.full_name}`)
|
|
|
|
issue.body = issue.body || ''
|
|
|
|
let body
|
|
if(issue.body.includes('Branchname')) {
|
|
body = issue.body.replaceAll(/\*\*Branchname\*\*: <code>.+<\/code>/g, `**Branchname**: <code>${branchName}</code>`)
|
|
}else {
|
|
body = `${issue.body}<br>**Branchname**: <code>${branchName}</code>`
|
|
}
|
|
|
|
body = body.replaceAll('\n', '\\n')
|
|
|
|
const path = `/repos/${repository.full_name}/issues/${issue.number}`
|
|
return gitea.patch(path, { body })
|
|
.then(() => {})
|
|
.catch(reason => reason)
|
|
}
|
|
|
|
} |