diff --git a/apps/github-app/.env.example b/apps/github-app/.env.example index 6e106aa9..cfc6f208 100644 --- a/apps/github-app/.env.example +++ b/apps/github-app/.env.example @@ -7,3 +7,6 @@ LOG_LEVEL=debug # Go to https://smee.io/new set this to the URL that you are redirected to. WEBHOOK_PROXY_URL= + +# Tailchat +TAILCHAT_API_URL= diff --git a/apps/github-app/package.json b/apps/github-app/package.json index 23791670..63fd803a 100644 --- a/apps/github-app/package.json +++ b/apps/github-app/package.json @@ -1,5 +1,5 @@ { - "name": "github-app", + "name": "tailchat-github-app", "version": "1.0.0", "private": true, "description": "Tailchat github integrations", @@ -12,8 +12,8 @@ "probot-app" ], "scripts": { + "dev": "nodemon --watch 'src/**' --ext 'ts' --ignore 'src/**/*.spec.ts' --exec 'ts-node --transpile-only src/index.ts'", "build": "tsc", - "dev": "probot run ./lib/index.js", "test": "jest" }, "dependencies": { @@ -24,8 +24,10 @@ "@types/node": "^18.0.0", "jest": "^26.6.3", "nock": "^13.0.5", + "nodemon": "^2.0.18", "smee-client": "^1.2.2", "ts-jest": "^26.4.4", + "ts-node": "^10.9.1", "typescript": "^4.1.3" }, "engines": { diff --git a/apps/github-app/src/app.ts b/apps/github-app/src/app.ts new file mode 100644 index 00000000..8f87a71f --- /dev/null +++ b/apps/github-app/src/app.ts @@ -0,0 +1,82 @@ +import { Probot } from 'probot'; + +// const tailchatApiUrl = process.env.TAILCHAT_API_URL; +const configPath = '.tailchat/topic.json'; + +export function app(app: Probot) { + app.on('issues.opened', async (ctx) => { + if (ctx.isBot) { + return; + } + + const { data } = await ctx.octokit.repos.getContent( + ctx.repo({ + path: configPath, + }) + ); + + try { + if (!(!Array.isArray(data) && 'content' in data)) { + throw new Error('config file type error'); + } + + // 是配置文件 + + 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'); + } + + console.log(json); + + // TODO: 发送到tailchat + + await Promise.all([ + ctx.octokit.issues.createComment( + ctx.issue({ + body: 'Thanks for opening this issue! Tailchat topic is created!', + }) + ), + ctx.octokit.issues.addLabels( + ctx.repo({ + issue_number: ctx.payload.issue.number, + labels: ['tailchat-topic'], + }) + ), + ]); + } catch (err) { + console.error(err); + await ctx.octokit.issues.createComment( + ctx.issue({ + body: `Tailchat topic create error, please checkout your config in \`${configPath}\`!`, + }) + ); + } + }); + + app.on('issue_comment.created', async () => { + // 发送到tailchat + }); + + 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, + }); + }); + + // For more information on building apps: + // https://probot.github.io/docs/ + + // To get your app running against GitHub, see: + // https://probot.github.io/docs/development/ +} diff --git a/apps/github-app/src/index.ts b/apps/github-app/src/index.ts index c5b28219..09489d7b 100644 --- a/apps/github-app/src/index.ts +++ b/apps/github-app/src/index.ts @@ -1,15 +1,4 @@ -import { Probot } from 'probot'; +import { run } from 'probot'; +import { app } from './app'; -export = (app: Probot) => { - app.on('issues.opened', async (context) => { - const issueComment = context.issue({ - body: 'Thanks for opening this issue!', - }); - await context.octokit.issues.createComment(issueComment); - }); - // For more information on building apps: - // https://probot.github.io/docs/ - - // To get your app running against GitHub, see: - // https://probot.github.io/docs/development/ -}; +run(app); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 162c0bc1..14fcac42 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -62,19 +62,23 @@ importers: '@types/node': ^18.0.0 jest: ^26.6.3 nock: ^13.0.5 + nodemon: ^2.0.18 probot: ^12.2.4 smee-client: ^1.2.2 ts-jest: ^26.4.4 + ts-node: ^10.9.1 typescript: ^4.1.3 dependencies: probot: 12.2.8 devDependencies: '@types/jest': 28.1.8 '@types/node': 18.7.11 - jest: 26.6.3 + jest: 26.6.3_ts-node@10.9.1 nock: 13.2.9 + nodemon: 2.0.20 smee-client: 1.2.3 ts-jest: 26.5.6_hiruyezz6whlouk4tx2v6ut2z4 + ts-node: 10.9.1_k2dsl7zculo2nmh5s33pladmoa typescript: 4.8.2 cli: @@ -4160,7 +4164,7 @@ packages: slash: 3.0.0 dev: true - /@jest/core/26.6.3: + /@jest/core/26.6.3_ts-node@10.9.1: resolution: {integrity: sha512-xvV1kKbhfUqFVuZ8Cyo+JPpipAHHAV3kcDBftiduK8EICXmTFddryy3P7NfZt8Pv37rA9nEJBKCCkglCPt/Xjw==} engines: {node: '>= 10.14.2'} dependencies: @@ -4175,14 +4179,14 @@ packages: exit: 0.1.2 graceful-fs: 4.2.10 jest-changed-files: 26.6.2 - jest-config: 26.6.3 + jest-config: 26.6.3_ts-node@10.9.1 jest-haste-map: 26.6.2 jest-message-util: 26.6.2 jest-regex-util: 26.0.0 jest-resolve: 26.6.2 jest-resolve-dependencies: 26.6.3 - jest-runner: 26.6.3 - jest-runtime: 26.6.3 + jest-runner: 26.6.3_ts-node@10.9.1 + jest-runtime: 26.6.3_ts-node@10.9.1 jest-snapshot: 26.6.2 jest-util: 26.6.2 jest-validate: 26.6.2 @@ -4445,15 +4449,15 @@ packages: collect-v8-coverage: 1.0.1 dev: true - /@jest/test-sequencer/26.6.3: + /@jest/test-sequencer/26.6.3_ts-node@10.9.1: resolution: {integrity: sha512-YHlVIjP5nfEyjlrSr8t/YdNfU/1XEt7c5b4OxcXCjyRhjzLYu/rO69/WHPuYcbCWkz8kAeZVZp2N2+IOLLEPGw==} engines: {node: '>= 10.14.2'} dependencies: '@jest/test-result': 26.6.2 graceful-fs: 4.2.10 jest-haste-map: 26.6.2 - jest-runner: 26.6.3 - jest-runtime: 26.6.3 + jest-runner: 26.6.3_ts-node@10.9.1 + jest-runtime: 26.6.3_ts-node@10.9.1 transitivePeerDependencies: - bufferutil - canvas @@ -19361,12 +19365,12 @@ packages: - supports-color dev: true - /jest-cli/26.6.3: + /jest-cli/26.6.3_ts-node@10.9.1: resolution: {integrity: sha512-GF9noBSa9t08pSyl3CY4frMrqp+aQXFGFkf5hEPbh/pIUFYWMK6ZLTfbmadxJVcJrdRoChlWQsA2VkJcDFK8hg==} engines: {node: '>= 10.14.2'} hasBin: true dependencies: - '@jest/core': 26.6.3 + '@jest/core': 26.6.3_ts-node@10.9.1 '@jest/test-result': 26.6.2 '@jest/types': 26.6.2 chalk: 4.1.2 @@ -19374,7 +19378,7 @@ packages: graceful-fs: 4.2.10 import-local: 3.1.0 is-ci: 2.0.0 - jest-config: 26.6.3 + jest-config: 26.6.3_ts-node@10.9.1 jest-util: 26.6.2 jest-validate: 26.6.2 prompts: 2.4.2 @@ -19417,7 +19421,7 @@ packages: - utf-8-validate dev: true - /jest-config/26.6.3: + /jest-config/26.6.3_ts-node@10.9.1: resolution: {integrity: sha512-t5qdIj/bCj2j7NFVHb2nFB4aUdfucDn3JRKgrZnplb8nieAirAzRSHP8uDEd+qV6ygzg9Pz4YG7UTJf94LPSyg==} engines: {node: '>= 10.14.2'} peerDependencies: @@ -19427,7 +19431,7 @@ packages: optional: true dependencies: '@babel/core': 7.18.13 - '@jest/test-sequencer': 26.6.3 + '@jest/test-sequencer': 26.6.3_ts-node@10.9.1 '@jest/types': 26.6.2 babel-jest: 26.6.3_@babel+core@7.18.13 chalk: 4.1.2 @@ -19437,13 +19441,14 @@ packages: jest-environment-jsdom: 26.6.2 jest-environment-node: 26.6.2 jest-get-type: 26.3.0 - jest-jasmine2: 26.6.3 + jest-jasmine2: 26.6.3_ts-node@10.9.1 jest-regex-util: 26.0.0 jest-resolve: 26.6.2 jest-util: 26.6.2 jest-validate: 26.6.2 micromatch: 4.0.5 pretty-format: 26.6.2 + ts-node: 10.9.1_k2dsl7zculo2nmh5s33pladmoa transitivePeerDependencies: - bufferutil - canvas @@ -19691,7 +19696,7 @@ packages: fsevents: 2.3.2 dev: true - /jest-jasmine2/26.6.3: + /jest-jasmine2/26.6.3_ts-node@10.9.1: resolution: {integrity: sha512-kPKUrQtc8aYwBV7CqBg5pu+tmYXlvFlSFYn18ev4gPFtrRzB15N2gW/Roew3187q2w2eHuu0MU9TJz6w0/nPEg==} engines: {node: '>= 10.14.2'} dependencies: @@ -19708,7 +19713,7 @@ packages: jest-each: 26.6.2 jest-matcher-utils: 26.6.2 jest-message-util: 26.6.2 - jest-runtime: 26.6.3 + jest-runtime: 26.6.3_ts-node@10.9.1 jest-snapshot: 26.6.2 jest-util: 26.6.2 pretty-format: 26.6.2 @@ -19964,7 +19969,7 @@ packages: slash: 3.0.0 dev: true - /jest-runner/26.6.3: + /jest-runner/26.6.3_ts-node@10.9.1: resolution: {integrity: sha512-atgKpRHnaA2OvByG/HpGA4g6CSPS/1LK0jK3gATJAoptC1ojltpmVlYC3TYgdmGp+GLuhzpH30Gvs36szSL2JQ==} engines: {node: '>= 10.14.2'} dependencies: @@ -19977,13 +19982,13 @@ packages: emittery: 0.7.2 exit: 0.1.2 graceful-fs: 4.2.10 - jest-config: 26.6.3 + jest-config: 26.6.3_ts-node@10.9.1 jest-docblock: 26.0.0 jest-haste-map: 26.6.2 jest-leak-detector: 26.6.2 jest-message-util: 26.6.2 jest-resolve: 26.6.2 - jest-runtime: 26.6.3 + jest-runtime: 26.6.3_ts-node@10.9.1 jest-util: 26.6.2 jest-worker: 26.6.2 source-map-support: 0.5.21 @@ -20028,7 +20033,7 @@ packages: - utf-8-validate dev: true - /jest-runtime/26.6.3: + /jest-runtime/26.6.3_ts-node@10.9.1: resolution: {integrity: sha512-lrzyR3N8sacTAMeonbqpnSka1dHNux2uk0qqDXVkMv2c/A3wYnvQ4EXuI013Y6+gSKSCxdaczvf4HF0mVXHRdw==} engines: {node: '>= 10.14.2'} hasBin: true @@ -20048,7 +20053,7 @@ packages: exit: 0.1.2 glob: 7.2.3 graceful-fs: 4.2.10 - jest-config: 26.6.3 + jest-config: 26.6.3_ts-node@10.9.1 jest-haste-map: 26.6.2 jest-message-util: 26.6.2 jest-mock: 26.6.2 @@ -20282,14 +20287,14 @@ packages: merge-stream: 2.0.0 supports-color: 8.1.1 - /jest/26.6.3: + /jest/26.6.3_ts-node@10.9.1: resolution: {integrity: sha512-lGS5PXGAzR4RF7V5+XObhqz2KZIDUA1yD0DG6pBVmy10eh0ZIXQImRuzocsI/N2XZ1GrLFwTS27In2i2jlpq1Q==} engines: {node: '>= 10.14.2'} hasBin: true dependencies: - '@jest/core': 26.6.3 + '@jest/core': 26.6.3_ts-node@10.9.1 import-local: 3.1.0 - jest-cli: 26.6.3 + jest-cli: 26.6.3_ts-node@10.9.1 transitivePeerDependencies: - bufferutil - canvas @@ -20531,7 +20536,7 @@ packages: lodash.isplainobject: 4.0.6 lodash.isstring: 4.0.1 lodash.once: 4.1.1 - ms: 2.1.2 + ms: 2.1.3 semver: 5.7.1 dev: false @@ -30361,7 +30366,7 @@ packages: bs-logger: 0.2.6 buffer-from: 1.1.2 fast-json-stable-stringify: 2.1.0 - jest: 26.6.3 + jest: 26.6.3_ts-node@10.9.1 jest-util: 26.6.2 json5: 2.2.1 lodash: 4.17.21