Skip to content

Commit 4933531

Browse files
authored
Merge pull request #984 from sheikhlimon/test-pr-sync-workflow
test pr sync
2 parents 966859f + 5160334 commit 4933531

File tree

1 file changed

+29
-43
lines changed

1 file changed

+29
-43
lines changed

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

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: Sync Issue Metadata to PR
22

33
on:
4-
pull_request_target:
4+
pull_request: # Changed from pull_request_target for testing
55
types: [opened, edited, synchronize, reopened]
66

77
permissions:
@@ -27,38 +27,40 @@ jobs:
2727
const prNumber = context.payload.pull_request.number;
2828
const prTitle = context.payload.pull_request.title || '';
2929
const prBody = context.payload.pull_request.body || '';
30-
30+
3131
// Regex patterns for issue references
3232
const patterns = [
3333
/(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?)\s+#(\d+)/gi,
3434
/#(\d+)/g
3535
];
36-
36+
3737
const issueNumbers = new Set();
3838
const text = prTitle + ' ' + prBody;
39-
39+
4040
for (const pattern of patterns) {
4141
for (const match of text.matchAll(pattern)) {
4242
issueNumbers.add(match[1]);
4343
}
4444
}
45-
46-
return JSON.stringify({ issues: Array.from(issueNumbers), pr: prNumber });
45+
46+
// Use core.setOutput instead of return
47+
core.setOutput('issues', JSON.stringify(Array.from(issueNumbers)));
48+
core.setOutput('pr', prNumber.toString());
4749
4850
- name: Sync Issue Metadata to PR
51+
if: steps.extract-issues.outputs.issues != '[]'
4952
uses: actions/github-script@v7
5053
with:
5154
github-token: ${{ secrets.GITHUB_TOKEN }}
5255
script: |
53-
const data = JSON.parse('${{ steps.extract-issues.outputs.result }}');
54-
const prNumber = data.pr;
55-
const issueNumbers = data.issues || [];
56-
56+
const issueNumbers = JSON.parse('${{ steps.extract-issues.outputs.issues }}');
57+
const prNumber = parseInt('${{ steps.extract-issues.outputs.pr }}');
58+
5759
if (issueNumbers.length === 0) {
5860
console.log("No linked issues found");
5961
return;
6062
}
61-
63+
6264
for (const issueNumber of issueNumbers) {
6365
try {
6466
// Fetch issue
@@ -67,9 +69,9 @@ jobs:
6769
repo: context.repo.repo,
6870
issue_number: parseInt(issueNumber)
6971
});
70-
72+
7173
console.log(`Syncing metadata from Issue #${issueNumber} to PR #${prNumber}`);
72-
74+
7375
// --- Sync Labels ---
7476
const issueLabels = issue.labels.map(l => l.name);
7577
const { data: pr } = await github.rest.pulls.get({
@@ -79,15 +81,17 @@ jobs:
7981
});
8082
const currentPRLabels = pr.labels.map(l => l.name);
8183
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-
84+
85+
if (combinedLabels.length > 0) {
86+
await github.rest.issues.setLabels({
87+
owner: context.repo.owner,
88+
repo: context.repo.repo,
89+
issue_number: prNumber,
90+
labels: combinedLabels
91+
});
92+
console.log(`Labels applied: ${combinedLabels.join(', ')}`);
93+
}
94+
9195
// --- Sync Milestone ---
9296
if (issue.milestone) {
9397
await github.rest.issues.update({
@@ -98,32 +102,14 @@ jobs:
98102
});
99103
console.log(`Milestone synced: ${issue.milestone.title}`);
100104
}
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 ---
105+
106+
// --- Add a comment on PR ---
120107
await github.rest.issues.createComment({
121108
owner: context.repo.owner,
122109
repo: context.repo.repo,
123110
issue_number: prNumber,
124-
body: `✅ Synchronized metadata from Issue #${issueNumber}:\nLabels: ${issueLabels.join(', ')}\nMilestone: ${issue.milestone ? issue.milestone.title : 'None'}`
111+
body: `✅ Synchronized metadata from Issue #${issueNumber}:\n- Labels: ${issueLabels.length > 0 ? issueLabels.join(', ') : 'None'}\n- Milestone: ${issue.milestone ? issue.milestone.title : 'None'}`
125112
});
126-
127113
} catch (error) {
128114
console.error(`Error syncing issue #${issueNumber} to PR #${prNumber}:`, error.message);
129115
}

0 commit comments

Comments
 (0)