diff --git a/.github/workflows/pr-highlight-changes.yml b/.github/workflows/pr-highlight-changes.yml index 2beaac201c2..4477b339b9b 100644 --- a/.github/workflows/pr-highlight-changes.yml +++ b/.github/workflows/pr-highlight-changes.yml @@ -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: diff --git a/tools/code-analyzer/src/commands/analyzer/index.ts b/tools/code-analyzer/src/commands/analyzer/index.ts index 725e0802781..34badd516ea 100644 --- a/tools/code-analyzer/src/commands/analyzer/index.ts +++ b/tools/code-analyzer/src/commands/analyzer/index.ts @@ -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.` diff --git a/tools/code-analyzer/src/utils.ts b/tools/code-analyzer/src/utils.ts index 019c09fcd0e..45a968b7235 100644 --- a/tools/code-analyzer/src/utils.ts +++ b/tools/code-analyzer/src/utils.ts @@ -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;