Skip to content

Commit

Permalink
feat(github): support xxx_comment.edited/deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Sep 4, 2020
1 parent 2610cd8 commit e8c4e70
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 35 deletions.
69 changes: 36 additions & 33 deletions packages/plugin-github/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { EventNames } from '@octokit/webhooks'
import { GetWebhookPayloadTypeFromEvent } from '@octokit/webhooks/dist-types/generated/get-webhook-payload-type-from-event'

export interface EventConfig {
commitComment?: boolean
commitComment?: boolean | {
created?: boolean
}
fork?: boolean
issueComment?: boolean | {
created?: boolean
Expand Down Expand Up @@ -64,7 +66,9 @@ export interface EventConfig {
}

export const defaultEvents: EventConfig = {
commitComment: true,
commitComment: {
created: true,
},
fork: true,
issueComment: {
created: true,
Expand Down Expand Up @@ -105,18 +109,32 @@ export function addListeners(on: <T extends EventNames.All>(event: T, handler: E
.replace(/\n\s*\n/g, '\n')
}

on('commit_comment.created', ({ repository, comment }) => {
const { full_name } = repository
const { user, url, html_url, commit_id, body, path, position } = comment
if (user.type === 'bot') return
type CommentEvent = 'commit_comment' | 'issue_comment' | 'pull_request_review_comment'
type CommandHandler<E extends CommentEvent> = (payload: Payload<E>) => [target: string, replies: ReplyPayloads]

return [[
`[GitHub] ${user.login} commented on commit ${full_name}@${commit_id.slice(0, 6)}`,
`Path: ${path}`,
formatMarkdown(body),
].join('\n'), {
link: html_url,
react: url + `/reactions`,
function onComment<E extends CommentEvent>(event: E, handler: CommandHandler<E>) {
on(event as CommentEvent, (payload) => {
const { user, body, html_url, url } = payload.comment
if (user.type === 'bot') return

const [target, replies] = handler(payload)
if (payload.action === 'deleted') {
return [`[GitHub] ${user.login} deleted a comment on ${target}`]
}

const operation = payload.action === 'created' ? 'commented' : 'edited a comment'
return [`[GitHub] ${user.login} ${operation} on ${target}\n${formatMarkdown(body)}`, {
link: html_url,
react: url + `/reactions`,
...replies,
}]
})
}

onComment('commit_comment', ({ repository, comment }) => {
const { full_name } = repository
const { commit_id, path, position } = comment
return [`commit ${full_name}@${commit_id.slice(0, 6)}\nPath: ${path}`, {
// https://docs.github.com/en/rest/reference/repos#create-a-commit-comment
reply: [`https://api.github.com/repos/${full_name}/commits/${commit_id}/comments`, { path, position }],
}]
Expand All @@ -127,19 +145,11 @@ export function addListeners(on: <T extends EventNames.All>(event: T, handler: E
return [`[GitHub] ${sender.login} forked ${full_name} to ${forkee.full_name} (total ${forks_count} forks)`]
})

on('issue_comment.created', ({ comment, issue, repository }) => {
onComment('issue_comment', ({ issue, repository }) => {
const { full_name } = repository
const { number, comments_url } = issue
const { user, url, html_url, body } = comment
if (user.type === 'bot') return

const type = issue['pull_request'] ? 'pull request' : 'issue'
return [[
`[GitHub] ${user.login} commented on ${type} ${full_name}#${number}`,
formatMarkdown(body),
].join('\n'), {
link: html_url,
react: url + `/reactions`,
return [`${type} ${full_name}#${number}`, {
reply: [comments_url],
}]
})
Expand Down Expand Up @@ -172,18 +182,11 @@ export function addListeners(on: <T extends EventNames.All>(event: T, handler: E
}]
})

on('pull_request_review_comment.created', ({ repository, comment, pull_request }) => {
onComment('pull_request_review_comment', ({ repository, comment, pull_request }) => {
const { full_name } = repository
const { number } = pull_request
const { user, path, body, html_url, url } = comment
if (user.type === 'bot') return
return [[
`[GitHub] ${user.login} commented on pull request review ${full_name}#${number}`,
`Path: ${path}`,
formatMarkdown(body),
].join('\n'), {
link: html_url,
react: url + `/reactions`,
const { path, url } = comment
return [`pull request review ${full_name}#${number}\nPath: ${path}`, {
reply: [url],
}]
})
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-github/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export function apply(ctx: Context, config: Config = {}) {
if (action && baseConfig !== true) {
const actionConfig = baseConfig[action]
if (actionConfig === false) return
if (actionConfig !== true && defaultEvents[base] !== true && !(defaultEvents[base] || {})[action]) return
if (actionConfig !== true && !(defaultEvents[base] || {})[action]) return
}

// step 3: handle event
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-github/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function check(file: string) {
})
}

describe('koishi-plugin-github', () => {
describe('GitHub Plugin', () => {
describe('Webhook Events', () => {
const files = readdirSync(resolve(__dirname, 'fixtures'))
files.forEach(file => {
Expand Down

0 comments on commit e8c4e70

Please sign in to comment.