feat: 增加自定义host,用于在不同的实例中共享同一个tailchat github app实例

pull/56/head
moonrailgun 3 years ago
parent 15741c3b2e
commit e758c69e64

@ -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,
};
}

@ -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,
});

Loading…
Cancel
Save