-
Notifications
You must be signed in to change notification settings - Fork 703
150 lines (138 loc) · 6.68 KB
/
labeler.yml
File metadata and controls
150 lines (138 loc) · 6.68 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
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This workflow will fetch the updated PR filenames, compute the Size Label
# and Arch Labels, then save the PR Labels into a PR Artifact. The
# PR Labels will be written to the PR inside the "workflow_run" trigger
# (pr_labeler.yml), because this "pull_request" trigger has read-only
# permission. Don't use "pull_request_target", it's unsafe.
# See https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=321719166#GitHubActionsSecurity-Buildstriggeredwithpull_request_target
name: "Pull Request Labeler"
on:
- pull_request
jobs:
labeler:
permissions:
contents: read
pull-requests: read
issues: read
runs-on: ubuntu-latest
steps:
# Checkout one file from our trusted source: .github/labeler.yml
# Never checkout and execute any untrusted code from the PR.
- name: Checkout labeler config
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
repository: apache/nuttx-apps
ref: master
path: labeler
fetch-depth: 1
persist-credentials: false
sparse-checkout: .github/labeler.yml
sparse-checkout-cone-mode: false
# Fetch the updated PR filenames. Compute the Size Label and Arch Labels.
- name: Compute PR labels
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
const pull_number = context.issue.number;
// Fetch the array of updated PR filenames:
// { status: 'added', filename: 'arch/arm/test.txt', additions: 3, deletions: 0, changes: 3 }
// { status: 'removed', filename: 'Documentation/legacy_README.md', additions: 0, deletions: 2531, changes: 2531 }
// { status: 'modified', filename: 'Documentation/security.rst', additions: 1, deletions: 0, changes: 1 }
const listFilesOptions = github.rest.pulls.listFiles
.endpoint.merge({ owner, repo, pull_number });
const listFilesResponse = await github.paginate(listFilesOptions);
// Sum up the number of lines changed
const sizeFiles = listFilesResponse
.filter(f => (f.status != 'removed')); // Ignore deleted files
var linesChanged = 0;
for (const file of sizeFiles) {
linesChanged += file.changes;
}
console.log({ linesChanged });
// Compute the Size Label
const sizeLabel =
(linesChanged <= 10) ? 'Size: XS'
: (linesChanged <= 100) ? 'Size: S'
: (linesChanged <= 500) ? 'Size: M'
: (linesChanged <= 1000) ? 'Size: L'
: 'Size: XL';
var prLabels = [ sizeLabel ];
// Parse the Arch Label Patterns in .github/labeler.yml. Condense into:
// "Arch: arm":
// - any-glob-to-any-file: 'arch/arm/**'
// - any-glob-to-any-file: ...
const fs = require('fs');
const config = fs.readFileSync('labeler/.github/labeler.yml', 'utf8')
.split('\n') // Split by newline
.map(s => s.trim()) // Remove leading and trailing spaces
.filter(s => (s != '')) // Remove empty lines
.filter(s => !s.startsWith('#')) // Remove comments
.filter(s => !s.startsWith('- changed-files:')); // Remove "changed-files"
// Convert the Arch Label Patterns from config to archLabels.
// archLabels will contain the mappings for Arch Label and Filename Pattern:
// { label: "Arch: arm", pattern: "arch/arm/.*" },
// { label: "Arch: arm64", pattern: "arch/arm64/.*" }, ...
var archLabels = [];
var label = "";
for (const c of config) {
// Get the Arch Label
if (c.startsWith('"')) { // "Arch: arm":
label = c.split('"')[1]; // Arch: arm
} else if (c.startsWith('- any-glob-to-any-file:')) { // - any-glob-to-any-file: 'arch/arm/**'
// Convert the Glob Pattern to Regex Pattern
const pattern = c.split("'")[1] // arch/arm/**
.split('.').join('\\.') // . becomes \.
.split('*').join('[^/]*') // * becomes [^/]*
.split('[^/]*[^/]*').join('.*'); // ** becomes .*
archLabels.push({
label, // Arch: arm
pattern: '^' + pattern + '$' // Match the Line Start and Line End
});
} else {
// We don't support all rules of `actions/labeler`
throw new Error('.github/labeler.yml should contain only changed-files and any-glob-to-any-file, not: ' + c);
}
}
// Search the filenames for matching Arch Labels
for (const archLabel of archLabels) {
if (prLabels.includes(archLabel.label)) {
break;
}
for (const file of listFilesResponse) {
const re = new RegExp(archLabel.pattern);
const match = re.test(file.filename);
if (match && !prLabels.includes(archLabel.label)) {
prLabels.push(archLabel.label);
break;
}
}
}
console.log({ prLabels });
// Save the PR Number and PR Labels into a PR Artifact
// e.g. 'Size: XS\nArch: avr\n'
const dir = 'pr';
fs.mkdirSync(dir);
fs.writeFileSync(dir + '/pr-id.txt', pull_number + '\n');
fs.writeFileSync(dir + '/pr-labels.txt', prLabels.join('\n') + '\n');
# Upload the PR Artifact as pr.zip
- name: Upload PR artifact
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
with:
name: pr
path: pr/