@@ -52,6 +52,34 @@ async function getLatestCodexVersion() {
52
52
return `${ stdout . split ( 'refs/tags/' ) [ 1 ] . trim ( ) } ` ;
53
53
}
54
54
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
+
55
83
/**
56
84
* @param {string } indexFileFullPath Full path to the index file.
57
85
* @param {string } bannerContent The banner content to prepend.
@@ -156,6 +184,23 @@ async function processCommand( type, opts, runSilently = false ) {
156
184
setEnvironmentFlagIfGroup ( 'ENABLE_WIKILAMBDA' , 'wikilambda' , group ) ;
157
185
158
186
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
+
159
204
const description = getDescription ( opts ) ;
160
205
updateContext ( group , type , activeBranch , description ) ;
161
206
@@ -181,22 +226,15 @@ async function processCommand( type, opts, runSilently = false ) {
181
226
182
227
async function getActiveBranch ( opts ) {
183
228
if ( opts . branch === LATEST_RELEASE_BRANCH ) {
184
- return await getLatestReleaseBranchAndUpdateOpts ( opts ) ;
229
+ opts . branch = await getLatestReleaseBranch ( ) ;
230
+ return opts . branch ;
185
231
} else if ( opts . branch !== 'master' ) {
186
232
return opts . branch ;
187
233
} else {
188
234
return opts . changeId ? opts . changeId [ 0 ] : opts . branch ;
189
235
}
190
236
}
191
237
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
-
200
238
function getDescription ( opts ) {
201
239
let description = '' ;
202
240
0 commit comments