Skip to content

Commit

Permalink
weekly dashboard 를 node-cron 에서 github action으로 마이그레이션
Browse files Browse the repository at this point in the history
  • Loading branch information
woohm402 committed Jan 13, 2024
1 parent 487d7b6 commit 8db3f81
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 162 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: deploy-dev
name: deploy

on:
push:
Expand Down Expand Up @@ -36,7 +36,6 @@ jobs:
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG \
--build-arg SLACK_BOT_TOKEN=${{ secrets.SLACK_BOT_TOKEN }} \
--build-arg SLACK_AUTH_TOKEN=${{ secrets.SLACK_AUTH_TOKEN }} \
--build-arg SLACK_PUPURI_CHANNEL_ID=${{ secrets.SLACK_PUPURI_CHANNEL_ID }} \
--build-arg SLACK_ACTIVE_CHANNEL_ID=${{ secrets.SLACK_ACTIVE_CHANNEL_ID }} \
--build-arg SLACK_WATCHER_CHANNEL_ID=${{ secrets.SLACK_WATCHER_CHANNEL_ID }} \
--build-arg GHP_ACCESS_TOKEN=${{ secrets.GHP_ACCESS_TOKEN }} \
Expand Down
34 changes: 34 additions & 0 deletions .github/workflows/weekly-dashboard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: send-weekly-dashboard

on:
schedule:
- cron: '20 2 * * 1' # 월요일 한국시간 오전 11시 20분

jobs:
cron:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x]

steps:
- name: Check out code
uses: actions/checkout@v3
with:
fetch-depth: 2

- name: Setup Node.js environment
uses: actions/setup-node@v3
with:
node-version: 18
cache: 'yarn'

# cron job을 실행합니다.
- name: run script
run: |
yarn install
SLACK_AUTH_TOKEN=${{ secrets.SLACK_AUTH_TOKEN }} \
SLACK_ACTIVE_CHANNEL_ID=${{ secrets.SLACK_ACTIVE_CHANNEL_ID }} \
SLACK_WATCHER_CHANNEL_ID=${{ secrets.SLACK_WATCHER_CHANNEL_ID }} \
GHP_ACCESS_TOKEN=${{ secrets.GHP_ACCESS_TOKEN }} \
yarn send:weekly-summary
7 changes: 2 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
FROM node:18.11.0-alpine
FROM node:18.17.1-alpine
ARG SLACK_BOT_TOKEN
ARG SLACK_AUTH_TOKEN
ARG SLACK_PUPURI_CHANNEL_ID
ARG SLACK_ACTIVE_CHANNEL_ID
ARG SLACK_WATCHER_CHANNEL_ID
ARG GHP_ACCESS_TOKEN
COPY . .
RUN echo "SLACK_BOT_TOKEN=${SLACK_BOT_TOKEN}" >> .env.local
RUN echo "SLACK_AUTH_TOKEN=${SLACK_AUTH_TOKEN}" >> .env.local
RUN echo "SLACK_PUPURI_CHANNEL_ID=${SLACK_PUPURI_CHANNEL_ID}" >> .env.local
RUN echo "SLACK_ACTIVE_CHANNEL_ID=${SLACK_ACTIVE_CHANNEL_ID}" >> .env.local
RUN echo "SLACK_WATCHER_CHANNEL_ID=${SLACK_WATCHER_CHANNEL_ID}" >> .env.local
RUN echo "GHP_ACCESS_TOKEN=${GHP_ACCESS_TOKEN}" >> .env.local
RUN yarn install
RUN yarn build
CMD ["yarn", "start"]
CMD ["yarn", "start:server"]

