Pull Request Comments #9424
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
| # Responsible for making comments on pull requests, such as commenting for first time contributors. | |
| name: Pull Request Comments | |
| on: | |
| pull_request_target: | |
| types: [ 'opened', 'synchronize', 'reopened', 'edited' ] | |
| workflow_run: | |
| workflows: [ 'Test Build Processes' ] | |
| types: | |
| - completed | |
| # Cancels all previous workflow runs for pull requests that have not completed. | |
| concurrency: | |
| # The concurrency group contains the workflow name and the branch name for pull requests | |
| # or the commit hash for any other events. | |
| group: ${{ github.workflow }}-${{ github.event_name == 'pull_request_target' && github.head_ref || github.event_name == 'workflow_dispatch' && github.event.number || github.sha }} | |
| # Disable permissions for all available scopes by default. | |
| # Any needed permissions should be configured at the job level. | |
| permissions: {} | |
| jobs: | |
| # Leaves a comment on a pull request with a link to test the changes in a WordPress Playground instance. | |
| playground-details: | |
| name: Comment on a pull request with Playground details | |
| runs-on: ubuntu-24.04 | |
| permissions: | |
| issues: write | |
| pull-requests: write | |
| if: > | |
| github.repository == 'WordPress/wordpress-develop' && | |
| github.event.workflow_run.event == 'pull_request' && | |
| github.event.workflow_run.conclusion == 'success' | |
| steps: | |
| - name: Download artifact | |
| uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 | |
| with: | |
| script: | | |
| const artifacts = await github.rest.actions.listWorkflowRunArtifacts( { | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| run_id: process.env.RUN_ID, | |
| } ); | |
| const matchArtifact = artifacts.data.artifacts.filter( ( artifact ) => { | |
| return artifact.name === 'pr-number' | |
| } )[0]; | |
| if ( ! matchArtifact ) { | |
| core.setFailed( 'No artifact found!' ); | |
| return; | |
| } | |
| const download = await github.rest.actions.downloadArtifact( { | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| artifact_id: matchArtifact.id, | |
| archive_format: 'zip', | |
| } ); | |
| const fs = require( 'fs' ); | |
| fs.writeFileSync( '${{github.workspace}}/pr-number.zip', Buffer.from( download.data ) ) | |
| env: | |
| RUN_ID: ${{ github.event.workflow_run.id }} | |
| - name: Unzip the artifact containing the PR number | |
| run: unzip pr-number.zip | |
| - name: Leave a comment about testing with Playground | |
| uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 | |
| with: | |
| script: | | |
| const fs = require( 'fs' ); | |
| const issue_number = Number( fs.readFileSync( './NR' ) ); | |
| core.info( `Checking pull request #${issue_number}.` ); | |
| // Confirm that the pull request is still open before leaving a comment. | |
| const pr = await github.rest.pulls.get({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: issue_number, | |
| }); | |
| if ( pr.data.state !== 'open' ) { | |
| core.info( 'The pull request has been closed. No comment will be left.' ); | |
| return; | |
| } | |
| // Comments are only added after the first successful build. Check for the presence of a comment and bail early. | |
| const commentInfo = { | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number, | |
| }; | |
| const comments = ( await github.rest.issues.listComments( commentInfo ) ).data; | |
| for ( const currentComment of comments ) { | |
| if ( currentComment.user.type === 'Bot' && currentComment.body.includes( 'Test using WordPress Playground' ) ) { | |
| core.info( 'A comment with instructions to test within a Playground instance already exists.' ); | |
| return; | |
| } | |
| }; | |
| // No comment was found. Create one. | |
| commentInfo.body = `## Test using WordPress Playground | |
| The changes in this pull request can previewed and tested using a [WordPress Playground](https://developer.wordpress.org/playground/) instance. | |
| [WordPress Playground](https://developer.wordpress.org/playground/) is an experimental project that creates a full WordPress instance entirely within the browser. | |
| ### Some things to be aware of | |
| - All changes will be lost when closing a tab with a Playground instance. | |
| - All changes will be lost when refreshing the page. | |
| - A fresh instance is created each time the link below is clicked. | |
| - Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance, | |
| it's possible that the most recent build failed, or has not completed. Check the [list of workflow runs to be sure](https://github.com/WordPress/wordpress-develop/actions/workflows/test-build-processes.yml). | |
| For more details about these limitations and more, check out the [Limitations page](https://wordpress.github.io/wordpress-playground/limitations/) in the WordPress Playground documentation. | |
| [Test this pull request with WordPress Playground](https://playground.wordpress.net/wordpress.html?pr=${ issue_number }). | |
| `; | |
| github.rest.issues.createComment( commentInfo ); | |
| # Manages comments reminding contributors to include a Trac ticket link when opening a pull request. | |
| trac-ticket-check: | |
| name: Manage Trac ticket reminders for pull requests | |
| runs-on: ubuntu-24.04 | |
| permissions: | |
| issues: write | |
| pull-requests: write | |
| if: ${{ github.repository == 'WordPress/wordpress-develop' && github.event_name == 'pull_request_target' && ! github.event.pull_request.draft && github.event.pull_request.state == 'open' }} | |
| steps: | |
| - name: Check for Trac ticket and manage comment | |
| uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 | |
| with: | |
| script: | | |
| const { owner, repo } = context.repo; | |
| const { number } = context.issue; | |
| // Check for the presence of a comment and bail early. | |
| const comments = ( await github.rest.issues.listComments( { owner, repo, issue_number: number } ) ).data; | |
| const hasMissingTicketComment = comments.find( comment => comment.user.type === 'Bot' && comment.body.includes( 'Trac Ticket Missing' ) ); | |
| if ( hasMissingTicketComment ) { | |
| // Trac ticket link found, delete existing "Trac Ticket Missing" comment. | |
| await github.rest.issues.deleteComment( { owner, repo, comment_id: hasMissingTicketComment.id } ); | |
| return; | |
| } | |
| // No comment was found. Create one. | |
| const pr = ( await github.rest.pulls.get( { owner, repo, pull_number: number } ) ).data; | |
| const prBody = pr.body ?? ''; | |
| const prTitle = pr.title ?? ''; | |
| const tracTicketRegex = new RegExp( '(https?://core.trac.wordpress.org/ticket/|Core-|ticket:)([0-9]+)', 'g' ); | |
| const tracTicketMatches = prBody.match( tracTicketRegex ) || prTitle.match( tracTicketRegex ); | |
| if ( ! tracTicketMatches ) { | |
| github.rest.issues.createComment( { | |
| owner, | |
| repo, | |
| issue_number: number, | |
| body: `## Trac Ticket Missing | |
| This pull request is missing a link to a [Trac ticket](https://core.trac.wordpress.org/). For a contribution to be considered, there must be a corresponding ticket in Trac. | |
| To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description. More information about contributing to WordPress on GitHub can be found in [the Core Handbook](https://make.wordpress.org/core/handbook/contribute/git/github-pull-requests-for-code-review/). | |
| `, | |
| } ); | |
| } |