From e758c69e64d5de414bb7340df066c4c35beb6abb Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sat, 29 Oct 2022 16:35:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89host=EF=BC=8C=E7=94=A8=E4=BA=8E=E5=9C=A8=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E7=9A=84=E5=AE=9E=E4=BE=8B=E4=B8=AD=E5=85=B1=E4=BA=AB?= =?UTF-8?q?=E5=90=8C=E4=B8=80=E4=B8=AAtailchat=20github=20app=E5=AE=9E?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/github-app/src/app.ts | 97 +++++++++++++++++++---------------- apps/github-app/src/client.ts | 6 +++ 2 files changed, 58 insertions(+), 45 deletions(-) diff --git a/apps/github-app/src/app.ts b/apps/github-app/src/app.ts index be276828..07497983 100644 --- a/apps/github-app/src/app.ts +++ b/apps/github-app/src/app.ts @@ -6,23 +6,21 @@ import { configPath, generateErrorBlock } from './utils'; const LABEL = 'tailchat-topic'; const TOPIC_KEY = 'tailchatTopicId'; -export function app(app: Probot) { - if ( - !process.env.TAILCHAT_API_URL || - !process.env.TAILCHAT_APP_ID || - !process.env.TAILCHAT_APP_SECRET - ) { - throw new Error( - 'Require env: TAILCHAT_API_URL, TAILCHAT_APP_ID, TAILCHAT_APP_SECRET' - ); - } - - const tailchatClient = new TailchatClient( - process.env.TAILCHAT_API_URL, - process.env.TAILCHAT_APP_ID, - process.env.TAILCHAT_APP_SECRET +if ( + !process.env.TAILCHAT_API_URL || + !process.env.TAILCHAT_APP_ID || + !process.env.TAILCHAT_APP_SECRET +) { + throw new Error( + 'Require env: TAILCHAT_API_URL, TAILCHAT_APP_ID, TAILCHAT_APP_SECRET' ); +} + +const defaultTailchatApiUrl = process.env.TAILCHAT_API_URL; +const tailchatAppId = process.env.TAILCHAT_APP_ID; +const tailchatAppSecret = process.env.TAILCHAT_APP_SECRET; +export function app(app: Probot) { app.on('issues.opened', async (ctx) => { if (ctx.isBot) { return; @@ -41,14 +39,8 @@ export function app(app: Probot) { // 是配置文件 - const content = Buffer.from(data.content, 'base64').toString(); - const json = await JSON.parse(content); - const groupId = json['groupId']; - const panelId = json['panelId']; - - if (!groupId || !panelId) { - throw new Error('config format error'); - } + const { tailchatClient, groupId, panelId } = + createTailchatContextWithConfig(data.content); // 发送到tailchat const topic = await tailchatClient.call( @@ -113,14 +105,8 @@ export function app(app: Probot) { // 是配置文件 - const content = Buffer.from(data.content, 'base64').toString(); - const json = await JSON.parse(content); - const groupId = json['groupId']; - const panelId = json['panelId']; - - if (!groupId || !panelId) { - throw new Error('config format error'); - } + const { tailchatClient, groupId, panelId } = + createTailchatContextWithConfig(data.content); // 发送到tailchat await tailchatClient.call('plugin:com.msgbyte.topic.createComment', { @@ -142,21 +128,42 @@ export function app(app: Probot) { } }); - app.on('installation.created', async (ctx) => { - const installationId = ctx.payload.installation.id; - const installationTargetName = ctx.payload.installation.account.login; - const installationTargetRepositories = ctx.payload.repositories; + // app.on('installation.created', async (ctx) => { + // const installationId = ctx.payload.installation.id; + // const installationTargetName = ctx.payload.installation.account.login; + // const installationTargetRepositories = ctx.payload.repositories; + + // console.log('installation.created', { + // installationId, + // installationTargetName, + // installationTargetRepositories, + // }); + // }); +} - console.log('installation.created', { - installationId, - installationTargetName, - installationTargetRepositories, - }); - }); +/** + * 从配置文件中创建上下文 + */ +function createTailchatContextWithConfig(githubRaw: string) { + const content = Buffer.from(githubRaw, 'base64').toString(); + const json = JSON.parse(content); + const tailchatHost = json['tailchatHost']; + const groupId = json['groupId']; + const panelId = json['panelId']; + + if (!groupId || !panelId) { + throw new Error('config format error'); + } - // For more information on building apps: - // https://probot.github.io/docs/ + const tailchatClient = new TailchatClient( + tailchatHost ?? defaultTailchatApiUrl, + tailchatAppId, + tailchatAppSecret + ); - // To get your app running against GitHub, see: - // https://probot.github.io/docs/development/ + return { + tailchatClient, + groupId, + panelId, + }; } diff --git a/apps/github-app/src/client.ts b/apps/github-app/src/client.ts index 6f67549a..9bde59e8 100644 --- a/apps/github-app/src/client.ts +++ b/apps/github-app/src/client.ts @@ -11,6 +11,12 @@ export class TailchatClient { public appId: string, public appSecret: string ) { + if (!url || !appId || !appSecret) { + throw new Error( + 'Require params: apiUrl, appId, appSecret. You can set it with env' + ); + } + this.request = axios.create({ baseURL: url, });