Skip to content

Commit 303e399

Browse files
committed
feat: 잘못된 숫자 값에 대한 예외 처리 구현
1 parent e838e59 commit 303e399

File tree

4 files changed

+71
-23
lines changed

4 files changed

+71
-23
lines changed

.github/workflows/RandomReviewer.yml

+28-23
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,50 @@ on:
88
- jay-so
99
- iey704
1010
- yerimming
11-
- makeUgreat
1211
- jiminseong
1312

1413
jobs:
15-
random-reviwer:
14+
random-reviewer:
1615
runs-on: ubuntu-latest
1716
steps:
18-
- id: random_reviwer
17+
- id: random_reviewer
1918
uses: actions/github-script@v5
2019
with:
21-
github-token: ${{ secrets.GITHUB_TOKEN }}
20+
github-token: ${{ secrets.GITHUB_TOKEN }}
2221
script: |
23-
const candidate = ['jay-so', 'iey704', 'yerimming', 'makeUgreat', 'jiminseong'];
24-
const randomReviewer = candidate[Math.floor(Math.random() * candidate.length)];
25-
const comment = `@${randomReviewer} 님 랜덤 리뷰어로 당첨되셨습니다! 리뷰를 부탁드립니다. 🙏`;
22+
const candidates = ['jay-so', 'iey704', 'yerimming', 'jiminseong'];
23+
const author = context.payload.pull_request.user.login;
24+
25+
// 작성자를 제외한 후보자 목록 만들기
26+
const filteredCandidates = candidates.filter(candidate => candidate !== author);
27+
28+
// 랜덤 리뷰어 선택 (3명)
29+
const randomReviewers = filteredCandidates.sort(() => Math.random() - 0.5).slice(0, 3);
30+
31+
// 메시지 생성
32+
const comment = `@${randomReviewers.join(' @')} 님 랜덤 리뷰어로 당첨되셨습니다! 리뷰를 부탁드립니다. 🙏`;
2633
console.log(`Comment: ${comment}`);
27-
return { comment, reviewer: randomReviewer };
2834
29-
- name: comment PR
35+
core.setOutput('comment', comment);
36+
core.setOutput('reviewers', randomReviewers.join(','));
37+
38+
- name: Comment PR
3039
uses: actions/github-script@v5
3140
with:
3241
github-token: ${{ secrets.GITHUB_TOKEN }}
3342
script: |
34-
const reviewer = '${{ steps.random_reviwer.outputs.reviewer }}';
35-
const comment = `@${reviewer} 님 랜덤 리뷰어로 당첨되셨습니다! 리뷰를 부탁드립니다. 🙏`;
36-
37-
const { owner, repo } = context.repo;
38-
const issue_number = context.issue.number;
39-
43+
const reviewers = '${{ steps.random_reviewer.outputs.reviewers }}'.split(',');
44+
const comment = `@${reviewers.join(' @')} 님 랜덤 리뷰어로 당첨되셨습니다! 리뷰를 부탁드립니다. 🙏`;
45+
4046
await github.rest.issues.createComment({
41-
owner,
42-
repo,
43-
issue_number,
44-
body: comment
47+
owner: context.repo.owner,
48+
repo: context.repo.repo,
49+
issue_number: context.issue.number,
50+
body: comment,
4551
});
4652
47-
- name: Add Pull Request Reviewer
53+
- name: Add Pull Request Reviewers
4854
uses: madrapps/add-reviewers@v1
4955
with:
50-
reviewers: ${{ steps.random_reviwer.outputs.reviewer }}
51-
token: ${{ secrets.GITHUB_TOKEN }}
52-
56+
reviewers: ${{ steps.random_reviewer.outputs.reviewers }}
57+
token: ${{ secrets.GITHUB_TOKEN }}

src/constant/messages.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { USER_COMMAND, GAME_CONDITION } from './conditions.mjs';
33
const MESSAGES = Object.freeze({
44
startGuide: `숫자 야구 게임을 시작합니다.`,
55
inputGuide: `숫자를 입력해주세요 : `,
6+
inputError: `[Error] 잘못된 숫자 형식입니다.`,
67
endGuide: `${GAME_CONDITION.maxLength}개의 숫자를 모두 맞히셨습니다! 게임종료`,
78
replayGuide: `게임을 새로 시작하려면 ${USER_COMMAND.replay}, 종료하려면 ${USER_COMMAND.end}를 입력하세요.\n`,
89
});

src/model/InputValidator.mjs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { USER_COMMAND, GAME_CONDITION } from '../constant/conditions.mjs';
2+
3+
const InputValidator = {
4+
/**
5+
* 유효범위의 중복이 없는 3자리 숫자가 입력되었는지 검증
6+
* @param {string} input //검증할 값
7+
* @return {boolean} //입력값이 유효한 경우 true, 그렇지 않을 경우 false를 반환
8+
*/
9+
10+
hasValidNumber(input) {
11+
const numberRegExp = /^\d+$/;
12+
const isNumeric = numberRegExp.test(input);
13+
const isValidLength = input.length === GAME_CONDITION.maxLength;
14+
const isValidUnique = new Set(input).size === GAME_CONDITION.maxLength;
15+
const isValidRange = !input.includes(GAME_CONDITION.limitNumber);
16+
17+
return isNumeric && isValidLength && isValidUnique && isValidRange;
18+
},
19+
20+
/**
21+
* 재시작 또는 종료 명령어와 일치하는지 검증
22+
* @param {string} input //검증할 입력값
23+
* @return {boolean} //입력값이 유효한 경우 true, 그렇지 않을 경우 false를 반환
24+
*/
25+
hasValidCommand(input) {
26+
const commandInput = parseInt(input, 10);
27+
28+
return (
29+
commandInput === USER_COMMAND.replay || commandInput === USER_COMMAND.end
30+
);
31+
},
32+
};
33+
34+
export default InputValidator;

src/view/InputView.mjs

+8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
import { Console } from '@woowacourse/mission-utils';
2+
import { MESSAGES } from '../constant/messages.mjs';
3+
import InputValidator from '../model/InputValidator.mjs';
24

35
const InputView = {
46
async getUserNumber(message) {
57
const userNumber = await Console.readLineAsync(message);
68

9+
if (!InputValidator.hasValidNumber(userNumber))
10+
throw new Error(MESSAGES.inputError);
11+
712
return parseInt(userNumber, 10);
813
},
914

1015
async getUserCommand(message) {
1116
const userCommand = await Console.readLineAsync(message);
1217

18+
if (!InputValidator.hasValidCommand(userCommand))
19+
throw new Error(MESSAGES.inputError);
20+
1321
return parseInt(userCommand, 10);
1422
},
1523
};

0 commit comments

Comments
 (0)