Skip to content

Fix: Convert branch names to valid Composer dev-branch format #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/package-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ function chooseNameAndVersion(magentoName, composerJson, ref, givenVersion) {
const composerConfig = JSON.parse(composerJson);
let {version, name} = composerConfig;
version = givenVersion || version;

// If no version is found and ref looks like a branch name, convert to dev-branch format
if (!version && ref && !ref.match(/^v?\d+\.\d+/)) {
version = `dev-${ref}`;
}

// ALWAYS convert invalid version strings that are branch names to dev-branch format
// This catches cases where composer.json itself contains "develop" as version
if (version && !version.match(/^v?\d+\.\d+/) && !version.startsWith('dev-') && version.match(/^[a-zA-Z][a-zA-Z0-9_-]*$/)) {
version = `dev-${version}`;
}

if (!name) {
throw {
kind: 'NAME_UNKNOWN',
Expand Down Expand Up @@ -166,7 +178,11 @@ async function determinePackageForRef(url, moduleDir, ref, options) {
// Some refs do not have a version in the composer.json (e.g. the base package or the magento-composer-installer 0.4.0-beta1=
// For those we use the ref as the version. This might be a problem, so for now we leave it as is.
if (exception.kind === 'VERSION_UNKNOWN') {
return {[exception.name]: exception.ref}
// For branch names, convert to Composer dev- format
const version = exception.ref.match(/^v?(?:\d+\.){0,3}\d+(?:-[a-z]\w*|)$/i)
? exception.ref
: `dev-${exception.ref}`;
return {[exception.name]: version}
}
console.log(`Unable to determine name and/or version for ${magentoName || url} in ${ref}: ${exception.message || exception}`);
return {};
Expand Down Expand Up @@ -375,7 +391,18 @@ async function createComposerJsonOnlyPackage(url, ref, name, transform, release)
isExecutable: false,
}];

const packageFilepath = archiveFilePath(name, release || ref);
// Convert branch names to proper dev-branch format for Composer
let versionForFilename = release || ref;
if (!release && ref && !ref.match(/^v?\d+\.\d+/)) {
versionForFilename = `dev-${ref}`;
}

// Also check if versionForFilename looks like a branch name and convert it
if (versionForFilename && !versionForFilename.match(/^v?\d+\.\d+/) && !versionForFilename.startsWith('dev-')) {
versionForFilename = `dev-${versionForFilename}`;
}

const packageFilepath = archiveFilePath(name, versionForFilename);
return {packageFilepath, files}
}

Expand Down Expand Up @@ -587,6 +614,11 @@ async function determineMetaPackageFromRepoDir(url, dir, ref, release) {
throw {message: `Unable find package name and in composer.json for metapackage ${ref} in ${dir}`}
}
version = release || version || ref;

// If no version is found and ref looks like a branch name, convert to dev-branch format
if (!release && !composerConfig.version && ref && !ref.match(/^v?\d+\.\d+/)) {
version = `dev-${ref}`;
}

return {[name]: version}
}
Expand All @@ -613,6 +645,11 @@ async function createMetaPackageFromRepoDir(url, dir, ref, options) {
throw {message: `Unable find package name and in composer.json for metapackage ${ref} in ${dir}`}
}
version = release || dependencyVersions[name] || version || ref;

// If no version is found and ref looks like a branch name, convert to dev-branch format
if (!release && !dependencyVersions[name] && !composerConfig.version && ref && !ref.match(/^v?\d+\.\d+/)) {
version = `dev-${ref}`;
}

// Ensure version is set on composer config because not all repos provide the version in composer.json (e.g.
// page-builder) and it is required by satis to be able to use artifact repositories.
Expand Down
10 changes: 10 additions & 0 deletions src/release-branch-build-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ async function getPackageVersionsForBuildInstructions(buildInstructions, suffix)
}

function addSuffixToVersion(version, buildSuffix) {
// Don't add suffix to dev- branch versions
if (version.startsWith('dev-')) {
return version;
}

const match = version.match(/(?<versions>(?:[\d]+\.?){1,4})(?<suffix>-[^+]+)?(?<legacy>\+.+)?/)
if (match) {
return `${match.groups.versions}-a${buildSuffix}${match.groups.legacy ? match.groups.legacy : ''}`
Expand All @@ -100,6 +105,11 @@ function transformVersionsToNightlyBuildVersion(baseVersion, buildSuffix) {
}

function calcNightlyBuildPackageBaseVersion(version) {
// Handle dev- branch versions (e.g., dev-develop, dev-master)
if (version.startsWith('dev-')) {
return version; // Return as-is, it's already a valid Composer branch version
}

if (! version.match(/^v?(?:\d+\.){0,3}\d+(?:-[a-z]\w*|)$/i)) {
throw Error(`Unable to determine branch release version for input version "${version}"`)
}
Expand Down