chore: minor improvement for docs #3336
This file contains hidden or 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 Title Check | |
| on: | |
| pull_request_target: | |
| types: | |
| - opened | |
| - edited | |
| - reopened | |
| - ready_for_review | |
| permissions: | |
| pull-requests: write | |
| concurrency: | |
| group: pr-title-check-${{ github.event.pull_request.number }} | |
| cancel-in-progress: true | |
| jobs: | |
| check-pr-title: | |
| if: github.event.pull_request.draft == false | |
| name: Check PR Title | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Check PR Title and Manage Review | |
| uses: actions/github-script@v6 | |
| with: | |
| github-token: ${{secrets.GITHUB_TOKEN}} | |
| script: | | |
| // Fetch the PR fresh rather than trusting context.payload.pull_request.title, | |
| // which is a snapshot from when the triggering event was dispatched. A run can | |
| // sit queued long enough that the title changes again before it executes, | |
| // causing it to validate (and post a status for) a title that's already stale. | |
| // Fall back to the payload title if the live fetch fails, so a transient API | |
| // error doesn't block a merge over a title that was already valid. | |
| let title = context.payload.pull_request.title; | |
| try { | |
| const { data: pr } = await github.rest.pulls.get({ | |
| ...context.repo, | |
| pull_number: context.payload.pull_request.number | |
| }); | |
| title = pr.title; | |
| } catch (error) { | |
| core.warning(`Failed to fetch live PR title, falling back to event payload title: ${error.message}`); | |
| } | |
| // This should match https://github.com/filecoin-project/lotus/blob/master/CONTRIBUTING.md#pr-title-conventions | |
| // 202408: Beyond Conventional Commit conventions, we also optionally support the "scope" outside of parenthesis for a transitionary period from legacy conventions per https://github.com/filecoin-project/lotus/pull/12340 | |
| const conventionalPattern = /^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([\w.-]+\))?!?:?\s([\w.-]+:)?\s?["'`a-z].+$/; | |
| // Also accept "Revert ..." titles from GitHub's revert functionality | |
| const revertPattern = /^Revert\s".+"$/; | |
| const pattern = new RegExp(`(${conventionalPattern.source})|(${revertPattern.source})`); | |
| if (!pattern.test(title)) { | |
| await github.rest.pulls.createReview({ | |
| ...context.repo, | |
| pull_number: context.payload.pull_request.number, | |
| body: 'Please update the PR title to match https://github.com/filecoin-project/lotus/blob/master/CONTRIBUTING.md#pr-title-conventions', | |
| event: 'REQUEST_CHANGES' | |
| }); | |
| core.setFailed('PR title does not match the required format'); | |
| } else if (context.payload.action === 'edited' && context.payload.changes.title) { | |
| // Only proceed with dismissal if the title was just edited | |
| await new Promise(resolve => setTimeout(resolve, 5000)); // 5 second delay | |
| const reviews = await github.rest.pulls.listReviews({ | |
| ...context.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({ | |
| ...context.repo, | |
| pull_number: context.payload.pull_request.number, | |
| review_id: botReview.id, | |
| message: 'PR title now matches the required format.' | |
| }); | |
| } | |
| } |