EXPOSE 3000
16 changes: 6 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
{
"name": "pupuri-bot",
"version": "0.0.1",
"version": "1.0.0",
"description": "wafflestudio pupuri bot",
"main": "index.ts",
"repository": "https://github.com/wafflestudio/pupuri-bot",
"author": "woohm402 <[email protected]>",
"license": "MIT",
"private": false,
"scripts": {
"start": "node dist/src/index.js",
"build": "tsc -p .",
"dev": "nodemon --watch \"src/**/*.ts\" --exec \"ts-node\" src/index.ts",
"deploy": "scripts/deploy.sh",
"start:server": "ts-node src/server.ts",
"send:weekly-dashboard": "ts-node src/weekly-dashboard.ts",
"deploy-server": "scripts/deploy.sh",
"test:unit": "jest",
"lint": "eslint ./src"
},
"dependencies": {
"dotenv": "16.0.3",
"express": "4.18.2",
"node-cron": "3.0.2"
"express": "4.18.2"
},
"devDependencies": {
"@jest/globals": "29.5.0",
"@types/express": "4.17.17",
"@types/node": "18.15.10",
"@types/node-cron": "3.0.7",
"@typescript-eslint/eslint-plugin": "5.57.0",
"@typescript-eslint/parser": "5.57.0",
"dotenv": "16.0.3",
"eslint": "8.37.0",
"eslint-config-prettier": "8.8.0",
"eslint-plugin-prettier": "4.2.1",
"eslint-plugin-simple-import-sort": "10.0.0",
"jest": "29.5.0",
"nodemon": "2.0.22",
"prettier": "2.8.7",
"ts-jest": "29.0.5",
"ts-node": "10.9.1",
Expand Down
12 changes: 1 addition & 11 deletions src/index.ts → src/server.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
import dotenv from 'dotenv';
import express from 'express';
import cron from 'node-cron';

import { implementDashboardService } from './infrastructures/implementDashboardService';
import { implementGithubApiRepository } from './infrastructures/implementGithubApiRepository';
import { implementGithubHttpClient } from './infrastructures/implementGithubHttpClient';
import { implementSlackEventService } from './infrastructures/implementSlackEventService';
import { implementSlackHttpClient } from './infrastructures/implementSlackHttpClient';

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

const slackAuthToken = process.env.SLACK_AUTH_TOKEN;
const slackBotToken = process.env.SLACK_BOT_TOKEN;
const githubAccessToken = process.env.GHP_ACCESS_TOKEN;
const slackWatcherChannelId = process.env.SLACK_WATCHER_CHANNEL_ID;
const slackActiveChannelId = process.env.SLACK_ACTIVE_CHANNEL_ID;

if (!slackAuthToken) throw new Error('Missint Slack Auth Token');
if (!slackAuthToken) throw new Error('Missing Slack Auth Token');
if (!slackBotToken) throw new Error('Missing Slack Bot Token');
if (!githubAccessToken) throw new Error('Missing Github Access Token');
if (!slackWatcherChannelId) throw new Error('Missing Slack Watcher Channel ID');
if (!slackActiveChannelId) throw new Error('Missing Slack Active Channel ID');

Expand All @@ -40,9 +34,6 @@ const slackClient = implementSlackHttpClient({
active: slackActiveChannelId,
},
});
const githubClient = implementGithubHttpClient({ githubAccessToken });
const githubApiRepository = implementGithubApiRepository({ githubClient });
const dashboardService = implementDashboardService({ githubApiRepository, slackClient });
const slackService = implementSlackEventService({ slackClient });

/**
Expand All @@ -54,7 +45,6 @@ const slackService = implementSlackEventService({ slackClient });
╚═════╝ ╚══════╝ ╚═════╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝
*/

cron.schedule('0 3 * * 1', () => dashboardService.sendGithubTopRepositoriesLastWeek('wafflestudio'));
app.post('/slack/action-endpoint', express.json(), (req, res) => {
try {
if (req.body.token !== slackBotToken) throw new Error('403');
Expand Down
39 changes: 39 additions & 0 deletions src/weekly-dashboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import dotenv from 'dotenv';

import { implementDashboardService } from './infrastructures/implementDashboardService';
import { implementGithubApiRepository } from './infrastructures/implementGithubApiRepository';
import { implementGithubHttpClient } from './infrastructures/implementGithubHttpClient';
import { implementSlackHttpClient } from './infrastructures/implementSlackHttpClient';

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

const slackAuthToken = process.env.SLACK_AUTH_TOKEN;
const githubAccessToken = process.env.GHP_ACCESS_TOKEN;
const slackWatcherChannelId = process.env.SLACK_WATCHER_CHANNEL_ID;
const slackActiveChannelId = process.env.SLACK_ACTIVE_CHANNEL_ID;

if (!slackAuthToken) throw new Error('Missing Slack Auth Token');
if (!githubAccessToken) throw new Error('Missing Github Access Token');
if (!slackWatcherChannelId) throw new Error('Missing Slack Watcher Channel ID');
if (!slackActiveChannelId) throw new Error('Missing Slack Active Channel ID');

/**
██████╗ ███████╗██████╗ ███████╗███╗ ██╗██████╗ ███████╗███╗ ██╗ ██████╗██╗███████╗███████╗
██╔══██╗██╔════╝██╔══██╗██╔════╝████╗ ██║██╔══██╗██╔════╝████╗ ██║██╔════╝██║██╔════╝██╔════╝
██║ ██║█████╗ ██████╔╝█████╗ ██╔██╗ ██║██║ ██║█████╗ ██╔██╗ ██║██║ ██║█████╗ ███████╗
██║ ██║██╔══╝ ██╔═══╝ ██╔══╝ ██║╚██╗██║██║ ██║██╔══╝ ██║╚██╗██║██║ ██║██╔══╝ ╚════██║
██████╔╝███████╗██║ ███████╗██║ ╚████║██████╔╝███████╗██║ ╚████║╚██████╗██║███████╗███████║
╚═════╝ ╚══════╝╚═╝ ╚══════╝╚═╝ ╚═══╝╚═════╝ ╚══════╝╚═╝ ╚═══╝ ╚═════╝╚═╝╚══════╝╚══════╝
*/
const slackClient = implementSlackHttpClient({
external: { slackAuthToken },
channelIds: {
'slack-watcher': slackWatcherChannelId,
active: slackActiveChannelId,
},
});
const githubClient = implementGithubHttpClient({ githubAccessToken });
const githubApiRepository = implementGithubApiRepository({ githubClient });
const dashboardService = implementDashboardService({ githubApiRepository, slackClient });

dashboardService.sendGithubTopRepositoriesLastWeek('wafflestudio');
Loading

0 comments on commit 8db3f81

Please sign in to comment.