test pr sync #486
Workflow file for this run
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: 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); | |
| } | |
| } |