From 86e0447cdbfb4852007e8f6d2fcdcc71536018ad Mon Sep 17 00:00:00 2001 From: "Hyunmin Woo (Hanul)" Date: Sat, 6 Jul 2024 18:48:55 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20slack=20=EC=A7=81=EC=A0=91=20?= =?UTF-8?q?=EC=8F=98=EB=8A=94=20=EB=8C=80=EC=8B=A0=20=EC=99=80=EB=8B=B7api?= =?UTF-8?q?=20=EC=A0=81=EC=9A=A9=20(#76)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit @minkyu97 이 유저정보 API를 뚫어줘서, 이 api 이용합니다. 참고: https://wafflestudio.slack.com/archives/C06H0PJPDNH/p1720256977566729?thread_ts=1707317300.593519&cid=C06H0PJPDNH --- package.json | 3 +- .../implementMemberSlackRepository.ts | 48 ------------------- .../implementMemberWaffleDotComRepository.ts | 21 ++++++++ src/server.ts | 4 +- src/weekly-dashboard.ts | 4 +- yarn.lock | 14 +++--- 6 files changed, 33 insertions(+), 61 deletions(-) delete mode 100644 src/infrastructures/implementMemberSlackRepository.ts create mode 100644 src/infrastructures/implementMemberWaffleDotComRepository.ts diff --git a/package.json b/package.json index 2c54734..a5f3e6a 100644 --- a/package.json +++ b/package.json @@ -21,8 +21,7 @@ "dependencies": { "express": "4.19.2", "octokit": "4.0.2", - "openai": "4.52.1", - "p-limit": "5.0.0" + "openai": "4.52.1" }, "devDependencies": { "@types/express": "4.17.21", diff --git a/src/infrastructures/implementMemberSlackRepository.ts b/src/infrastructures/implementMemberSlackRepository.ts deleted file mode 100644 index c0a9a0d..0000000 --- a/src/infrastructures/implementMemberSlackRepository.ts +++ /dev/null @@ -1,48 +0,0 @@ -import pLimit from 'p-limit'; - -import { Member } from '../entities/Member'; - -export const implementMemberSlackRepository = ({ - slackAuthToken, -}: { - slackAuthToken: string; -}): { getAllMembers: () => Promise<{ members: Member[] }> } => { - return { - getAllMembers: async () => { - const users = await fetch('https://slack.com/api/users.list', { - method: 'GET', - headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${slackAuthToken}` }, - }).then((res) => res.json() as Promise<{ ok: true; members: { id: string }[] } | { ok: false }>); - - if (!users.ok) throw new Error('Failed to fetch users'); - - const limit = pLimit(10); - - const fetchMemberProfile = async (member: { id: string }) => { - const profile = await fetch(`https://slack.com/api/users.profile.get?user=${member.id}`, { - method: 'GET', - headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${slackAuthToken}` }, - }).then( - (res) => - res.json() as Promise< - { ok: true; profile: { fields: { [githubField]?: { value: string } } } } | { ok: false } - >, - ); - - if (!profile.ok) throw new Error('Failed to fetch profile: ' + JSON.stringify(profile)); - - const githubUsername = profile.profile.fields[githubField]?.value; - - if (!githubUsername) return []; - - return [{ githubUsername: githubUsername.replace('https://github.com/', ''), slackUserId: member.id }]; - }; - - return { - members: (await Promise.all(users.members.map((member) => limit(() => fetchMemberProfile(member))))).flat(), - }; - }, - }; -}; - -const githubField = 'Xf01UD0C7526'; diff --git a/src/infrastructures/implementMemberWaffleDotComRepository.ts b/src/infrastructures/implementMemberWaffleDotComRepository.ts new file mode 100644 index 0000000..69ff457 --- /dev/null +++ b/src/infrastructures/implementMemberWaffleDotComRepository.ts @@ -0,0 +1,21 @@ +import { Member } from '../entities/Member'; + +/* + * API + * @see https://wafflestudio.slack.com/archives/C06H0PJPDNH/p1720256977566729?thread_ts=1707317300.593519&cid=C06H0PJPDNH + */ +export const implementMemberWaffleDotComRepository = (): { + getAllMembers: () => Promise<{ members: Member[] }>; +} => { + return { + getAllMembers: async () => { + const users = await fetch('https://wadot-api.wafflestudio.com/api/v1/users', { + method: 'GET', + }).then((res) => res.json() as Promise<{ github_id: string; slack_id: string }[]>); + + return { + members: users.map((user) => ({ slackUserId: user.slack_id, githubUsername: user.github_id })), + }; + }, + }; +}; diff --git a/src/server.ts b/src/server.ts index c4fd2b5..99cef4c 100644 --- a/src/server.ts +++ b/src/server.ts @@ -2,7 +2,7 @@ import dotenv from 'dotenv'; import express from 'express'; import { implementGitHubDeployWebhookController } from './infrastructures/implementGitHubDeployWebhookController'; -import { implementMemberSlackRepository } from './infrastructures/implementMemberSlackRepository'; +import { implementMemberWaffleDotComRepository } from './infrastructures/implementMemberWaffleDotComRepository'; import { implementOpenAiSummarizeRepository } from './infrastructures/implementOpenAiSummarizeRepository'; import { implementSlackPresenter } from './infrastructures/implementSlackPresenter'; import { implementDeploymentService } from './services/GithubDeploymentService'; @@ -46,7 +46,7 @@ const deployWebhookController = implementGitHubDeployWebhookController({ channelId: deployWatcherChannelId, }), summarizeLLMRepository: implementOpenAiSummarizeRepository({ openaiApiKey }), - memberRepository: implementMemberSlackRepository({ slackAuthToken }), + memberRepository: implementMemberWaffleDotComRepository(), }), }); diff --git a/src/weekly-dashboard.ts b/src/weekly-dashboard.ts index 8696656..bc5e285 100644 --- a/src/weekly-dashboard.ts +++ b/src/weekly-dashboard.ts @@ -1,7 +1,7 @@ import dotenv from 'dotenv'; import { implementGithubOctokitRepository } from './infrastructures/implementGithubOctokitRepository'; -import { implementMemberSlackRepository } from './infrastructures/implementMemberSlackRepository'; +import { implementMemberWaffleDotComRepository } from './infrastructures/implementMemberWaffleDotComRepository'; import { implementSlackPresenter } from './infrastructures/implementSlackPresenter'; import { implementDashboardService } from './services/DashboardService'; @@ -29,7 +29,7 @@ if (!githubOrganization) throw new Error('Missing Github Organization'); const dashboardService = implementDashboardService({ githubApiRepository: implementGithubOctokitRepository({ githubAuthToken: githubAccessToken }), messengerPresenter: implementSlackPresenter({ slackAuthToken, channelId: slackWeeklyChannelId }), - memberRepository: implementMemberSlackRepository({ slackAuthToken }), + memberRepository: implementMemberWaffleDotComRepository(), }); dashboardService.sendWeeklyDashboard(githubOrganization).catch(console.error); diff --git a/yarn.lock b/yarn.lock index cddbc2f..8f89e89 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1901,13 +1901,6 @@ optionator@^0.9.3: type-check "^0.4.0" word-wrap "^1.2.5" -p-limit@5.0.0, p-limit@^5.0.0: - version "5.0.0" - resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-5.0.0.tgz#6946d5b7140b649b7a33a027d89b4c625b3a5985" - integrity sha512-/Eaoq+QyLSiXQ4lyYV23f14mZRQcXnxfHrN0vCai+ak9G0pp9iEQukIIZq5NccEvwRB8PUnZT0KsOoDCINS1qQ== - dependencies: - yocto-queue "^1.0.0" - p-limit@^3.0.2: version "3.1.0" resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-3.1.0.tgz#e1daccbe78d0d1388ca18c64fea38e3e57e3706b" @@ -1915,6 +1908,13 @@ p-limit@^3.0.2: dependencies: yocto-queue "^0.1.0" +p-limit@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-5.0.0.tgz#6946d5b7140b649b7a33a027d89b4c625b3a5985" + integrity sha512-/Eaoq+QyLSiXQ4lyYV23f14mZRQcXnxfHrN0vCai+ak9G0pp9iEQukIIZq5NccEvwRB8PUnZT0KsOoDCINS1qQ== + dependencies: + yocto-queue "^1.0.0" + p-locate@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-5.0.0.tgz#83c8315c6785005e3bd021839411c9e110e6d834"