Skip to content

Commit

Permalink
feat: API 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
Tekiter committed Nov 17, 2023
1 parent a19268e commit e72b21a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
26 changes: 14 additions & 12 deletions .github/actions/randomReviewer.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/');
const prNumber = process.env.PR_NUMBER;
const token = process.env.GITHUB_TOKEN;
const currentUser = process.env.CURRENT_USER.trim();

const reviewers = ['juno7803', 'Tekiter', 'NamJwong', 'seojisoosoo'].filter((reviewer) => reviewer !== currentUser);
const reviewers = ['juno7803', 'Tekiter', 'NamJwong', 'seojisoosoo'];

const headers = {
'Authorization': `Bearer ${token}`,
'Accept': 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
};

const getCurrentReviewers = async () => {
console.log(`https://api.github.com/repos/${owner}/${repo}/pulls/${prNumber}/requested_reviewers`);

const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/pulls/${prNumber}/requested_reviewers`, {
const getPullRequest = async () => {
const response = await fetch(`https://api.github.com/repos/repos/${owner}/${repo}/pulls/${prNumber}`, {
method: 'GET',
headers,
});

if (!response.ok) {
console.error(await response.text());
throw new Error('기존 PR의 reviewer 읽어오는데 실패했어요.');
throw new Error('PR 정보를 읽어오는데 실패했어요.');
}

const { users } = await response.json();

return users;
return await response.json();
};

const setReviewers = async (reviewers) => {
Expand All @@ -48,7 +43,14 @@ const setReviewers = async (reviewers) => {
};

async function main() {
const existingReviewers = await getCurrentReviewers();
const {
requested_reviewers,
user: { login: prUser },
} = await getPullRequest();

console.log('Setting for repo:', owner, repo, ', PR:', prNumber);

const existingReviewers = requested_reviewers.map((reviewer) => reviewer.login);

if (existingReviewers > 0) {
console.log('이미 리뷰어가 지정되어 있으므로 스킵할게요.');
Expand All @@ -57,7 +59,7 @@ async function main() {

const pickReviewer = () => {
const picked = reviewers[Math.floor(Math.random() * reviewers.length)];
if (picked === currentUser) {
if (picked === prUser) {
return pickReviewer();
}

Expand Down
8 changes: 6 additions & 2 deletions .github/workflows/assign-reviewer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ jobs:
with:
node-version: 20

- uses: jwalton/gh-find-current-pr@v1
id: findPr
with:
state: open

- name: Assign Reviewer
run: node .github/actions/randomReviewer.js
if: success() && steps.findPr.outputs.number
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_OWNER: ${{ github.repository_owner }}
GITHUB_REPOSITORY: ${{ github.repository }}
PR_NUMBER: ${{ github.event.pull_request.number }}
CURRENT_USER: ${{ github.actor }}

0 comments on commit e72b21a

Please sign in to comment.