Add line number reference links to release posts for hooks (#35354)

This commit is contained in:
Roy Ho 2022-10-27 11:03:19 -07:00 committed by GitHub
parent 04d6e8849c
commit 8d73a03f97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 67 additions and 27 deletions

View File

@ -53,7 +53,8 @@ const program = new Command()
sinceVersion,
skipSchemaCheck,
source,
base
base,
outputStyle
);
if ( changes.templates.size ) {

View File

@ -2,6 +2,7 @@
* External dependencies
*/
import { getFilename, getPatches } from 'cli-core/src/util';
import fs from 'node:fs';
/**
* Internal dependencies
@ -20,9 +21,14 @@ export type HookChangeDescription = {
hookType: string;
changeType: 'new' | 'updated';
version: string;
ghLink: string;
};
export const scanForHookChanges = ( content: string, version: string ) => {
export const scanForHookChanges = (
content: string,
version: string,
tmpRepoPath: string
) => {
const changes: Map< string, HookChangeDescription > = new Map();
if ( ! content.match( /diff --git a\/(.+).php/g ) ) {
@ -73,14 +79,39 @@ export const scanForHookChanges = ( content: string, version: string ) => {
const changeType = getHookChangeType( raw );
if ( ! hookName[ 2 ].startsWith( '-' ) ) {
changes.set( filePath, {
filePath,
name,
hookType,
description,
changeType,
version,
} );
let ghLink = '';
fs.readFile(
tmpRepoPath + filePath,
'utf-8',
function ( err, data ) {
if ( err ) {
console.error( err );
}
const reg = new RegExp( name );
data.split( '\n' ).forEach( ( line, index ) => {
if ( line.match( reg ) ) {
const lineNum = index + 1;
ghLink = `https://github.com/woocommerce/woocommerce/blob/${ version }/${ filePath.replace(
/(^\/)/,
''
) }#L${ lineNum }`;
}
} );
changes.set( filePath, {
filePath,
name,
hookType,
description,
changeType,
version,
ghLink,
} );
}
);
}
}
}

View File

@ -5,6 +5,7 @@ import { Logger } from 'cli-core/src/logger';
import { join } from 'path';
import { cloneRepo, generateDiff } from 'cli-core/src/git';
import { readFile } from 'fs/promises';
import { execSync } from 'child_process';
/**
* Internal dependencies
@ -20,7 +21,8 @@ export const scanForChanges = async (
sinceVersion: string,
skipSchemaCheck: boolean,
source: string,
base: string
base: string,
outputStyle: string
) => {
Logger.startTask( `Making temporary clone of ${ source }...` );
const tmpRepoPath = await cloneRepo( source );
@ -37,10 +39,17 @@ export const scanForChanges = async (
Logger.error
);
// Only checkout the compare version if we're in CLI mode.
if ( outputStyle === 'cli' ) {
execSync( `cd ${ tmpRepoPath } && git checkout ${ compareVersion }`, {
stdio: 'pipe',
} );
}
const pluginPath = join( tmpRepoPath, 'plugins/woocommerce' );
Logger.startTask( 'Detecting hook changes...' );
const hookChanges = scanForHookChanges( diff, sinceVersion );
const hookChanges = scanForHookChanges( diff, sinceVersion, tmpRepoPath );
Logger.endTask();
Logger.startTask( 'Detecting template changes...' );

View File

@ -19,11 +19,10 @@ export const printTemplateResults = (
title: string,
log: ( s: string ) => void
): void => {
//[code,title,message]
if ( output === 'github' ) {
let opt = '\\n\\n### Template changes:';
for ( const { filePath, code, message } of data ) {
opt += `\\n* **file:** ${ filePath }`;
opt += `\\n* **File:** ${ filePath }`;
opt += `\\n * ${ code.toUpperCase() }: ${ message }`;
log(
`::${ code } file=${ filePath },line=1,title=${ title }::${ message }`
@ -56,12 +55,6 @@ export const printHookResults = (
sectionTitle: string,
log: ( s: string ) => void
) => {
// [
// 'NOTICE',
// title,
// message,
// description,
// ]
if ( output === 'github' ) {
let opt = '\\n\\n### New hooks:';
for ( const {
@ -70,9 +63,9 @@ export const printHookResults = (
version,
description,
hookType,
changeType,
changeType
} of data ) {
opt += `\\n* **file:** ${ filePath }`;
opt += `\\n* **File:** ${ filePath }`;
const cliMessage = `**${ name }** introduced in ${ version }`;
const ghMessage = `\\'${ name }\\' introduced in ${ version }`;
@ -96,6 +89,7 @@ export const printHookResults = (
description,
hookType,
changeType,
ghLink,
} of data ) {
const cliMessage = `**${ name }** introduced in ${ version }`;
const ghMessage = `\\'${ name }\\' introduced in ${ version }`;
@ -106,8 +100,10 @@ export const printHookResults = (
log( '---------------------------------------------------' );
log( `HOOK: ${ name }: ${ description }` );
log( '---------------------------------------------------' );
log( `NOTICE | ${ title } | ${ message }` );
log( `GITHUB: ${ ghLink }` );
log( '---------------------------------------------------' );
log( `NOTICE | ${ title } | ${ message }` );
log( '---------------------------------------------------\n' );
}
}
};

View File

@ -74,7 +74,8 @@ const program = new Command()
currentVersion,
false,
'https://github.com/woocommerce/woocommerce.git',
previousVersion.toString()
previousVersion.toString(),
'cli'
);
const schemaChanges = changes.schema.filter(

View File

@ -10,11 +10,13 @@
<tr>
<th><strong>Filter</strong></th>
<th><strong>Description</strong></th>
<th><strong>GitHub Link</strong></th>
</tr>
<% changes.hooks.forEach(({ name, description }) => { %>
<% changes.hooks.forEach(({ name, description, ghLink }) => { %>
<tr>
<td><%= name %></td>
<td><%= description %></td>
<td><a target="_blank" href="<%= ghLink %>">Link</a></td>
</tr>
<% }) %>
<!-- end each hook change-->

View File

@ -12,11 +12,11 @@
</th>
</thead>
<tbody>
<!-- for each template change (template / description) -->
<% changes.templates.forEach((change) => { %>
<tr>
<td><%= change %></td>
<td><%= change.filePath %></td>
</tr>
<% }) %>
</tbody>