-
Notifications
You must be signed in to change notification settings - Fork 764
137 lines (117 loc) · 6.05 KB
/
issue-labeler.yml
File metadata and controls
137 lines (117 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
name: Issue Labeler
on:
issues:
types: [opened, edited]
permissions:
issues: write
jobs:
label-issue:
runs-on: ubuntu-latest
concurrency:
group: issue-${{ github.event.issue.number }}
cancel-in-progress: false
steps:
- name: Apply Area and Namespace Labels
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
github-token: ${{ github.token }}
script: |
const { owner, repo } = context.repo;
const issueNumber = context.payload.issue?.number;
if (!issueNumber) return core.setFailed('No issue number in payload');
const { data: issue } = await github.rest.issues.get({ owner, repo, issue_number: issueNumber });
const currentLabels = (issue.labels || []).map(l => typeof l === 'string' ? l : l.name);
const currentLower = currentLabels.map(s => s.toLowerCase());
core.startGroup('Diagnostics');
core.info(`Issue #${issue.number}`);
core.info(`Current labels: ${currentLabels.join(', ') || '(none)'}`);
core.endGroup();
// Gate: only proceed if bug/enhancement label is present
if (!currentLower.includes('bug') && !currentLower.includes('enhancement')) {
core.info('Gate not met (requires "bug" or "enhancement"); skipping.');
return;
}
const body = issue.body || '';
core.startGroup('Body preview');
core.info(`Length: ${body.length}`);
core.info(`First 400 chars: ${body.slice(0, 400).replace(/\r?\n/g, '\\n')}`);
core.endGroup();
// Extract the Component value from Issue Forms ("### Component" section)
function getByHeading(text, title) {
const esc = title.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const re = new RegExp(
`(?:^|\\r?\\n)#{2,}\\s*${esc}\\s*\\r?\\n+([\\s\\S]*?)(?=\\r?\\n#{2,}\\s|\\r?\\n<!--|$)`,
'i'
);
const m = text.match(re);
if (!m) return '';
const lines = m[1].split(/\r?\n/).map(s => s.trim());
for (const ln of lines) {
if (!ln) continue;
const cleaned = ln.replace(/^>+\s*/, '').replace(/^[-*]\s*/, '');
if (/^no response$/i.test(cleaned)) continue;
if (/^```/.test(cleaned)) continue; // skip code fences
return cleaned;
}
return '';
}
// Try "### Component" first; fall back to inline "Component: X" or "otel_component: X"
let component = getByHeading(body, 'Component');
if (!component) {
let m = body.match(/(?:^|\r?\n)\s*(?:component|otel[_\s-]*component)\s*[:\-–]\s*(.+)/i);
if (m) component = m[1].trim();
}
core.info(`Extracted component: "${component || '(empty)'}"`);
if (!component) {
return core.setFailed('Gate met, but component not found. Ensure the form has a "Component" field with a value.');
}
const sanitize = s => s.toLowerCase().replace(/\s+/g, ':').replace(/:+$/,'').trim();
let area = '';
let ns = '';
let m;
if ((m = component.match(/^detector\s*[:\-–]\s*(.+)$/i))) {
area = 'area: detector';
ns = `detector: ${sanitize(m[1])}`;
} else if ((m = component.match(/^instrumentation\s*[:\-–]\s*(.+)$/i))) {
area = 'area: instrumentation';
ns = `instrumentation: ${sanitize(m[1])}`;
} else if ((m = component.match(/^propagator\s*[:\-–]\s*(.+)$/i))) {
area = 'area: propagators';
ns = `propagator: ${sanitize(m[1])}`;
} else if ((m = component.match(/^sampler\s*[:\-–]\s*(.+)$/i))) {
area = 'area: sampler';
ns = `sampler: ${sanitize(m[1])}`;
} else if (/^autoexporter$/i.test(component.trim())) {
area = 'area: exporter';
ns = 'exporter: autoexport';
} else if (/^zpages$/i.test(component.trim())) {
area = 'area: zpages';
ns = 'zpages';
} else if (/^config$/i.test(component.trim())) {
area = 'area: file-config';
ns = '';
}
core.info(`Computed area: "${area || '(none)'}", namespace: "${ns || '(none)'}"`);
const toAdd = [area, ns].filter(Boolean).filter(l => !currentLower.includes(l.toLowerCase()));
core.info(`Labels to add: ${toAdd.join(', ') || '(none)'}`);
if (!toAdd.length) {
return core.setFailed('Gate met but no labels computed (unrecognized component) or labels already present.');
}
// Ensure labels exist; fail if missing (so we get an explicit error)
const existing = await github.paginate(github.rest.issues.listLabelsForRepo, { owner, repo, per_page: 100 });
const existingSet = new Set(existing.map(l => l.name.toLowerCase()));
const missing = toAdd.filter(l => !existingSet.has(l.toLowerCase()));
if (missing.length) {
return core.setFailed(`Required labels do not exist in the repository: ${missing.join(', ')}. Create them and re-run.`);
}
// Add labels
await github.rest.issues.addLabels({ owner, repo, issue_number: issue.number, labels: toAdd });
// Verify
const updated = await github.rest.issues.get({ owner, repo, issue_number: issue.number });
const updatedSet = new Set(updated.data.labels.map(l => l.name.toLowerCase()));
const stillMissing = toAdd.filter(l => !updatedSet.has(l.toLowerCase()));
if (stillMissing.length) {
core.setFailed(`Attempted to add labels but they are missing after update: ${stillMissing.join(', ')}`);
} else {
core.info(`Labels added successfully: ${toAdd.join(', ')}`);
}