Skip to content

Commit

Permalink
feat: add slack notify support
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit committed Nov 15, 2024
1 parent b4e7d4f commit 11dc648
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .github/actions/deploy/deploy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const {
COPILOT_OPENAI_API_KEY,
COPILOT_FAL_API_KEY,
COPILOT_UNSPLASH_API_KEY,
SLACK_BOT_TOKEN,
RELEASE_SLACK_CHNNEL_ID,
MAILER_SENDER,
MAILER_USER,
MAILER_PASSWORD,
Expand Down Expand Up @@ -148,6 +150,8 @@ const createHelmCommand = ({ isDryRun }) => {
`--set-string graphql.app.copilot.openai.key="${COPILOT_OPENAI_API_KEY}"`,
`--set-string graphql.app.copilot.fal.key="${COPILOT_FAL_API_KEY}"`,
`--set-string graphql.app.copilot.unsplash.key="${COPILOT_UNSPLASH_API_KEY}"`,
`--set-string graphql.app.copilot.slack.botToken="${SLACK_BOT_TOKEN}"`,
`--set-string graphql.app.copilot.slack.channelId="${RELEASE_SLACK_CHNNEL_ID}"`,
`--set-string graphql.app.mailer.sender="${MAILER_SENDER}"`,
`--set-string graphql.app.mailer.user="${MAILER_USER}"`,
`--set-string graphql.app.mailer.password="${MAILER_PASSWORD}"`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ data:
openaiSecret: {{ .Values.app.copilot.openai.key | b64enc }}
falSecret: {{ .Values.app.copilot.fal.key | b64enc }}
unsplashSecret: {{ .Values.app.copilot.unsplash.key | b64enc }}
slackBotToken: {{ .Values.app.copilot.slack.botToken | b64enc }}
slackChannelId: {{ .Values.app.copilot.slack.channelId | b64enc }}
{{- end }}
12 changes: 12 additions & 0 deletions .github/helm/affine/charts/graphql/templates/copilot-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ spec:
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
command: ["yarn", "test:copilot:e2e:cron"]
env:
- name: AFFINE_ENV
value: "{{ .Release.Namespace }}"
- name: SLACK_BOT_TOKEN
valueFrom:
secretKeyRef:
name: "{{ .Values.app.copilot.secretName }}"
key: slackBotToken
- name: CHANNEL_ID
valueFrom:
secretKeyRef:
name: "{{ .Values.app.copilot.secretName }}"
key: slackChannelId
- name: COPILOT_E2E_ENDPOINT
value: "http://{{ include "graphql.fullname" . }}.{{ .Release.Namespace }}.svc.cluster.local:3000"
- name: DATABASE_PASSWORD
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ jobs:
COPILOT_OPENAI_API_KEY: ${{ secrets.COPILOT_OPENAI_API_KEY }}
COPILOT_FAL_API_KEY: ${{ secrets.COPILOT_FAL_API_KEY }}
COPILOT_UNSPLASH_API_KEY: ${{ secrets.COPILOT_UNSPLASH_API_KEY }}
# used for slack notifications
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
RELEASE_SLACK_CHNNEL_ID: ${{ secrets.RELEASE_SLACK_CHNNEL_ID }}
METRICS_CUSTOMER_IO_TOKEN: ${{ secrets.METRICS_CUSTOMER_IO_TOKEN }}
MAILER_SENDER: ${{ secrets.OAUTH_EMAIL_SENDER }}
MAILER_USER: ${{ secrets.OAUTH_EMAIL_LOGIN }}
Expand Down
1 change: 0 additions & 1 deletion packages/backend/native/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ export declare function mergeUpdatesInApplyWay(updates: Array<Buffer>): Buffer
export declare function mintChallengeResponse(resource: string, bits?: number | undefined | null): Promise<string>

export declare function verifyChallengeResponse(response: string, bits: number, resource: string): Promise<boolean>

6 changes: 5 additions & 1 deletion packages/backend/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"test:coverage": "c8 ava --concurrency 1 --serial",
"test:copilot:e2e:coverage": "c8 ava --timeout=5m \"tests/**/copilot-*.e2e.ts\"",
"test:copilot:spec:coverage": "c8 ava --timeout=5m \"tests/**/copilot-*.spec.ts\"",
"test:copilot:e2e:cron": "ava --config \"tests/ava.docker.config.js\" \"tests/**/copilot-*.e2e.ts\"",
"test:copilot:e2e:cron": "node ./scripts/copilot-cron-test.js",
"postinstall": "prisma generate",
"data-migration": "node --loader ts-node/esm/transpile-only.mjs ./src/data/index.ts",
"predeploy": "yarn prisma migrate deploy && node --import ./scripts/register.js ./dist/data/index.js run",
Expand Down Expand Up @@ -60,6 +60,7 @@
"@opentelemetry/semantic-conventions": "^1.25.0",
"@prisma/client": "^5.15.0",
"@prisma/instrumentation": "^5.15.0",
"@slack/web-api": "^7.3.4",
"@socket.io/redis-adapter": "^8.3.0",
"ava": "^6.1.2",
"cookie-parser": "^1.4.6",
Expand All @@ -73,8 +74,10 @@
"html-validate": "^8.20.1",
"ioredis": "^5.3.2",
"is-mobile": "^5.0.0",
"jsx-slack": "^6.1.1",
"keyv": "^5.0.0",
"lodash-es": "^4.17.21",
"marked": "^15.0.0",
"mixpanel": "^0.18.0",
"mustache": "^4.2.0",
"nanoid": "^5.0.7",
Expand All @@ -91,6 +94,7 @@
"socket.io": "^4.7.5",
"stripe": "^17.0.0",
"supertest": "^7.0.0",
"tap-parser": "^18.0.0",
"ts-node": "^10.9.2",
"typescript": "^5.6.3",
"yjs": "patch:yjs@npm%3A13.6.18#~/.yarn/patches/yjs-npm-13.6.18-ad0d5f7c43.patch",
Expand Down
89 changes: 89 additions & 0 deletions packages/backend/server/scripts/copilot-cron-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// start process

import { spawn } from 'node:child_process';

import { WebClient } from '@slack/web-api';
import { jsxslack } from 'jsx-slack';
import { marked, Renderer } from 'marked';
import { Parser } from 'tap-parser';

async function runTest() {
const tester = new Promise(resolve => {
const test = spawn(
'npx',
[
'ava',
'--config',
'tests/ava.docker.config.js',
'tests/**/copilot-*.e2e.ts',
'--tap',
],
{ env: { ...process.env, NODE_NO_WARNINGS: 1 } }
);

const parser = new Parser();
test.stdout.pipe(parser);

test.on('close', _ => {
const failures = parser?.failures.filter(f => !!f.fullname);
const report = [
`Test finished with ${parser.results.pass} passed, ${parser.results.fail} failed, ${parser.results.skip} skipped.`,
failures?.length > 0
? `Failed tests: \n\n${failures.map(failure => `- ${failure.fullname}`).join('\n')}`
: '',
];
resolve(report.join('\n\n'));
});
});

try {
return await tester;
} catch (e) {
return e.message;
}
}

function render(markdown) {
const rendered = marked(markdown, {
renderer: new (class CustomRenderer extends Renderer {
heading({ tokens }) {
return `
<Fragment>
<Section><b>${tokens[0].text}</b></Section>
<Divider />
</Fragment>`;
}

paragraph({ tokens }) {
return `<Section><p>${tokens[0].text}</p></Section>`;
}

list(token) {
return `<Section>${super.list(token)}</Section>`;
}

hr() {
return `<Divider />`;
}
})(),
});
return jsxslack([`<Blocks>${rendered}</Blocks>`]);
}

async function main() {
const { CHANNEL_ID, SLACK_BOT_TOKEN, AFFINE_ENV } = process.env;

const report = await runTest();
const blocks = render(
[`# AFFiNE Copilot Test ${AFFINE_ENV} Env Test Result`, report].join('\n\n')
);
const { ok } = await new WebClient(SLACK_BOT_TOKEN).chat.postMessage({
channel: CHANNEL_ID,
text: `AFFiNE Copilot Test ${AFFINE_ENV} Env Test Result`,
blocks,
});

console.assert(ok, 'Failed to send a message to Slack');
}

await main();
1 change: 0 additions & 1 deletion packages/frontend/native/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,3 @@ export declare enum ValidationResult {
}

export declare function verifyChallengeResponse(response: string, bits: number, resource: string): Promise<boolean>

5 changes: 4 additions & 1 deletion packages/frontend/native/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// prettier-ignore
/* eslint-disable */
// @ts-nocheck
/* auto-generated by NAPI-RS */

const { readFileSync } = require('fs')
const { createRequire } = require('node:module')
require = createRequire(__filename)

const { readFileSync } = require('node:fs')
let nativeBinding = null
const loadErrors = []

Expand Down
44 changes: 43 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,7 @@ __metadata:
"@opentelemetry/semantic-conventions": "npm:^1.25.0"
"@prisma/client": "npm:^5.15.0"
"@prisma/instrumentation": "npm:^5.15.0"
"@slack/web-api": "npm:^7.3.4"
"@socket.io/redis-adapter": "npm:^8.3.0"
"@types/cookie-parser": "npm:^1.4.7"
"@types/express": "npm:^4.17.21"
Expand All @@ -819,8 +820,10 @@ __metadata:
html-validate: "npm:^8.20.1"
ioredis: "npm:^5.3.2"
is-mobile: "npm:^5.0.0"
jsx-slack: "npm:^6.1.1"
keyv: "npm:^5.0.0"
lodash-es: "npm:^4.17.21"
marked: "npm:^15.0.0"
mixpanel: "npm:^0.18.0"
mustache: "npm:^4.2.0"
nanoid: "npm:^5.0.7"
Expand All @@ -839,6 +842,7 @@ __metadata:
socket.io: "npm:^4.7.5"
stripe: "npm:^17.0.0"
supertest: "npm:^7.0.0"
tap-parser: "npm:^18.0.0"
ts-node: "npm:^10.9.2"
typescript: "npm:^5.6.3"
yjs: "patch:yjs@npm%3A13.6.18#~/.yarn/patches/yjs-npm-13.6.18-ad0d5f7c43.patch"
Expand Down Expand Up @@ -20044,6 +20048,13 @@ __metadata:
languageName: node
linkType: hard

"events-to-array@npm:^2.0.3":
version: 2.0.3
resolution: "events-to-array@npm:2.0.3"
checksum: 10/d392eb0013013c3dfa66710a017902760edb2a588f6b1a3f1c92219563ba1c24bcb99c48e3754423a3538ebfd70318c3536d30bfd80c00e7fec77fdd088540d0
languageName: node
linkType: hard

"events@npm:^3.2.0":
version: 3.3.0
resolution: "events@npm:3.3.0"
Expand Down Expand Up @@ -31267,6 +31278,28 @@ __metadata:
languageName: node
linkType: hard

"tap-parser@npm:^18.0.0":
version: 18.0.0
resolution: "tap-parser@npm:18.0.0"
dependencies:
events-to-array: "npm:^2.0.3"
tap-yaml: "npm:4.0.0"
bin:
tap-parser: bin/cmd.cjs
checksum: 10/6e13dc475bfdc880307cc935b9917d43255f7e2b0902d171b7f51b41b029aae9dce5b8683aa48170bb720819b3551d793663c0a11f0660ef78d4a0fe87228ad0
languageName: node
linkType: hard

"tap-yaml@npm:4.0.0":
version: 4.0.0
resolution: "tap-yaml@npm:4.0.0"
dependencies:
yaml: "npm:^2.4.1"
yaml-types: "npm:^0.4.0"
checksum: 10/21d3a27328aa419bb90249357689446488351d350a4ba56c5b1afce9dfe33f60db49014170ea7d16eb90381d3f9f31b59d9637bda632e55167f33c834eb02171
languageName: node
linkType: hard

"tapable@npm:^2.0.0, tapable@npm:^2.1.1, tapable@npm:^2.2.0, tapable@npm:^2.2.1":
version: 2.2.1
resolution: "tapable@npm:2.2.1"
Expand Down Expand Up @@ -33501,14 +33534,23 @@ __metadata:
languageName: node
linkType: hard

"yaml-types@npm:^0.4.0":
version: 0.4.0
resolution: "yaml-types@npm:0.4.0"
peerDependencies:
yaml: ^2.3.0
checksum: 10/8a3cd3a0420d5d09981e3e1add46d7482336531e3bdc02192d26caa915c7d0795ad28dd8766e357234d6bfa3a2bd986687f967079e47aecfd4b191250f041cec
languageName: node
linkType: hard

"yaml@npm:^1.10.0":
version: 1.10.2
resolution: "yaml@npm:1.10.2"
checksum: 10/e088b37b4d4885b70b50c9fa1b7e54bd2e27f5c87205f9deaffd1fb293ab263d9c964feadb9817a7b129a5bf30a06582cb08750f810568ecc14f3cdbabb79cb3
languageName: node
linkType: hard

"yaml@npm:^2.3.1, yaml@npm:^2.3.4, yaml@npm:^2.5.1":
"yaml@npm:^2.3.1, yaml@npm:^2.3.4, yaml@npm:^2.4.1, yaml@npm:^2.5.1":
version: 2.6.0
resolution: "yaml@npm:2.6.0"
bin:
Expand Down

0 comments on commit 11dc648

Please sign in to comment.