[Week15] PGS 1939: 중량제한 #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Auto Fill PR from Weekly Issue | |
| on: | |
| pull_request: | |
| types: [opened, edited] | |
| jobs: | |
| fill-pr-info: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| issues: read | |
| pull-requests: write | |
| contents: read | |
| steps: | |
| - name: Extract info and Update PR | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const pr = context.payload.pull_request; | |
| const title = pr.title; | |
| let body = pr.body || ""; | |
| // 1. 제목에서 주차와 문제번호 추출 | |
| const weekMatch = title.match(/Week\s*(\d+)/i); | |
| const probNumMatch = title.match(/\d{4,}/); | |
| if (!weekMatch || !probNumMatch) { | |
| console.log("주차(Week) 또는 문제번호를 제목에서 찾을 수 없습니다."); | |
| return; | |
| } | |
| const targetWeek = `Week ${weekMatch[1]}`; | |
| const targetProbNum = probNumMatch[0]; | |
| // 2. 해당 주차 이슈 찾기 | |
| const issues = await github.rest.issues.listForRepo({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| state: 'open' | |
| }); | |
| const targetIssue = issues.data.find(issue => issue.title.includes(targetWeek)); | |
| if (!targetIssue) { | |
| console.log(`${targetWeek} 이슈를 찾지 못했습니다.`); | |
| return; | |
| } | |
| // 3. 이슈에서 문제 섹션 추출 | |
| const sections = targetIssue.body.split(/문제 \d/); | |
| const targetSection = sections.find(s => s.includes(targetProbNum)); | |
| if (!targetSection) { | |
| console.log(`이슈 내에서 ${targetProbNum}번 정보를 찾지 못했습니다.`); | |
| return; | |
| } | |
| // 4. 정보 추출 | |
| const platform = targetSection.match(/플랫폼:\s*(.+)/)?.[1]?.trim() || ""; | |
| const probName = targetSection.match(/문제 이름:\s*(.+)/)?.[1]?.trim() || ""; | |
| const probLink = targetSection.match(/링크:\s*(https?:\/\/[^\s]+)/)?.[1]?.trim() || ""; | |
| const difficulty = targetSection.match(/난이도:\s*(.+)/)?.[1]?.trim() || ""; | |
| // 5. PR 템플릿의 빈 칸 채우기 (정규표현식 치환) | |
| // 템플릿에 있는 기본 문구들을 이슈에서 가져온 실제 데이터로 교체합니다. | |
| let newBody = body | |
| .replace(/- \*\*플랫폼\*\*:\s*.*$/m, `- **플랫폼**: ${platform}`) | |
| .replace(/- \*\*문제 번호\*\*:\s*.*$/m, `- **문제 번호**: ${targetProbNum}`) | |
| .replace(/- \*\*문제 이름\*\*:\s*.*$/m, `- **문제 이름**: ${probName}`) | |
| .replace(/- \*\*문제 링크\*\*:\s*.*$/m, `- **문제 링크**: ${probLink}`) | |
| .replace(/- \*\*난이도\*\*:\s*.*$/m, `- **난이도**: ${difficulty}`); | |
| // 6. 변경사항이 있을 때만 업데이트 | |
| if (newBody !== body) { | |
| await github.rest.pulls.update({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: pr.number, | |
| body: newBody | |
| }); | |
| console.log("PR 본문이 성공적으로 업데이트되었습니다."); | |
| } | |
| // 7. 라벨 추가 | |
| const labels = []; | |
| if (platform.includes("프로그래머스") || title.includes("PGS")) labels.push("프로그래머스"); | |
| if (platform.includes("백준") || title.includes("BOJ")) labels.push("백준"); | |
| if (labels.length > 0) { | |
| await github.rest.issues.addLabels({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: pr.number, | |
| labels: labels | |
| }); | |
| } |