( app: Probot, getRouter: Parameters<ApplicationFunction>[1]['getRouter'] )
| 208 | } |
| 209 | |
| 210 | export function buildRouter( |
| 211 | app: Probot, |
| 212 | getRouter: Parameters<ApplicationFunction>[1]['getRouter'] |
| 213 | ) { |
| 214 | if (getRouter) { |
| 215 | getRouter('/') |
| 216 | .get('/api', (_req, res) => { |
| 217 | res.send('Hello World! Github app api server is working!'); |
| 218 | }) |
| 219 | .get('/api/message/webhook', (_req, res) => { |
| 220 | res.send('Please use POST method'); |
| 221 | }) |
| 222 | .post('/api/message/webhook', bodyParser.json(), (req, res) => { |
| 223 | (async () => { |
| 224 | try { |
| 225 | // 根据收件箱内容向 Github Issue 创建话题 |
| 226 | const inboxItem = req.body ?? {}; |
| 227 | if (inboxItem.type !== 'plugin:com.msgbyte.topic.comment') { |
| 228 | // 如果不是回复消息,则跳过 |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | const newComment: any = |
| 233 | _.last(_.get(inboxItem, ['payload', 'comments'])) ?? {}; |
| 234 | const meta = _.get(inboxItem, ['payload', 'meta']) ?? {}; |
| 235 | if ( |
| 236 | !meta.installationId || |
| 237 | !meta.githubRepoOwner || |
| 238 | !meta.githubRepoName || |
| 239 | !meta.githubIssueNumber |
| 240 | ) { |
| 241 | console.warn('Cannot pass meta info check:', { meta }); |
| 242 | return; |
| 243 | } |
| 244 | if (!newComment.author || !newComment.content) { |
| 245 | console.warn( |
| 246 | 'Cannot get "newComment.author" or "newComment.content"' |
| 247 | ); |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | // 发送到github comment |
| 252 | const octokit = await app.auth(Number(meta.installationId)); |
| 253 | const tailchatClient = createTailchatClient(meta.tailchatHost); |
| 254 | const userInfo = await tailchatClient.call('user.getUserInfo', { |
| 255 | userId: newComment.author, |
| 256 | }); |
| 257 | |
| 258 | const payload = { |
| 259 | owner: meta.githubRepoOwner, |
| 260 | repo: meta.githubRepoName, |
| 261 | issue_number: meta.githubIssueNumber, |
| 262 | body: `[${_.get(userInfo, [ |
| 263 | 'data', |
| 264 | 'nickname', |
| 265 | ])}] 在 Tailchat 回复:\n\`\`\`\n${ |
| 266 | newComment.content ?? '' |
| 267 | }\n\`\`\``, |
no test coverage detected