2022-07-14 14:36:15 +00:00
|
|
|
/**
|
|
|
|
* Tool to automate steps to cherry pick fixes into release branch.
|
|
|
|
*
|
|
|
|
* @package
|
|
|
|
*/
|
|
|
|
|
|
|
|
import fetch from 'node-fetch';
|
|
|
|
import os from 'os';
|
|
|
|
import fs from 'node:fs';
|
|
|
|
import path from 'node:path';
|
|
|
|
import { spawnSync, spawn } from 'node:child_process';
|
|
|
|
import ora from 'ora';
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
const args = process.argv.slice( 2 );
|
|
|
|
const usage =
|
|
|
|
'Usage: pnpm cherry-pick <release_branch> <pull_request_number>. Separate pull request numbers with a comma (no space) if more than one.';
|
|
|
|
const releaseBranch = args[ 0 ];
|
|
|
|
const prs = args[ 1 ];
|
|
|
|
let githubToken = '';
|
|
|
|
let tempWooDir = '';
|
|
|
|
const changelogsToBeDeleted = [];
|
|
|
|
const prCommits = {};
|
|
|
|
let githubRemoteURL = 'https';
|
2022-07-14 14:36:15 +00:00
|
|
|
|
|
|
|
if ( typeof process.env.GITHUB_CHERRY_PICK_TOKEN === 'undefined' ) {
|
2022-08-02 18:54:41 +00:00
|
|
|
console.error(
|
|
|
|
'A GitHub token needs to be assigned to the "GITHUB_CHERRY_PICK_TOKEN" environment variable'
|
|
|
|
);
|
2022-07-14 14:36:15 +00:00
|
|
|
process.exit( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( typeof process.env.GITHUB_REMOTE_URL !== 'undefined' ) {
|
|
|
|
githubRemoteURL = process.env.GITHUB_REMOTE_URL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no arguments are passed.
|
|
|
|
if ( ! args.length ) {
|
|
|
|
console.error( usage );
|
|
|
|
process.exit( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
// If missing one of the arguments.
|
|
|
|
if ( typeof releaseBranch === 'undefined' || typeof prs === 'undefined' ) {
|
|
|
|
console.error( usage );
|
|
|
|
process.exit( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accounts for multiple PRs.
|
2022-08-02 18:54:41 +00:00
|
|
|
const prsArr = prs.split( ',' );
|
|
|
|
const version = releaseBranch.replace( 'release/', '' );
|
|
|
|
const cherryPickBranch =
|
|
|
|
'cherry-pick-' + version + '/' + prsArr.toString().replace( ',', '-' );
|
|
|
|
githubToken = process.env.GITHUB_CHERRY_PICK_TOKEN;
|
2022-07-14 14:36:15 +00:00
|
|
|
|
|
|
|
async function getCommitFromPrs( prsArr ) {
|
2022-08-02 18:54:41 +00:00
|
|
|
const properties = {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/vnd.github.v3+json',
|
|
|
|
Authorization: `token ${ githubToken }`,
|
|
|
|
},
|
|
|
|
};
|
2022-07-14 14:36:15 +00:00
|
|
|
|
|
|
|
for ( const pr of prsArr ) {
|
2022-08-02 18:54:41 +00:00
|
|
|
const response = await fetch(
|
|
|
|
'https://api.github.com/repos/woocommerce/woocommerce/pulls/' + pr,
|
|
|
|
properties
|
|
|
|
);
|
2022-07-14 14:36:15 +00:00
|
|
|
|
|
|
|
if ( response.status !== 200 ) {
|
|
|
|
throw 'One or more of the PR reference was not found.';
|
|
|
|
}
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
await response.json().then( ( data ) => {
|
2022-07-14 14:36:15 +00:00
|
|
|
prCommits[ pr ] = data.merge_commit_sha;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks out a branch.
|
|
|
|
*
|
2022-08-02 18:54:41 +00:00
|
|
|
* @param string branch The branch to checkout.
|
|
|
|
* @param boolean newBranch A flag to create a new branch.
|
|
|
|
* @param branch
|
|
|
|
* @param newBranch
|
2022-07-14 14:36:15 +00:00
|
|
|
*/
|
|
|
|
function checkoutBranch( branch, newBranch = false ) {
|
|
|
|
const spinner = ora( {
|
|
|
|
spinner: 'bouncingBar',
|
2022-08-02 18:54:41 +00:00
|
|
|
color: 'green',
|
2022-07-14 14:36:15 +00:00
|
|
|
} );
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
const output = [];
|
2022-07-14 14:36:15 +00:00
|
|
|
|
|
|
|
return new Promise( ( resolve, reject ) => {
|
2022-08-02 18:54:41 +00:00
|
|
|
const response = newBranch
|
|
|
|
? spawn( 'git', [ 'checkout', '-b', branch ] )
|
|
|
|
: spawn( 'git', [ 'checkout', branch ] );
|
2022-07-14 14:36:15 +00:00
|
|
|
|
|
|
|
response.stdout.on( 'data', ( data ) => {
|
2022-08-02 18:54:41 +00:00
|
|
|
output.push( `${ data }` );
|
2022-07-14 14:36:15 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
response.stderr.on( 'data', ( data ) => {
|
2022-08-02 18:54:41 +00:00
|
|
|
output.push( `${ data }` );
|
2022-07-14 14:36:15 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
response.on( 'close', ( code ) => {
|
2022-08-02 18:54:41 +00:00
|
|
|
if ( `${ code }` == 0 ) {
|
|
|
|
spinner.succeed( `Switched to '${ branch }'` );
|
2022-07-14 14:36:15 +00:00
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
reject( `error: ${ output.toString().replace( ',', '\n' ) }` );
|
2022-07-14 14:36:15 +00:00
|
|
|
} );
|
2022-08-02 18:54:41 +00:00
|
|
|
} ).catch( ( err ) => {
|
2022-07-14 14:36:15 +00:00
|
|
|
spinner.fail( 'Failed to switch branch' );
|
|
|
|
throw err;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the temp directory in system temp.
|
|
|
|
*
|
2022-08-02 18:54:41 +00:00
|
|
|
* @param string path The path to create.
|
|
|
|
* @param path
|
2022-07-14 14:36:15 +00:00
|
|
|
*/
|
|
|
|
function createDir( path ) {
|
|
|
|
return new Promise( ( resolve, reject ) => {
|
|
|
|
fs.mkdtemp( path, ( err, directory ) => {
|
|
|
|
if ( err ) {
|
|
|
|
reject( err );
|
|
|
|
}
|
|
|
|
|
|
|
|
tempWooDir = directory;
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
} );
|
2022-08-02 18:54:41 +00:00
|
|
|
} ).catch( ( err ) => {
|
2022-07-14 14:36:15 +00:00
|
|
|
throw err;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clones the WooCommerce repo into temp dir.
|
|
|
|
*
|
2022-08-02 18:54:41 +00:00
|
|
|
* @param string woodir The temporary system directory.
|
2022-07-14 14:36:15 +00:00
|
|
|
*/
|
|
|
|
function cloneWoo() {
|
|
|
|
const spinner = ora( {
|
2022-08-02 18:54:41 +00:00
|
|
|
text: `Cloning WooCommerce into ${ tempWooDir }/woocommerce`,
|
2022-07-14 14:36:15 +00:00
|
|
|
spinner: 'bouncingBar',
|
2022-08-02 18:54:41 +00:00
|
|
|
color: 'green',
|
2022-07-14 14:36:15 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
spinner.start();
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
const output = [];
|
2022-07-14 14:36:15 +00:00
|
|
|
|
|
|
|
return new Promise( ( resolve, reject ) => {
|
|
|
|
let url = 'https://github.com/woocommerce/woocommerce.git';
|
|
|
|
|
|
|
|
if ( githubRemoteURL === 'ssh' ) {
|
|
|
|
url = 'git@github.com:woocommerce/woocommerce.git';
|
|
|
|
}
|
|
|
|
|
|
|
|
const response = spawn( 'git', [ 'clone', url ] );
|
|
|
|
|
|
|
|
response.stdout.on( 'data', ( data ) => {
|
2022-08-02 18:54:41 +00:00
|
|
|
output.push( `${ data }` );
|
2022-07-14 14:36:15 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
response.stderr.on( 'data', ( data ) => {
|
2022-08-02 18:54:41 +00:00
|
|
|
output.push( `${ data }` );
|
2022-07-14 14:36:15 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
response.on( 'close', ( code ) => {
|
2022-08-02 18:54:41 +00:00
|
|
|
if ( `${ code }` == 0 ) {
|
2022-07-14 14:36:15 +00:00
|
|
|
spinner.succeed();
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
reject( `error: ${ output.toString().replace( ',', '\n' ) }` );
|
2022-07-14 14:36:15 +00:00
|
|
|
} );
|
2022-08-02 18:54:41 +00:00
|
|
|
} ).catch( ( err ) => {
|
2022-07-14 14:36:15 +00:00
|
|
|
spinner.fail( 'Fail to clone WooCommerce!' );
|
|
|
|
throw err;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cherry picks.
|
|
|
|
*
|
2022-08-02 18:54:41 +00:00
|
|
|
* @param string commit The commit hash to cherry pick.
|
|
|
|
* @param commit
|
2022-07-14 14:36:15 +00:00
|
|
|
*/
|
|
|
|
function cherryPick( commit ) {
|
|
|
|
const spinner = ora( {
|
2022-08-02 18:54:41 +00:00
|
|
|
text: `Cherry picking ${ commit }`,
|
2022-07-14 14:36:15 +00:00
|
|
|
spinner: 'bouncingBar',
|
2022-08-02 18:54:41 +00:00
|
|
|
color: 'green',
|
2022-07-14 14:36:15 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
spinner.start();
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
const response = spawnSync( 'git', [ 'cherry-pick', commit ], {
|
|
|
|
encoding: 'utf-8',
|
|
|
|
} );
|
2022-07-14 14:36:15 +00:00
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
if ( response.status === 0 ) {
|
2022-07-14 14:36:15 +00:00
|
|
|
spinner.succeed();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( response.stderr ) {
|
2022-08-02 18:54:41 +00:00
|
|
|
if (
|
|
|
|
response.stderr.match( 'fatal: bad revision' ) ||
|
|
|
|
response.stderr.match( 'error: could not apply' )
|
|
|
|
) {
|
|
|
|
spinner.fail( `Fail cherry picking ${ commit }` );
|
|
|
|
console.log( response.stdout );
|
|
|
|
throw `stderr: ${ response.stderr }`;
|
2022-07-14 14:36:15 +00:00
|
|
|
}
|
2022-08-02 18:54:41 +00:00
|
|
|
}
|
2022-07-14 14:36:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to change directories.
|
|
|
|
*
|
2022-08-02 18:54:41 +00:00
|
|
|
* @param string dir The directory to change to.
|
|
|
|
* @param dir
|
2022-07-14 14:36:15 +00:00
|
|
|
*/
|
|
|
|
function chdir( dir ) {
|
|
|
|
try {
|
|
|
|
process.chdir( dir );
|
|
|
|
} catch ( e ) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the changelog for readme.txt.
|
|
|
|
*
|
2022-08-02 18:54:41 +00:00
|
|
|
* @param string pr The PR to use.
|
|
|
|
* @param string commit The commit to use.
|
|
|
|
* @param pr
|
|
|
|
* @param commit
|
2022-07-14 14:36:15 +00:00
|
|
|
*/
|
|
|
|
async function generateChangelog( pr, commit ) {
|
2022-08-02 18:54:41 +00:00
|
|
|
const properties = {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/vnd.github.v3+json',
|
|
|
|
},
|
|
|
|
};
|
2022-07-14 14:36:15 +00:00
|
|
|
|
|
|
|
if ( githubToken ) {
|
|
|
|
properties.headers.Authorization = 'token ' + githubToken;
|
|
|
|
}
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
const response = await fetch(
|
|
|
|
'https://api.github.com/repos/woocommerce/woocommerce/commits/' +
|
|
|
|
commit,
|
|
|
|
properties
|
|
|
|
);
|
2022-07-14 14:36:15 +00:00
|
|
|
|
|
|
|
if ( response.status !== 200 ) {
|
|
|
|
throw 'Commit was not found.';
|
|
|
|
}
|
|
|
|
|
|
|
|
let changelogTxt = '';
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
await response.json().then( ( data ) => {
|
2022-07-14 14:36:15 +00:00
|
|
|
for ( const file of data.files ) {
|
|
|
|
if ( file.filename.match( 'plugins/woocommerce/changelog/' ) ) {
|
|
|
|
if ( changelogsToBeDeleted.indexOf( file.filename ) === -1 ) {
|
|
|
|
changelogsToBeDeleted.push( file.filename );
|
|
|
|
}
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
let changelogEntry = '';
|
2022-07-14 14:36:15 +00:00
|
|
|
let changelogEntryType = '';
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
fs.readFile(
|
|
|
|
'./' + file.filename,
|
|
|
|
'utf-8',
|
|
|
|
function ( err, data ) {
|
|
|
|
if ( err ) {
|
|
|
|
throw err;
|
|
|
|
}
|
2022-07-14 14:36:15 +00:00
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
const changelogEntryArr = data.split( '\n' );
|
|
|
|
changelogEntryType = data.match( /Type: (.+)/i );
|
|
|
|
changelogEntryType =
|
|
|
|
changelogEntryType[ 1 ].charAt( 0 ).toUpperCase() +
|
|
|
|
changelogEntryType[ 1 ].slice( 1 );
|
|
|
|
|
|
|
|
changelogEntry = changelogEntryArr.filter( ( el ) => {
|
|
|
|
return (
|
|
|
|
el !== null &&
|
|
|
|
typeof el !== 'undefined' &&
|
|
|
|
el !== ''
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
changelogEntry =
|
|
|
|
changelogEntry[ changelogEntry.length - 1 ];
|
|
|
|
|
|
|
|
// Check if changelogEntry is what we want.
|
|
|
|
if ( changelogEntry.length < 1 ) {
|
|
|
|
changelogEntry = false;
|
|
|
|
}
|
2022-07-14 14:36:15 +00:00
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
if ( changelogEntry.match( /significance:/i ) ) {
|
|
|
|
changelogEntry = false;
|
|
|
|
}
|
2022-07-14 14:36:15 +00:00
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
if ( changelogEntry.match( /type:/i ) ) {
|
|
|
|
changelogEntry = false;
|
|
|
|
}
|
2022-07-14 14:36:15 +00:00
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
if ( changelogEntry.match( /comment:/i ) ) {
|
|
|
|
changelogEntry = false;
|
|
|
|
}
|
2022-07-14 14:36:15 +00:00
|
|
|
}
|
2022-08-02 18:54:41 +00:00
|
|
|
);
|
2022-07-14 14:36:15 +00:00
|
|
|
|
|
|
|
if ( changelogEntry === false ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
fs.readFile(
|
|
|
|
'./plugins/woocommerce/readme.txt',
|
|
|
|
'utf-8',
|
|
|
|
function ( err, data ) {
|
|
|
|
if ( err ) {
|
|
|
|
throw err;
|
2022-07-14 14:36:15 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
const spinner = ora( {
|
|
|
|
text: `Generating changelog entry for PR ${ pr }`,
|
|
|
|
spinner: 'bouncingBar',
|
|
|
|
color: 'green',
|
|
|
|
} );
|
|
|
|
|
|
|
|
spinner.start();
|
|
|
|
|
|
|
|
changelogTxt = data.split( '\n' );
|
|
|
|
let isInRange = false;
|
|
|
|
const newChangelogTxt = [];
|
|
|
|
|
|
|
|
for ( const line of changelogTxt ) {
|
|
|
|
if (
|
|
|
|
isInRange === false &&
|
|
|
|
line === '== Changelog =='
|
|
|
|
) {
|
|
|
|
isInRange = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
isInRange === true &&
|
|
|
|
line.match( /\*\*WooCommerce Blocks/ )
|
|
|
|
) {
|
|
|
|
isInRange = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the first match of the entry "Type".
|
|
|
|
if (
|
|
|
|
isInRange &&
|
|
|
|
line.match( `\\* ${ changelogEntryType } -` )
|
|
|
|
) {
|
|
|
|
newChangelogTxt.push(
|
|
|
|
'* ' +
|
|
|
|
changelogEntryType +
|
|
|
|
' - ' +
|
|
|
|
changelogEntry +
|
|
|
|
` [#${ pr }](https://github.com/woocommerce/woocommerce/pull/${ pr })`
|
|
|
|
);
|
|
|
|
newChangelogTxt.push( line );
|
|
|
|
isInRange = false;
|
|
|
|
continue;
|
|
|
|
}
|
2022-07-14 14:36:15 +00:00
|
|
|
|
|
|
|
newChangelogTxt.push( line );
|
|
|
|
}
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
fs.writeFile(
|
|
|
|
'./plugins/woocommerce/readme.txt',
|
|
|
|
newChangelogTxt.join( '\n' ),
|
|
|
|
( err ) => {
|
|
|
|
if ( err ) {
|
|
|
|
spinner.fail(
|
|
|
|
`Unable to generate the changelog entry for PR ${ pr }`
|
|
|
|
);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
spinner.succeed();
|
|
|
|
}
|
|
|
|
);
|
2022-07-14 14:36:15 +00:00
|
|
|
}
|
2022-08-02 18:54:41 +00:00
|
|
|
);
|
2022-07-14 14:36:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Git remove changelog files.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function deleteChangelogFiles() {
|
|
|
|
const spinner = ora( {
|
|
|
|
spinner: 'bouncingBar',
|
2022-08-02 18:54:41 +00:00
|
|
|
color: 'green',
|
2022-07-14 14:36:15 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
const files = changelogsToBeDeleted.join( ' ' );
|
2022-08-02 18:54:41 +00:00
|
|
|
const filesFormatted = '\n' + files.replace( ' ', '\n' );
|
|
|
|
const output = [];
|
2022-07-14 14:36:15 +00:00
|
|
|
|
|
|
|
return new Promise( ( resolve, reject ) => {
|
|
|
|
const response = spawn( 'git', [ 'rm', files ], { shell: true } );
|
|
|
|
|
|
|
|
response.stdout.on( 'data', ( data ) => {
|
2022-08-02 18:54:41 +00:00
|
|
|
output.push( `${ data }` );
|
2022-07-14 14:36:15 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
response.stderr.on( 'data', ( data ) => {
|
2022-08-02 18:54:41 +00:00
|
|
|
output.push( `${ data }` );
|
2022-07-14 14:36:15 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
response.on( 'close', ( code ) => {
|
2022-08-02 18:54:41 +00:00
|
|
|
if ( `${ code }` == 0 ) {
|
|
|
|
spinner.succeed(
|
|
|
|
`Removed changelog files:${ filesFormatted }`
|
|
|
|
);
|
2022-07-14 14:36:15 +00:00
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
reject( `error: ${ output.toString().replace( ',', '\n' ) }` );
|
2022-07-14 14:36:15 +00:00
|
|
|
} );
|
2022-08-02 18:54:41 +00:00
|
|
|
} ).catch( ( err ) => {
|
2022-07-14 14:36:15 +00:00
|
|
|
spinner.fail( 'Fail to delete changelog files' );
|
|
|
|
throw err;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Commit changes.
|
|
|
|
*
|
2022-08-02 18:54:41 +00:00
|
|
|
* @param string msg The message for the commit.
|
|
|
|
* @param msg
|
2022-07-14 14:36:15 +00:00
|
|
|
*/
|
|
|
|
function commitChanges( msg ) {
|
|
|
|
const spinner = ora( {
|
|
|
|
spinner: 'bouncingBar',
|
2022-08-02 18:54:41 +00:00
|
|
|
color: 'green',
|
2022-07-14 14:36:15 +00:00
|
|
|
} );
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
const output = [];
|
2022-07-14 14:36:15 +00:00
|
|
|
|
|
|
|
return new Promise( ( resolve, reject ) => {
|
2022-08-02 18:54:41 +00:00
|
|
|
const response = spawn( 'git', [
|
|
|
|
'commit',
|
|
|
|
'--no-verify',
|
|
|
|
'-am',
|
|
|
|
msg,
|
|
|
|
] );
|
2022-07-14 14:36:15 +00:00
|
|
|
|
|
|
|
response.stdout.on( 'data', ( data ) => {
|
2022-08-02 18:54:41 +00:00
|
|
|
output.push( `${ data }` );
|
2022-07-14 14:36:15 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
response.stderr.on( 'data', ( data ) => {
|
2022-08-02 18:54:41 +00:00
|
|
|
output.push( `${ data }` );
|
2022-07-14 14:36:15 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
response.on( 'close', ( code ) => {
|
2022-08-02 18:54:41 +00:00
|
|
|
if ( `${ code }` == 0 ) {
|
2022-07-14 14:36:15 +00:00
|
|
|
spinner.succeed( `Commited changes.` );
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
reject( `error: ${ output.toString().replace( ',', '\n' ) }` );
|
2022-07-14 14:36:15 +00:00
|
|
|
} );
|
2022-08-02 18:54:41 +00:00
|
|
|
} ).catch( ( err ) => {
|
2022-07-14 14:36:15 +00:00
|
|
|
spinner.fail( 'Fail to commit changes.' );
|
|
|
|
throw err;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Push the branch up to GitHub.
|
|
|
|
*
|
2022-08-02 18:54:41 +00:00
|
|
|
* @param string branch The branch to push to GitHub.
|
|
|
|
* @param branch
|
2022-07-14 14:36:15 +00:00
|
|
|
*/
|
|
|
|
function pushBranch( branch ) {
|
|
|
|
const spinner = ora( {
|
|
|
|
spinner: 'bouncingBar',
|
2022-08-02 18:54:41 +00:00
|
|
|
color: 'green',
|
2022-07-14 14:36:15 +00:00
|
|
|
} );
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
spinner.start( `Pushing ${ branch } to GitHub...` );
|
2022-07-14 14:36:15 +00:00
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
const output = [];
|
2022-07-14 14:36:15 +00:00
|
|
|
|
|
|
|
return new Promise( ( resolve, reject ) => {
|
|
|
|
const response = spawn( 'git', [ 'push', 'origin', branch ] );
|
|
|
|
|
|
|
|
response.stdout.on( 'data', ( data ) => {
|
2022-08-02 18:54:41 +00:00
|
|
|
output.push( `${ data }` );
|
2022-07-14 14:36:15 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
response.stderr.on( 'data', ( data ) => {
|
2022-08-02 18:54:41 +00:00
|
|
|
output.push( `${ data }` );
|
2022-07-14 14:36:15 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
response.on( 'close', ( code ) => {
|
2022-08-02 18:54:41 +00:00
|
|
|
if ( `${ code }` == 0 ) {
|
2022-07-14 14:36:15 +00:00
|
|
|
spinner.succeed();
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
reject( `error: ${ output.toString().replace( ',', '\n' ) }` );
|
2022-07-14 14:36:15 +00:00
|
|
|
} );
|
2022-08-02 18:54:41 +00:00
|
|
|
} ).catch( ( err ) => {
|
|
|
|
spinner.fail( `Fail to push ${ branch } up.` );
|
2022-07-14 14:36:15 +00:00
|
|
|
throw err;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create pull request on GitHub.
|
|
|
|
*
|
2022-08-02 18:54:41 +00:00
|
|
|
* @param string title The title of the PR.
|
|
|
|
* @param string body The body content of the PR.
|
|
|
|
* @param string head The head of the branch to use.
|
|
|
|
* @param string base The base branch to targe against.
|
|
|
|
* @param title
|
|
|
|
* @param body
|
|
|
|
* @param head
|
|
|
|
* @param base
|
2022-07-14 14:36:15 +00:00
|
|
|
*/
|
|
|
|
async function createPR( title, body, head, base ) {
|
|
|
|
const spinner = ora( {
|
|
|
|
spinner: 'bouncingBar',
|
2022-08-02 18:54:41 +00:00
|
|
|
color: 'green',
|
2022-07-14 14:36:15 +00:00
|
|
|
} );
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
spinner.start( `Creating pull request for ${ head } on GitHub...` );
|
2022-07-14 14:36:15 +00:00
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
const response = await fetch(
|
|
|
|
'https://api.github.com/repos/woocommerce/woocommerce/pulls',
|
|
|
|
{
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/vnd.github.v3+json',
|
|
|
|
Authorization: `token ${ githubToken }`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify( {
|
|
|
|
title,
|
|
|
|
body,
|
|
|
|
head,
|
|
|
|
base,
|
|
|
|
} ),
|
|
|
|
}
|
|
|
|
);
|
2022-07-14 14:36:15 +00:00
|
|
|
|
|
|
|
if ( response.status !== 201 ) {
|
|
|
|
throw 'Fail to create PR on GitHub.';
|
|
|
|
}
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
return await response.json().then( ( data ) => {
|
2022-07-14 14:36:15 +00:00
|
|
|
spinner.succeed();
|
|
|
|
return {
|
2022-08-02 18:54:41 +00:00
|
|
|
url: data.url,
|
|
|
|
number: data.number,
|
|
|
|
};
|
2022-07-14 14:36:15 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
( async function () {
|
2022-07-14 14:36:15 +00:00
|
|
|
try {
|
|
|
|
// Creates a temp directory to work with.
|
2022-08-02 18:54:41 +00:00
|
|
|
await createDir( path.join( os.tmpdir(), 'woo-cherry-pick-' ) );
|
2022-07-14 14:36:15 +00:00
|
|
|
|
|
|
|
// Gets the commits from GitHub based on the PRs passed in.
|
|
|
|
await getCommitFromPrs( prsArr );
|
|
|
|
|
|
|
|
chdir( tempWooDir );
|
|
|
|
|
|
|
|
await cloneWoo();
|
|
|
|
|
|
|
|
chdir( tempWooDir + '/woocommerce' );
|
|
|
|
|
|
|
|
// This checks out the release branch.
|
|
|
|
await checkoutBranch( releaseBranch );
|
|
|
|
|
|
|
|
// This checks out a new branch based on the release branch.
|
|
|
|
await checkoutBranch( cherryPickBranch, true );
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
let cherryPickPRBody =
|
|
|
|
'This PR cherry-picks the following PRs into the release branch:\n';
|
2022-07-14 14:36:15 +00:00
|
|
|
|
|
|
|
for ( const pr of Object.keys( prCommits ) ) {
|
2022-08-02 18:54:41 +00:00
|
|
|
cherryPickPRBody = `${ cherryPickPRBody }` + `* #${ pr }` + '\n';
|
2022-07-14 14:36:15 +00:00
|
|
|
cherryPick( prCommits[ pr ] );
|
2022-08-02 18:54:41 +00:00
|
|
|
await generateChangelog( pr, prCommits[ pr ] );
|
2022-07-14 14:36:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deletes the changelog files from the release branch.
|
|
|
|
await deleteChangelogFiles();
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
await commitChanges( `Prep for cherry pick ${ prsArr.toString() }` );
|
2022-07-14 14:36:15 +00:00
|
|
|
|
|
|
|
await pushBranch( cherryPickBranch );
|
|
|
|
|
|
|
|
const cherryPickPR = await createPR(
|
2022-08-02 18:54:41 +00:00
|
|
|
`Prep for cherry pick ${ prsArr.toString() }`,
|
2022-07-14 14:36:15 +00:00
|
|
|
cherryPickPRBody,
|
|
|
|
cherryPickBranch,
|
|
|
|
releaseBranch
|
|
|
|
);
|
|
|
|
|
|
|
|
await checkoutBranch( 'trunk' );
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
const deleteChangelogBranch = `delete-changelogs/${ prsArr
|
|
|
|
.toString()
|
|
|
|
.replace( ',', '-' ) }`;
|
2022-07-14 14:36:15 +00:00
|
|
|
|
|
|
|
// This checks out a new branch based on the trunk branch.
|
2022-08-02 18:54:41 +00:00
|
|
|
await checkoutBranch( `${ deleteChangelogBranch }`, true );
|
2022-07-14 14:36:15 +00:00
|
|
|
|
|
|
|
// Deletes the changelog files from the trunk branch.
|
|
|
|
await deleteChangelogFiles();
|
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
await commitChanges(
|
|
|
|
`Delete changelog files for ${ prsArr.toString() }`
|
|
|
|
);
|
2022-07-14 14:36:15 +00:00
|
|
|
|
2022-08-02 18:54:41 +00:00
|
|
|
await pushBranch( `${ deleteChangelogBranch }` );
|
2022-07-14 14:36:15 +00:00
|
|
|
|
|
|
|
const deleteChangelogsPR = await createPR(
|
2022-08-02 18:54:41 +00:00
|
|
|
`Delete changelog files based on PR ${ cherryPickPR.number }`,
|
|
|
|
`Delete changelog files based on PR #${ cherryPickPR.number }`,
|
2022-07-14 14:36:15 +00:00
|
|
|
deleteChangelogBranch,
|
|
|
|
'trunk'
|
|
|
|
);
|
|
|
|
|
|
|
|
console.log( `Two PRs created by this process:` );
|
|
|
|
console.log( cherryPickPR.url );
|
|
|
|
console.log( deleteChangelogsPR.url );
|
|
|
|
} catch ( e ) {
|
|
|
|
console.error( e );
|
|
|
|
process.exit( 1 );
|
|
|
|
}
|
|
|
|
} )();
|