-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
212 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,103 @@ | ||
import { EmitterWebhookEventName } from '@octokit/webhooks'; | ||
import chain from 'lodash/chain'; | ||
import groupBy from 'lodash/groupBy'; | ||
import MultiMap from 'mnemonist/multi-map'; | ||
|
||
import { initApp } from '@/github/app'; | ||
import { setupWebhooksTemplate } from '@/github/handler'; | ||
import { MarkdownContent } from '@/github/types'; | ||
import { sendToDing } from '@/github/utils'; | ||
import { GitHubKVManager } from '@/kv/github'; | ||
import { Logger } from '@/utils/logger'; | ||
|
||
import { IGitHubEventQueueMessage } from '../types'; | ||
|
||
import { BaseWorker } from '.'; | ||
|
||
export class GitHubAppWorker extends BaseWorker<IGitHubEventQueueMessage> { | ||
logger = Logger.instance(); | ||
|
||
async run() { | ||
await Promise.allSettled( | ||
chain(this.queue) | ||
.groupBy((v) => v.body.botId) | ||
.map(async (messages, botId) => { | ||
const githubKVManager = new GitHubKVManager(); | ||
const setting = await githubKVManager.getAppSettingById(botId); | ||
|
||
if (setting && setting.githubSecret) { | ||
const app = await initApp(setting); | ||
|
||
const results = new MultiMap<string, MarkdownContent>(); | ||
|
||
setupWebhooksTemplate( | ||
app.webhooks, | ||
{ setting }, | ||
async ({ markdown, eventName }) => { | ||
results.set(eventName, markdown); | ||
}, | ||
); | ||
const byId = groupBy(this.queue, (v) => v.body.botId); | ||
|
||
const result = await Promise.allSettled( | ||
Object.entries(byId).map(async ([botId, messages]) => { | ||
this.logger.info('consume for', botId, messages.length); | ||
|
||
const setting = await GitHubKVManager.instance().getAppSettingById( | ||
botId, | ||
); | ||
|
||
if (!setting) { | ||
console.error('github app worker error: setting not found', botId); | ||
return; | ||
} | ||
|
||
if (!setting.githubSecret) { | ||
console.error( | ||
'github app worker error: please set app webhook secret in database', | ||
botId, | ||
); | ||
return; | ||
} | ||
|
||
const app = await initApp(setting); | ||
|
||
const results = new MultiMap<string, MarkdownContent>(); | ||
|
||
setupWebhooksTemplate( | ||
app.webhooks, | ||
{ setting }, | ||
async ({ markdown, eventName }) => { | ||
results.set(eventName, markdown); | ||
}, | ||
); | ||
|
||
const byEvent = groupBy(messages, (v) => v.body.data.name) as Record< | ||
EmitterWebhookEventName, | ||
Message<IGitHubEventQueueMessage>[] | ||
>; | ||
|
||
await Promise.allSettled( | ||
Object.entries(byEvent).map(async ([eventName, messages]) => { | ||
await Promise.allSettled( | ||
chain(messages) | ||
.groupBy((v) => v.body.data.event) | ||
.map(async (messages, eventName: EmitterWebhookEventName) => { | ||
await Promise.all( | ||
messages.map(async (message) => { | ||
try { | ||
const { data } = message.body; | ||
await app.webhooks.receive({ | ||
id: data.id, | ||
name: data.event as any, | ||
payload: data.payload, | ||
}); | ||
message.ack(); | ||
} catch (error) { | ||
console.error('github app worker error', error); | ||
message.retry(); | ||
} | ||
}), | ||
); | ||
|
||
const markdowns = results.get(eventName); | ||
if (markdowns && markdowns.length > 0) { | ||
// 只有特定内容的 content 要被合并起来 | ||
await Promise.allSettled( | ||
markdowns.map((markdown) => | ||
sendToDing(markdown, eventName, setting), | ||
), | ||
); | ||
} | ||
}) | ||
.value(), | ||
messages.map(async (message) => { | ||
try { | ||
const { data } = message.body; | ||
await app.webhooks.receive({ | ||
id: data.id, | ||
name: data.name as any, | ||
payload: data.payload, | ||
}); | ||
message.ack(); | ||
} catch (error) { | ||
console.error('github app worker error', error); | ||
message.retry(); | ||
} | ||
}), | ||
); | ||
} else { | ||
console.error('github app worker error: setting not found', botId); | ||
} | ||
}) | ||
.value(), | ||
|
||
const markdowns = results.get(eventName); | ||
if (markdowns && markdowns.length > 0) { | ||
// 只有特定内容的 content 要被合并起来 | ||
await Promise.allSettled( | ||
markdowns.map((markdown) => | ||
sendToDing( | ||
markdown, | ||
eventName as EmitterWebhookEventName, | ||
setting, | ||
), | ||
), | ||
); | ||
} | ||
}), | ||
); | ||
}), | ||
); | ||
|
||
result.forEach((v) => { | ||
if (v.status === 'rejected') { | ||
console.error('github app worker error', v); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { EdgeKVNamespace } from 'edge-mock'; | ||
|
||
import Environment from '@/env'; | ||
import { GitHubCommon } from '@/kv/constants'; | ||
|
||
export function prepareEnv() { | ||
const kv = new EdgeKVNamespace(); | ||
Environment.from('node', { | ||
KV: kv, | ||
MESSAGE_QUEUE: {} as any, | ||
}); | ||
|
||
kv.put( | ||
`${GitHubCommon.GITHUB_APP_SETTINGS_PREFIX}mock`, | ||
JSON.stringify({ | ||
appSettings: { | ||
appId: process.env.GITHUB_APPID, | ||
privateKey: process.env.GITHUB_APP_PRIVATE_KEY!.replace(/\\n/g, '\n'), | ||
}, | ||
githubSecret: process.env.GITHUB_TOKEN, | ||
dingWebhooks: [], | ||
contentLimit: 300, | ||
}), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.