-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
64 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,73 @@ | ||
name: PR Issue Checker | ||
name: PR Validation | ||
|
||
on: | ||
pull_request: | ||
types: [opened, edited] | ||
|
||
jobs: | ||
check_pr_description: | ||
validate-pr: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Check PR Description | ||
id: check_pr_description | ||
run: | | ||
PR_DESCRIPTION="${{ github.event.pull_request.body }}" | ||
if [[ -z "$PR_DESCRIPTION" ]]; then | ||
echo "PR description is missing." | ||
exit 1 | ||
fi | ||
if [[ ! "$PR_DESCRIPTION" =~ Fixes\ #[0-9]+ ]]; then | ||
echo "The PR description should include 'Fixes #<issue-number>' if not addressing any issue." | ||
echo "##[error]Fixes #NEW must be included in the description." | ||
exit 1 | ||
fi | ||
echo "PR description is valid." | ||
- name: Output result | ||
run: echo "All checks passed." | ||
- name: Check out code | ||
uses: actions/checkout@v3 | ||
|
||
- name: Set up Node.js | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: '14' | ||
|
||
- name: Validate PR Description | ||
id: pr-check | ||
run: | | ||
# Fetch PR information | ||
PR_DESCRIPTION=$(jq -r .pull_request.body < "$GITHUB_EVENT_PATH") | ||
PR_TITLE=$(jq -r .pull_request.title < "$GITHUB_EVENT_PATH") | ||
# Check if PR description is empty | ||
if [ -z "$PR_DESCRIPTION" ] || [ "$PR_DESCRIPTION" == "null" ]; then | ||
echo "Empty PR description" | ||
echo "::set-output name=pr-valid::false" | ||
echo "::set-output name=error-message::'❌ Error: PR description is empty!'" | ||
exit 1 | ||
fi | ||
# Check for issue reference in the description | ||
ISSUE_PATTERN="(Fixes|Close|Closes|Closed|Fix|Fixed|Resolve|Resolves) #[0-9]+" | ||
if [[ ! "$PR_DESCRIPTION" =~ $ISSUE_PATTERN ]]; then | ||
echo "Invalid or missing issue reference" | ||
echo "::set-output name=pr-valid::false" | ||
echo "::set-output name=error-message::'❌ Error: PR must reference an issue with the format Fixes #Issue_Number'" | ||
exit 1 | ||
fi | ||
# If both checks pass | ||
echo "::set-output name=pr-valid::true" | ||
echo "::set-output name=success-message::'✅ Success: PR is valid!'" | ||
- name: Post comment on PR | ||
uses: actions/github-script@v6 | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
script: | | ||
const prValid = ${{ steps.pr-check.outputs.pr-valid }}; | ||
const errorMessage = ${{ steps.pr-check.outputs.error-message }}; | ||
const successMessage = ${{ steps.pr-check.outputs.success-message }}; | ||
const prNumber = context.payload.pull_request.number; | ||
if (prValid === 'false') { | ||
github.rest.issues.createComment({ | ||
issue_number: prNumber, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
body: errorMessage | ||
}); | ||
core.setFailed(errorMessage); | ||
} else { | ||
github.rest.issues.createComment({ | ||
issue_number: prNumber, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
body: successMessage | ||
}); | ||
} |