|
| 1 | +name: PR Validation |
| 2 | + |
| 3 | +# Created by smog-root |
| 4 | + |
| 5 | +on: |
| 6 | + pull_request: |
| 7 | + types: [opened, edited] |
| 8 | + |
| 9 | +jobs: |
| 10 | + validate-pr: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + |
| 13 | + steps: |
| 14 | + - name: Check out code |
| 15 | + uses: actions/checkout@v3 |
| 16 | + |
| 17 | + - name: Set up Node.js |
| 18 | + uses: actions/setup-node@v3 |
| 19 | + with: |
| 20 | + node-version: '14' |
| 21 | + |
| 22 | + - name: Validate PR Description |
| 23 | + id: pr-check |
| 24 | + run: | |
| 25 | + # Fetch PR information |
| 26 | + PR_DESCRIPTION=$(jq -r .pull_request.body < "$GITHUB_EVENT_PATH") |
| 27 | + PR_TITLE=$(jq -r .pull_request.title < "$GITHUB_EVENT_PATH") |
| 28 | +
|
| 29 | + # Define file paths for the output variables |
| 30 | + PR_VALID_FILE=$(mktemp) |
| 31 | + ERROR_MESSAGE_FILE=$(mktemp) |
| 32 | + SUCCESS_MESSAGE_FILE=$(mktemp) |
| 33 | +
|
| 34 | + # Default value for PR_VALID |
| 35 | + PR_VALID="true" |
| 36 | +
|
| 37 | + # Check if PR description is empty |
| 38 | + if [ -z "$PR_DESCRIPTION" ] || [ "$PR_DESCRIPTION" == "null" ]; then |
| 39 | + echo "Empty PR description" |
| 40 | + PR_VALID="false" |
| 41 | + echo '❌ Error: PR description is empty!' > "$ERROR_MESSAGE_FILE" |
| 42 | + fi |
| 43 | +
|
| 44 | + # Check for issue reference in the description |
| 45 | + ISSUE_PATTERN="(Fixes|Close|Closes|Closed|Fix|Fixed|Resolve|Resolves) #[0-9]+" |
| 46 | + if [[ ! "$PR_DESCRIPTION" =~ $ISSUE_PATTERN ]]; then |
| 47 | + echo "Invalid or missing issue reference" |
| 48 | + PR_VALID="false" |
| 49 | + echo '❌ Error: PR must reference an issue with the format Fixes ,Close ,Closes ,Closed ,Fix ,Fixed ,Resolve ,Resolves #Issue_Number' > "$ERROR_MESSAGE_FILE" |
| 50 | + fi |
| 51 | +
|
| 52 | + # If both checks pass |
| 53 | + if [ "$PR_VALID" == "true" ]; then |
| 54 | + echo '✅ Success: PR is valid!' > "$SUCCESS_MESSAGE_FILE" |
| 55 | + fi |
| 56 | +
|
| 57 | + # Save the outputs to environment files |
| 58 | + echo "PR_VALID=$PR_VALID" >> $GITHUB_ENV |
| 59 | + echo "ERROR_MESSAGE=$(cat $ERROR_MESSAGE_FILE)" >> $GITHUB_ENV |
| 60 | + echo "SUCCESS_MESSAGE=$(cat $SUCCESS_MESSAGE_FILE)" >> $GITHUB_ENV |
| 61 | +
|
| 62 | + - name: Post comment on PR |
| 63 | + uses: actions/github-script@v6 |
| 64 | + with: |
| 65 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 66 | + script: | |
| 67 | + const prValid = process.env.PR_VALID; |
| 68 | + const errorMessage = process.env.ERROR_MESSAGE; |
| 69 | + const successMessage = process.env.SUCCESS_MESSAGE; |
| 70 | + const prNumber = context.payload.pull_request.number; |
| 71 | +
|
| 72 | + if (prValid === 'false') { |
| 73 | + github.rest.issues.createComment({ |
| 74 | + issue_number: prNumber, |
| 75 | + owner: context.repo.owner, |
| 76 | + repo: context.repo.repo, |
| 77 | + body: errorMessage |
| 78 | + }); |
| 79 | + core.setFailed(errorMessage); |
| 80 | + } else { |
| 81 | + github.rest.issues.createComment({ |
| 82 | + issue_number: prNumber, |
| 83 | + owner: context.repo.owner, |
| 84 | + repo: context.repo.repo, |
| 85 | + body: successMessage |
| 86 | + }); |
| 87 | + } |
| 88 | +
|
| 89 | + - name: Fail if validation failed |
| 90 | + if: env.PR_VALID == 'false' |
| 91 | + run: exit 1 |
0 commit comments