Skip to content

Commit 7723e0a

Browse files
ci: fix pr title verifier
1 parent 4979e43 commit 7723e0a

File tree

2 files changed

+82
-13
lines changed

2 files changed

+82
-13
lines changed

.github/workflows/verify.yml

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,51 @@
1-
name: PR Verifier
1+
name: "PR Title Verifier"
22

33
on:
44
pull_request:
5-
types: [opened, edited, synchronize,reopened]
6-
7-
permissions:
8-
contents: read # Read-only access to repository contents
9-
pull-requests: read # Read-only access to pull request details
5+
types: [opened, edited, synchronize, reopened]
106

117
jobs:
12-
138
verify:
14-
name: Verify PR contents
159
runs-on: ubuntu-latest
10+
1611
steps:
17-
- name: Verifier action
18-
id: verifier
19-
uses: kubernetes-sigs/[email protected]
20-
with:
21-
github_token: ${{ secrets.GITHUB_TOKEN }}
12+
- name: Checkout code
13+
uses: actions/checkout@v4
14+
15+
- name: Get PR title
16+
id: get_title
17+
run: echo "title=${{ github.event.pull_request.title }}" >> $GITHUB_ENV
18+
19+
- name: Run PR Title Checker
20+
id: check_title
21+
run: |
22+
if ! ./hack/ci/pr_title_checker.sh "${{ env.title }}" 2>error_message.txt; then
23+
echo "failed=true" >> $GITHUB_ENV
24+
fi
25+
26+
- name: Set failure status and save error message
27+
if: always()
28+
run: |
29+
if [ -f error_message.txt ]; then
30+
echo "failed=true" >> $GITHUB_ENV
31+
fi
32+
33+
- name: Create PR comment if title is invalid
34+
if: always() && env.failed == 'true'
35+
uses: actions/github-script@v6
36+
with:
37+
github-token: ${{ secrets.GITHUB_TOKEN }}
38+
script: |
39+
const fs = require('fs');
40+
const errorMessage = fs.readFileSync('error_message.txt', 'utf8');
41+
const prNumber = context.payload.pull_request.number;
42+
github.issues.createComment({
43+
owner: context.repo.owner,
44+
repo: context.repo.repo,
45+
issue_number: prNumber,
46+
body: `### :x: PR Title Check Failed\n${errorMessage}`
47+
});
48+
49+
- name: Fail the job
50+
if: env.failed == 'true'
51+
run: exit 1

hack/ci/pr_title_checker.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
3+
# Copyright 2024 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# Define regex patterns
18+
WIP_REGEX="^\W?WIP\W"
19+
TAG_REGEX="^\[[[:alnum:]\._-]*\]"
20+
PR_TITLE="$1"
21+
22+
# Trim WIP and tags from title
23+
trimmed_title=$(echo "$PR_TITLE" | sed -E "s/$WIP_REGEX//" | sed -E "s/$TAG_REGEX//" | xargs)
24+
25+
# Check PR type prefix
26+
if [[ "$trimmed_title" =~ ^⚠ ]] || [[ "$trimmed_title" =~ ^✨ ]] || [[ "$trimmed_title" =~ ^🐛 ]] || [[ "$trimmed_title" =~ ^📖 ]] || [[ "$trimmed_title" =~ ^🚀 ]] || [[ "$trimmed_title" =~ ^🌱 ]]; then
27+
echo "PR title is valid: $trimmed_title"
28+
exit 0
29+
else
30+
echo "Error: No matching PR type indicator found in title."
31+
echo "You need to have one of these as the prefix of your PR title:"
32+
echo "- Breaking change: ⚠ (:warning:)"
33+
echo "- Non-breaking feature: ✨ (:sparkles:)"
34+
echo "- Patch fix: 🐛 (:bug:)"
35+
echo "- Docs: 📖 (:book:)"
36+
echo "- Release: 🚀 (:rocket:)"
37+
echo "- Infra/Tests/Other: 🌱 (:seedling:)"
38+
exit 1
39+
fi

0 commit comments

Comments
 (0)