2022-09-06 03:51:28 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-09-10 21:55:53 +00:00
|
|
|
import { scanForChanges } from 'code-analyzer/src/lib/scan-changes';
|
2022-09-06 03:51:28 +00:00
|
|
|
import semver from 'semver';
|
|
|
|
import { writeFile } from 'fs/promises';
|
|
|
|
import { tmpdir } from 'os';
|
|
|
|
import { join } from 'path';
|
2023-01-23 14:31:59 +00:00
|
|
|
import { cloneRepo, getCommitHash } from 'cli-core/src/git';
|
2022-09-10 21:55:53 +00:00
|
|
|
import { Logger } from 'cli-core/src/logger';
|
|
|
|
import { Command } from '@commander-js/extra-typings';
|
|
|
|
import dotenv from 'dotenv';
|
2022-09-06 03:51:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2022-09-10 21:55:53 +00:00
|
|
|
import { renderTemplate } from '../../lib/render-template';
|
2023-01-23 14:31:59 +00:00
|
|
|
import {
|
|
|
|
createWpComDraftPost,
|
|
|
|
fetchWpComPost,
|
|
|
|
editWpComPostContent,
|
|
|
|
} from '../../lib/draft-post';
|
|
|
|
import { getWordpressComAuthToken } from '../../lib/oauth-helper';
|
|
|
|
import { getEnvVar } from '../../lib/environment';
|
2022-09-10 21:55:53 +00:00
|
|
|
import { generateContributors } from '../../lib/contributors';
|
2023-01-23 14:31:59 +00:00
|
|
|
import { editPostHTML } from '../../lib/edit-post';
|
2022-09-06 03:51:28 +00:00
|
|
|
|
|
|
|
const DEVELOPER_WOOCOMMERCE_SITE_ID = '96396764';
|
|
|
|
|
2023-01-23 14:31:59 +00:00
|
|
|
const SOURCE_REPO = 'https://github.com/woocommerce/woocommerce.git';
|
|
|
|
|
2022-09-06 03:51:28 +00:00
|
|
|
const VERSION_VALIDATION_REGEX =
|
|
|
|
/^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$/;
|
|
|
|
|
2022-09-10 21:55:53 +00:00
|
|
|
dotenv.config();
|
|
|
|
|
2022-09-06 03:51:28 +00:00
|
|
|
// Define the release post command
|
2022-09-10 21:55:53 +00:00
|
|
|
const program = new Command()
|
2022-09-06 03:51:28 +00:00
|
|
|
.command( 'release' )
|
|
|
|
.description( 'CLI to automate generation of a release post.' )
|
|
|
|
.argument(
|
|
|
|
'<currentVersion>',
|
2023-01-23 14:31:59 +00:00
|
|
|
'The current version in x.y.z or x.y.z-stage.n format. Ex: 7.1.0, 7.1.0-rc.1'
|
2022-09-06 03:51:28 +00:00
|
|
|
)
|
2023-01-23 14:31:59 +00:00
|
|
|
.argument(
|
|
|
|
'<previousVersion>',
|
|
|
|
'The previous version in x.y.z format. Ex: 7.0.0'
|
2022-09-06 03:51:28 +00:00
|
|
|
)
|
2023-01-23 14:31:59 +00:00
|
|
|
.option( '--outputOnly', 'Only output the post, do not publish it' )
|
|
|
|
.option( '--editPostId <postId>', 'Updates an existing post' )
|
2022-09-10 21:55:53 +00:00
|
|
|
.option(
|
|
|
|
'--tags <tags>',
|
|
|
|
'Comma separated list of tags to add to the post.',
|
|
|
|
'Releases,WooCommerce Core'
|
|
|
|
)
|
2023-01-23 14:31:59 +00:00
|
|
|
.option(
|
|
|
|
'--siteId <siteId>',
|
|
|
|
'For posting to a non-default site (for testing)'
|
|
|
|
)
|
|
|
|
.action( async ( currentVersion, previousVersion, options ) => {
|
|
|
|
const siteId = options.siteId || DEVELOPER_WOOCOMMERCE_SITE_ID;
|
|
|
|
const tags = ( options.tags &&
|
|
|
|
options.tags.split( ',' ).map( ( tag ) => tag.trim() ) ) || [
|
|
|
|
'WooCommerce Core',
|
|
|
|
'Releases',
|
|
|
|
];
|
|
|
|
const isOutputOnly = !! options.outputOnly;
|
2022-09-10 21:55:53 +00:00
|
|
|
|
2023-01-23 14:31:59 +00:00
|
|
|
if ( ! VERSION_VALIDATION_REGEX.test( currentVersion ) ) {
|
|
|
|
throw new Error(
|
|
|
|
`Invalid current version: ${ currentVersion }. Provide current version in x.y.z or x.y.z-stage.n format.`
|
|
|
|
);
|
|
|
|
}
|
2022-09-06 03:51:28 +00:00
|
|
|
|
2023-01-23 14:31:59 +00:00
|
|
|
if ( ! VERSION_VALIDATION_REGEX.test( previousVersion ) ) {
|
|
|
|
throw new Error(
|
|
|
|
`Invalid previous version: ${ previousVersion }. Provide previous version in x.y.z format.`
|
|
|
|
);
|
|
|
|
}
|
2022-10-28 12:42:46 +00:00
|
|
|
|
2023-01-23 14:31:59 +00:00
|
|
|
const clientId = getEnvVar( 'WPCOM_OAUTH_CLIENT_ID', true );
|
|
|
|
const clientSecret = getEnvVar( 'WPCOM_OAUTH_CLIENT_SECRET', true );
|
|
|
|
const redirectUri =
|
|
|
|
getEnvVar( 'WPCOM_OAUTH_REDIRECT_URI' ) ||
|
|
|
|
'http://localhost:3000/oauth';
|
|
|
|
const authToken =
|
|
|
|
isOutputOnly ||
|
|
|
|
( await getWordpressComAuthToken(
|
|
|
|
clientId,
|
|
|
|
clientSecret,
|
|
|
|
siteId,
|
|
|
|
redirectUri,
|
|
|
|
'posts'
|
|
|
|
) );
|
2022-10-28 12:42:46 +00:00
|
|
|
|
2023-01-23 14:31:59 +00:00
|
|
|
if ( ! authToken ) {
|
|
|
|
throw new Error(
|
|
|
|
'Error getting auth token, check your env settings are correct.'
|
|
|
|
);
|
2022-09-06 03:51:28 +00:00
|
|
|
}
|
|
|
|
|
2023-01-23 14:31:59 +00:00
|
|
|
Logger.startTask( `Making temporary clone of ${ SOURCE_REPO }...` );
|
|
|
|
const currentParsed = semver.parse( currentVersion );
|
|
|
|
const previousParsed = semver.parse( previousVersion );
|
|
|
|
const tmpRepoPath = await cloneRepo( SOURCE_REPO );
|
|
|
|
Logger.endTask();
|
|
|
|
let currentBranch;
|
|
|
|
let previousBranch;
|
|
|
|
let currentVersionRef;
|
|
|
|
let previousVersionRef;
|
2022-09-06 03:51:28 +00:00
|
|
|
|
2023-01-23 14:31:59 +00:00
|
|
|
try {
|
|
|
|
if ( ! currentParsed ) {
|
|
|
|
throw new Error( 'Unable to parse current version' );
|
|
|
|
}
|
|
|
|
currentBranch = `release/${ currentParsed.major }.${ currentParsed.minor }`;
|
|
|
|
currentVersionRef = await getCommitHash(
|
|
|
|
tmpRepoPath,
|
|
|
|
`remotes/origin/${ currentBranch }`
|
|
|
|
);
|
|
|
|
} catch ( error: unknown ) {
|
|
|
|
Logger.notice(
|
|
|
|
`Unable to find '${ currentBranch }', using 'trunk'.`
|
|
|
|
);
|
|
|
|
currentBranch = 'trunk';
|
|
|
|
currentVersionRef = await getCommitHash(
|
|
|
|
tmpRepoPath,
|
|
|
|
'remotes/origin/trunk'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if ( ! previousParsed ) {
|
|
|
|
throw new Error( 'Unable to parse previous version' );
|
2022-09-06 03:51:28 +00:00
|
|
|
}
|
2023-01-23 14:31:59 +00:00
|
|
|
previousBranch = `release/${ previousParsed.major }.${ previousParsed.minor }`;
|
|
|
|
previousVersionRef = await getCommitHash(
|
|
|
|
tmpRepoPath,
|
|
|
|
`remotes/origin/${ previousBranch }`
|
|
|
|
);
|
|
|
|
} catch ( error: unknown ) {
|
|
|
|
throw new Error(
|
|
|
|
`Unable to find '${ previousBranch }'. Branch for previous version must exist.`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.notice(
|
|
|
|
`Using ${ currentBranch }(${ currentVersionRef }) for current and ${ previousBranch }(${ previousVersionRef }) for previous.`
|
|
|
|
);
|
|
|
|
|
|
|
|
let postContent;
|
|
|
|
|
2023-03-16 19:37:53 +00:00
|
|
|
if ( typeof options.editPostId !== 'undefined' ) {
|
2023-01-23 14:31:59 +00:00
|
|
|
try {
|
|
|
|
const prevPost = await fetchWpComPost(
|
|
|
|
siteId,
|
|
|
|
options.editPostId,
|
|
|
|
authToken
|
|
|
|
);
|
|
|
|
postContent = prevPost.content;
|
|
|
|
} catch ( error: unknown ) {
|
2022-09-06 03:51:28 +00:00
|
|
|
throw new Error(
|
2023-01-23 14:31:59 +00:00
|
|
|
`Unable to fetch existing post with ID: ${ options.editPostId }`
|
2022-09-06 03:51:28 +00:00
|
|
|
);
|
|
|
|
}
|
2023-01-23 14:31:59 +00:00
|
|
|
}
|
2022-09-06 03:51:28 +00:00
|
|
|
|
2023-01-23 14:31:59 +00:00
|
|
|
const changes = await scanForChanges(
|
|
|
|
currentVersionRef,
|
|
|
|
`${ previousParsed.major }.${ previousParsed.minor }.${ previousParsed.patch }`,
|
2023-04-19 03:40:14 +00:00
|
|
|
false,
|
2023-01-23 14:31:59 +00:00
|
|
|
SOURCE_REPO,
|
|
|
|
previousVersionRef,
|
|
|
|
'cli',
|
|
|
|
tmpRepoPath
|
|
|
|
);
|
2022-09-06 03:51:28 +00:00
|
|
|
|
2023-01-23 14:31:59 +00:00
|
|
|
const schemaChanges = changes.schema.filter( ( s ) => ! s.areEqual );
|
2022-09-06 03:51:28 +00:00
|
|
|
|
2023-01-23 14:31:59 +00:00
|
|
|
Logger.startTask( 'Finding contributors' );
|
|
|
|
const title = `WooCommerce ${ currentVersion } Released`;
|
2022-09-06 03:51:28 +00:00
|
|
|
|
2023-01-23 14:31:59 +00:00
|
|
|
const contributors = await generateContributors(
|
|
|
|
currentVersion,
|
|
|
|
previousVersion.toString()
|
|
|
|
);
|
|
|
|
|
|
|
|
const postVariables = {
|
|
|
|
contributors,
|
|
|
|
title,
|
|
|
|
changes: {
|
|
|
|
...changes,
|
|
|
|
schema: schemaChanges,
|
|
|
|
},
|
|
|
|
displayVersion: currentVersion,
|
|
|
|
};
|
|
|
|
|
|
|
|
const html =
|
2023-03-16 19:37:53 +00:00
|
|
|
typeof options.editPostId !== 'undefined'
|
2023-01-23 14:31:59 +00:00
|
|
|
? editPostHTML( postContent, {
|
|
|
|
hooks: await renderTemplate(
|
|
|
|
'hooks.ejs',
|
|
|
|
postVariables
|
|
|
|
),
|
|
|
|
database: await renderTemplate(
|
|
|
|
'database.ejs',
|
|
|
|
postVariables
|
|
|
|
),
|
|
|
|
templates: await renderTemplate(
|
|
|
|
'templates.ejs',
|
|
|
|
postVariables
|
|
|
|
),
|
|
|
|
contributors: await renderTemplate(
|
|
|
|
'contributors.ejs',
|
|
|
|
postVariables
|
|
|
|
),
|
|
|
|
} )
|
|
|
|
: await renderTemplate( 'release.ejs', postVariables );
|
|
|
|
|
|
|
|
Logger.endTask();
|
|
|
|
|
|
|
|
if ( isOutputOnly ) {
|
|
|
|
const tmpFile = join(
|
|
|
|
tmpdir(),
|
|
|
|
`release-${ currentVersion }.html`
|
2022-09-06 03:51:28 +00:00
|
|
|
);
|
|
|
|
|
2023-01-23 14:31:59 +00:00
|
|
|
await writeFile( tmpFile, html );
|
2022-09-06 03:51:28 +00:00
|
|
|
|
2023-01-23 14:31:59 +00:00
|
|
|
Logger.notice( `Output written to ${ tmpFile }` );
|
|
|
|
} else {
|
|
|
|
Logger.startTask( 'Publishing draft release post' );
|
|
|
|
|
|
|
|
try {
|
|
|
|
const { URL } =
|
2023-03-16 19:37:53 +00:00
|
|
|
typeof options.editPostId !== 'undefined'
|
2023-01-23 14:31:59 +00:00
|
|
|
? await editWpComPostContent(
|
|
|
|
siteId,
|
|
|
|
options.editPostId,
|
|
|
|
html,
|
|
|
|
authToken
|
|
|
|
)
|
|
|
|
: await createWpComDraftPost(
|
|
|
|
siteId,
|
|
|
|
title,
|
|
|
|
html,
|
|
|
|
tags,
|
|
|
|
authToken
|
|
|
|
);
|
|
|
|
|
|
|
|
Logger.notice( `Published draft release post at ${ URL }` );
|
|
|
|
Logger.endTask();
|
|
|
|
} catch ( error: unknown ) {
|
|
|
|
if ( error instanceof Error ) {
|
|
|
|
Logger.error( error.message );
|
2022-09-06 03:51:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
2022-09-10 21:55:53 +00:00
|
|
|
|
|
|
|
program.parse( process.argv );
|