From 085e45cf51dc1ed81532558a260eea2dc0f519a8 Mon Sep 17 00:00:00 2001 From: Phi Date: Mon, 5 Aug 2024 11:06:24 +0200 Subject: [PATCH] Add PR title check workflow Add PR title check workflow Add colon Add colon Request change to PR title instead of blocking PR Request change to PR title instead of blocking PR Remove change requested if title conforms to the formate Remove change requested if title conforms to the formate --- .github/workflows/pr-title-check.yml | 70 ++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 .github/workflows/pr-title-check.yml diff --git a/.github/workflows/pr-title-check.yml b/.github/workflows/pr-title-check.yml new file mode 100644 index 00000000000..1241a9fbb67 --- /dev/null +++ b/.github/workflows/pr-title-check.yml @@ -0,0 +1,70 @@ +name: PR Title Check + +on: + pull_request: + types: [opened, edited, synchronize, reopened] + +permissions: + contents: read + pull-requests: write + +jobs: + check-pr-title: + name: Check PR Title + runs-on: ubuntu-latest + steps: + - name: Check PR Title + id: check_title + uses: actions/github-script@v6 + with: + github-token: ${{secrets.GITHUB_TOKEN}} + script: | + const title = context.payload.pull_request.title; + const pattern = /^(feat|fix|docs|style|refactor|test|chore):\s\w+:\s.+/; + + if (!pattern.test(title)) { + core.setFailed('PR title does not match the required format: : : '); + return 'invalid'; + } + return 'valid'; + + - name: Request changes + if: steps.check_title.outputs.result == 'invalid' + uses: actions/github-script@v6 + with: + github-token: ${{secrets.GITHUB_TOKEN}} + script: | + github.rest.pulls.createReview({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.payload.pull_request.number, + body: 'Please update the PR title to match the required format: `: : `\n\nTypes: feat, fix, docs, style, refactor, test, chore\nScope: required\nSubject: start with lowercase', + event: 'REQUEST_CHANGES' + }) + + - name: Dismiss previous review if title is now valid + if: steps.check_title.outputs.result == 'valid' + uses: actions/github-script@v6 + with: + github-token: ${{secrets.GITHUB_TOKEN}} + script: | + const reviews = await github.rest.pulls.listReviews({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.payload.pull_request.number + }); + + const botReview = reviews.data.find(review => + review.user.type === 'Bot' && + review.state === 'CHANGES_REQUESTED' + ); + + if (botReview) { + await github.rest.pulls.dismissReview({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.payload.pull_request.number, + review_id: botReview.id, + message: 'PR title now matches the required format.' + }); + } \ No newline at end of file