-
Notifications
You must be signed in to change notification settings - Fork 21.3k
175 lines (157 loc) · 6.18 KB
/
Copy pathlabels.yml
File metadata and controls
175 lines (157 loc) · 6.18 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
name: Apply PR Labels
# workflow_run is used intentionally here: the elevated-permission step runs
# in the base-repo context but never checks out PR code, so it is safe.
on:
workflow_run: # zizmor: ignore[dangerous-triggers]
workflows: ["Labels Trigger"]
types:
- completed
permissions: {}
concurrency:
group: labels-apply-${{ github.event.workflow_run.id }}
cancel-in-progress: true
jobs:
labels-by-language:
name: labels-by-language
runs-on: ubuntu-slim
if: github.event.workflow_run.conclusion == 'success'
permissions:
pull-requests: write # So we can add labels to the pull request
steps:
- uses: actions/github-script@v9
with:
script: |
// For same-repo PRs the pull_requests array is populated directly.
// For fork PRs it is empty, so fall back to a commit-SHA lookup.
let prNumber;
const prs = context.payload.workflow_run.pull_requests;
if (prs && prs.length > 0) {
prNumber = prs[0].number;
} else {
// For fork PRs, commits don't exist in the base repo history so
// listPullRequestsAssociatedWithCommit returns empty. Use the
// head owner + branch from the workflow_run payload instead.
const headOwner = context.payload.workflow_run.head_repository.owner.login;
const headBranch = context.payload.workflow_run.head_branch;
const result = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
head: `${headOwner}:${headBranch}`,
state: 'open',
});
if (result.data.length === 0) {
core.warning(`No open PR found for ${headOwner}:${headBranch}`);
return;
}
prNumber = result.data[0].number;
}
const filenames = (await github.paginate(github.rest.pulls.listFiles, {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
per_page: 100,
})).map(file => file.filename);
console.log(filenames);
const vehicle_labels = {
AntennaTracker: 'AntennaTracker',
ArduCopter: 'Copter',
ArduPlane: 'Plane',
ArduSub: 'Sub',
Blimp: 'Blimp',
Rover: 'Rover',
};
const vehicle_sim_labels = {
Helicopter: 'TradHeli',
QuadPlane: 'VTOL-Plane',
Sailboat: 'Sailboat',
Blimp: 'Blimp',
Plane: 'Plane',
Rover: 'Rover',
Submarine: 'Sub',
};
const generic_suffix_labels = {
'.lua': 'Scripting',
'.py': 'Python',
};
const generic_prefix_labels = {
'libraries/AP_ExternalAHRS': 'ExternalAHRS',
'libraries/AP_Compass': 'Compass',
'libraries/AP_NavEKF': 'EKF',
'libraries/AP_HAL_Linux': 'Linux',
'libraries/GCS_MAVLink': 'MAVLink',
'libraries/SITL': 'SITL',
'Tools/AP_Periph': 'AP_Periph',
'Tools/autotest': 'CI',
'.github/workflows': 'github_actions',
};
const specific_vehicle_checks = {
'VTOL-Plane': ['ArduPlane/quadplane'],
'TradHeli': [
'ArduCopter/heli.cpp',
'Tools/autotest/default_params/copter-heli',
'libraries/AP_Motors/AP_MotorsHeli',
],
'Sailboat': ['Rover/sailboat'],
'Sub': ['libraries/AP_Motors/AP_Motors6DOF'],
};
const labels = new Set();
for (const filename of filenames) {
// Generic checks
for (const [suffix, label] of Object.entries(generic_suffix_labels)) {
if (filename.endsWith(suffix)) {
labels.add(label);
break;
}
}
for (const [prefix, label] of Object.entries(generic_prefix_labels)) {
if (filename.startsWith(prefix)) {
labels.add(label);
break;
}
}
// Specific/niche vehicle checks
var specific = false; // There can only be one specific label per file.
for (const [vehicle, prefixes] of Object.entries(specific_vehicle_checks)) {
for (const prefix of prefixes) {
if (filename.startsWith(prefix)) {
labels.add(vehicle);
specific = true;
break;
}
}
if (specific) break;
}
if (specific) continue;
for (const [vehicle, label] of Object.entries(vehicle_sim_labels)) {
if (
filename === `Tools/autotest/${vehicle.toLowerCase()}.py` ||
filename.startsWith(`Tools/autotest/default_params/${vehicle.toLowerCase()}`) ||
filename.startsWith(`libraries/SITL/SIM_${vehicle}`)
) {
labels.add(label);
specific = true;
break;
}
}
if (specific) continue;
// General vehicle checks
for (const [vehicle, label] of Object.entries(vehicle_labels)) {
if (
filename.startsWith(vehicle) ||
filename.startsWith(`Tools/autotest/${vehicle}_Test`) ||
filename === `Tools/autotest/${vehicle.toLowerCase()}.py`
) {
labels.add(label);
break;
}
}
}
console.log(labels);
if (labels.size > 0) {
github.rest.issues.addLabels({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
labels: Array.from(labels),
});
}