Skip to content

Commit

Permalink
feat(github): support create/delete event
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Oct 18, 2020
1 parent 91d2719 commit 67529d1
Show file tree
Hide file tree
Showing 8 changed files with 732 additions and 21 deletions.
30 changes: 18 additions & 12 deletions packages/plugin-github/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ interface CommentEventConfig {

export interface EventConfig {
commitComment?: boolean | CommentEventConfig
create?: boolean
delete?: boolean
fork?: boolean
issueComment?: boolean | CommentEventConfig
issues?: boolean | {
Expand Down Expand Up @@ -70,7 +72,6 @@ export const defaultEvents: EventConfig = {
commitComment: {
created: true,
},
fork: true,
issueComment: {
created: true,
},
Expand All @@ -88,7 +89,6 @@ export const defaultEvents: EventConfig = {
pullRequestReviewComment: {
created: true,
},
push: true,
star: {
created: true,
},
Expand Down Expand Up @@ -158,6 +158,19 @@ export function addListeners(on: <T extends WebhookEvent>(event: T, handler: Eve
}]
})

function onReference<T extends 'create' | 'delete'>(event: T) {
on(event, ({ repository, ref, ref_type, sender }) => {
const ref_name = `${repository.full_name}${ref_type === 'tag' ? '@' : ':'}${ref}`
return {
message: `${sender.login} ${event}d ${ref_type} ${ref_name}`,
}
})
}

onReference('create')

onReference('delete')

on('fork', ({ repository, sender, forkee }) => {
const { full_name, forks_count } = repository
return {
Expand Down Expand Up @@ -268,18 +281,11 @@ export function addListeners(on: <T extends WebhookEvent>(event: T, handler: Eve
}
})

on('push', ({ compare, pusher, commits, repository, ref, after }) => {
on('push', ({ compare, pusher, commits, repository, ref, before, after }) => {
const { full_name } = repository

// do not show pull request merge
if (/^0+$/.test(after)) return

// use short form for tag releases
if (ref.startsWith('refs/tags')) {
return {
message: `${pusher.name} published tag ${full_name}@${ref.slice(10)}`,
}
}
// do not show branch create / delete
if (/^0+$/.test(before) || /^0+$/.test(after)) return

return {
message: [
Expand Down
10 changes: 4 additions & 6 deletions packages/plugin-github/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,25 +166,23 @@ export function apply(ctx: Context, config: Config = {}) {

addListeners((event, handler) => {
const base = camelize(event.split('.', 1)[0])
github.on(event, async (callback) => {
const { repository } = callback.payload

github.on(event, async ({ payload }) => {
// step 1: filter repository
const groupIds = config.repos[repository.full_name]
const groupIds = config.repos[payload.repository.full_name]
if (!groupIds) return

// step 2: filter event
const baseConfig = config.events[base] || {}
if (baseConfig === false) return
const action = camelize(callback.payload.action)
const action = camelize(payload.action)
if (action && baseConfig !== true) {
const actionConfig = baseConfig[action]
if (actionConfig === false) return
if (actionConfig !== true && !(defaultEvents[base] || {})[action]) return
}

// step 3: handle event
const result = handler(callback.payload)
const result = handler(payload)
if (!result) return

// step 4: broadcast message
Expand Down
141 changes: 141 additions & 0 deletions packages/plugin-github/tests/fixtures/create.1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
{
"ref": "reply-api",
"ref_type": "branch",
"master_branch": "master",
"description": "QQ bot out of the box",
"pusher_type": "user",
"repository": {
"id": 225572038,
"node_id": "MDEwOlJlcG9zaXRvcnkyMjU1NzIwMzg=",
"name": "koishi",
"full_name": "koishijs/koishi",
"private": false,
"owner": {
"login": "koishijs",
"id": 58179220,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjU4MTc5MjIw",
"avatar_url": "https://avatars3.githubusercontent.com/u/58179220?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/koishijs",
"html_url": "https://github.com/koishijs",
"followers_url": "https://api.github.com/users/koishijs/followers",
"following_url": "https://api.github.com/users/koishijs/following{/other_user}",
"gists_url": "https://api.github.com/users/koishijs/gists{/gist_id}",
"starred_url": "https://api.github.com/users/koishijs/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/koishijs/subscriptions",
"organizations_url": "https://api.github.com/users/koishijs/orgs",
"repos_url": "https://api.github.com/users/koishijs/repos",
"events_url": "https://api.github.com/users/koishijs/events{/privacy}",
"received_events_url": "https://api.github.com/users/koishijs/received_events",
"type": "Organization",
"site_admin": false
},
"html_url": "https://github.com/koishijs/koishi",
"description": "QQ bot out of the box",
"fork": false,
"url": "https://api.github.com/repos/koishijs/koishi",
"forks_url": "https://api.github.com/repos/koishijs/koishi/forks",
"keys_url": "https://api.github.com/repos/koishijs/koishi/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/koishijs/koishi/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/koishijs/koishi/teams",
"hooks_url": "https://api.github.com/repos/koishijs/koishi/hooks",
"issue_events_url": "https://api.github.com/repos/koishijs/koishi/issues/events{/number}",
"events_url": "https://api.github.com/repos/koishijs/koishi/events",
"assignees_url": "https://api.github.com/repos/koishijs/koishi/assignees{/user}",
"branches_url": "https://api.github.com/repos/koishijs/koishi/branches{/branch}",
"tags_url": "https://api.github.com/repos/koishijs/koishi/tags",
"blobs_url": "https://api.github.com/repos/koishijs/koishi/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/koishijs/koishi/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/koishijs/koishi/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/koishijs/koishi/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/koishijs/koishi/statuses/{sha}",
"languages_url": "https://api.github.com/repos/koishijs/koishi/languages",
"stargazers_url": "https://api.github.com/repos/koishijs/koishi/stargazers",
"contributors_url": "https://api.github.com/repos/koishijs/koishi/contributors",
"subscribers_url": "https://api.github.com/repos/koishijs/koishi/subscribers",
"subscription_url": "https://api.github.com/repos/koishijs/koishi/subscription",
"commits_url": "https://api.github.com/repos/koishijs/koishi/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/koishijs/koishi/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/koishijs/koishi/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/koishijs/koishi/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/koishijs/koishi/contents/{+path}",
"compare_url": "https://api.github.com/repos/koishijs/koishi/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/koishijs/koishi/merges",
"archive_url": "https://api.github.com/repos/koishijs/koishi/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/koishijs/koishi/downloads",
"issues_url": "https://api.github.com/repos/koishijs/koishi/issues{/number}",
"pulls_url": "https://api.github.com/repos/koishijs/koishi/pulls{/number}",
"milestones_url": "https://api.github.com/repos/koishijs/koishi/milestones{/number}",
"notifications_url": "https://api.github.com/repos/koishijs/koishi/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/koishijs/koishi/labels{/name}",
"releases_url": "https://api.github.com/repos/koishijs/koishi/releases{/id}",
"deployments_url": "https://api.github.com/repos/koishijs/koishi/deployments",
"created_at": "2019-12-03T08:47:29Z",
"updated_at": "2020-10-17T05:26:38Z",
"pushed_at": "2020-10-18T16:20:28Z",
"git_url": "git://github.com/koishijs/koishi.git",
"ssh_url": "git@github.com:koishijs/koishi.git",
"clone_url": "https://github.com/koishijs/koishi.git",
"svn_url": "https://github.com/koishijs/koishi",
"homepage": "https://koishi.js.org",
"size": 2952,
"stargazers_count": 208,
"watchers_count": 208,
"language": "TypeScript",
"has_issues": true,
"has_projects": true,
"has_downloads": true,
"has_wiki": false,
"has_pages": false,
"forks_count": 15,
"mirror_url": null,
"archived": false,
"disabled": false,
"open_issues_count": 7,
"license": {
"key": "mit",
"name": "MIT License",
"spdx_id": "MIT",
"url": "https://api.github.com/licenses/mit",
"node_id": "MDc6TGljZW5zZTEz"
},
"forks": 15,
"open_issues": 7,
"watchers": 208,
"default_branch": "master"
},
"organization": {
"login": "koishijs",
"id": 58179220,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjU4MTc5MjIw",
"url": "https://api.github.com/orgs/koishijs",
"repos_url": "https://api.github.com/orgs/koishijs/repos",
"events_url": "https://api.github.com/orgs/koishijs/events",
"hooks_url": "https://api.github.com/orgs/koishijs/hooks",
"issues_url": "https://api.github.com/orgs/koishijs/issues",
"members_url": "https://api.github.com/orgs/koishijs/members{/member}",
"public_members_url": "https://api.github.com/orgs/koishijs/public_members{/member}",
"avatar_url": "https://avatars3.githubusercontent.com/u/58179220?v=4",
"description": ""
},
"sender": {
"login": "Shigma",
"id": 33423008,
"node_id": "MDQ6VXNlcjMzNDIzMDA4",
"avatar_url": "https://avatars3.githubusercontent.com/u/33423008?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/Shigma",
"html_url": "https://github.com/Shigma",
"followers_url": "https://api.github.com/users/Shigma/followers",
"following_url": "https://api.github.com/users/Shigma/following{/other_user}",
"gists_url": "https://api.github.com/users/Shigma/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Shigma/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Shigma/subscriptions",
"organizations_url": "https://api.github.com/users/Shigma/orgs",
"repos_url": "https://api.github.com/users/Shigma/repos",
"events_url": "https://api.github.com/users/Shigma/events{/privacy}",
"received_events_url": "https://api.github.com/users/Shigma/received_events",
"type": "User",
"site_admin": false
}
}
141 changes: 141 additions & 0 deletions packages/plugin-github/tests/fixtures/create.2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
{
"ref": "2.3.2",
"ref_type": "tag",
"master_branch": "master",
"description": "QQ bot out of the box",
"pusher_type": "user",
"repository": {
"id": 225572038,
"node_id": "MDEwOlJlcG9zaXRvcnkyMjU1NzIwMzg=",
"name": "koishi",
"full_name": "koishijs/koishi",
"private": false,
"owner": {
"login": "koishijs",
"id": 58179220,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjU4MTc5MjIw",
"avatar_url": "https://avatars3.githubusercontent.com/u/58179220?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/koishijs",
"html_url": "https://github.com/koishijs",
"followers_url": "https://api.github.com/users/koishijs/followers",
"following_url": "https://api.github.com/users/koishijs/following{/other_user}",
"gists_url": "https://api.github.com/users/koishijs/gists{/gist_id}",
"starred_url": "https://api.github.com/users/koishijs/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/koishijs/subscriptions",
"organizations_url": "https://api.github.com/users/koishijs/orgs",
"repos_url": "https://api.github.com/users/koishijs/repos",
"events_url": "https://api.github.com/users/koishijs/events{/privacy}",
"received_events_url": "https://api.github.com/users/koishijs/received_events",
"type": "Organization",
"site_admin": false
},
"html_url": "https://github.com/koishijs/koishi",
"description": "QQ bot out of the box",
"fork": false,
"url": "https://api.github.com/repos/koishijs/koishi",
"forks_url": "https://api.github.com/repos/koishijs/koishi/forks",
"keys_url": "https://api.github.com/repos/koishijs/koishi/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/koishijs/koishi/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/koishijs/koishi/teams",
"hooks_url": "https://api.github.com/repos/koishijs/koishi/hooks",
"issue_events_url": "https://api.github.com/repos/koishijs/koishi/issues/events{/number}",
"events_url": "https://api.github.com/repos/koishijs/koishi/events",
"assignees_url": "https://api.github.com/repos/koishijs/koishi/assignees{/user}",
"branches_url": "https://api.github.com/repos/koishijs/koishi/branches{/branch}",
"tags_url": "https://api.github.com/repos/koishijs/koishi/tags",
"blobs_url": "https://api.github.com/repos/koishijs/koishi/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/koishijs/koishi/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/koishijs/koishi/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/koishijs/koishi/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/koishijs/koishi/statuses/{sha}",
"languages_url": "https://api.github.com/repos/koishijs/koishi/languages",
"stargazers_url": "https://api.github.com/repos/koishijs/koishi/stargazers",
"contributors_url": "https://api.github.com/repos/koishijs/koishi/contributors",
"subscribers_url": "https://api.github.com/repos/koishijs/koishi/subscribers",
"subscription_url": "https://api.github.com/repos/koishijs/koishi/subscription",
"commits_url": "https://api.github.com/repos/koishijs/koishi/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/koishijs/koishi/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/koishijs/koishi/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/koishijs/koishi/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/koishijs/koishi/contents/{+path}",
"compare_url": "https://api.github.com/repos/koishijs/koishi/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/koishijs/koishi/merges",
"archive_url": "https://api.github.com/repos/koishijs/koishi/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/koishijs/koishi/downloads",
"issues_url": "https://api.github.com/repos/koishijs/koishi/issues{/number}",
"pulls_url": "https://api.github.com/repos/koishijs/koishi/pulls{/number}",
"milestones_url": "https://api.github.com/repos/koishijs/koishi/milestones{/number}",
"notifications_url": "https://api.github.com/repos/koishijs/koishi/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/koishijs/koishi/labels{/name}",
"releases_url": "https://api.github.com/repos/koishijs/koishi/releases{/id}",
"deployments_url": "https://api.github.com/repos/koishijs/koishi/deployments",
"created_at": "2019-12-03T08:47:29Z",
"updated_at": "2020-10-15T15:42:23Z",
"pushed_at": "2020-10-15T15:43:55Z",
"git_url": "git://github.com/koishijs/koishi.git",
"ssh_url": "git@github.com:koishijs/koishi.git",
"clone_url": "https://github.com/koishijs/koishi.git",
"svn_url": "https://github.com/koishijs/koishi",
"homepage": "https://koishi.js.org",
"size": 2894,
"stargazers_count": 206,
"watchers_count": 206,
"language": "TypeScript",
"has_issues": true,
"has_projects": true,
"has_downloads": true,
"has_wiki": false,
"has_pages": false,
"forks_count": 15,
"mirror_url": null,
"archived": false,
"disabled": false,
"open_issues_count": 8,
"license": {
"key": "mit",
"name": "MIT License",
"spdx_id": "MIT",
"url": "https://api.github.com/licenses/mit",
"node_id": "MDc6TGljZW5zZTEz"
},
"forks": 15,
"open_issues": 8,
"watchers": 206,
"default_branch": "master"
},
"organization": {
"login": "koishijs",
"id": 58179220,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjU4MTc5MjIw",
"url": "https://api.github.com/orgs/koishijs",
"repos_url": "https://api.github.com/orgs/koishijs/repos",
"events_url": "https://api.github.com/orgs/koishijs/events",
"hooks_url": "https://api.github.com/orgs/koishijs/hooks",
"issues_url": "https://api.github.com/orgs/koishijs/issues",
"members_url": "https://api.github.com/orgs/koishijs/members{/member}",
"public_members_url": "https://api.github.com/orgs/koishijs/public_members{/member}",
"avatar_url": "https://avatars3.githubusercontent.com/u/58179220?v=4",
"description": ""
},
"sender": {
"login": "Shigma",
"id": 33423008,
"node_id": "MDQ6VXNlcjMzNDIzMDA4",
"avatar_url": "https://avatars3.githubusercontent.com/u/33423008?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/Shigma",
"html_url": "https://github.com/Shigma",
"followers_url": "https://api.github.com/users/Shigma/followers",
"following_url": "https://api.github.com/users/Shigma/following{/other_user}",
"gists_url": "https://api.github.com/users/Shigma/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Shigma/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Shigma/subscriptions",
"organizations_url": "https://api.github.com/users/Shigma/orgs",
"repos_url": "https://api.github.com/users/Shigma/repos",
"events_url": "https://api.github.com/users/Shigma/events{/privacy}",
"received_events_url": "https://api.github.com/users/Shigma/received_events",
"type": "User",
"site_admin": false
}
}
Loading

0 comments on commit 67529d1

Please sign in to comment.