Skip to content

Commit d312e4c

Browse files
authored
Github: PR Labeler action (#972)
Stealing this from Agones, this GitHub action will label PRs based on the PR template, to make life a bit easier when doing releases, as labels will already be applied (if the template is used). Also removed the `/kind hotfix` since that never applied to this project anyway.
1 parent faffaa4 commit d312e4c

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

.github/pull_request_template.md

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
> /kind cleanup
1515
> /kind documentation
1616
> /kind feature
17-
> /kind hotfix
1817
1918
**What this PR does / Why we need it**:
2019

.github/workflows/label-pr.yml

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
# Copyright 2023 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
# This workflow utilizes https://github.com/actions/github-script in GitHub Actions to apply labels to pull requests.
17+
#
18+
name: Label PR
19+
on: [pull_request_target]
20+
jobs:
21+
label:
22+
runs-on: ubuntu-latest
23+
permissions:
24+
contents: read
25+
pull-requests: write
26+
steps:
27+
- name: Label PR
28+
uses: actions/github-script@v6
29+
with:
30+
script: |-
31+
const keywords = {
32+
"breaking": "kind/breaking",
33+
"bug": "kind/bug",
34+
"feature": "kind/feature",
35+
"cleanup": "kind/cleanup",
36+
"documentation": "kind/documentation",
37+
"release": "kind/release"
38+
};
39+
const prBody = context.payload.pull_request.body;
40+
const prLabels = [];
41+
if (prBody === null || prBody.trim() === "") {
42+
console.log("Pull Request body is empty");
43+
prLabels.push("kind/other");
44+
} else {
45+
const regex = /^\s*\/kind\s+(.+)$/m;
46+
const match = prBody.match(regex);
47+
console.log(`PR body: '${prBody}'`);
48+
console.log(`Regex match: '${match}'`);
49+
if (match && match[1] in keywords) {
50+
const keyword = match[1];
51+
const label = keywords[keyword];
52+
console.log(`Adding label: '${label}' based on keyword '${keyword}'`);
53+
prLabels.push(label);
54+
} else {
55+
console.log(`Adding label: 'kind/other' as no matching keyword found.`);
56+
prLabels.push("kind/other");
57+
}
58+
}
59+
try {
60+
github.rest.issues.addLabels({
61+
owner: context.repo.owner,
62+
repo: context.repo.repo,
63+
issue_number: context.payload.pull_request.number,
64+
labels: prLabels
65+
});
66+
} catch (error) {
67+
console.error(`Error retrieving files: ${error}`);
68+
}

0 commit comments

Comments
 (0)