From da684bcc54555bf654277d558613fa25b8afdf2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B5=E1=84=80=E1=85=AA=E1=86=BC=E1=84=92?= =?UTF-8?q?=E1=85=AE=E1=86=AB?= Date: Sun, 19 May 2024 13:53:53 +0900 Subject: [PATCH 1/4] =?UTF-8?q?Feat:=20github=20=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EC=A0=95=EB=B3=B4=20=EB=B0=9B=EC=95=84=EC=99=80=EC=84=9C=20?= =?UTF-8?q?=EC=A0=90=EC=88=98=20=EA=B3=84=EC=82=B0=20=EB=B0=8F=20=EC=A0=80?= =?UTF-8?q?=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/stat/rank.controller.ts | 4 ++ src/stat/service/github.service.ts | 61 +++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/src/stat/rank.controller.ts b/src/stat/rank.controller.ts index 61d053d..4395877 100644 --- a/src/stat/rank.controller.ts +++ b/src/stat/rank.controller.ts @@ -16,6 +16,7 @@ import { } from '@nestjs/swagger'; import { UserService } from '../user/user.service'; import { PointFindDto, RankFindDto } from './dto/rank-find.dto'; +import { ErrorDto } from './dto/error.dto'; @UseGuards(JwtAuthGuard) @Controller('api/rank') @@ -41,12 +42,15 @@ export class RankController { }) @ApiUnauthorizedResponse({ description: 'jwt 관련 문제 (인증 시간이 만료됨, jwt를 보내지 않음)', + type: ErrorDto, }) @ApiForbiddenResponse({ description: '허용되지 않은 자원에 접근한 경우. 즉, 권한이 없는 경우', + type: ErrorDto, }) @ApiInternalServerErrorResponse({ description: '서버 오류', + type: ErrorDto, }) async findAlgorithmRank(@Query() options: RankListOptionDto) { return await this.algorithmService.getAlgorithms(options); diff --git a/src/stat/service/github.service.ts b/src/stat/service/github.service.ts index d82d99b..0ceb717 100644 --- a/src/stat/service/github.service.ts +++ b/src/stat/service/github.service.ts @@ -32,7 +32,7 @@ export class GithubService { throw new BadRequestException('이미 등록된 id 입니다'); } - const githubPoint = this.calculateGithubPoint(userResource); + const githubPoint = await this.calculateGithubPoint(userResource); const github = new Github(); github.userId = userId; @@ -44,7 +44,7 @@ export class GithubService { public async modifyGithub(tokens: CreateGithubDto, userId: string) { const userResource = await this.getUserResource(tokens.accessToken); - const githubPoint = this.calculateGithubPoint(userResource); + const githubPoint = await this.calculateGithubPoint(userResource); const isExist = await this.githubRepository.findOneById(userId); @@ -69,7 +69,7 @@ export class GithubService { } try { const githubInfo = await this.getUserResource(github.accessToken); - github.point = this.calculateGithubPoint(githubInfo); + github.point = await this.calculateGithubPoint(githubInfo); await this.githubRepository.updateGithub(github); } catch (e) { this.logger.error( @@ -89,8 +89,59 @@ export class GithubService { await this.githubRepository.deleteGithub(userId); } - public calculateGithubPoint(userResource: object) { - return 0; + public async calculateGithubPoint(userResource: object) { + const commitInfo = await this.getCommits(userResource['login']); + const PRInfo = await this.getPRs(userResource['login']); + const issueInfo = await this.getIssues(userResource['login']); + const followers = userResource['followers']; + const [COMMIT_WEIGHT, PR_WEIGHT, ISSUE_WEIGHT, FOLLOWER_WEIGHT] = [ + 2, 3, 2, 1, + ]; + return ( + commitInfo * COMMIT_WEIGHT + + issueInfo * ISSUE_WEIGHT + + PRInfo * PR_WEIGHT + + followers * FOLLOWER_WEIGHT + ); + } + + public async getIssues(userName: string) { + const requestURL = `https://api.github.com/search/issues?q=author:${userName}+is:issue`; + + const commitInfo = await fetch(requestURL, { + headers: { + Accept: 'application/json', + }, + }); + + const commitInfoJson = await commitInfo.json(); + return commitInfoJson.total_count; + } + + public async getPRs(userName: string) { + const requestURL = `https://api.github.com/search/issues?q=author:${userName}+is:pr`; + + const commitInfo = await fetch(requestURL, { + headers: { + Accept: 'application/json', + }, + }); + + const commitInfoJson = await commitInfo.json(); + return commitInfoJson.total_count; + } + + public async getCommits(userName: string) { + const requestURL = `https://api.github.com/search/commits?q=author:${userName}`; + + const commitInfo = await fetch(requestURL, { + headers: { + Accept: 'application/json', + }, + }); + + const commitInfoJson = await commitInfo.json(); + return commitInfoJson.total_count; } public async fetchAccessToken(authorization_code: string) { From 87cd95a2dcc419d87165cdef13894cb848514b1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B5=E1=84=80=E1=85=AA=E1=86=BC=E1=84=92?= =?UTF-8?q?=E1=85=AE=E1=86=AB?= Date: Sun, 19 May 2024 13:57:12 +0900 Subject: [PATCH 2/4] =?UTF-8?q?Fix=20:=20=EC=98=A4=EB=A5=98=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/stat/dto/error.dto.ts | 4 ++++ src/stat/rank.controller.ts | 4 ---- 2 files changed, 4 insertions(+), 4 deletions(-) create mode 100644 src/stat/dto/error.dto.ts diff --git a/src/stat/dto/error.dto.ts b/src/stat/dto/error.dto.ts new file mode 100644 index 0000000..dc610ea --- /dev/null +++ b/src/stat/dto/error.dto.ts @@ -0,0 +1,4 @@ +export class ErrorDto { + statusConde: number; + message: string; +} diff --git a/src/stat/rank.controller.ts b/src/stat/rank.controller.ts index 4395877..61d053d 100644 --- a/src/stat/rank.controller.ts +++ b/src/stat/rank.controller.ts @@ -16,7 +16,6 @@ import { } from '@nestjs/swagger'; import { UserService } from '../user/user.service'; import { PointFindDto, RankFindDto } from './dto/rank-find.dto'; -import { ErrorDto } from './dto/error.dto'; @UseGuards(JwtAuthGuard) @Controller('api/rank') @@ -42,15 +41,12 @@ export class RankController { }) @ApiUnauthorizedResponse({ description: 'jwt 관련 문제 (인증 시간이 만료됨, jwt를 보내지 않음)', - type: ErrorDto, }) @ApiForbiddenResponse({ description: '허용되지 않은 자원에 접근한 경우. 즉, 권한이 없는 경우', - type: ErrorDto, }) @ApiInternalServerErrorResponse({ description: '서버 오류', - type: ErrorDto, }) async findAlgorithmRank(@Query() options: RankListOptionDto) { return await this.algorithmService.getAlgorithms(options); From a05d82ee898de62cc8a9e097fcf4069e45d1305e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B5=E1=84=80=E1=85=AA=E1=86=BC=E1=84=92?= =?UTF-8?q?=E1=85=AE=E1=86=AB?= Date: Sun, 19 May 2024 13:58:54 +0900 Subject: [PATCH 3/4] =?UTF-8?q?Fix=20:=20=EC=88=98=EC=A0=95=20=EC=82=AC?= =?UTF-8?q?=ED=95=AD=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/stat/dto/error.dto.ts | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 src/stat/dto/error.dto.ts diff --git a/src/stat/dto/error.dto.ts b/src/stat/dto/error.dto.ts deleted file mode 100644 index dc610ea..0000000 --- a/src/stat/dto/error.dto.ts +++ /dev/null @@ -1,4 +0,0 @@ -export class ErrorDto { - statusConde: number; - message: string; -} From b40eb778096484834a396a51e90700642d194e99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B5=E1=84=80=E1=85=AA=E1=86=BC=E1=84=92?= =?UTF-8?q?=E1=85=AE=E1=86=AB?= Date: Sun, 19 May 2024 14:05:12 +0900 Subject: [PATCH 4/4] =?UTF-8?q?Fix=20:=20=EB=A1=9C=EA=B7=B8=20=EB=A9=94?= =?UTF-8?q?=EC=8B=9C=EC=A7=80=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/stat/service/github.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stat/service/github.service.ts b/src/stat/service/github.service.ts index 0ceb717..cb87087 100644 --- a/src/stat/service/github.service.ts +++ b/src/stat/service/github.service.ts @@ -73,7 +73,7 @@ export class GithubService { await this.githubRepository.updateGithub(github); } catch (e) { this.logger.error( - `${userId} 님의 알고리즘 스탯이 업데이트 되지 않음. ${e}`, + `${userId} 님의 깃허브 스탯이 업데이트 되지 않음. ${e}`, ); } }