-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add live preview URL on Pull Request #54303
Open
bangank36
wants to merge
11
commits into
WordPress:trunk
Choose a base branch
from
bangank36:feat/gutenberg-preview-link
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+508
−2
Open
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4c177c7
Enable workflow_run event for Build
bangank36 b30e554
Add new tasks
bangank36 f0d7c4c
Skip repo check on Build Step
bangank36 0c147dd
Clean up pr preview
bangank36 40230ba
Update changelog
bangank36 8757cd3
Fix spacing issue on Readme
bangank36 cb1822d
Update unit tests
bangank36 0c9f966
Update use constants
bangank36 81e8610
Remove icons
bangank36 19fe4f1
Remove remaining icons
bangank36 2653f17
Update type for WorkflowRun
bangank36 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
12 changes: 12 additions & 0 deletions
12
packages/project-management-automation/lib/tasks/pr-preview-link/README.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
PR Preview Link | ||
=== | ||
|
||
Adds a comment to new PRs with a comment about Gutenberg plugin build status: | ||
- Latest commit | ||
- Build status of [Build Gutenberg Plugin Zip](https://github.com/WordPress/gutenberg/blob/d19eb92d96886f4bb8e1028c5d54d365e37d71e4/.github/workflows/build-plugin-zip.yml#L1) | ||
- Link to live preview `http://gutenberg.run/{pr_number}` | ||
- Link to artifact download URL | ||
|
||
## Rationale | ||
|
||
Preview sites make PRs much easier to test, especially for folks who don't have a dev environment setup. The comment also saves contributors some extra clicks to download the latest build of the Gutenberg plugin. |
168 changes: 168 additions & 0 deletions
168
packages/project-management-automation/lib/tasks/pr-preview-link/index.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
// @ts-nocheck | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
const debug = require( '../../debug' ); | ||
|
||
/** @typedef {ReturnType<import('@actions/github').getOctokit>} GitHub */ | ||
/** @typedef {any} WorkflowRunEvent */ | ||
bangank36 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Identifier used to find existing comment. | ||
* | ||
* @type {string} | ||
*/ | ||
const COMMENT_PLACEHOLDER = 'gutenberg-run-placeholder:cmt@v1'; | ||
|
||
const createBuildSummary = async ( | ||
{ buildStatus, latestCommit, pullRequestNumber, artifact }, | ||
octokit | ||
) => { | ||
let status, previewMsg, artifactMsg; | ||
status = previewMsg = artifactMsg = '🚧 Building in progress...'; | ||
bangank36 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ( buildStatus === 'success' ) { | ||
status = '✅ Build successful!'; | ||
bangank36 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
previewMsg = `🔗 [gutenberg.run/${ pullRequestNumber }](gutenberg.run/${ pullRequestNumber } )`; | ||
bangank36 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
artifactMsg = `📦 [gutenberg-plugin](${ artifact.url }) - ${ artifact.size } MB`; | ||
bangank36 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else if ( buildStatus === 'failure' ) { | ||
status = previewMsg = artifactMsg = '🚫 Build failed!'; | ||
bangank36 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
const response = await octokit.rest.markdown.render( { | ||
mode: 'gfm', | ||
text: ` | ||
# Gutenberg Plugin build status | ||
|
||
| Name | Result | | ||
| ----------------------- | - | | ||
| **Last commit:** | ${ latestCommit } | | ||
| **Status**: | ${ status } | | ||
| **Preview URL**: | ${ previewMsg } | | ||
| **Gutenberg plugin zip**: | ${ artifactMsg } | | ||
`, | ||
} ); | ||
return `<!--${ COMMENT_PLACEHOLDER }-->${ response.data }`; | ||
}; | ||
|
||
const writeComment = async ( | ||
{ owner, repo, pullRequestNumber, commentBody }, | ||
octokit | ||
) => { | ||
debug( 'pr-preview-link: Find and replace build status comment' ); | ||
// First check if there is already a comment from this action | ||
const comments = await octokit.rest.issues.listComments( { | ||
issue_number: pullRequestNumber, | ||
owner, | ||
repo, | ||
} ); | ||
|
||
const existingComment = comments.data.filter( ( comment ) => | ||
comment.body.includes( COMMENT_PLACEHOLDER ) | ||
); | ||
|
||
if ( existingComment.length ) { | ||
await octokit.rest.issues.updateComment( { | ||
owner, | ||
repo, | ||
comment_id: existingComment[ 0 ].id, | ||
body: commentBody, | ||
} ); | ||
} else { | ||
await octokit.rest.issues.createComment( { | ||
owner, | ||
repo, | ||
issue_number: pullRequestNumber, | ||
body: commentBody, | ||
} ); | ||
} | ||
}; | ||
|
||
/** | ||
* Adds a comment to new PRs with a link to the corresponding gutenberg.run preview site. | ||
* | ||
* @param {WorkflowRunEvent} payload Workflow Run event payload. | ||
* @param {GitHub} octokit Initialized Octokit REST client. | ||
*/ | ||
async function prPreviewLink( payload, octokit ) { | ||
const action = payload.action; | ||
const repo = payload.repository.name; | ||
const owner = payload.repository.owner.login; | ||
const repoHtmlUrl = payload.repository.html_url; | ||
const workflowRun = payload.workflow_run; | ||
const workflowRunId = workflowRun.id; | ||
const pullRequestNumber = workflowRun.pull_requests[ 0 ].number; | ||
const checkSuiteId = workflowRun.check_suite_id; | ||
const latestCommit = `${ repoHtmlUrl }/pull/${ pullRequestNumber }/commits/${ workflowRun.head_sha }`; | ||
|
||
if ( action === 'in_progress' ) { | ||
const commentBody = await createBuildSummary( | ||
{ | ||
buildStatus: action, | ||
latestCommit, | ||
pullRequestNumber, | ||
artifact: null, | ||
}, | ||
octokit | ||
); | ||
|
||
await writeComment( | ||
{ owner, repo, pullRequestNumber, commentBody }, | ||
octokit | ||
); | ||
} | ||
|
||
if ( action === 'completed' ) { | ||
debug( 'pr-preview-link: Build complete, request artifact from API' ); | ||
const artifactsResponse = | ||
await octokit.rest.actions.listWorkflowRunArtifacts( { | ||
owner, | ||
repo, | ||
run_id: workflowRunId, | ||
name: 'gutenberg-plugin', | ||
per_page: 1, | ||
} ); | ||
const artifacts = artifactsResponse.data.artifacts; | ||
|
||
let commentBody; | ||
if ( ! artifacts.length ) { | ||
debug( 'pr-preview-link: No artifact found, mark as failure' ); | ||
|
||
commentBody = await createBuildSummary( | ||
{ | ||
buildStatus: 'failure', | ||
latestCommit, | ||
pullRequestNumber, | ||
artifactsUrl: null, | ||
}, | ||
octokit | ||
); | ||
} else { | ||
debug( 'pr-preview-link: Found artifact, mark as success' ); | ||
|
||
const artifact = artifacts[ 0 ]; | ||
// The artifact URL on Checks screen | ||
const artifactUrl = `${ repoHtmlUrl }/suites/${ checkSuiteId }/artifacts/${ artifact.id }`; | ||
const sizeInMB = artifact.size_in_bytes / ( 1024 * 1024 ); | ||
commentBody = await createBuildSummary( | ||
{ | ||
buildStatus: 'success', | ||
latestCommit, | ||
pullRequestNumber, | ||
artifact: { | ||
url: artifactUrl, | ||
size: sizeInMB.toFixed( 2 ), | ||
}, | ||
}, | ||
octokit | ||
); | ||
} | ||
|
||
await writeComment( | ||
{ owner, repo, pullRequestNumber, commentBody }, | ||
octokit | ||
); | ||
} | ||
} | ||
|
||
module.exports = prPreviewLink; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: I made this always true to test on personal repo, should be removed on trunk