Skip to content

Commit efdf336

Browse files
Centralize auto-label mappings into configuration file
Signed-off-by: Sanketjadhav31 <sj546400@gmail.com>
1 parent b1fd11e commit efdf336

2 files changed

Lines changed: 118 additions & 70 deletions

File tree

.github/label-mappings.json

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"description": "Centralized label mappings for auto-labeling issues and pull requests based on title keywords",
3+
"version": "1.0.0",
4+
"mappings": {
5+
"issues": {
6+
"comment": "Note: Only labels that exist in the repository are included",
7+
"[infra]": "infrastructure",
8+
"[infrastructure]": "infrastructure",
9+
"[docs]": "documentation",
10+
"[documentation]": "documentation",
11+
"[dpdk]": "dpdk",
12+
"[bmv2]": "bmv2",
13+
"[ebpf]": "ebpf",
14+
"[p4testgen]": "p4tools",
15+
"[p4smith]": "p4tools",
16+
"[p4tools]": "p4tools",
17+
"[p4tc]": "p4tc",
18+
"[p4fmt]": "p4fmt",
19+
"[core]": "core",
20+
"[p4-spec]": "p4-spec",
21+
"[control-plane]": "control-plane",
22+
"[p4runtime]": "control-plane",
23+
"[frontend]": "core",
24+
"[midend]": "core",
25+
"[parser]": "core",
26+
"[python]": "python",
27+
"[pna-bmv2]": "pna-bmv2",
28+
"[psa-bmv2]": "psa-bmv2",
29+
"[ebpf-psa]": "ebpf-psa"
30+
},
31+
"pull_requests": {
32+
"comment": "Note: Only labels that exist in the repository are included",
33+
"[core]": "core",
34+
"[infra]": "infrastructure",
35+
"[infrastructure]": "infrastructure",
36+
"[midend]": "core",
37+
"[parser]": "core",
38+
"[dpdk]": "dpdk",
39+
"[p4-spec]": "p4-spec",
40+
"[bmv2]": "bmv2",
41+
"[docs]": "documentation",
42+
"[documentation]": "documentation",
43+
"[p4testgen]": "p4tools",
44+
"[p4smith]": "p4tools",
45+
"[p4tools]": "p4tools",
46+
"[control-plane]": "control-plane",
47+
"[p4runtime]": "control-plane",
48+
"[p4tc]": "p4tc",
49+
"[p4fmt]": "p4fmt",
50+
"[frontend]": "core",
51+
"[ebpf]": "ebpf",
52+
"[python]": "python",
53+
"[pna-bmv2]": "pna-bmv2",
54+
"[psa-bmv2]": "psa-bmv2",
55+
"[ebpf-psa]": "ebpf-psa"
56+
}
57+
},
58+
"notes": {
59+
"case_sensitivity": "All keyword matching is case-insensitive",
60+
"format": "Keywords should be in [brackets] format",
61+
"label_values": "Must match existing GitHub labels in the repository",
62+
"adding_labels": "To add new label mappings, first ensure the label exists in the repository, then add the mapping here",
63+
"excluded_labels": "The following keywords were mentioned in issue #5649 but labels don't exist: [bug], [feature]/[enhancement], [ubpf], [graphs], [tofino]. These can be added once the labels are created in the repository."
64+
}
65+
}

.github/workflows/auto-label.yml

