From 585c6da197c20159bfe9b360ff353ab1649da850 Mon Sep 17 00:00:00 2001 From: SimenBergo Date: Tue, 16 Jun 2026 08:51:15 +0200 Subject: [PATCH] H5PT-227 Add optional branch arg to setup --- cli.js | 11 ++++++++--- logic.js | 18 +++++++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/cli.js b/cli.js index 276df757..c5309aa7 100644 --- a/cli.js +++ b/cli.js @@ -187,7 +187,7 @@ const cli = { } }, // computes & installs dependencies for h5p library - setup: async function(library, version, download) { + setup: async function(library, version, download, branch) { const isUrl = ['http', 'git@'].includes(library.slice(0, 4)) ? true : false; const url = library; const missingOptionals = {}; @@ -196,6 +196,11 @@ const cli = { const entry = await this.register(url); library = logic.machineToShort(Object.keys(entry)[0]); } + // branch is interpolated into a git clone shell command, so reject unsafe refs + if (branch && !/^[\w./-]+$/.test(branch)) { + console.log(`> error: invalid branch name "${branch}"`); + return; + } let toSkip = []; const action = parseInt(download) ? 'download' : 'clone'; const latest = version ? false : true; @@ -217,9 +222,9 @@ const cli = { } toSkip = []; console.log(`> ${action} ${library} library "view" dependencies into "${config.folders.libraries}" folder`); - toSkip = await logic.getWithDependencies(action, library, 'view', latest, toSkip); + toSkip = await logic.getWithDependencies(action, library, 'view', latest, toSkip, branch); console.log(`> ${action} ${library} library "edit" dependencies into "${config.folders.libraries}" folder`); - toSkip = await logic.getWithDependencies(action, library, 'edit', latest, toSkip); + toSkip = await logic.getWithDependencies(action, library, 'edit', latest, toSkip, branch); if (Object.keys(missingOptionals).length) { console.log('!!! missing optional libraries'); for (let item in missingOptionals) { diff --git a/logic.js b/logic.js index ef542a30..68c84096 100644 --- a/logic.js +++ b/logic.js @@ -424,8 +424,9 @@ module.exports = { /* clones/downloads dependencies to libraries folder using git and runs relevant npm commands mode - 'view' or 'edit' to fetch non-editor or editor libraries latest - if true master branch versions of libraries are used - toSkip - optional array of libraries to skip; after a library is parsed by the function it's auto-added to the array so it's skipped for efficiency */ - getWithDependencies: async (action, library, mode, latest, toSkip = []) => { + toSkip - optional array of libraries to skip; after a library is parsed by the function it's auto-added to the array so it's skipped for efficiency + branch - optional git branch/ref to clone for the target library only; its dependencies still use master/tag */ + getWithDependencies: async (action, library, mode, latest, toSkip = [], branch = null) => { const list = await module.exports.computeDependencies(library, mode); for (let item in list) { if (toSkip.indexOf(item) != -1) { @@ -443,21 +444,24 @@ module.exports = { } const label = `${list[item].id}-${list[item].version.major}.${list[item].version.minor}`; const listVersion = `${list[item].version.major}.${list[item].version.minor}.${list[item].version.patch}`; - const version = latest ? 'master' : listVersion; + // only the library under test follows the PR branch; its deps stay on master/tag + const useBranch = branch && item === library; + const version = useBranch ? branch : (latest ? 'master' : listVersion); const folder = `${config.folders.libraries}/${label}`; if (fs.existsSync(folder)) { - if (latest && !process.env.H5P_NO_UPDATES) { + if (latest && !useBranch && !process.env.H5P_NO_UPDATES) { console.log(`>> ~ updating to ${list[item].repoName} ${listVersion}`); execSync(`git checkout master`, { cwd: folder, stdio : 'pipe' }); console.log(execSync('git pull origin', { cwd: folder }).toString()); } else { - console.log(`>> ~ skipping updates for ${list[item].repoName} ${listVersion}`); + // branch libs are intentionally left as-is on re-runs (fresh CI runners never hit this) + console.log(`>> ~ skipping updates for ${list[item].repoName} ${useBranch ? branch : listVersion}`); } continue; } - console.log(`>> + installing ${list[item].repoName} ${listVersion}`); - if (action == 'download') { + console.log(`>> + installing ${list[item].repoName} ${useBranch ? branch : listVersion}`); + if (action == 'download' && !useBranch) { await module.exports.download(list[item].org, list[item].repoName, version, folder); } else {