Skip to content

Commit

Permalink
Make sure we do not double count any PRs for concurrent count (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriksm committed Jun 15, 2023
1 parent 0d2f782 commit ca3ada9
Show file tree
Hide file tree
Showing 5 changed files with 356 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/CosyComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class CosyComposer

private $urlArray;

private $openRelevantPrs = [];

/**
* @var ChangelogRetriever
*/
Expand Down Expand Up @@ -975,15 +977,14 @@ public function run()
if ($default_base && $default_branch) {
$this->log(sprintf('Current commit SHA for %s is %s', $default_branch, $default_base));
}
$total_prs = 0;
$is_allowed_out_of_date_pr = [];
$one_pr_per_dependency = $config->shouldUseOnePullRequestPerPackage();
foreach ($data as $delta => $item) {
$branch_name = $this->createBranchName($item, $one_pr_per_dependency, $config);
if (in_array($branch_name, $branches_flattened)) {
// Is there a PR for this?
if (array_key_exists($branch_name, $prs_named)) {
$total_prs++;
$this->countPR($item->name);
if (!$default_base && !$one_pr_per_dependency) {
$this->log(sprintf('Skipping %s because a pull request already exists', $item->name), Message::PR_EXISTS, [
'package' => $item->name,
Expand Down Expand Up @@ -1031,7 +1032,6 @@ public function run()
$this->log(sprintf('Skipping %s because a pull request already exists', $item->name), Message::PR_EXISTS, $context);
$this->closeOutdatedPrsForPackage($item->name, $item->version, $config, $prs_named[$branch_name]['number'], $prs_named, $default_branch);
unset($data[$delta]);
$total_prs++;
} else {
$is_allowed_out_of_date_pr[] = $item->name;
}
Expand Down Expand Up @@ -1071,7 +1071,7 @@ public function run()
}
switch ($update_type) {
case self::UPDATE_INDIVIDUAL:
$this->handleIndividualUpdates($data, $composer_lock_after_installing, $composer_json_data, $one_pr_per_dependency, $initial_composer_lock_data, $prs_named, $default_base, $hostname, $default_branch, $security_alerts, $total_prs, $is_allowed_out_of_date_pr);
$this->handleIndividualUpdates($data, $composer_lock_after_installing, $composer_json_data, $one_pr_per_dependency, $initial_composer_lock_data, $prs_named, $default_base, $hostname, $default_branch, $security_alerts, $is_allowed_out_of_date_pr);
break;

case self::UPDATE_ALL:
Expand All @@ -1082,6 +1082,17 @@ public function run()
$this->cleanUp();
}

protected function getPrCount() : int
{
return (int) count($this->openRelevantPrs);
}

protected function countPR($item)
{
$package_lower = strtolower($item);
$this->openRelevantPrs[$package_lower] = true;
}

protected function handleUpdateAll($initial_composer_lock_data, $composer_lock_after_installing, $alerts, Config $config, $default_base, $default_branch, $prs_named)
{
// We are going to hack an item here. We want the package to be "all" and the versions to be blank.
Expand Down Expand Up @@ -1349,13 +1360,13 @@ protected function runAuthExport($hostname)
$this->runAuthExportToken($hostname, $this->userToken);
}

protected function handleIndividualUpdates($data, $lockdata, $cdata, $one_pr_per_dependency, $lock_file_contents, $prs_named, $default_base, $hostname, $default_branch, $alerts, $total_prs, $is_allowed_out_of_date_pr)
protected function handleIndividualUpdates($data, $lockdata, $cdata, $one_pr_per_dependency, $lock_file_contents, $prs_named, $default_base, $hostname, $default_branch, $alerts, $is_allowed_out_of_date_pr)
{
$config = Config::createFromComposerData($cdata);
$can_update_beyond = $config->shouldAllowUpdatesBeyondConstraint();
$max_number_of_prs = $config->getNumberOfAllowedPrs();
foreach ($data as $item) {
if ($max_number_of_prs && $total_prs >= $max_number_of_prs) {
if ($max_number_of_prs && $this->getPrCount() >= $max_number_of_prs) {
if (!in_array($item->name, $is_allowed_out_of_date_pr)) {
$this->log(sprintf('Skipping %s because the number of max concurrent PRs (%d) seems to have been reached', $item->name, $max_number_of_prs), Message::PR_EXISTS, [
'package' => $item->name,
Expand Down Expand Up @@ -1556,7 +1567,7 @@ protected function handleIndividualUpdates($data, $lockdata, $cdata, $one_pr_per
$this->log(sprintf('Skipping %s because a pull request already exists', $item->name), Message::PR_EXISTS, [
'package' => $item->name,
]);
$total_prs++;
$this->countPR($item->name);
$this->closeOutdatedPrsForPackage($item->name, $item->version, $config, $prs_named[$branch_name]['number'], $prs_named, $default_branch);
continue;
}
Expand All @@ -1565,7 +1576,7 @@ protected function handleIndividualUpdates($data, $lockdata, $cdata, $one_pr_per
$this->log(sprintf('Skipping %s because a pull request already exists', $item->name), Message::PR_EXISTS, [
'package' => $item->name,
]);
$total_prs++;
$this->countPR($item->name);
$this->closeOutdatedPrsForPackage($item->name, $item->version, $config, $prs_named[$branch_name]['number'], $prs_named, $default_branch);
continue;
}
Expand All @@ -1584,7 +1595,7 @@ protected function handleIndividualUpdates($data, $lockdata, $cdata, $one_pr_per
$this->closeOutdatedPrsForPackage($item->name, $item->version, $config, $pullRequest['number'], $prs_named, $default_branch);
}
}
$total_prs++;
$this->countPR($item->name);
} catch (CanNotUpdateException $e) {
$this->log($e->getMessage(), Message::UNUPDATEABLE, [
'package' => $package_name,
Expand Down Expand Up @@ -1626,13 +1637,13 @@ protected function handleIndividualUpdates($data, $lockdata, $cdata, $one_pr_per
// If it failed validation because it already exists, we also want to make sure all outdated PRs are
// closed.
if (!empty($prs_named[$branch_name]['number'])) {
$total_prs++;
$this->countPR($item->name);
$this->closeOutdatedPrsForPackage($item->name, $item->version, $config, $prs_named[$branch_name]['number'], $prs_named, $default_branch);
}
} catch (\Gitlab\Exception\RuntimeException $e) {
$this->handlePossibleUpdatePrScenario($e, $branch_name, $pr_params, $prs_named, $config, $security_update);
if (!empty($prs_named[$branch_name]['number'])) {
$total_prs++;
$this->countPR($item->name);
$this->closeOutdatedPrsForPackage($item->name, $item->version, $config, $prs_named[$branch_name]['number'], $prs_named, $default_branch);
}
} catch (ComposerUpdateProcessFailedException $e) {
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/composer.concurrent.two.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"require": {
"psr/log": "~1.1.3",
"psr/cache": "~1.0.0"
},
"extra": {
"violinist": {
"number_of_concurrent_updates": 2
}
}
}

119 changes: 119 additions & 0 deletions test/fixtures/composer.concurrent.two.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "acdb4b88e34f535362a6d25f90b1deea",
"packages": [
{
"name": "psr/cache",
"version": "1.0.0",
"source": {
"type": "git",
"url": "https://github.com/php-fig/cache.git",
"reference": "9e66031f41fbbdda45ee11e93c45d480ccba3eb3"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/cache/zipball/9e66031f41fbbdda45ee11e93c45d480ccba3eb3",
"reference": "9e66031f41fbbdda45ee11e93c45d480ccba3eb3",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Cache\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for caching libraries",
"keywords": [
"cache",
"psr",
"psr-6"
],
"support": {
"issues": "https://github.com/php-fig/cache/issues",
"source": "https://github.com/php-fig/cache/tree/master"
},
"time": "2015-12-11T02:52:07+00:00"
},
{
"name": "psr/log",
"version": "1.1.3",
"source": {
"type": "git",
"url": "https://github.com/php-fig/log.git",
"reference": "0f73288fd15629204f9d42b7055f72dacbe811fc"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/log/zipball/0f73288fd15629204f9d42b7055f72dacbe811fc",
"reference": "0f73288fd15629204f9d42b7055f72dacbe811fc",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.1.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Log\\": "Psr/Log/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for logging libraries",
"homepage": "https://github.com/php-fig/log",
"keywords": [
"log",
"psr",
"psr-3"
],
"support": {
"source": "https://github.com/php-fig/log/tree/1.1.3"
},
"time": "2020-03-23T09:12:05+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": [],
"plugin-api-version": "2.3.0"
}
118 changes: 118 additions & 0 deletions test/fixtures/composer.concurrent.two.lock.updated
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "acdb4b88e34f535362a6d25f90b1deea",
"packages": [
{
"name": "psr/cache",
"version": "1.0.1",
"source": {
"type": "git",
"url": "https://github.com/php-fig/cache.git",
"reference": "d11b50ad223250cf17b86e38383413f5a6764bf8"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/cache/zipball/d11b50ad223250cf17b86e38383413f5a6764bf8",
"reference": "d11b50ad223250cf17b86e38383413f5a6764bf8",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Cache\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for caching libraries",
"keywords": [
"cache",
"psr",
"psr-6"
],
"support": {
"source": "https://github.com/php-fig/cache/tree/master"
},
"time": "2016-08-06T20:24:11+00:00"
},
{
"name": "psr/log",
"version": "1.1.4",
"source": {
"type": "git",
"url": "https://github.com/php-fig/log.git",
"reference": "d49695b909c3b7628b6289db5479a1c204601f11"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/log/zipball/d49695b909c3b7628b6289db5479a1c204601f11",
"reference": "d49695b909c3b7628b6289db5479a1c204601f11",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.1.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Log\\": "Psr/Log/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "https://www.php-fig.org/"
}
],
"description": "Common interface for logging libraries",
"homepage": "https://github.com/php-fig/log",
"keywords": [
"log",
"psr",
"psr-3"
],
"support": {
"source": "https://github.com/php-fig/log/tree/1.1.4"
},
"time": "2021-05-03T11:20:27+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": [],
"plugin-api-version": "2.3.0"
}

0 comments on commit ca3ada9

Please sign in to comment.