Skip to content

Commit 59ecbc2

Browse files
committed
Optimize version constraints
- Only add one range constraint for most recent security release - Exclude individual unsupported releases that are covered by a security release range
1 parent f8bd240 commit 59ecbc2

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

build/build-composer-json.php

+43-7
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ function fetchAllData($url, Client $client) {
5656

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

7576
try {
76-
$is_core = ($project->field_project_machine_name == 'drupal') ? TRUE : FALSE;
77-
$constraint = VersionParser::generateRangeConstraint($result->field_release_version, $is_core);
78-
if (!$constraint) {
79-
throw new InvalidArgumentException('Invalid version number.');
77+
$is_core = ($project->field_project_machine_name == 'drupal');
78+
$versionGroup = $result->field_release_version_major . (($is_core && $core == 8) ? '.' . $result->field_release_version_minor : '');
79+
80+
if (empty($securityVersions[$core]['drupal/' . $project->field_project_machine_name][$versionGroup])
81+
||
82+
version_compare($securityVersions[$core]['drupal/' . $project->field_project_machine_name][$versionGroup], $result->field_release_version, '<')
83+
) {
84+
$securityVersions[$core]['drupal/' . $project->field_project_machine_name][$versionGroup] = $result->field_release_version;
8085
}
81-
$conflict[$core]['drupal/' . $project->field_project_machine_name][] = $constraint;
8286
} catch (\Exception $e) {
8387
// @todo: log exception
8488
continue;
8589
}
8690
}
8791

92+
foreach ($securityVersions as $core => $packages) {
93+
foreach ($packages as $package => $majorVersions) {
94+
foreach ($majorVersions as $versionGroup => $version) {
95+
$constraint = VersionParser::generateRangeConstraint($version, ($package == 'drupal/drupal'));
96+
if (!$constraint) {
97+
throw new InvalidArgumentException('Invalid version number.');
98+
}
99+
$conflict[$core][$package][] = $constraint;
100+
}
101+
}
102+
}
103+
88104
// Insecure releases
89105
$results = fetchAllData('https://www.drupal.org/api-d7/node.json?type=project_release&taxonomy_vocabulary_7=188131&field_release_build_type=static', $client);
90106
foreach ($results as $result) {
@@ -104,7 +120,23 @@ function fetchAllData($url, Client $client) {
104120
}
105121

106122
try {
107-
$is_core = ($project->field_project_machine_name == 'drupal') ? TRUE : FALSE;
123+
$is_core = ($project->field_project_machine_name == 'drupal');
124+
$versionGroup = $result->field_release_version_major . (($is_core && $core == 8) ? '.' . $result->field_release_version_minor : '');
125+
126+
// Cleanup core versions prior to SemVer (e.g. 8.0-alpha1).
127+
if ($is_core && $core == 8 && empty($result->field_release_version_patch)) {
128+
continue;
129+
}
130+
131+
// Filter any individual releases older than a security release.
132+
if (
133+
!empty($securityVersions[$core]['drupal/' . $project->field_project_machine_name][$versionGroup])
134+
&&
135+
version_compare($securityVersions[$core]['drupal/' . $project->field_project_machine_name][$versionGroup], $result->field_release_version, '>')
136+
) {
137+
continue;
138+
}
139+
108140
$constraint = VersionParser::generateExplicitConstraint($result->field_release_version, $is_core);
109141
if (!$constraint) {
110142
throw new InvalidArgumentException('Invalid version number.');
@@ -131,7 +163,11 @@ function fetchAllData($url, Client $client) {
131163
];
132164

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

build/src/VersionParser.php

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public static function generateExplicitConstraint($version, $isCore) {
2626

2727
public static function handleCore($version) {
2828
list($major, $minor) = explode('.', $version);
29+
if ($major == '7') {
30+
return ">=$major,<$version";
31+
}
2932
return ">=$major.$minor,<$version";
3033
}
3134

0 commit comments

Comments
 (0)