From 778ba6fad9e07cec6fa3d0803cbf589bdd5ea8b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Tue, 16 Apr 2024 00:44:32 +0200 Subject: [PATCH] CI: Correctly post and automatically update the PR preview comment link --- .../workflows/has-valid-blueprint-json.yml | 3 + .github/workflows/preview-comment.yml | 112 +++++++++--------- validate_pr.py | 2 +- 3 files changed, 62 insertions(+), 55 deletions(-) diff --git a/.github/workflows/has-valid-blueprint-json.yml b/.github/workflows/has-valid-blueprint-json.yml index fc094fe..d435cc1 100644 --- a/.github/workflows/has-valid-blueprint-json.yml +++ b/.github/workflows/has-valid-blueprint-json.yml @@ -7,6 +7,9 @@ jobs: check_blueprints: runs-on: ubuntu-latest + env: + GITHUB_BRANCH: ${{ github.event.pull_request.head.ref }} + steps: - name: Checkout code uses: actions/checkout@v2 diff --git a/.github/workflows/preview-comment.yml b/.github/workflows/preview-comment.yml index 21c92a8..36303c3 100644 --- a/.github/workflows/preview-comment.yml +++ b/.github/workflows/preview-comment.yml @@ -2,73 +2,77 @@ name: Pull Request Comments on: - pull_request_target: - types: [ 'opened' ] + pull_request: # 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-latest - permissions: - issues: write - pull-requests: write - steps: - - name: Leave a comment about testing with Playground - uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 - with: - script: | - const fs = require( 'fs' ); - const issue_number = context.payload.pull_request.number; - const branch_name = context.payload.pull_request.head.ref; + # 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-latest + permissions: + issues: write + pull-requests: write + steps: + - name: Leave a comment about testing with Playground + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 + with: + script: | + const fs = require( 'fs' ); + const issue_number = context.payload.pull_request.number; - // First, find a blueprint.json file in the pull request - const blueprintFile = ( await github.rest.pulls.listFiles( commentInfo ) ).data.filter( file => file.filename.endsWith( '/blueprint.json' ) )[0]; - if ( !blueprintFile ) { - return; - } + const prInfo = { + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: issue_number, + }; - // 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, - }; + // First, find a blueprint.json file in the pull request + const blueprintFile = ( await github.rest.pulls.listFiles( prInfo ) ).data.filter( file => file.filename.endsWith( '/blueprint.json' ) )[0]; + if ( !blueprintFile ) { + return; + } - const comments = ( await github.rest.issues.listComments( commentInfo ) ).data; + // 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' ) ) { - commentInfo.comment_id = comment.id; - break; - } - }; + for ( const currentComment of comments ) { + if ( currentComment.user.type === 'Bot' && currentComment.body.includes( 'Test using WordPress Playground' ) ) { + commentInfo.comment_id = currentComment.id; + break; + } + }; - // 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. + // No comment was found. Create one. + const branch_name = context.payload.pull_request.merged ? context.payload.repository.default_branch : context.payload.pull_request.head.ref; + 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. + [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 - - The Plugin and Theme Directories cannot be accessed within Playground. - - 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/wordpress-playground.yml). + ### Some things to be aware of + - The Plugin and Theme Directories cannot be accessed within Playground. + - 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/wordpress-playground.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. + 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/?blueprint-url=https://raw.githubusercontent.com/adamziel/blueprints/${ branch_name }/${ blueprintFile }). - `; + [Test this pull request with WordPress Playground](https://playground.wordpress.net/?blueprint-url=https://raw.githubusercontent.com/adamziel/blueprints/${ branch_name }/${ blueprintFile.filename }). + `; - if ( commentInfo.comment_id ) { - await github.rest.issues.updateComment( commentInfo ); - } else { - await github.rest.issues.createComment( commentInfo ); - } \ No newline at end of file + if ( commentInfo.comment_id ) { + await github.rest.issues.updateComment( commentInfo ); + } else { + await github.rest.issues.createComment( commentInfo ); + } diff --git a/validate_pr.py b/validate_pr.py index 4c4f9f8..ca298b7 100644 --- a/validate_pr.py +++ b/validate_pr.py @@ -49,7 +49,7 @@ def validate_blueprints(): # Check if the URLs all point to raw.githubusercontent.com/adamziel/blueprints/{CURRENT BRANCH} urls_valid = True - current_branch = os.popen('git rev-parse --abbrev-ref HEAD').read().strip() + current_branch = os.environ.get('GITHUB_BRANCH') or os.popen('git rev-parse --abbrev-ref HEAD').read().strip() for url in urls: if not url.startswith('https://') and not url.startswith('http://'): continue