Skip to content

Commit 840123e

Browse files
Enhance auto-labeling workflow for issues and PRs
Updated workflow to apply labels for both issues and PRs, including those from forks.
1 parent 28a8286 commit 840123e

File tree

1 file changed

+52
-8
lines changed

1 file changed

+52
-8
lines changed

.github/workflows/labeler.yml

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,65 @@
11
name: 🏷️ Auto Label Issues & PRs
22

3+
# Use pull_request_target so labels can be applied for PRs from forks.
4+
# SECURITY: Do NOT check out or execute untrusted PR code here.
35
on:
4-
pull_request:
5-
types: [opened, reopened, synchronize]
66
issues:
7-
types: [opened, edited]
7+
types: [opened, reopened]
8+
pull_request_target:
9+
types: [opened, reopened, synchronize]
810

911
permissions:
10-
contents: read
1112
issues: write
1213
pull-requests: write
1314

1415
jobs:
15-
label:
16+
apply-labels:
1617
runs-on: ubuntu-latest
1718
steps:
18-
- name: 🏷️ Apply labels automatically
19-
uses: actions/labeler@v5
19+
- name: Determine and apply labels
20+
uses: actions/github-script@v7
2021
with:
21-
repo-token: ${{ secrets.GITHUB_TOKEN }}
22+
github-token: ${{ secrets.GITHUB_TOKEN }}
23+
script: |
24+
const repo = context.repo;
25+
26+
if (context.payload.issue) {
27+
const number = context.payload.issue.number;
28+
// Example: add triage label to new issues
29+
try {
30+
await github.rest.issues.addLabels({
31+
owner: repo.owner,
32+
repo: repo.repo,
33+
issue_number: number,
34+
labels: ["triage"]
35+
});
36+
} catch (err) {
37+
core && core.info && core.info("Could not add labels to issue: " + err.message);
38+
}
39+
return;
40+
}
41+
42+
if (context.payload.pull_request) {
43+
const pr = context.payload.pull_request;
44+
const prNumber = pr.number;
45+
const headRepoFull = pr.head && pr.head.repo && pr.head.repo.full_name ? pr.head.repo.full_name : '';
46+
const baseFull = `${repo.owner}/${repo.repo}`;
47+
const isFork = headRepoFull.toLowerCase() !== baseFull.toLowerCase();
48+
49+
const labels = ["needs-review"];
50+
if (isFork) labels.push("from-fork");
51+
52+
try {
53+
await github.rest.issues.addLabels({
54+
owner: repo.owner,
55+
repo: repo.repo,
56+
issue_number: prNumber,
57+
labels: labels
58+
});
59+
} catch (err) {
60+
core && core.info && core.info("Could not add labels to PR: " + err.message);
61+
}
62+
return;
63+
}
64+
65+
core && core.info && core.info("No issue or PR payload found; skipping labeling.");

0 commit comments

Comments
 (0)