mirror of
https://github.com/xuexb/github-bot.git
synced 2025-12-08 17:36:07 +00:00
feat: unified api
This commit is contained in:
parent
4c7faf15d8
commit
b4c2a2bb80
@ -43,8 +43,7 @@ module.exports = on => {
|
||||
})
|
||||
|
||||
on('pull_request_edited', async ({ payload, repo }) => {
|
||||
if (match(payload.pull_request.title)) {
|
||||
await pullRequestHasLabel(payload, 'invalid')
|
||||
if (match(payload.pull_request.title) && await pullRequestHasLabel(payload, 'invalid')) {
|
||||
commentPullRequest(
|
||||
payload,
|
||||
format(commentSuccess, {
|
||||
|
||||
@ -18,13 +18,13 @@ const ACTION_TO_LABEL_MAP = {
|
||||
docs: 'document'
|
||||
}
|
||||
|
||||
const handle = ({ payload, repo }) => {
|
||||
const handle = async ({ payload, repo }) => {
|
||||
const action = getAction(payload.pull_request.title)
|
||||
|
||||
if (action && ACTION_TO_LABEL_MAP[action]) {
|
||||
pullRequestHasLabel(payload, ACTION_TO_LABEL_MAP[action]).catch(() => {
|
||||
const exist = await pullRequestHasLabel(payload, ACTION_TO_LABEL_MAP[action])
|
||||
if (!exist) {
|
||||
addLabelsToPullRequest(payload, ACTION_TO_LABEL_MAP[action])
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -18,77 +18,73 @@ module.exports = on => {
|
||||
tag_name: payload.ref
|
||||
})
|
||||
// 如果该 tag 存在则直接返回
|
||||
if (tag !== false) {
|
||||
if (tag !== null) {
|
||||
return
|
||||
}
|
||||
|
||||
// 创建 release note
|
||||
try {
|
||||
const tags = await getTags(payload)
|
||||
const head = tags[0].name
|
||||
const base = tags.length > 1 ? tags[1].name : tags[0].name
|
||||
const tags = await getTags(payload)
|
||||
|
||||
const commitsLog = await compareCommits(payload, {
|
||||
base,
|
||||
head
|
||||
})
|
||||
// 如果只有一个 tag 则没法对比,忽略
|
||||
if (tags.length < 2) {
|
||||
return
|
||||
}
|
||||
|
||||
const commits = commitsLog.commits
|
||||
const changes = Object.keys(RELEASE_CHANGE_MAP).map(title => {
|
||||
let data = []
|
||||
commits.map((commit) => {
|
||||
if (commit.commit.message.indexOf(`${RELEASE_CHANGE_MAP[title]}:`) === 0) {
|
||||
const head = tags[0].name
|
||||
const base = tags[1].name
|
||||
|
||||
const commitsLog = await compareCommits(payload, {
|
||||
base,
|
||||
head
|
||||
})
|
||||
|
||||
const commits = commitsLog.commits
|
||||
const changes = Object.keys(RELEASE_CHANGE_MAP).map(title => {
|
||||
return {
|
||||
title,
|
||||
data: commits
|
||||
.filter((commit) => commit.commit.message.indexOf(`${RELEASE_CHANGE_MAP[title]}:`) === 0)
|
||||
.map((commit) => {
|
||||
let message = commit.commit.message
|
||||
// 处理 squash merge 的 commit message
|
||||
// 后期看看有没有更好的解决办法?
|
||||
if (message.indexOf('\n') !== -1) {
|
||||
message = message.substr(0, message.indexOf('\n'))
|
||||
}
|
||||
data.push(`- ${message}, by @${commit.commit.author.name} <<${commit.commit.author.email}>>`)
|
||||
}
|
||||
})
|
||||
return {
|
||||
title,
|
||||
data
|
||||
}
|
||||
}).filter(v => v.data.length)
|
||||
return `- ${message}, by @${commit.author.login} <<${commit.commit.author.email}>>`
|
||||
})
|
||||
}
|
||||
}).filter(v => v.data.length)
|
||||
|
||||
const hashChanges = commits.map((commit) => {
|
||||
let message = commit.commit.message
|
||||
// 处理 squash merge 的 commit message
|
||||
if (message.indexOf('\n') !== -1) {
|
||||
message = message.substr(0, message.indexOf('\n'))
|
||||
}
|
||||
return `- [${commit.sha.substr(0, 7)}](${commit.html_url}) - ${message}, by @${commit.commit.author.name} <<${commit.commit.author.email}>>`
|
||||
const hashChanges = commits.map((commit) => {
|
||||
let message = commit.commit.message
|
||||
// 处理 squash merge 的 commit message
|
||||
if (message.indexOf('\n') !== -1) {
|
||||
message = message.substr(0, message.indexOf('\n'))
|
||||
}
|
||||
return `- [${commit.sha.substr(0, 7)}](${commit.html_url}) - ${message}, by @${commit.author.login} <<${commit.commit.author.email}>>`
|
||||
})
|
||||
|
||||
let body = []
|
||||
|
||||
if (changes.length) {
|
||||
body.push('## Notable changes\n')
|
||||
changes.forEach(v => {
|
||||
body.push(`- ${v.title}`)
|
||||
|
||||
v.data.forEach(line => body.push(' ' + line))
|
||||
})
|
||||
}
|
||||
|
||||
let body = []
|
||||
if (hashChanges.length) {
|
||||
body.push('\n## Commits\n')
|
||||
body = body.concat(hashChanges)
|
||||
}
|
||||
|
||||
if (changes.length) {
|
||||
body.push('## Notable changes\n')
|
||||
changes.forEach(v => {
|
||||
body.push([
|
||||
`- ${v.title}`
|
||||
])
|
||||
|
||||
v.data.forEach(line => body.push(' ' + line))
|
||||
})
|
||||
}
|
||||
|
||||
if (hashChanges.length) {
|
||||
body.push('\n## Commits\n')
|
||||
body = body.concat(hashChanges)
|
||||
}
|
||||
|
||||
if (body.length) {
|
||||
createRelease(payload, {
|
||||
tag_name: payload.ref,
|
||||
name: `${payload.ref} @${payload.repository.owner.login}`,
|
||||
body: body.join('\n')
|
||||
})
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
if (body.length) {
|
||||
createRelease(payload, {
|
||||
tag_name: payload.ref,
|
||||
name: `${payload.ref} @${payload.repository.owner.login}`,
|
||||
body: body.join('\n')
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user