-
Notifications
You must be signed in to change notification settings - Fork 137
88 lines (75 loc) · 2.9 KB
/
pr-issue-sync.yml
File metadata and controls
88 lines (75 loc) · 2.9 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
- name: Sync Issue Metadata to PR
uses: actions/github-script@v7
env:
RESULT_JSON: ${{ steps.extract-issues.outputs.result }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const core = require('@actions/core');
// Safely parse JSON from previous step
const raw = process.env.RESULT_JSON;
console.log("Raw JSON output:", raw);
let data;
try {
data = JSON.parse(raw);
// Handle case where the JSON is double-stringified
if (typeof data === "string") {
data = JSON.parse(data);
}
} catch (err) {
core.setFailed(`Invalid JSON from extract-issues: ${err.message}\nRaw: ${raw}`);
return;
}
console.log("Parsed data:", data);
const prNumber = data.pr;
const issueNumbers = data.issues || [];
if (!issueNumbers.length) {
console.log("No linked issues found.");
return;
}
for (const issueNumber of issueNumbers) {
try {
// Fetch issue
const { data: issue } = await github.rest.issues.get({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: parseInt(issueNumber)
});
console.log(`Syncing metadata from Issue #${issueNumber} to PR #${prNumber}`);
// --- Sync Labels ---
const issueLabels = issue.labels.map(l => l.name);
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
const currentPRLabels = pr.labels.map(l => l.name);
const combinedLabels = Array.from(new Set([...currentPRLabels, ...issueLabels]));
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
labels: combinedLabels
});
console.log(`Labels applied: ${combinedLabels.join(', ')}`);
// --- Sync Milestone ---
if (issue.milestone) {
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
milestone: issue.milestone.number
});
console.log(`Milestone synced: ${issue.milestone.title}`);
}
// --- Optional: Add a comment on PR ---
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: `✅ Synchronized metadata from Issue #${issueNumber}:\nLabels: ${issueLabels.join(', ')}\nMilestone: ${issue.milestone ? issue.milestone.title : 'None'}`
});
} catch (error) {
console.error(`Error syncing issue #${issueNumber} to PR #${prNumber}:`, error.message);
}
}