|
| 1 | +import { components } from 'npm:@octokit/openapi-types'; |
| 2 | +import { stdin } from 'npm:zx'; |
| 3 | + |
| 4 | +type GitHubSchema = components['schemas']; |
| 5 | + |
| 6 | +type GitHubUser = GitHubSchema['simple-user']; |
| 7 | + |
| 8 | +interface GitHubAction |
| 9 | + extends Record<'event_name' | 'actor' | 'server_url' | 'repository', string> { |
| 10 | + action?: string; |
| 11 | + ref?: string; |
| 12 | + ref_name?: string; |
| 13 | + event: { |
| 14 | + head_commit?: GitHubSchema['git-commit']; |
| 15 | + issue?: GitHubSchema['webhook-issues-opened']['issue']; |
| 16 | + pull_request?: GitHubSchema['pull-request']; |
| 17 | + discussion?: GitHubSchema['discussion']; |
| 18 | + comment?: GitHubSchema['issue-comment']; |
| 19 | + release?: GitHubSchema['release']; |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +// Helper functions |
| 24 | +const getActionText = (action?: string) => |
| 25 | + action === 'closed' ? '关闭' : action?.includes('open') ? '打开' : '编辑'; |
| 26 | + |
| 27 | +const createLink = (href: string, text = href) => ({ tag: 'a', href, text }); |
| 28 | + |
| 29 | +const createText = (text: string) => ({ tag: 'text', text }); |
| 30 | + |
| 31 | +// create user link |
| 32 | +const createUserLink = (user: GitHubUser) => |
| 33 | + user ? createLink(user.html_url, user.login) : createText('无'); |
| 34 | + |
| 35 | +const createContentItem = ( |
| 36 | + label: string, |
| 37 | + value?: string | { tag: string; text: string }, |
| 38 | +) => |
| 39 | + [ |
| 40 | + createText(label), |
| 41 | + typeof value === 'string' |
| 42 | + ? createText(value || '无') |
| 43 | + : value || createText('无'), |
| 44 | + ] as [object, object]; |
| 45 | + |
| 46 | +type EventHandler = ( |
| 47 | + event: GitHubAction, |
| 48 | + actionText: string, |
| 49 | +) => { |
| 50 | + title: string; |
| 51 | + content: [object, object][]; |
| 52 | +}; |
| 53 | + |
| 54 | +// Event handlers |
| 55 | +const eventHandlers: Record<string, EventHandler> = { |
| 56 | + push: ({ |
| 57 | + event: { head_commit }, |
| 58 | + ref, |
| 59 | + ref_name, |
| 60 | + server_url, |
| 61 | + repository, |
| 62 | + actor, |
| 63 | + }) => ({ |
| 64 | + title: 'GitHub 代码提交', |
| 65 | + content: [ |
| 66 | + [createText('提交链接:'), createLink(head_commit!.url)], |
| 67 | + [ |
| 68 | + createText('代码分支:'), |
| 69 | + createLink(`${server_url}/${repository}/tree/${ref_name}`, ref), |
| 70 | + ], |
| 71 | + [createText('提交作者:'), createLink(`${server_url}/${actor}`, actor)], |
| 72 | + [createText('提交信息:'), createText(head_commit!.message)], |
| 73 | + ], |
| 74 | + }), |
| 75 | + |
| 76 | + issues: ({ event: { issue } }, actionText) => ({ |
| 77 | + title: `GitHub issue ${actionText}:${issue?.title}`, |
| 78 | + content: [ |
| 79 | + [createText('链接:'), createLink(issue!.html_url)], |
| 80 | + [ |
| 81 | + createText('作者:'), |
| 82 | + createLink(issue!.user!.html_url!, issue!.user!.login), |
| 83 | + ], |
| 84 | + [ |
| 85 | + createText('指派:'), |
| 86 | + issue?.assignee |
| 87 | + ? createLink(issue.assignee.html_url!, issue.assignee.login) |
| 88 | + : createText('无'), |
| 89 | + ], |
| 90 | + [ |
| 91 | + createText('标签:'), |
| 92 | + createText(issue?.labels?.map(({ name }) => name).join(', ') || '无'), |
| 93 | + ], |
| 94 | + [createText('里程碑:'), createText(issue?.milestone?.title || '无')], |
| 95 | + [createText('描述:'), createText(issue?.body || '无')], |
| 96 | + ], |
| 97 | + }), |
| 98 | + |
| 99 | + pull_request: ({ event: { pull_request } }, actionText) => ({ |
| 100 | + title: `GitHub PR ${actionText}:${pull_request?.title}`, |
| 101 | + content: [ |
| 102 | + [createText('链接:'), createLink(pull_request!.html_url)], |
| 103 | + [ |
| 104 | + createText('作者:'), |
| 105 | + createLink(pull_request!.user.html_url, pull_request!.user.login), |
| 106 | + ], |
| 107 | + [ |
| 108 | + createText('指派:'), |
| 109 | + pull_request?.assignee |
| 110 | + ? createLink( |
| 111 | + pull_request.assignee.html_url, |
| 112 | + pull_request.assignee.login, |
| 113 | + ) |
| 114 | + : createText('无'), |
| 115 | + ], |
| 116 | + [ |
| 117 | + createText('标签:'), |
| 118 | + createText( |
| 119 | + pull_request?.labels?.map(({ name }) => name).join(', ') || '无', |
| 120 | + ), |
| 121 | + ], |
| 122 | + [ |
| 123 | + createText('里程碑:'), |
| 124 | + createText(pull_request?.milestone?.title || '无'), |
| 125 | + ], |
| 126 | + [createText('描述:'), createText(pull_request?.body || '无')], |
| 127 | + ], |
| 128 | + }), |
| 129 | + |
| 130 | + discussion: ({ event: { discussion } }, actionText) => ({ |
| 131 | + title: `GitHub 讨论 ${actionText}:${discussion?.title || '无'}`, |
| 132 | + content: [ |
| 133 | + createContentItem('链接:', discussion?.html_url), |
| 134 | + createContentItem( |
| 135 | + '作者:', |
| 136 | + createUserLink(discussion!.user as GitHubUser), |
| 137 | + ), |
| 138 | + createContentItem('描述:', discussion?.body || '无'), |
| 139 | + ], |
| 140 | + }), |
| 141 | + |
| 142 | + issue_comment: ({ event: { comment, issue } }) => ({ |
| 143 | + title: `GitHub issue 评论:${issue?.title || '未知 issue'}`, |
| 144 | + content: [ |
| 145 | + createContentItem('链接:', comment?.html_url), |
| 146 | + createContentItem('作者:', createUserLink(comment!.user!)), |
| 147 | + createContentItem('描述:', comment?.body || '无'), |
| 148 | + ], |
| 149 | + }), |
| 150 | + |
| 151 | + discussion_comment: ({ event: { comment, discussion } }) => ({ |
| 152 | + title: `GitHub 讨论评论:${discussion?.title || '无'}`, |
| 153 | + content: [ |
| 154 | + createContentItem('链接:', comment?.html_url), |
| 155 | + createContentItem('作者:', createUserLink(comment!.user!)), |
| 156 | + createContentItem('描述:', comment?.body || '无'), |
| 157 | + ], |
| 158 | + }), |
| 159 | + |
| 160 | + release: ({ event: { release } }) => ({ |
| 161 | + title: `GitHub Release 发布:${release!.name || release!.tag_name}`, |
| 162 | + content: [ |
| 163 | + createContentItem('链接:', release!.html_url), |
| 164 | + createContentItem('作者:', createUserLink(release!.author)), |
| 165 | + createContentItem('描述:', release!.body!), |
| 166 | + ], |
| 167 | + }), |
| 168 | +}; |
| 169 | + |
| 170 | +// Main processor |
| 171 | +const processEvent = (event: GitHubAction) => { |
| 172 | + const { event_name, action } = event; |
| 173 | + const actionText = getActionText(action); |
| 174 | + const handler = eventHandlers[event_name]; |
| 175 | + |
| 176 | + if (!handler) throw new Error(`No handler found for event: ${event_name}`); |
| 177 | + |
| 178 | + try { |
| 179 | + return handler(event, actionText); |
| 180 | + } catch (cause) { |
| 181 | + throw new Error( |
| 182 | + `Error processing ${event_name} event: ${(cause as Error).message}`, |
| 183 | + { cause }, |
| 184 | + ); |
| 185 | + } |
| 186 | +}; |
| 187 | + |
| 188 | +// Main execution:Processing GitHub Events and Outputting Results |
| 189 | +const event = JSON.parse((await stdin()) || '{}') as GitHubAction; |
| 190 | +const zh_cn = processEvent(event); |
| 191 | + |
| 192 | +if (zh_cn) console.log(JSON.stringify({ post: { zh_cn } })); |
| 193 | +else |
| 194 | + throw new Error( |
| 195 | + `Unsupported ${event.event_name} event & ${event.action} action`, |
| 196 | + ); |
0 commit comments