Analyzer: Fix regex to handle multiple changes in a single file (#33651)

This commit is contained in:
Paul Sealock 2022-07-01 07:12:34 +12:00 committed by GitHub
parent eb223038f6
commit a2b6b9bd2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 7 deletions

View File

@ -22,7 +22,7 @@ jobs:
id: results
run: echo "::set-output name=results::${{ steps.run.outputs.templates }}${{ steps.run.outputs.wphooks }}${{ steps.run.outputs.schema }}${{ steps.run.outputs.database }}"
comment:
name: Add comment to hightlight changes
name: Add comment to highlight changes
needs: analyze
runs-on: ubuntu-20.04
steps:

View File

@ -371,12 +371,21 @@ export default class Analyzer extends Command {
const matchPatches = /^a\/(.+).php/g;
const patches = getPatches( content, matchPatches );
const verRegEx = getVersionRegex( version );
const matchHooks = `\/\\*\\*(.*?)@since\\s+(${ verRegEx })(.*?)(apply_filters|do_action)\\((\\s+)?(\\'|\\")(.*?)(\\'|\\")`;
const matchHooks = `\(.*?)@since\\s+(${ verRegEx })(.*?)(apply_filters|do_action)\\((\\s+)?(\\'|\\")(.*?)(\\'|\\")`;
const newRegEx = new RegExp( matchHooks, 'gs' );
for ( const p in patches ) {
const patch = patches[ p ];
const results = patch.match( newRegEx );
// Separate patches into bits beginning with a comment. If a bit does not have an action, disregard.
const patchWithHook = patch.split( '/**' ).find( ( s ) => {
return (
s.includes( 'apply_filters' ) || s.includes( 'do_action' )
);
} );
if ( ! patchWithHook ) {
continue;
}
const results = patchWithHook.match( newRegEx );
const hooksList: Map< string, string[] > = new Map<
string,
string[]
@ -399,10 +408,10 @@ export default class Analyzer extends Command {
continue;
}
const description = getHookDescription( raw );
const name = getHookName( hookName[ 3 ] );
const description = getHookDescription( raw, name );
if ( ! description ) {
this.error(
`Hook ${ name } has no description. Please add a description.`

View File

@ -198,13 +198,25 @@ export const isValidCommitHash = ( branch: string ): boolean => {
* Extrace hook description from a raw diff.
*
* @param {string} diff raw diff.
* @param {string} name hook name.
* @return {string|false} hook description or false if none exists.
*/
export const getHookDescription = ( diff: string ): string | false => {
export const getHookDescription = (
diff: string,
name: string
): string | false => {
const diffWithoutDeletions = diff.replace( /-.*\n/g, '' );
const diffWithHook = diffWithoutDeletions
.split( '/**' ) // Separate by the beginning of a comment.
.find( ( d ) => d.includes( name ) ); // Use just the one associated with our hook.
if ( ! diffWithHook ) {
return false;
}
// Extract hook description.
const description = diffWithoutDeletions.match( /\/\*\*([\s\S]*) @since/ );
const description = diffWithHook.match( /([\s\S]*?) @since/ );
if ( ! description ) {
return false;