From 0f68d567c5a713651b7fcf1c39faff1bcfeb7e9e Mon Sep 17 00:00:00 2001 From: montehurd Date: Fri, 24 May 2024 12:34:33 -0700 Subject: [PATCH 1/3] Make Pixel smarter about needed Codex version --- pixel.js | 56 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/pixel.js b/pixel.js index 845e6b1f..9506e824 100755 --- a/pixel.js +++ b/pixel.js @@ -52,6 +52,34 @@ async function getLatestCodexVersion() { return `${stdout.split( 'refs/tags/' )[ 1 ].trim()}`; } +/** + * @param {string} mwBranch + * @return {Promise} + */ +async function getCodexVersionForMWBranch( mwBranch ) { + // Get the version referenced in the MW branch's 'foreign-resources.yaml' + // First remove the remote prefix from the branch, if present + // So "origin/wmf/1.43.0-wmf.5" becomes "wmf/1.43.0-wmf.5" + mwBranch = ( mwBranch.match( /\//g ) || [] ).length > 1 ? + mwBranch.slice( mwBranch.indexOf( '/' ) + 1 ) : + mwBranch; + try { + const { stdout } = await exec( ` +curl -sf --compressed --retry 5 --retry-delay 5 --retry-max-time 120 'https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/core/+/refs/heads/${mwBranch}/resources/lib/foreign-resources.yaml?format=TEXT' | \ +docker run --rm -i mikefarah/yq eval '@base64d | from_yaml | .codex.version' - + ` ); + if ( stdout.trim() === '' ) { + throw new Error( 'Codex version string is unexpectedly blank' ); + } + return `v${stdout.trim()}`; + } catch ( error ) { + error.message = "Error getting Codex version for MW branch '" + mwBranch + "'\n" + + error.message + + "Either 'foreign-resources.yaml' doesn't exist at the curled url, couldn't be retrieved, or the yaml key '.codex.version' is not present in that version of the yaml\n"; + throw error; + } +} + /** * @param {string} indexFileFullPath Full path to the index file. * @param {string} bannerContent The banner content to prepend. @@ -156,6 +184,23 @@ async function processCommand( type, opts, runSilently = false ) { setEnvironmentFlagIfGroup( 'ENABLE_WIKILAMBDA', 'wikilambda', group ); const activeBranch = await getActiveBranch( opts ); + + // If the user hasn't specified a '--repo-branch' for codex + // determine which version the MW branch wants and use it + if ( !opts.repoBranch?.some( ( branch ) => branch.startsWith( 'design/codex:' ) ) ) { + let codexVersion; + try { + codexVersion = await getCodexVersionForMWBranch( activeBranch ); + } catch ( error ) { + codexVersion = await getLatestCodexVersion(); + console.log( `\x1b[33m${error.message}Falling back to latest Codex version ${codexVersion}\x1b[0m` ); + } + opts.repoBranch = [ ...( opts.repoBranch ?? [] ), `design/codex:${codexVersion}` ]; + } + console.log( 'repoBranch:' ); + console.log( opts.repoBranch ); + // process.exit ( 1 ) + const description = getDescription( opts ); updateContext( group, type, activeBranch, description ); @@ -181,7 +226,8 @@ async function processCommand( type, opts, runSilently = false ) { async function getActiveBranch( opts ) { if ( opts.branch === LATEST_RELEASE_BRANCH ) { - return await getLatestReleaseBranchAndUpdateOpts( opts ); + opts.branch = await getLatestReleaseBranch(); + return opts.branch; } else if ( opts.branch !== 'master' ) { return opts.branch; } else { @@ -189,14 +235,6 @@ async function getActiveBranch( opts ) { } } -async function getLatestReleaseBranchAndUpdateOpts( opts ) { - opts.branch = await getLatestReleaseBranch(); - const codexTag = await getLatestCodexVersion(); - opts.repoBranch = [ ...( opts.repoBranch ?? [] ), `design/codex:${codexTag}` ]; - console.log( `Using latest branch "${opts.branch}" (for Codex, "${codexTag}")` ); - return opts.branch; -} - function getDescription( opts ) { let description = ''; From e6c210bc795f925a99881028cfcaa62fd73f6e61 Mon Sep 17 00:00:00 2001 From: montehurd Date: Fri, 24 May 2024 16:42:47 -0700 Subject: [PATCH 2/3] Move Codex repo branch logic to func --- pixel.js | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/pixel.js b/pixel.js index 9506e824..b22b9b12 100755 --- a/pixel.js +++ b/pixel.js @@ -162,6 +162,21 @@ function removeFolder( relativePath ) { fs.rmSync( `${__dirname}/${relativePath}`, { recursive: true, force: true } ); } +async function updateCodexRepoBranchIfNecessary( opts, mwBranch ) { + // If the user hasn't specified a '--repo-branch' for codex + // determine which version the MW branch wants and use it + if ( !opts.repoBranch?.some( ( branch ) => branch.startsWith( 'design/codex:' ) ) ) { + let codexVersion; + try { + codexVersion = await getCodexVersionForMWBranch( mwBranch ); + } catch ( error ) { + codexVersion = await getLatestCodexVersion(); + console.log( `\x1b[33m${error.message}Falling back to latest Codex version ${codexVersion}\x1b[0m` ); + } + opts.repoBranch = [ ...( opts.repoBranch ?? [] ), `design/codex:${codexVersion}` ]; + } +} + /** * @typedef {Object} CommandOptions * @property {string[]} [changeId] @@ -183,26 +198,12 @@ async function processCommand( type, opts, runSilently = false ) { setEnvironmentFlagIfGroup( 'ENABLE_WIKILAMBDA', 'wikilambda', group ); - const activeBranch = await getActiveBranch( opts ); - - // If the user hasn't specified a '--repo-branch' for codex - // determine which version the MW branch wants and use it - if ( !opts.repoBranch?.some( ( branch ) => branch.startsWith( 'design/codex:' ) ) ) { - let codexVersion; - try { - codexVersion = await getCodexVersionForMWBranch( activeBranch ); - } catch ( error ) { - codexVersion = await getLatestCodexVersion(); - console.log( `\x1b[33m${error.message}Falling back to latest Codex version ${codexVersion}\x1b[0m` ); - } - opts.repoBranch = [ ...( opts.repoBranch ?? [] ), `design/codex:${codexVersion}` ]; - } - console.log( 'repoBranch:' ); - console.log( opts.repoBranch ); - // process.exit ( 1 ) + const activeMWBranch = await getActiveBranch( opts ); + + await updateCodexRepoBranchIfNecessary( opts, activeMWBranch ); const description = getDescription( opts ); - updateContext( group, type, activeBranch, description ); + updateContext( group, type, activeMWBranch, description ); await prepareDockerEnvironment( opts ); const { stdout: stdout1 } = await simpleSpawn.exec( './purgeParserCache.sh' ); From b91e290884ec46f44601cbe952f168f5ce58e097 Mon Sep 17 00:00:00 2001 From: montehurd Date: Fri, 24 May 2024 16:46:29 -0700 Subject: [PATCH 3/3] Reduce nesting --- pixel.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/pixel.js b/pixel.js index b22b9b12..19a80254 100755 --- a/pixel.js +++ b/pixel.js @@ -163,18 +163,20 @@ function removeFolder( relativePath ) { } async function updateCodexRepoBranchIfNecessary( opts, mwBranch ) { - // If the user hasn't specified a '--repo-branch' for codex - // determine which version the MW branch wants and use it - if ( !opts.repoBranch?.some( ( branch ) => branch.startsWith( 'design/codex:' ) ) ) { - let codexVersion; - try { - codexVersion = await getCodexVersionForMWBranch( mwBranch ); - } catch ( error ) { - codexVersion = await getLatestCodexVersion(); - console.log( `\x1b[33m${error.message}Falling back to latest Codex version ${codexVersion}\x1b[0m` ); - } - opts.repoBranch = [ ...( opts.repoBranch ?? [] ), `design/codex:${codexVersion}` ]; + // Return if the user has already specified a '--repo-branch' for Codex + if ( opts.repoBranch?.some( ( branch ) => branch.startsWith( 'design/codex:' ) ) ) { + return; + } + // Determine which Codex version the MW branch wants and use it, + // fallback on latest version + let codexVersion; + try { + codexVersion = await getCodexVersionForMWBranch( mwBranch ); + } catch ( error ) { + codexVersion = await getLatestCodexVersion(); + console.log( `\x1b[33m${error.message}Falling back to latest Codex version ${codexVersion}\x1b[0m` ); } + opts.repoBranch = [ ...( opts.repoBranch ?? [] ), `design/codex:${codexVersion}` ]; } /**