Skip to content

Commit

Permalink
feat: run tests on pr workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Balint Dolla committed Oct 25, 2023
1 parent 383dacd commit c375d7d
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ VITE_SLACK_BOT_TOKEN=
VITE_SLACK_SIGNING_SECRET=
# Your slack user id
VITE_SLACK_USER_ID=
# The slack bots user id
# The slack bots user id (can be found by running npx ts-node --esm tests/scripts/getBotUserId.ts after setting the slack bot token and signing secret)
VITE_SLACK_BOT_USER_ID=
# The slack channel id used for testing
VITE_SLACK_CHANNEL_ID=
VITE_CORE_SLACK_CHANNEL_ID=
# The slack dm id between the bot and you
VITE_SLACK_DM_ID=
74 changes: 69 additions & 5 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
name: PR checks

on: pull_request
on:
pull_request:
types: [opened, synchronize, closed]
branches: [main]

jobs:
lint-build-and-test:
if: github.event.action == 'opened' || github.event.action == 'synchronize'
runs-on: ubuntu-latest
steps:
- name: Inject slug variables
uses: rlespinasse/github-slug-action@v4

- run: echo "STAGE=pr-${{ github.event.number }}-${{ env.GITHUB_HEAD_REF_SLUG }}" >> $GITHUB_ENV

- name: Checkout repository
uses: actions/checkout@v3

Expand Down Expand Up @@ -49,10 +58,65 @@ jobs:
run: pnpm format:check

- name: Build
run: pnpm build --stage pr

- name: Test
run: pnpm test:ci
run: pnpm build --stage ${{ env.STAGE }}

- name: SIB
run: pnpm sib --pipeline

- name: Run unit tests
run: pnpm test:unit

- name: Set secrets
run: |
npx sst secrets set SLACK_LOG_LEVEL debug --stage ${{ env.STAGE }}
npx sst secrets set SLACK_BOT_TOKEN "${{ secrets.SLACK_BOT_TOKEN_TEST }}" --stage ${{ env.STAGE }}
npx sst secrets set SLACK_SIGNING_SECRET "${{ secrets.SLACK_SIGNING_SECRET_TEST }}" --stage ${{ env.STAGE }}
npx sst secrets set CORE_SLACK_CHANNEL_ID "${{ secrets.CORE_SLACK_CHANNEL_ID_TEST }}" --stage ${{ env.STAGE }}
npx sst secrets set RANDOM_SLACK_CHANNEL_ID "${{ secrets.RANDOM_SLACK_CHANNEL_ID_TEST }}" --stage ${{ env.STAGE }}
- name: Deploy stack
run: pnpm run deploy --stage ${{ env.STAGE }}

- name: Extract api endpoint
id: sst-output
run: |
URL=$(jq -r '.["${{env.STAGE}}-birthday-slack-bot-MyStack"].ApiEndpoint' .sst/outputs.json)
echo "apiEndpoint=$URL" >> "$GITHUB_OUTPUT"
- name: Run integration tests
run: pnpm test:integration
env:
VITE_API_URL: ${{ steps.sst-output.outputs.apiEndpoint }}
VITE_SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN_TEST }}
VITE_SLACK_SIGNING_SECRET: ${{ secrets.SLACK_SIGNING_SECRET_TEST }}
VITE_SLACK_USER_ID: ${{ secrets.SLACK_USER_ID_TEST }}
VITE_SLACK_BOT_USER_ID: ${{ secrets.SLACK_BOT_USER_ID_TEST }}
VITE_CORE_SLACK_CHANNEL_ID: ${{ secrets.CORE_SLACK_CHANNEL_ID_TEST }}
VITE_SLACK_DM_ID: ${{ secrets.SLACK_DM_ID_TEST }}

destroy:
if: github.event.action == 'closed'
runs-on: ubuntu-latest

steps:
- name: Inject slug variables
uses: rlespinasse/github-slug-action@v4

- run: echo "STAGE=pr-${{ github.event.number }}-${{ env.GITHUB_HEAD_REF_SLUG }}" >> $GITHUB_ENV

- name: Checkout repository
uses: actions/checkout@v3

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_STAGING }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_STAGING }}
aws-region: eu-central-1

- uses: actions/setup-node@v3
with:
node-version: "18"

- name: Delete stack
run: npx sst remove --stage ${{ env.STAGE }}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"format:check": "prettier --check \"**/*.ts\"",
"prepare": "husky install",
"test": "vitest watch",
"test:ci": "vitest run --passWithNoTests"
"test:unit": "vitest run unit",
"test:integration": "vitest run integration"
},
"dependencies": {
"sst": "^2.28.1"
Expand All @@ -32,6 +33,7 @@
"@typescript-eslint/parser": "^6.7.5",
"aws-cdk-lib": "2.95.1",
"constructs": "10.2.69",
"dotenv": "^16.3.1",
"eslint": "^8.51.0",
"eslint-config-prettier": "^9.0.0",
"eslint-define-config": "^1.24.1",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface ImportMetaEnv {
readonly VITE_SLACK_SIGNING_SECRET: string;
readonly VITE_SLACK_BOT_TOKEN: string;
readonly VITE_SLACK_USER_ID: string;
readonly VITE_SLACK_CHANNEL_ID: string;
readonly VITE_CORE_SLACK_CHANNEL_ID: string;
readonly VITE_SLACK_BOT_USER_ID: string;
readonly VITE_SLACK_DM_ID: string;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/slackEvents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe("Slack events", () => {
type: "event_callback",
event: {
type: "member_joined_channel",
channel: import.meta.env.VITE_SLACK_CHANNEL_ID,
channel: import.meta.env.VITE_CORE_SLACK_CHANNEL_ID,
user: import.meta.env.VITE_SLACK_USER_ID,
},
});
Expand All @@ -57,7 +57,7 @@ describe("Slack events", () => {
type: "event_callback",
event: {
type: "member_joined_channel",
channel: import.meta.env.VITE_SLACK_CHANNEL_ID,
channel: import.meta.env.VITE_CORE_SLACK_CHANNEL_ID,
user: import.meta.env.VITE_SLACK_BOT_USER_ID,
},
});
Expand Down
17 changes: 17 additions & 0 deletions tests/scripts/getBotUserId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Slack from "@slack/bolt";
const { App } = Slack;

import dotenv from "dotenv";

dotenv.config({
path: ".env.local",
});

const app = new App({
signingSecret: process.env.VITE_SLACK_SIGNING_SECRET,
token: process.env.VITE_SLACK_BOT_TOKEN,
});

app.client.auth.test().then((res) => {
console.log(`Bot user id: ${res.user_id}`);
});

0 comments on commit c375d7d

Please sign in to comment.