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';
|
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';
|
|
|
|
import { createWpComDraftPost } from '../../lib/draft-post';
|
|
|
|
import { generateContributors } from '../../lib/contributors';
|
2022-09-06 03:51:28 +00:00
|
|
|
|
|
|
|
const DEVELOPER_WOOCOMMERCE_SITE_ID = '96396764';
|
|
|
|
|
|
|
|
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>',
|
|
|
|
'The version of the plugin to generate a post for, please use the tag version from Github.'
|
|
|
|
)
|
|
|
|
.option( '--outputOnly', 'Only output the post, do not publish it' )
|
|
|
|
.option(
|
|
|
|
'--previousVersion <previousVersion>',
|
|
|
|
'If you would like to compare against a version other than last minor you can provide a tag version from Github.'
|
|
|
|
)
|
2022-09-10 21:55:53 +00:00
|
|
|
.option(
|
|
|
|
'--tags <tags>',
|
|
|
|
'Comma separated list of tags to add to the post.',
|
|
|
|
'Releases,WooCommerce Core'
|
|
|
|
)
|
2022-09-06 03:51:28 +00:00
|
|
|
.action( async ( currentVersion, options ) => {
|
2022-09-10 21:55:53 +00:00
|
|
|
const tags = options.tags.split( ',' ).map( ( tag ) => tag.trim() );
|
|
|
|
|
2022-09-06 03:51:28 +00:00
|
|
|
const previousVersion = options.previousVersion
|
|
|
|
? semver.parse( options.previousVersion )
|
|
|
|
: semver.parse( currentVersion );
|
|
|
|
|
|
|
|
if ( ! options.previousVersion && previousVersion ) {
|
|
|
|
// e.g 6.8.0 -> 6.7.0
|
|
|
|
previousVersion.minor -= 1;
|
|
|
|
previousVersion.format();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( previousVersion && previousVersion.major ) {
|
|
|
|
const isOutputOnly = !! options.outputOnly;
|
|
|
|
|
|
|
|
if ( ! VERSION_VALIDATION_REGEX.test( previousVersion.raw ) ) {
|
|
|
|
throw new Error(
|
|
|
|
`Invalid previous version: ${ previousVersion.raw }`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ( ! VERSION_VALIDATION_REGEX.test( currentVersion ) ) {
|
|
|
|
throw new Error(
|
|
|
|
`Invalid current version: ${ currentVersion }`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-10 21:55:53 +00:00
|
|
|
const changes = await scanForChanges(
|
2022-09-06 03:51:28 +00:00
|
|
|
currentVersion,
|
|
|
|
currentVersion,
|
2022-09-10 21:55:53 +00:00
|
|
|
false,
|
2022-09-06 03:51:28 +00:00
|
|
|
'https://github.com/woocommerce/woocommerce.git',
|
2022-10-27 18:03:19 +00:00
|
|
|
previousVersion.toString(),
|
|
|
|
'cli'
|
2022-09-06 03:51:28 +00:00
|
|
|
);
|
|
|
|
|
2022-09-10 21:55:53 +00:00
|
|
|
const schemaChanges = changes.schema.filter(
|
|
|
|
( s ) => ! s.areEqual
|
|
|
|
);
|
2022-09-06 03:51:28 +00:00
|
|
|
|
|
|
|
Logger.startTask( 'Finding contributors' );
|
|
|
|
const title = `WooCommerce ${ currentVersion } Released`;
|
|
|
|
|
|
|
|
const contributors = await generateContributors(
|
|
|
|
currentVersion,
|
|
|
|
previousVersion.toString()
|
|
|
|
);
|
|
|
|
|
|
|
|
const html = await renderTemplate( 'release.ejs', {
|
|
|
|
contributors,
|
|
|
|
title,
|
2022-09-10 21:55:53 +00:00
|
|
|
changes: {
|
|
|
|
...changes,
|
|
|
|
schema: schemaChanges,
|
|
|
|
},
|
2022-09-06 03:51:28 +00:00
|
|
|
displayVersion: currentVersion,
|
|
|
|
} );
|
|
|
|
|
|
|
|
Logger.endTask();
|
|
|
|
|
|
|
|
if ( isOutputOnly ) {
|
|
|
|
const tmpFile = join(
|
|
|
|
tmpdir(),
|
|
|
|
`release-${ currentVersion }.html`
|
|
|
|
);
|
|
|
|
|
|
|
|
await writeFile( tmpFile, html );
|
|
|
|
|
|
|
|
Logger.notice( `Output written to ${ tmpFile }` );
|
|
|
|
} else {
|
|
|
|
Logger.startTask( 'Publishing draft release post' );
|
|
|
|
|
|
|
|
try {
|
|
|
|
const { URL } = await createWpComDraftPost(
|
|
|
|
DEVELOPER_WOOCOMMERCE_SITE_ID,
|
|
|
|
title,
|
2022-09-10 21:55:53 +00:00
|
|
|
html,
|
|
|
|
tags
|
2022-09-06 03:51:28 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
Logger.notice( `Published draft release post at ${ URL }` );
|
|
|
|
Logger.endTask();
|
|
|
|
} catch ( error: unknown ) {
|
|
|
|
if ( error instanceof Error ) {
|
|
|
|
Logger.error( error.message );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new Error(
|
|
|
|
`Could not find previous version for ${ currentVersion }`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} );
|
2022-09-10 21:55:53 +00:00
|
|
|
|
|
|
|
program.parse( process.argv );
|