Skip to content

Commit 59eadb3

Browse files
committed
fix(ci): safely sync issue metadata to PR and fix JSON parsing
- Stop direct interpolation of workflow outputs into github-script to avoid malformed JSON errors. - Pass issues and PR number via environment variables and parse from process.env. - Add defensive JSON parsing with cleaning for extra quotes/newlines. - Fix typo in PR comment: use issue.milestone correctly. - Use addLabels() instead of setLabels() to prevent race conditions and preserve existing PR labels. - Sync milestones and add a comment summarizing the synchronized metadata.
1 parent ee8f3ce commit 59eadb3

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

.github/workflows/autolabel-pr-issue.yml

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ jobs:
3030
const prTitle = context.payload.pull_request.title || '';
3131
const prBody = context.payload.pull_request.body || '';
3232
33+
// Regex patterns to find linked issues
3334
const patterns = [
3435
/(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?)\s+#(\d+)/gi,
3536
/#(\d+)/g
@@ -51,21 +52,33 @@ jobs:
5152
- name: Sync Issue Metadata to PR
5253
if: steps.extract-issues.outputs.issues != '' && steps.extract-issues.outputs.issues != '[]'
5354
uses: actions/github-script@v7
55+
env:
56+
ISSUES_JSON: ${{ steps.extract-issues.outputs.issues }}
57+
PR_NUMBER: ${{ steps.extract-issues.outputs.pr }}
5458
with:
5559
github-token: ${{ secrets.GITHUB_TOKEN }}
5660
script: |
57-
const issuesOutput = '${{ steps.extract-issues.outputs.issues }}';
58-
const prNumber = parseInt('${{ steps.extract-issues.outputs.pr }}');
61+
// Read inputs from environment variables to avoid YAML quoting issues
62+
const issuesOutput = process.env.ISSUES_JSON || '[]';
63+
const prNumber = parseInt(process.env.PR_NUMBER, 10);
5964
6065
let issues = [];
6166
try {
6267
issues = JSON.parse(issuesOutput);
6368
} catch (e) {
64-
console.error("Failed to parse issues:", issuesOutput);
69+
// Clean extra quotes/newlines
70+
const cleaned = issuesOutput.replace(/^\s*['"]?/, '').replace(/['"]?\s*$/, '');
71+
try {
72+
issues = JSON.parse(cleaned);
73+
} catch (err) {
74+
console.error('Failed to parse issues output:', err.message);
75+
console.error('Raw issues output:', JSON.stringify(issuesOutput));
76+
return;
77+
}
6578
}
6679
67-
if (!issues || issues.length === 0) {
68-
console.log("No linked issues found");
80+
if (!Array.isArray(issues) || issues.length === 0) {
81+
console.log('No linked issues found');
6982
return;
7083
}
7184
@@ -75,13 +88,13 @@ jobs:
7588
const { data: issue } = await github.rest.issues.get({
7689
owner: context.repo.owner,
7790
repo: context.repo.repo,
78-
issue_number: parseInt(issueNumber)
91+
issue_number: parseInt(issueNumber, 10)
7992
});
8093
8194
console.log(`Syncing metadata from Issue #${issueNumber} to PR #${prNumber}`);
8295
8396
// --- Sync Labels safely using addLabels ---
84-
const issueLabels = issue.labels.map(l => l.name);
97+
const issueLabels = Array.isArray(issue.labels) ? issue.labels.map(l => l.name) : [];
8598
if (issueLabels.length > 0) {
8699
await github.rest.issues.addLabels({
87100
owner: context.repo.owner,
@@ -112,6 +125,6 @@ jobs:
112125
});
113126
114127
} catch (error) {
115-
console.error(`Error syncing issue #${issueNumber} to PR #${prNumber}:`, error.message);
128+
console.error(`Error syncing issue #${issueNumber} to PR #${prNumber}:`, error);
116129
}
117130
}

0 commit comments

Comments
 (0)