11name : Auto Label PR from Linked Issue
22
33on :
4- pull_request_target :
4+ pull_request_target :
55 types : [opened, edited, synchronize, reopened]
66
77permissions :
@@ -12,11 +12,8 @@ permissions:
1212jobs :
1313 label-pr :
1414 runs-on : ubuntu-latest
15-
16- steps :
17- - name : Checkout code
18- uses : actions/checkout@v4
1915
16+ steps :
2017 - name : Extract Issue Numbers from PR Body
2118 id : extract-issues
2219 uses : actions/github-script@v7
@@ -26,31 +23,19 @@ jobs:
2623 script : |
2724 const prBody = context.payload.pull_request.body || '';
2825 const prTitle = context.payload.pull_request.title || '';
29-
30- // Regex patterns to find linked issues
31- // Matches: #123, fixes #123, closes #123, resolves #123, etc.
3226 const patterns = [
3327 /(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?)\s+#(\d+)/gi,
3428 /#(\d+)/g
3529 ];
36-
3730 const issueNumbers = new Set();
38-
39- // Search in PR body and title
4031 const textToSearch = prBody + ' ' + prTitle;
41-
4232 patterns.forEach(pattern => {
4333 const matches = [...textToSearch.matchAll(pattern)];
4434 matches.forEach(match => {
45- issueNumbers.add(match[1]);
35+ if (/^\d+$/.test(match[1])) issueNumbers.add(match[1]);
4636 });
4737 });
48-
49- const issues = Array.from(issueNumbers);
50- console.log('Found linked issues:', issues);
51-
52- // Return as JSON string
53- return JSON.stringify(issues);
38+ return JSON.stringify([...issueNumbers]);
5439
5540 - name : Get Labels from Linked Issues
5641 id : get-labels
@@ -60,75 +45,42 @@ jobs:
6045 result-encoding : string
6146 script : |
6247 const issueNumbers = JSON.parse('${{ steps.extract-issues.outputs.result }}');
63-
64- // Labels to exclude from being applied to PRs
6548 const excludedLabels = ['recode', 'hacktoberfest-accepted'];
66-
67- if (!issueNumbers || issueNumbers.length === 0) {
68- console.log('No linked issues found');
69- return JSON.stringify([]);
70- }
71-
7249 const allLabels = new Set();
73-
50+
7451 for (const issueNumber of issueNumbers) {
7552 try {
7653 const issue = await github.rest.issues.get({
7754 owner: context.repo.owner,
7855 repo: context.repo.repo,
7956 issue_number: parseInt(issueNumber)
8057 });
81-
82- console.log(`Issue #${issueNumber} labels:`, issue.data.labels.map(l => l.name));
83-
84- issue.data.labels.forEach(label => {
85- // Only add label if it's not in the excluded list
86- if (!excludedLabels.includes(label.name.toLowerCase())) {
58+ for (const label of issue.data.labels) {
59+ if (!excludedLabels.includes(label.name.toLowerCase()))
8760 allLabels.add(label.name);
88- } else {
89- console.log(`Excluding label: ${label.name}`);
90- }
91- });
61+ }
9262 } catch (error) {
9363 console.log(`Could not fetch issue #${issueNumber}:`, error.message);
9464 }
9565 }
96-
97- const labels = Array.from(allLabels);
98- console.log('All labels to apply:', labels);
99-
100- return JSON.stringify(labels);
66+
67+ return JSON.stringify([...allLabels]);
10168
10269 - name : Apply Labels to PR
10370 uses : actions/github-script@v7
10471 with :
10572 github-token : ${{ secrets.GITHUB_TOKEN }}
10673 script : |
107- const labels = ${{ steps.get-labels.outputs.result }};
108-
109- if (!labels || labels.length === 0) {
74+ const labels = JSON.parse(' ${{ steps.get-labels.outputs.result }}')
75+ .filter(l => typeof l === 'string' && l.trim() !== '');
76+ if (labels.length === 0) {
11077 console.log('No labels to apply');
11178 return;
11279 }
113-
114- try {
115- await github.rest.issues.addLabels({
116- owner: context.repo.owner,
117- repo: context.repo.repo,
118- issue_number: context.payload.pull_request.number,
119- labels: labels
120- });
121-
122- console.log(`Successfully applied ${labels.length} labels to PR #${context.payload.pull_request.number}`);
123-
124- // Add a comment to the PR
125- await github.rest.issues.createComment({
126- owner: context.repo.owner,
127- repo: context.repo.repo,
128- issue_number: context.payload.pull_request.number,
129- body: `🏷️ Labels automatically applied from linked issue(s): ${labels.map(l => `\`${l}\``).join(', ')}`
130- });
131- } catch (error) {
132- console.error('Error applying labels:', error.message);
133- core.setFailed(`Failed to apply labels: ${error.message}`);
134- }
80+ await github.rest.issues.addLabels({
81+ owner: context.repo.owner,
82+ repo: context.repo.repo,
83+ issue_number: context.payload.pull_request.number,
84+ labels
85+ });
86+ console.log(`Applied labels: ${labels.join(', ')}`);
0 commit comments