feat: Test #16
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
name: "pr-validation" | |
on: | |
pull_request: | |
types: [opened, reopened, edited, synchronize] | |
jobs: | |
validate: | |
name: Validate | |
runs-on: ubuntu-latest | |
steps: | |
- name: Validate PR title | |
id: validation | |
env: | |
REGEX: '^(fix|feat|chore)(!)?:\ .+' | |
run: | | |
PR_TITLE=$(jq -r .pull_request.title "$GITHUB_EVENT_PATH") | |
REGEX=$REGEX | |
DEPENDABOT_REGEX="^Bump\\s.+" | |
if [[ ! "$PR_TITLE" =~ $REGEX && ! "$PR_TITLE" =~ $DEPENDABOT_REGEX ]]; then | |
echo "Invalid PR title" | |
echo "PR_STATUS=false" >> $GITHUB_OUTPUT | |
else | |
echo "Valid PR title" | |
echo "PR_STATUS=true" >> $GITHUB_OUTPUT | |
fi | |
- name: Update Status and Comment on PR | |
uses: actions/github-script@v6 | |
env: | |
REGEX: '^(fix|feat|chore)(!)?:\ .+' | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const regex = process.env.REGEX; | |
const state = ${{ steps.validation.outputs.PR_STATUS }} ? "success" : "failure"; | |
const description = ${{ steps.validation.outputs.PR_STATUS }} ? "PR title is valid." : `PR title is invalid. Ensure it matches the pattern: ${regex}`; | |
const prNumber = context.payload.pull_request.number; | |
const commitSha = context.payload.pull_request.head.sha; | |
const runId = context.runId; | |
const repoOwner = context.repo.owner; | |
const repoName = context.repo.repo; | |
const contextName = process.env.GITHUB_WORKFLOW; | |
const workflowRun = await github.rest.actions.getWorkflowRun({ | |
owner: repoOwner, | |
repo: repoName, | |
run_id: runId | |
}); | |
console.log(workflowRun.data); | |
// Create a status check | |
await github.rest.repos.createCommitStatus({ | |
owner: repoOwner, | |
repo: repoName, | |
sha: commitSha, | |
state: state, | |
context: contextName, | |
description: description | |
}); |