Lines changed: 53 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -16,105 +16,88 @@ jobs:
1616
label:
1717
runs-on: ubuntu-latest
1818
steps:
19+
- name: Checkout repository
20+
uses: actions/checkout@v4
21+
with:
22+
sparse-checkout: |
23+
.github/label-mappings.json
24+
sparse-checkout-cone-mode: false
25+
1926
- name: Auto-label Issues and PRs
2027
uses: actions/github-script@v9
2128
with:
2229
github-token: ${{ secrets.GITHUB_TOKEN }}
2330
script: |
31+
const fs = require('fs');
32+
const path = require('path');
33+
34+
// Load label mappings from configuration file
35+
const configPath = path.join(process.env.GITHUB_WORKSPACE, '.github', 'label-mappings.json');
36+
let config;
37+
38+
try {
39+
const configContent = fs.readFileSync(configPath, 'utf8');
40+
config = JSON.parse(configContent);
41+
console.log('Successfully loaded label mappings configuration');
42+
} catch (error) {
43+
console.error('Failed to load label-mappings.json:', error.message);
44+
core.setFailed('Could not load label mappings configuration');
45+
return;
46+
}
47+
2448
// Determine if we're processing an issue or PR
25-
const isIssue = !!context.payload.issue;
49+
// Check PR first as pull_request_target events are more specific
2650
const isPR = !!context.payload.pull_request;
51+
const isIssue = !!context.payload.issue;
2752
2853
let itemToLabel;
2954
let itemNumber;
3055
31-
if (isIssue) {
32-
itemToLabel = context.payload.issue;
33-
itemNumber = context.issue.number;
34-
} else if (isPR) {
56+
if (isPR) {
3557
itemToLabel = context.payload.pull_request;
3658
itemNumber = context.payload.pull_request.number;
59+
} else if (isIssue) {
60+
itemToLabel = context.payload.issue;
61+
itemNumber = context.issue.number;
3762
} else {
3863
console.log("Not an issue or PR event, exiting");
3964
return;
4065
}
4166
4267
const labelsToAdd = [];
4368
44-
// Define label mappings based on brackets in title
45-
let labelMappings;
69+
// Get appropriate label mappings from config based on item type
70+
const labelMappings = isPR ? config.mappings.pull_requests : config.mappings.issues;
4671
47-
if (isIssue) {
48-
// Issue label mappings (original set)
49-
labelMappings = {
50-
"[bug]": "bug",
51-
"[infra]": "infrastructure",
52-
"[feature]": "enhancement",
53-
"[docs]": "documentation",
54-
"[dpdk]": "dpdk",
55-
"[bmv2]": "bmv2",
56-
"[P4Testgen]": "p4tools",
57-
"[P4Smith]": "p4tools",
58-
"[P4tools]": "p4tools",
59-
"[Tofino]":"tofino",
60-
"[p4tc]": "p4tc",
61-
"[p4fmt]": "p4fmt",
62-
"[core]":"core",
63-
"[p4-spec]": "p4-spec",
64-
"[control-plane]": "control-plane",
65-
"[P4runtime]":"control-plane",
66-
"[frontend]":"core",
67-
"[midend]":"core",
68-
"[parser]":"core",
69-
};
70-
} else {
71-
// PR label mappings (only the ones you want for PRs)
72-
labelMappings = {
73-
"[core]":"core",
74-
"[infra]": "infrastructure",
75-
"[midend]":"core",
76-
"[parser]":"core",
77-
"[dpdk]": "dpdk",
78-
"[p4-spec]": "p4-spec",
79-
"[tofino]":"tofino",
80-
"[bmv2]":"bmv2",
81-
"[docs]":"documentation",
82-
"[documentation]":"documentation",
83-
"[P4Testgen]": "p4tools",
84-
"[P4Smith]": "p4tools",
85-
"[P4tools]": "p4tools",
86-
"[control-plane]": "control-plane",
87-
"[P4runtime]":"control-plane",
88-
"[p4tc]": "p4tc",
89-
"[p4fmt]": "p4fmt",
90-
// Add any other PR-specific labels here
91-
};
72+
if (!labelMappings) {
73+
console.error(`No label mappings found for ${isIssue ? 'issues' : 'pull_requests'}`);
74+
return;
9275
}
9376
94-
// Check if title contains bracketed keywords
77+
// Check if title contains bracketed keywords (case-insensitive)
9578
const title = itemToLabel.title.toLowerCase();
79+
9680
for (const [keyword, label] of Object.entries(labelMappings)) {
9781
if (title.includes(keyword.toLowerCase())) {
98-
labelsToAdd.push(label);
82+
if (!labelsToAdd.includes(label)) {
83+
labelsToAdd.push(label);
84+
}
85+
console.log(`Matched keyword "${keyword}" -> label "${label}"`);
9986
}
10087
}
10188
10289
if (labelsToAdd.length > 0) {
103-
if (isIssue) {
104-
await github.rest.issues.addLabels({
105-
issue_number: itemNumber,
106-
owner: context.repo.owner,
107-
repo: context.repo.repo,
108-
labels: labelsToAdd
109-
});
110-
} else {
111-
// PRs use the issues API for labels
112-
await github.rest.issues.addLabels({
113-
issue_number: itemNumber,
114-
owner: context.repo.owner,
115-
repo: context.repo.repo,
116-
labels: labelsToAdd
117-
});
118-
}
90+
console.log(`Adding labels: ${labelsToAdd.join(', ')}`);
91+
92+
await github.rest.issues.addLabels({
93+
issue_number: itemNumber,
94+
owner: context.repo.owner,
95+
repo: context.repo.repo,
96+
labels: labelsToAdd
97+
});
98+
99+
console.log(`Successfully added ${labelsToAdd.length} label(s)`);
100+
} else {
101+
console.log('No matching labels found for this title');
119102
}
120103

0 commit comments

Comments
 (0)