Skip to content

Commit 37aea0f

Browse files
committed
.github/workflows: Add PR formatting validator
This workflow runs when GitHub PRs are modified (edited, opened, reopened, and synchronized) to perform basic validation of the PR title and description. Right now, this includes: - Checking that PR template text does not remain in the PR description. - Checking that the PR body is not empty. If a check fails, a GitHub comment will be left on the PR and a PR status check failure will be present on the PR until the issue is resolved. Signed-off-by: Michael Kubacki <[email protected]>
1 parent d1c1f7e commit 37aea0f

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# This workflow validates basic pull request formatting requirements are met.
2+
#
3+
# Copyright (c) Microsoft Corporation.
4+
# SPDX-License-Identifier: BSD-2-Clause-Patent
5+
#
6+
7+
name: Validate Pull Request Formatting
8+
9+
on:
10+
pull_request_target:
11+
types:
12+
- edited
13+
- opened
14+
- reopened
15+
- synchronize
16+
17+
jobs:
18+
validate_pr:
19+
runs-on: ubuntu-latest
20+
21+
steps:
22+
- name: Generate Token
23+
id: app-token
24+
uses: actions/create-github-app-token@v2
25+
with:
26+
app-id: ${{ secrets.TIANOCORE_ASSIGN_REVIEWERS_APPLICATION_ID }}
27+
private-key: ${{ secrets.TIANOCORE_ASSIGN_REVIEWERS_APPLICATION_PRIVATE_KEY }}
28+
29+
- run: |
30+
prData="$(gh api graphql -F owner=$OWNER -F name=$REPO -F pr_number=$PR_NUMBER -f query='
31+
query($name: String!, $owner: String!, $pr_number: Int!) {
32+
repository(owner: $owner, name: $name) {
33+
pullRequest(number: $pr_number) {
34+
title
35+
body
36+
}
37+
}
38+
}')"
39+
40+
prTitle=$(echo "$prData" | jq -r '.data.repository.pullRequest.title')
41+
prBody=$(echo "$prData" | jq -r '.data.repository.pullRequest.body')
42+
43+
templateContent="$(gh api repos/$OWNER/$REPO/contents/.github/pull_request_template.md --jq '.content' | base64 -d)"
44+
45+
# Template lines are those that begin with '<'
46+
templateLines=$(echo "$templateContent" | grep '^<' || true)
47+
48+
if [[ -n "$templateLines" ]]; then
49+
templateLinesFound=""
50+
while IFS= read -r line; do
51+
if [[ -n "$line" && "$prBody" == *"$line"* ]]; then
52+
if [[ -n "$templateLinesFound" ]]; then
53+
templateLinesFound="$templateLinesFound"$'\n'"\`$line\`"
54+
else
55+
templateLinesFound="\`$line\`"
56+
fi
57+
fi
58+
done <<< "$templateLines"
59+
60+
if [[ -n "$templateLinesFound" ]]; then
61+
gh pr comment $PR_URL --body "⚠️ Please remove the following template lines from your PR description:"$'\n'"$templateLinesFound"
62+
echo 'VALIDATION_ERROR=true' >> $GITHUB_ENV
63+
fi
64+
fi
65+
66+
# Check if the PR body is empty
67+
if [[ -z "$prBody" || "$prBody" == "null" ]]; then
68+
gh pr comment $PR_URL --body "⚠️ Please add a pull request description."
69+
echo 'VALIDATION_ERROR=true' >> $GITHUB_ENV
70+
fi
71+
env:
72+
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
73+
OWNER: ${{ github.repository_owner }}
74+
PR_NUMBER: ${{ github.event.number }}
75+
PR_URL: ${{ github.event.pull_request.html_url }}
76+
REPO: ${{ github.event.repository.name }}
77+
78+
- name: Check for Validation Errors
79+
if: env.VALIDATION_ERROR
80+
uses: actions/github-script@v7
81+
with:
82+
script: |
83+
core.setFailed('PR Formatting Validation Check Failed!')

0 commit comments

Comments
 (0)