Skip to content

Commit fcbaf0f

Browse files
committed
CI Automation: Add comment with PR preview link
1 parent 6b473ed commit fcbaf0f

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

.github/workflows/preview-comment.yml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Responsible for making comments on pull requests, such as commenting for first time contributors.
2+
name: Pull Request Comments
3+
4+
on:
5+
pull_request_target:
6+
types: [ 'opened' ]
7+
8+
# Disable permissions for all available scopes by default.
9+
# Any needed permissions should be configured at the job level.
10+
permissions: {}
11+
12+
jobs:
13+
# Leaves a comment on a pull request with a link to test the changes in a WordPress Playground instance.
14+
playground-details:
15+
name: Comment on a pull request with Playground details
16+
runs-on: ubuntu-latest
17+
permissions:
18+
issues: write
19+
pull-requests: write
20+
steps:
21+
- name: Leave a comment about testing with Playground
22+
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
23+
with:
24+
script: |
25+
const fs = require( 'fs' );
26+
const issue_number = context.payload.pull_request.number;
27+
const branch_name = context.payload.pull_request.head.ref;
28+
29+
// First, find a blueprint.json file in the pull request
30+
const blueprintFile = ( await github.rest.pulls.listFiles( commentInfo ) ).data.filter( file => file.filename.endsWith( '/blueprint.json' ) )[0];
31+
if ( !blueprintFile ) {
32+
return;
33+
}
34+
35+
// Comments are only added after the first successful build. Check for the presence of a comment and bail early.
36+
const commentInfo = {
37+
owner: context.repo.owner,
38+
repo: context.repo.repo,
39+
issue_number,
40+
};
41+
42+
const comments = ( await github.rest.issues.listComments( commentInfo ) ).data;
43+
44+
for ( const currentComment of comments ) {
45+
if ( currentComment.user.type === 'Bot' && currentComment.body.includes( 'Test using WordPress Playground' ) ) {
46+
commentInfo.comment_id = comment.id;
47+
break;
48+
}
49+
};
50+
51+
// No comment was found. Create one.
52+
commentInfo.body = `## Test using WordPress Playground
53+
The changes in this pull request can previewed and tested using a [WordPress Playground](https://developer.wordpress.org/playground/) instance.
54+
55+
[WordPress Playground](https://developer.wordpress.org/playground/) is an experimental project that creates a full WordPress instance entirely within the browser.
56+
57+
### Some things to be aware of
58+
- The Plugin and Theme Directories cannot be accessed within Playground.
59+
- All changes will be lost when closing a tab with a Playground instance.
60+
- All changes will be lost when refreshing the page.
61+
- A fresh instance is created each time the link below is clicked.
62+
- 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,
63+
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).
64+
65+
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.
66+
67+
[Test this pull request with WordPress Playground](https://playground.wordpress.net/?blueprint-url=https://raw.githubusercontent.com/adamziel/blueprints/${ branch_name }/${ blueprintFile }).
68+
`;
69+
70+
if ( commentInfo.comment_id ) {
71+
await github.rest.issues.updateComment( commentInfo );
72+
} else {
73+
await github.rest.issues.createComment( commentInfo );
74+
}

0 commit comments

Comments
 (0)