Skip to content

Commit

Permalink
feat: unified api
Browse files Browse the repository at this point in the history
  • Loading branch information
xuexb authored and yugasun committed Oct 23, 2017
1 parent 4c7faf1 commit b4c2a2b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 61 deletions.
3 changes: 1 addition & 2 deletions src/modules/pull_request/replyInvalidTitle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
8 changes: 4 additions & 4 deletions src/modules/pull_request/titlePrefixToLabel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
})
}
}
}

Expand Down
106 changes: 51 additions & 55 deletions src/modules/releases/autoReleaseNote.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
let body = []

if (changes.length) {
body.push('## Notable changes\n')
changes.forEach(v => {
body.push([
`- ${v.title}`
])
if (changes.length) {
body.push('## Notable changes\n')
changes.forEach(v => {
body.push(`- ${v.title}`)

v.data.forEach(line => body.push(' ' + line))
})
}
v.data.forEach(line => body.push(' ' + line))
})
}

if (hashChanges.length) {
body.push('\n## Commits\n')
body = body.concat(hashChanges)
}
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')
})
}
})
}

0 comments on commit b4c2a2b

Please sign in to comment.