Skip to content

Commit 0f68d56

Browse files
committed
Make Pixel smarter about needed Codex version
1 parent f40c325 commit 0f68d56

File tree

1 file changed

+47
-9
lines changed

1 file changed

+47
-9
lines changed

pixel.js

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,34 @@ async function getLatestCodexVersion() {
5252
return `${stdout.split( 'refs/tags/' )[ 1 ].trim()}`;
5353
}
5454

55+
/**
56+
* @param {string} mwBranch
57+
* @return {Promise<string>}
58+
*/
59+
async function getCodexVersionForMWBranch( mwBranch ) {
60+
// Get the version referenced in the MW branch's 'foreign-resources.yaml'
61+
// First remove the remote prefix from the branch, if present
62+
// So "origin/wmf/1.43.0-wmf.5" becomes "wmf/1.43.0-wmf.5"
63+
mwBranch = ( mwBranch.match( /\//g ) || [] ).length > 1 ?
64+
mwBranch.slice( mwBranch.indexOf( '/' ) + 1 ) :
65+
mwBranch;
66+
try {
67+
const { stdout } = await exec( `
68+
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' | \
69+
docker run --rm -i mikefarah/yq eval '@base64d | from_yaml | .codex.version' -
70+
` );
71+
if ( stdout.trim() === '' ) {
72+
throw new Error( 'Codex version string is unexpectedly blank' );
73+
}
74+
return `v${stdout.trim()}`;
75+
} catch ( error ) {
76+
error.message = "Error getting Codex version for MW branch '" + mwBranch + "'\n" +
77+
error.message +
78+
"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";
79+
throw error;
80+
}
81+
}
82+
5583
/**
5684
* @param {string} indexFileFullPath Full path to the index file.
5785
* @param {string} bannerContent The banner content to prepend.
@@ -156,6 +184,23 @@ async function processCommand( type, opts, runSilently = false ) {
156184
setEnvironmentFlagIfGroup( 'ENABLE_WIKILAMBDA', 'wikilambda', group );
157185

158186
const activeBranch = await getActiveBranch( opts );
187+
188+
// If the user hasn't specified a '--repo-branch' for codex
189+
// determine which version the MW branch wants and use it
190+
if ( !opts.repoBranch?.some( ( branch ) => branch.startsWith( 'design/codex:' ) ) ) {
191+
let codexVersion;
192+
try {
193+
codexVersion = await getCodexVersionForMWBranch( activeBranch );
194+
} catch ( error ) {
195+
codexVersion = await getLatestCodexVersion();
196+
console.log( `\x1b[33m${error.message}Falling back to latest Codex version ${codexVersion}\x1b[0m` );
197+
}
198+
opts.repoBranch = [ ...( opts.repoBranch ?? [] ), `design/codex:${codexVersion}` ];
199+
}
200+
console.log( 'repoBranch:' );
201+
console.log( opts.repoBranch );
202+
// process.exit ( 1 )
203+
159204
const description = getDescription( opts );
160205
updateContext( group, type, activeBranch, description );
161206

@@ -181,22 +226,15 @@ async function processCommand( type, opts, runSilently = false ) {
181226

182227
async function getActiveBranch( opts ) {
183228
if ( opts.branch === LATEST_RELEASE_BRANCH ) {
184-
return await getLatestReleaseBranchAndUpdateOpts( opts );
229+
opts.branch = await getLatestReleaseBranch();
230+
return opts.branch;
185231
} else if ( opts.branch !== 'master' ) {
186232
return opts.branch;
187233
} else {
188234
return opts.changeId ? opts.changeId[ 0 ] : opts.branch;
189235
}
190236
}
191237

192-
async function getLatestReleaseBranchAndUpdateOpts( opts ) {
193-
opts.branch = await getLatestReleaseBranch();
194-
const codexTag = await getLatestCodexVersion();
195-
opts.repoBranch = [ ...( opts.repoBranch ?? [] ), `design/codex:${codexTag}` ];
196-
console.log( `Using latest branch "${opts.branch}" (for Codex, "${codexTag}")` );
197-
return opts.branch;
198-
}
199-
200238
function getDescription( opts ) {
201239
let description = '';
202240

0 commit comments

Comments
 (0)