Skip to content
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

Optimize constraints #17

Open
wants to merge 1 commit into
base: build-v2
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
50 changes: 43 additions & 7 deletions build/build-composer-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function fetchAllData($url, Client $client) {

// Security releases
$results = fetchAllData('https://www.drupal.org/api-d7/node.json?type=project_release&taxonomy_vocabulary_7=100&field_release_build_type=static', $client);
$securityVersions = [];
foreach ($results as $result) {
$nid = $result->field_release_project->id;
$core = (int) substr($result->field_release_version, 0, 1);
Expand All @@ -73,18 +74,33 @@ function fetchAllData($url, Client $client) {
}

try {
$is_core = ($project->field_project_machine_name == 'drupal') ? TRUE : FALSE;
$constraint = VersionParser::generateRangeConstraint($result->field_release_version, $is_core);
if (!$constraint) {
throw new InvalidArgumentException('Invalid version number.');
$is_core = ($project->field_project_machine_name == 'drupal');
$versionGroup = $result->field_release_version_major . (($is_core && $core == 8) ? '.' . $result->field_release_version_minor : '');

if (empty($securityVersions[$core]['drupal/' . $project->field_project_machine_name][$versionGroup])
||
version_compare($securityVersions[$core]['drupal/' . $project->field_project_machine_name][$versionGroup], $result->field_release_version, '<')
) {
$securityVersions[$core]['drupal/' . $project->field_project_machine_name][$versionGroup] = $result->field_release_version;
}
$conflict[$core]['drupal/' . $project->field_project_machine_name][] = $constraint;
} catch (\Exception $e) {
// @todo: log exception
continue;
}
}

foreach ($securityVersions as $core => $packages) {
foreach ($packages as $package => $majorVersions) {
foreach ($majorVersions as $versionGroup => $version) {
$constraint = VersionParser::generateRangeConstraint($version, ($package == 'drupal/drupal'));
if (!$constraint) {
throw new InvalidArgumentException('Invalid version number.');
}
$conflict[$core][$package][] = $constraint;
}
}
}

// Insecure releases
$results = fetchAllData('https://www.drupal.org/api-d7/node.json?type=project_release&taxonomy_vocabulary_7=188131&field_release_build_type=static', $client);
foreach ($results as $result) {
Expand All @@ -104,7 +120,23 @@ function fetchAllData($url, Client $client) {
}

try {
$is_core = ($project->field_project_machine_name == 'drupal') ? TRUE : FALSE;
$is_core = ($project->field_project_machine_name == 'drupal');
$versionGroup = $result->field_release_version_major . (($is_core && $core == 8) ? '.' . $result->field_release_version_minor : '');

// Cleanup core versions prior to SemVer (e.g. 8.0-alpha1).
if ($is_core && $core == 8 && empty($result->field_release_version_patch)) {
continue;
}

// Filter any individual releases older than a security release.
if (
!empty($securityVersions[$core]['drupal/' . $project->field_project_machine_name][$versionGroup])
&&
version_compare($securityVersions[$core]['drupal/' . $project->field_project_machine_name][$versionGroup], $result->field_release_version, '>')
) {
continue;
}

$constraint = VersionParser::generateExplicitConstraint($result->field_release_version, $is_core);
if (!$constraint) {
throw new InvalidArgumentException('Invalid version number.');
Expand All @@ -131,7 +163,11 @@ function fetchAllData($url, Client $client) {
];

foreach ($packages as $package => $constraints) {
natsort($constraints);
usort($constraints, function ($a, $b) {
preg_match('/<?(\d+(?:.\d+)+?(?:-.+)?)$/', $a, $aMatches);
preg_match('/<?(\d+(?:.\d+)+?(?:-.+)?)$/', $b, $bMatches);
return version_compare($aMatches[1], $bMatches[1]);
});
$composer['conflict'][$package] = implode('|', $constraints);
}

Expand Down
3 changes: 3 additions & 0 deletions build/src/VersionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public static function generateExplicitConstraint($version, $isCore) {

public static function handleCore($version) {
list($major, $minor) = explode('.', $version);
if ($major == '7') {
return ">=$major,<$version";
}
return ">=$major.$minor,<$version";
}

Expand Down