Skip to content

Commit b947b1c

Browse files
committed
fix: resolve JSON double-encoding in sync metadata workflow
1 parent 700cf93 commit b947b1c

File tree

1 file changed

+21
-36
lines changed

1 file changed

+21
-36
lines changed

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

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,17 @@ jobs:
4343
}
4444
}
4545
46-
return JSON.stringify({ issues: Array.from(issueNumbers), pr: prNumber });
46+
core.setOutput('issues', JSON.stringify(Array.from(issueNumbers)));
47+
core.setOutput('pr', prNumber.toString());
4748
4849
- name: Sync Issue Metadata to PR
50+
if: steps.extract-issues.outputs.issues != '[]'
4951
uses: actions/github-script@v7
5052
with:
5153
github-token: ${{ secrets.GITHUB_TOKEN }}
5254
script: |
53-
const data = JSON.parse('${{ steps.extract-issues.outputs.result }}');
54-
const prNumber = data.pr;
55-
const issueNumbers = data.issues || [];
55+
const issueNumbers = JSON.parse('${{ steps.extract-issues.outputs.issues }}');
56+
const prNumber = parseInt('${{ steps.extract-issues.outputs.pr }}');
5657
5758
if (issueNumbers.length === 0) {
5859
console.log("No linked issues found");
@@ -67,9 +68,9 @@ jobs:
6768
repo: context.repo.repo,
6869
issue_number: parseInt(issueNumber)
6970
});
70-
71+
7172
console.log(`Syncing metadata from Issue #${issueNumber} to PR #${prNumber}`);
72-
73+
7374
// --- Sync Labels ---
7475
const issueLabels = issue.labels.map(l => l.name);
7576
const { data: pr } = await github.rest.pulls.get({
@@ -79,15 +80,17 @@ jobs:
7980
});
8081
const currentPRLabels = pr.labels.map(l => l.name);
8182
const combinedLabels = Array.from(new Set([...currentPRLabels, ...issueLabels]));
82-
83-
await github.rest.issues.addLabels({
84-
owner: context.repo.owner,
85-
repo: context.repo.repo,
86-
issue_number: prNumber,
87-
labels: combinedLabels
88-
});
89-
console.log(`Labels applied: ${combinedLabels.join(', ')}`);
90-
83+
84+
if (combinedLabels.length > 0) {
85+
await github.rest.issues.setLabels({
86+
owner: context.repo.owner,
87+
repo: context.repo.repo,
88+
issue_number: prNumber,
89+
labels: combinedLabels
90+
});
91+
console.log(`Labels applied: ${combinedLabels.join(', ')}`);
92+
}
93+
9194
// --- Sync Milestone ---
9295
if (issue.milestone) {
9396
await github.rest.issues.update({
@@ -98,32 +101,14 @@ jobs:
98101
});
99102
console.log(`Milestone synced: ${issue.milestone.title}`);
100103
}
101-
102-
// --- Sync Projects (GitHub Projects v2) ---
103-
if(issue.project_cards_url) {
104-
// Fetch project cards of issue
105-
const cardsResponse = await github.rest.projects.listCards({
106-
column_id: issue.project_cards_url.split('/').pop() // last part is column_id
107-
}).catch(()=>({data:[]}));
108-
109-
for(const card of cardsResponse.data || []) {
110-
await github.rest.projects.createCard({
111-
column_id: card.column_id,
112-
content_id: prNumber,
113-
content_type: 'PullRequest'
114-
});
115-
console.log(`Added PR #${prNumber} to project card in column ${card.column_id}`);
116-
}
117-
}
118-
119-
// --- Optionally: Add a comment on PR ---
104+
105+
// --- Add a comment on PR ---
120106
await github.rest.issues.createComment({
121107
owner: context.repo.owner,
122108
repo: context.repo.repo,
123109
issue_number: prNumber,
124-
body: `✅ Synchronized metadata from Issue #${issueNumber}:\nLabels: ${issueLabels.join(', ')}\nMilestone: ${issue.milestone ? issue.milestone.title : 'None'}`
110+
body: `✅ Synchronized metadata from Issue #${issueNumber}:\n- Labels: ${issueLabels.length > 0 ? issueLabels.join(', ') : 'None'}\n- Milestone: ${issue.milestone ? issue.milestone.title : 'None'}`
125111
});
126-
127112
} catch (error) {
128113
console.error(`Error syncing issue #${issueNumber} to PR #${prNumber}:`, error.message);
129114
}

0 commit comments

Comments
 (0)