Skip to content
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

Fix Jetpack live branches #11823

Merged
merged 1 commit into from
Apr 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 35 additions & 25 deletions tools/jetpack-live-branches/jetpack-live-branches.user.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ==UserScript==
// @name Jetpack Live Branches
// @namespace https://wordpress.com/
// @version 1.12
// @version 1.13
// @description Adds links to PRs pointing to Jurassic Ninja sites for live-testing a changeset
// @require https://code.jquery.com/jquery-3.3.1.min.js
// @match https://github.com/Automattic/jetpack/pull/*
Expand All @@ -15,15 +15,31 @@

function doit() {
const markdownBody = document.querySelectorAll( '.markdown-body' )[ 0 ];
const branch = jQuery( '.head-ref:first' ).text();
const branchIsForked = branch.includes( ':' );
const branchIsMerged =
$( '.gh-header-meta .State' )
.text()
.trim() === 'Merged';
const currentBranch = jQuery( '.head-ref:first' ).text();
const branchIsForked = currentBranch.includes( ':' );
const branchStatus = $( '.gh-header-meta .State' )
.text()
.trim();

if ( ! branchIsForked && ! branchIsMerged ) {
const link = getLink();
if ( branchStatus === 'Merged' ) {
const contents = `
<p><strong>This branch is already merged.</strong></p>
<p><a target="_blank" rel="nofollow noopener" href="${ getLink( 'master' ) }">
Test with <code>master</code> branch instead.
</a></p>
`;
appendHtml( markdownBody, contents );
} else if ( branchStatus === 'Draft' ) {
appendHtml(
markdownBody,
'<p><strong>This branch is a draft. You can open live branches only from open pull requests.</strong></p>'
);
} else if ( branchIsForked ) {
appendHtml(
markdownBody,
"<p><strong>This branch can't be tested live because it comes from a forked version of this repo.</strong></p>"
);
} else {
const contents = `
<h4>Settings</h4>
${ getOptionsList(
Expand Down Expand Up @@ -111,22 +127,14 @@
33
) }
<p>
<a id="jetpack-beta-branch-link" target="_blank" rel="nofollow noopener" href="${ link }">
${ link }
</a>
<a id="jetpack-beta-branch-link" target="_blank" rel="nofollow noopener" href="#">…</a>
</p>
`;
appendHtml( markdownBody, contents );
} else if ( ! branchIsMerged ) {
appendHtml(
markdownBody,
"<p><strong>This branch can't be tested live because it comes from a forked version of this repo.</strong></p>"
);
} else {
appendHtml( markdownBody, '<p><strong>This branch is already merged.</strong></p>' );
updateLink();
}

function getLink() {
function getLink( branch ) {
const query = [ 'jetpack-beta', `branch=${ branch }` ];
$( '#jetpack-live-branches input[type=checkbox]:checked' ).each( ( i, input ) => {
query.push( input.name );
Expand Down Expand Up @@ -163,13 +171,15 @@
`<h2>Jetpack Live Branches</h2> ${ contents }`
);
$el.append( liveBranches );
$el.find( 'input[type=checkbox]' ).change( toggle );
$el.find( 'input[type=checkbox]' ).change( function( e ) {
e.stopPropagation();
e.preventDefault();
updateLink();
} );
}

function toggle( e ) {
e.stopPropagation();
e.preventDefault();
const link = getLink();
function updateLink() {
const link = getLink( currentBranch );
$( '#jetpack-beta-branch-link' )
.attr( 'href', link )
.text( link );
Expand Down