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-results
# Code analyzer output
changes.json
# Playwright output & working files
/plugins/woocommerce/e2e/output
/plugins/woocommerce/e2e/report

View File

@ -22,9 +22,11 @@ import {
areSchemasEqual,
getHookDescription,
getHookChangeType,
generateJSONFile,
} from '../../utils';
import { cloneRepo, generateDiff, generateSchemaDiff } from '../../git';
import { execSync } from 'child_process';
import { OutputFlags } from '@oclif/core/lib/interfaces';
/**
* Analyzer class
@ -67,6 +69,11 @@ export default class Analyzer extends Command {
description: 'Git repo url or local path to a git repo.',
default: process.cwd(),
} ),
file: Flags.string( {
char: 'f',
description: 'Filename for change description JSON.',
default: 'changes.json',
} ),
plugin: Flags.string( {
char: 'p',
description: 'Plugin to check for',
@ -133,9 +140,9 @@ export default class Analyzer extends Command {
CliUx.ux.action.stop();
this.scanChanges( diff, pluginData[ 0 ], flags.output, schemaDiff );
this.scanChanges( diff, pluginData[ 0 ], flags, schemaDiff );
} else {
this.scanChanges( diff, pluginData[ 0 ], flags.output );
this.scanChanges( diff, pluginData[ 0 ], flags );
}
// 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
*
* @param {string} content Patch content.
* @param {string} version Current product version.
* @param {string} output Output style.
* @param {boolean} schemaEquality if schemas are equal between branches.
* @param {string} content Patch content.
* @param {string} version Current product version.
* @param {string} output Output style.
* @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,
version: string,
output: string,
flags: OutputFlags< typeof Analyzer[ 'flags' ] >,
schemaDiff: {
[ key: string ]: {
description: string;
@ -222,12 +230,19 @@ export default class Analyzer extends Command {
areEqual: boolean;
};
} | void
): void {
) {
const { output, file } = flags;
CliUx.ux.action.start( 'Generating changes' );
const templates = this.scanTemplates( content, version );
const hooks = this.scanHooks( content, version, output );
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 ) {
printTemplateResults(
templates,

View File

@ -4,6 +4,7 @@
import { createServer, Server } from 'net';
import { execSync } from 'child_process';
import { join } from 'path';
import { writeFile } from 'fs/promises';
/**
* 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.
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 );
};