Skip to content

Commit 798b2f6

Browse files
committed
feat: 깃허브 어댑터 구현
1 parent 37fa0fe commit 798b2f6

8 files changed

+192
-0
lines changed

package-lock.json

+97
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
"@mikro-orm/migrations": "^6.2.9",
2525
"@mikro-orm/reflection": "^6.2.9",
2626
"@mikro-orm/sqlite": "^6.2.9",
27+
"axios": "^1.7.2",
28+
"dayjs": "^1.11.11",
2729
"discord.js": "^14.15.2",
2830
"dotenv": "^16.4.5",
2931
"ulid": "^2.3.0"

src/config/index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,21 @@ export type DiscordConfig = {
77
token: string;
88
};
99

10+
export type GithubConfig = {
11+
token: string;
12+
};
13+
1014
export type RootConfig = {
1115
discord: DiscordConfig;
16+
github: GithubConfig;
1217
};
1318

1419
export const config: RootConfig = {
1520
discord: {
1621
clientId: process.env.DISCORD_BOT_CLIENT_ID || '',
1722
token: process.env.DISCORD_BOT_TOKEN || '',
1823
},
24+
github: {
25+
token: process.env.GITHUB_TOKEN || '',
26+
},
1927
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export type GetUserContributionInfoDto = {
2+
user: {
3+
contributionsCollection: {
4+
hasAnyContributions: boolean;
5+
hasAnyRestrictedContributions: boolean;
6+
};
7+
};
8+
};

src/github/dto/get-user.dto.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export type GetUserDto = {
2+
login: string;
3+
name: string;
4+
bio: string;
5+
avatarUrl: string;
6+
url: string;
7+
};

src/github/dto/user-query.dto.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type UserQueryDto<T> = { data: { user: T } };

src/github/github-client.ts

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import axios, { AxiosInstance } from 'axios';
2+
3+
import { config } from '@susc/config';
4+
import { UserQueryDto } from '@susc/github/dto/user-query.dto';
5+
import { GetUserDto } from '@susc/github/dto/get-user.dto';
6+
import { GetUserContributionInfoDto } from '@susc/github/dto/get-user-contribution-info.dto';
7+
import dayjs from 'dayjs';
8+
9+
export class GithubClient {
10+
client: AxiosInstance;
11+
12+
constructor() {
13+
const token = config.github.token;
14+
this.client = axios.create({
15+
baseURL: 'https://api.github.com/graphql',
16+
headers: {
17+
Authorization: `Bearer ${token}`,
18+
},
19+
});
20+
}
21+
22+
async getUser(githubId: string): Promise<GetUserDto> {
23+
const query = `
24+
query userInfo($login: String!) {
25+
user(login: $login) {
26+
login
27+
name
28+
bio
29+
avatarUrl
30+
url
31+
}
32+
}
33+
`;
34+
35+
const { data } = await this.client.post<UserQueryDto<GetUserDto>>('', {
36+
query,
37+
variables: { login: githubId },
38+
});
39+
return data.data.user;
40+
}
41+
42+
async getContribution(githubId: string): Promise<GetUserContributionInfoDto> {
43+
const query = `
44+
query userInfo($login: String!, $date: DateTime!) {
45+
user(login: $login) {
46+
contributionsCollection(from: $date) {
47+
hasAnyContributions,
48+
hasAnyRestrictedContribution
49+
}
50+
}
51+
}
52+
`;
53+
54+
const startOfToday = dayjs().tz('Asia/Seoul').startOf('day').toISOString();
55+
const { data } = await this.client.post<
56+
UserQueryDto<GetUserContributionInfoDto>
57+
>('', {
58+
query,
59+
variables: { login: githubId },
60+
});
61+
return data.data.user;
62+
}
63+
}

src/main.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
import dayjs from 'dayjs';
2+
import timezone from 'dayjs/plugin/timezone';
3+
import utc from 'dayjs/plugin/utc';
14
import { RegisterCommandHandler } from './commands/register';
25
import { DiscordBot } from './discord/discord-bot';
36

7+
dayjs.extend(utc);
8+
dayjs.extend(timezone);
9+
410
async function bootstrap() {
511
const client = new DiscordBot();
612
await client.init();

0 commit comments

Comments
 (0)