Skip to content

Commit

Permalink
feat(composer): filter releases using php constraint
Browse files Browse the repository at this point in the history
Closes #18715
  • Loading branch information
rarkins committed Jan 15, 2023
1 parent b4d1ad8 commit a4178af
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
55 changes: 55 additions & 0 deletions lib/modules/datasource/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { regEx } from '../../util/regex';
import { trimTrailingSlash } from '../../util/url';
import * as allVersioning from '../versioning';
import datasources from './api';
import { GithubTagsDatasource } from './github-tags';
import { addMetaData } from './metadata';
import { setNpmrc } from './npm';
import { resolveRegistryUrl } from './npm/npmrc';
Expand Down Expand Up @@ -415,6 +416,60 @@ export async function getPkgReleases(
version.matches(constraintValue, releaseConstraint)
);
});
} else {
if (constraintName === 'php') {
const composerVersioning = allVersioning.get('composer');
if (composerVersioning.isValid(constraintValue)) {
const lookupConfig: GetPkgReleasesConfig = {
datasource: GithubTagsDatasource.id,
depName: 'php/php-src',
extractVersion: '^php-(?<version>\\S+)',
};
const phpVersions = (
await getPkgReleases(lookupConfig)
)?.releases.map((release) => release.version);
if (!phpVersions) {
logger.warn('Could not fetch php releases for compatibility check');
return null;
}
const matchingVersions = phpVersions.filter((version) =>
composerVersioning.matches(version, constraintValue)
);
logger.debug(
`Found ${matchingVersions.length} matching php versions`
);
if (matchingVersions.length) {
const originalReleaseCount = res.releases.length;
res.releases = res.releases.filter((release) => {
const constraint = release.constraints?.[constraintName];
if (!is.nonEmptyArray(constraint)) {
// A release with no constraints is OK
return true;
}
return constraint.some(
// If any of the release's constraints match, then it's OK
(releaseConstraint) => {
const releaseMatchingVersions = phpVersions.filter(
(version) =>
composerVersioning.matches(version, releaseConstraint)
);

const isMatch = matchingVersions.every((version) =>
releaseMatchingVersions.includes(version)
);
return isMatch;
}
);
});
const filteredReleaseCount = res.releases.length;
logger.debug(
`${filteredReleaseCount} of ${originalReleaseCount} releases have matching constraints`
);
}
} else {
logger.warn({ constraintValue }, 'Invalid php constraint');
}
}
}
}
// Strip constraints from releases result
Expand Down
22 changes: 20 additions & 2 deletions lib/modules/datasource/packagist/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ export class PackagistDatasource extends Datasource {
return url;
}

private static minifyExpand(releases: any[]): any[] {
let expandedRelease: any;
return releases.map((release) => {
if (release.require) {
expandedRelease = release;
}
return { ...expandedRelease, ...release };
});
}

@cache({
namespace: `datasource-${PackagistDatasource.id}-public-files`,
key: (regUrl: string, regFile: RegistryFile) =>
Expand Down Expand Up @@ -137,10 +147,16 @@ export class PackagistDatasource extends Datasource {
if (release.source?.url) {
dep.sourceUrl = release.source.url;
}
const constraints: Record<string, string[]> = {};
if (release.require?.php) {
constraints.php = [release.require.php];
}

return {
version: parsedVersion.replace(regEx(/^v/), ''),
gitRef: parsedVersion,
releaseTimestamp: release.time,
constraints,
};
});
return dep;
Expand Down Expand Up @@ -218,8 +234,10 @@ export class PackagistDatasource extends Datasource {
// TODO: fix types (#9610)
let res = (await this.http.getJson<any>(pkgUrl[0])).body.packages[name];
res = [
...res,
...(await this.http.getJson<any>(pkgUrl[1])).body.packages[name],
...PackagistDatasource.minifyExpand(res),
...PackagistDatasource.minifyExpand(
(await this.http.getJson<any>(pkgUrl[1])).body.packages[name]
),
];
if (res) {
dep = PackagistDatasource.extractDepReleases(res);
Expand Down
5 changes: 5 additions & 0 deletions lib/modules/manager/composer/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,10 @@ export async function extractPackageFile(
};
res.managerData = managerData;
}

if (is.nonEmptyString(composerJson.require?.php)) {
res.extractedConstraints = { php: composerJson.require.php };
}

return res;
}

0 comments on commit a4178af

Please sign in to comment.