Generate a JSON file with changes from code-analyzer (#33955)

This commit is contained in:
Sam Seay 2022-07-21 19:37:59 +12:00 committed by GitHub
parent d09d59186d
commit ecd17484bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 9 deletions

3
.gitignore vendored
View File

@ -88,6 +88,9 @@ phpcs-report.xml
allure-report allure-report
allure-results allure-results
# Code analyzer output
changes.json
# Playwright output & working files # Playwright output & working files
/plugins/woocommerce/e2e/output /plugins/woocommerce/e2e/output
/plugins/woocommerce/e2e/report /plugins/woocommerce/e2e/report

View File

@ -22,9 +22,11 @@ import {
areSchemasEqual, areSchemasEqual,
getHookDescription, getHookDescription,
getHookChangeType, getHookChangeType,
generateJSONFile,
} from '../../utils'; } from '../../utils';
import { cloneRepo, generateDiff, generateSchemaDiff } from '../../git'; import { cloneRepo, generateDiff, generateSchemaDiff } from '../../git';
import { execSync } from 'child_process'; import { execSync } from 'child_process';
import { OutputFlags } from '@oclif/core/lib/interfaces';
/** /**
* Analyzer class * Analyzer class
@ -67,6 +69,11 @@ export default class Analyzer extends Command {
description: 'Git repo url or local path to a git repo.', description: 'Git repo url or local path to a git repo.',
default: process.cwd(), default: process.cwd(),
} ), } ),
file: Flags.string( {
char: 'f',
description: 'Filename for change description JSON.',
default: 'changes.json',
} ),
plugin: Flags.string( { plugin: Flags.string( {
char: 'p', char: 'p',
description: 'Plugin to check for', description: 'Plugin to check for',
@ -133,9 +140,9 @@ export default class Analyzer extends Command {
CliUx.ux.action.stop(); CliUx.ux.action.stop();
this.scanChanges( diff, pluginData[ 0 ], flags.output, schemaDiff ); this.scanChanges( diff, pluginData[ 0 ], flags, schemaDiff );
} else { } else {
this.scanChanges( diff, pluginData[ 0 ], flags.output ); this.scanChanges( diff, pluginData[ 0 ], flags );
} }
// Clean up the temporary repo. // Clean up the temporary repo.
@ -204,15 +211,16 @@ export default class Analyzer extends Command {
/** /**
* Scan patches for changes in templates, hooks and database schema * Scan patches for changes in templates, hooks and database schema
* *
* @param {string} content Patch content. * @param {string} content Patch content.
* @param {string} version Current product version. * @param {string} version Current product version.
* @param {string} output Output style. * @param {string} output Output style.
* @param {boolean} schemaEquality if schemas are equal between branches. * @param {string} changesFileName Name of a file to output change information to.
* @param {boolean} schemaEquality if schemas are equal between branches.
*/ */
private scanChanges( private async scanChanges(
content: string, content: string,
version: string, version: string,
output: string, flags: OutputFlags< typeof Analyzer[ 'flags' ] >,
schemaDiff: { schemaDiff: {
[ key: string ]: { [ key: string ]: {
description: string; description: string;
@ -222,12 +230,19 @@ export default class Analyzer extends Command {
areEqual: boolean; areEqual: boolean;
}; };
} | void } | void
): void { ) {
const { output, file } = flags;
CliUx.ux.action.start( 'Generating changes' ); CliUx.ux.action.start( 'Generating changes' );
const templates = this.scanTemplates( content, version ); const templates = this.scanTemplates( content, version );
const hooks = this.scanHooks( content, version, output ); const hooks = this.scanHooks( content, version, output );
const databaseUpdates = this.scanDatabases( content ); const databaseUpdates = this.scanDatabases( content );
await generateJSONFile( join( process.cwd(), file ), {
templates: Object.fromEntries( templates.entries() ),
hooks: Object.fromEntries( hooks.entries() ),
schema: databaseUpdates || {},
} );
if ( templates.size ) { if ( templates.size ) {
printTemplateResults( printTemplateResults(
templates, templates,

View File

@ -4,6 +4,7 @@
import { createServer, Server } from 'net'; import { createServer, Server } from 'net';
import { execSync } from 'child_process'; import { execSync } from 'child_process';
import { join } from 'path'; import { join } from 'path';
import { writeFile } from 'fs/promises';
/** /**
* Format version string for regex. * Format version string for regex.
@ -234,3 +235,17 @@ export const getHookChangeType = ( diff: string ): 'Updated' | 'New' => {
// If there is more than one 'since' in the diff, it means that line was updated meaning the hook already exists. // If there is more than one 'since' in the diff, it means that line was updated meaning the hook already exists.
return sinces.length > 1 ? 'Updated' : 'New'; return sinces.length > 1 ? 'Updated' : 'New';
}; };
export const generateJSONFile = ( filePath: string, data: unknown ) => {
const json = JSON.stringify(
data,
function replacer( key, value ) {
if ( value instanceof Map ) {
return Array.from( value.entries() );
}
return value;
},
2
);
return writeFile( filePath, json );
};