diff --git a/.github/workflows/pull-request-comments.yml b/.github/workflows/pull-request-comments.yml index df276270f1396..189eb1c1737d0 100644 --- a/.github/workflows/pull-request-comments.yml +++ b/.github/workflows/pull-request-comments.yml @@ -49,7 +49,7 @@ jobs: **Pull requests are never merged on GitHub.** The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making. - More information about how GitHub pull requests can be used to contribute to WordPress can be found in [this blog post](https://make.wordpress.org/core/2020/02/21/working-on-trac-tickets-using-github-pull-requests/). + More information about how GitHub pull requests can be used to contribute to WordPress can be found in [the Core Handbook](https://make.wordpress.org/core/handbook/contribute/git/github-pull-requests-for-code-review/). **Please include automated tests.** Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the [Automated Testing](https://make.wordpress.org/core/handbook/testing/automated-testing/) page in the handbook. @@ -163,3 +163,50 @@ jobs: `; github.rest.issues.createComment( commentInfo ); + + # Leaves a comment on a pull request when no Trac ticket is included in the pull request description. + trac-ticket-check: + name: Comment on a pull request when no Trac ticket is included + runs-on: ubuntu-latest + 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 comment if missing + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 + 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.some( comment => + comment.user.type === 'Bot' && comment.body.includes( 'Trac Ticket Missing' ) + ); + + if ( hasMissingTicketComment ) 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/([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/). + `, + } ); + }