Skip to content

Commit

Permalink
CI: Correctly post and automatically update the PR preview comment link
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Apr 15, 2024
1 parent 48ecf13 commit 778ba6f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 55 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/has-valid-blueprint-json.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
112 changes: 58 additions & 54 deletions .github/workflows/preview-comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
if ( commentInfo.comment_id ) {
await github.rest.issues.updateComment( commentInfo );
} else {
await github.rest.issues.createComment( commentInfo );
}
2 changes: 1 addition & 1 deletion validate_pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 778ba6f

Please sign in to comment.