-
Notifications
You must be signed in to change notification settings - Fork 1
108 lines (91 loc) · 4.65 KB
/
auto-pr-fill.yml
File metadata and controls
108 lines (91 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
name: Auto Fill PR from Weekly Issue
on:
pull_request:
types: [opened, edited, synchronized]
jobs:
fill-pr-info:
runs-on: ubuntu-latest
permissions:
issues: read
pull-requests: write
contents: read
steps:
- name: Update PR with Issue Info
uses: actions/github-script@v7
with:
script: |
const pr = context.payload.pull_request;
const title = pr.title;
let body = pr.body || "";
console.log(`[분석 시작] PR 제목: ${title}`);
// 1. PR 제목에서 주차 숫자와 문제 번호 추출
const weekMatch = title.match(/Week\s*(\d+)/i);
const probNumMatch = title.match(/\d{4,}/);
if (!weekMatch || !probNumMatch) {
console.log("❌ 제목에서 주차(Week) 또는 문제번호를 추출하지 못했습니다.");
return;
}
const weekNum = weekMatch[1];
const targetProbNum = probNumMatch[0];
console.log(`[추출 성공] 주차: ${weekNum}, 문제번호: ${targetProbNum}`);
// 2. 이슈 검색 (상태 상관없이 최근 100개 스캔)
const issues = await github.rest.issues.listForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'all',
per_page: 100
});
// 3. 이슈 제목 매칭 ([Week15] 등 대괄호와 공백 무시)
const targetIssue = issues.data.find(i => {
if (i.pull_request) return false;
const cleanTitle = i.title.replace(/[\s\[\]]/g, '').toLowerCase();
return cleanTitle.includes(`week${weekNum}`);
});
if (!targetIssue) {
console.log(`❌ [Week${weekNum}] 이슈를 찾지 못했습니다.`);
return;
}
console.log(`✅ 이슈 발견: #${targetIssue.number} (${targetIssue.title})`);
// 4. 이슈 본문에서 해당 문제 섹션 찾기
const issueBody = targetIssue.body || "";
// '#### 문제 1' 또는 '문제 1' 패턴으로 섹션 분할
const sections = issueBody.split(/####\s*문제\s*\d+|문제\s*\d+/);
const targetSection = sections.find(s => s.includes(targetProbNum));
if (!targetSection) {
console.log(`❌ 이슈 본문에서 ${targetProbNum}번 문제 정보를 찾지 못했습니다.`);
return;
}
// 5. 상세 정보 추출 함수 (마크다운 볼드체 ** 기호 대응)
const getVal = (key) => {
// '**키워드**: 값' 형태에서 값을 추출 (볼드 기호가 있어도 작동)
const regex = new RegExp(`(?:\\*\\*|)${key}(?:\\*\\*|)\\s*:\\s*([^\\r\\n]+)`, 'i');
const match = targetSection.match(regex);
return match ? match[1].trim() : "정보 없음";
};
const platform = getVal("플랫폼");
const probName = getVal("문제 이름");
const difficulty = getVal("난이도");
// 링크는 별도로 URL 패턴으로 추출
const probLinkMatch = targetSection.match(/(?:https?:\/\/[^\s\r\n]+)/);
const probLink = probLinkMatch ? probLinkMatch[0].trim() : "정보 없음";
console.log(`[데이터 추출] 플랫폼: ${platform}, 이름: ${probName}, 난이도: ${difficulty}`);
// 6. PR 본문 업데이트 (기존 템플릿의 빈 항목을 치환)
// 정규표현식을 사용하여 해당 라인 전체를 교체합니다.
let newBody = body
.replace(/- \*\*플랫폼\*\*:.*$/m, `- **플랫폼**: ${platform}`)
.replace(/- \*\*문제 번호\*\*:.*$/m, `- **문제 번호**: ${targetProbNum}`)
.replace(/- \*\*문제 이름\*\*:.*$/m, `- **문제 이름**: ${probName}`)
.replace(/- \*\*문제 링크\*\*:.*$/m, `- **문제 링크**: ${probLink}`)
.replace(/- \*\*난이도\*\*:.*$/m, `- **난이도**: ${difficulty}`);
// 변경사항이 있을 때만 업데이트 실행
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 본문이 성공적으로 업데이트되었습니다.");
} else {
console.log("⚠️ 변경할 내용이 없거나 이미 정보가 채워져 있습니다.");
}