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

Allow to select the branch to release #1507

Merged
merged 8 commits into from
May 10, 2021
Merged
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
22 changes: 8 additions & 14 deletions src/Action/DetermineNextRelease.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use App\Action\Exception\CannotDetermineNextRelease;
use App\Action\Exception\NoPullRequestsMergedSinceLastRelease;
use App\Command\AbstractCommand;
use App\Domain\Exception\NoBranchesAvailable;
use App\Domain\Value\Branch;
use App\Domain\Value\NextRelease;
use App\Domain\Value\Project;
use App\Github\Api\Branches;
Expand Down Expand Up @@ -49,24 +49,16 @@ public function __construct(
$this->pullRequests = $pullRequests;
}

public function __invoke(Project $project): NextRelease
public function __invoke(Project $project, Branch $branch): NextRelease
{
$repository = $project->repository();

try {
$branch = $project->stableBranch() ?? $project->unstableBranch();
} catch (NoBranchesAvailable $e) {
throw CannotDetermineNextRelease::forProject(
$project,
$e
);
}

try {
$currentRelease = $this->releases->latest($repository);
$currentRelease = $this->releases->latestForBranch($repository, $branch);
} catch (LatestReleaseNotFound $e) {
throw CannotDetermineNextRelease::forProject(
throw CannotDetermineNextRelease::forBranch(
$project,
$branch,
$e
);
}
Expand All @@ -77,8 +69,9 @@ public function __invoke(Project $project): NextRelease
);

if ([] === $pullRequests) {
throw NoPullRequestsMergedSinceLastRelease::forProject(
throw NoPullRequestsMergedSinceLastRelease::forBranch(
$project,
$branch,
$currentRelease->publishedAt()
);
}
Expand All @@ -100,6 +93,7 @@ public function __invoke(Project $project): NextRelease

return NextRelease::fromValues(
$project,
$branch,
$currentRelease->tag(),
$combinedStatus,
$checkRuns,
Expand Down
12 changes: 11 additions & 1 deletion src/Action/DetermineNextReleaseVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,17 @@ public static function forTagAndPullRequests(Tag $current, array $pullRequests):
return $pr->stability()->toString();
}, $pullRequests);

$parts = explode('.', $current->toString());
// Add compatibility for non-semantical versioning version like `4.0.0-alpha-1`
$currentTag = str_replace('-', '.', $current->toString());
$parts = explode('.', $currentTag);

if (isset($parts[3])) {
return Tag::fromString(implode('.', [$parts[0], $parts[1], $parts[2]]));
}

if (\in_array(Stability::major()->toString(), $stabilities, true)) {
return Tag::fromString(implode('.', [(int) $parts[0] + 1, 0, 0]));
}

if (\in_array(Stability::minor()->toString(), $stabilities, true)) {
return Tag::fromString(implode('.', [$parts[0], (int) $parts[1] + 1, 0]));
Expand Down
6 changes: 4 additions & 2 deletions src/Action/Exception/CannotDetermineNextRelease.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@

namespace App\Action\Exception;

use App\Domain\Value\Branch;
use App\Domain\Value\Project;

/**
* @author Oskar Stark <[email protected]>
*/
final class CannotDetermineNextRelease extends \RuntimeException
{
public static function forProject(Project $project, ?\Throwable $previous = null): self
public static function forBranch(Project $project, Branch $branch, ?\Throwable $previous = null): self
{
return new self(
sprintf(
'Cannot determine next release for Project "%s".',
'Cannot determine next release for branch "%s" of project "%s".',
$branch->name(),
$project->name()
),
0,
Expand Down
6 changes: 4 additions & 2 deletions src/Action/Exception/NoPullRequestsMergedSinceLastRelease.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@

namespace App\Action\Exception;

use App\Domain\Value\Branch;
use App\Domain\Value\Project;

/**
* @author Oskar Stark <[email protected]>
*/
final class NoPullRequestsMergedSinceLastRelease extends \RuntimeException
{
public static function forProject(Project $project, \DateTimeImmutable $lastRelease, ?\Throwable $previous = null): self
public static function forBranch(Project $project, Branch $branch, \DateTimeImmutable $lastRelease, ?\Throwable $previous = null): self
{
return new self(
sprintf(
'No pull requests merged since last release "%s" for Project "%s".',
'No pull requests merged since last release "%s" for branch "%s" of project "%s".',
$lastRelease->format('Y-m-d H:i:s'),
$branch->name(),
$project->name()
),
0,
Expand Down
33 changes: 27 additions & 6 deletions src/Command/ReleaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use App\Action\Exception\CannotDetermineNextRelease;
use App\Action\Exception\NoPullRequestsMergedSinceLastRelease;
use App\Config\Projects;
use App\Domain\Value\Branch;
use App\Domain\Value\Project;
use App\Domain\Value\Stability;
use App\Github\Domain\Value\CheckRun;
Expand All @@ -27,6 +28,7 @@
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\Question;

/**
Expand Down Expand Up @@ -83,10 +85,11 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
$project = $this->selectProject($input, $output);
$branch = $this->selectBranch($input, $output, $project);

$this->io->getErrorStyle()->title($project->name());

return $this->renderNextRelease($project);
return $this->renderNextRelease($project, $branch);
}

private function selectProject(InputInterface $input, OutputInterface $output): Project
Expand All @@ -95,9 +98,7 @@ private function selectProject(InputInterface $input, OutputInterface $output):

$question = new Question('<info>Please enter the name of the project to release:</info> ');
$question->setAutocompleterValues(array_keys($this->projects->all()));
$question->setNormalizer(static function ($answer) {
return $answer ? trim($answer) : '';
});
$question->setTrimmable(true);
$question->setValidator(function ($answer): Project {
return $this->projects->byName($answer);
});
Expand All @@ -106,11 +107,31 @@ private function selectProject(InputInterface $input, OutputInterface $output):
return $helper->ask($input, $output, $question);
}

private function renderNextRelease(Project $project): int
private function selectBranch(InputInterface $input, OutputInterface $output, Project $project): Branch
{
$helper = $this->getHelper('question');

$default = ($project->stableBranch() ?? $project->unstableBranch())->name();

$question = new ChoiceQuestion(
sprintf('<info>Please select the branch of the project to release:</info> (Default: "%s")', $default),
$project->branchNamesReverse(),
$default
);
$question->setTrimmable(true);
$question->setValidator(static function ($answer) use ($project): Branch {
return $project->branch($answer);
});
$question->setMaxAttempts(3);

return $helper->ask($input, $output, $question);
}

private function renderNextRelease(Project $project, Branch $branch): int
{
$notificationStyle = $this->io->getErrorStyle();
try {
$nextRelease = $this->determineNextRelease->__invoke($project);
$nextRelease = $this->determineNextRelease->__invoke($project, $branch);
} catch (NoPullRequestsMergedSinceLastRelease $e) {
$notificationStyle->warning($e->getMessage());

Expand Down
31 changes: 31 additions & 0 deletions src/Config/Exception/UnknownBranch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Config\Exception;

use App\Domain\Value\Project;

/**
* @author Oskar Stark <[email protected]>
*/
final class UnknownBranch extends \InvalidArgumentException
{
public static function forName(Project $project, string $name): self
{
return new self(sprintf(
'Could not find branch with name "%s" for project "%s".',
$name,
$project->name()
));
}
}
2 changes: 1 addition & 1 deletion src/Config/Exception/UnknownProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class UnknownProject extends \InvalidArgumentException
public static function forName(string $name): self
{
return new self(sprintf(
'Could not find Project with name "%s".',
'Could not find project with name "%s".',
$name
));
}
Expand Down
7 changes: 4 additions & 3 deletions src/Controller/NextReleaseForProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,18 @@ public function __construct(Projects $projects, DetermineNextRelease $determineN
}

/**
* @Route("/next-release/{projectName}", name="next_release_project")
* @Route("/next-release/{projectName}/{branchName}", name="next_release_project")
*/
public function __invoke(string $projectName): Response
public function __invoke(string $projectName, string $branchName): Response
{
try {
$project = $this->projects->byName($projectName);
$branch = $project->branch($branchName);
} catch (UnknownProject $e) {
throw new NotFoundHttpException($e->getMessage());
}

$release = $this->determineNextRelease->__invoke($project);
$release = $this->determineNextRelease->__invoke($project, $branch);

$content = $this->twig->render(
'releases/project.html.twig',
Expand Down
18 changes: 12 additions & 6 deletions src/Controller/NextReleaseOverviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,19 @@ public function __construct(Projects $projects, DetermineNextRelease $determineN
public function __invoke(): Response
{
$releases = array_reduce($this->projects->all(), function (array $releases, Project $project): array {
try {
$release = $this->determineNextRelease->__invoke($project);
} catch (CannotDetermineNextRelease | NoPullRequestsMergedSinceLastRelease $e) {
return $releases;
}
foreach ($project->branches() as $branch) {
if ('master' === $branch->name() && $project->isStable()) {
continue;
}

try {
$release = $this->determineNextRelease->__invoke($project, $branch);
} catch (CannotDetermineNextRelease | NoPullRequestsMergedSinceLastRelease $e) {
continue;
}

$releases[] = $release;
$releases[] = $release;
}

return $releases;
}, []);
Expand Down
14 changes: 14 additions & 0 deletions src/Domain/Value/NextRelease.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
final class NextRelease
{
private Project $project;
private Branch $branch;
private Tag $currentTag;
private CombinedStatus $combinedStatus;
private CheckRuns $checkRuns;
Expand All @@ -38,12 +39,14 @@ final class NextRelease

private function __construct(
Project $project,
Branch $branch,
Tag $currentTag,
CombinedStatus $combinedStatus,
CheckRuns $checkRuns,
array $pullRequests
) {
$this->project = $project;
$this->branch = $branch;
$this->currentTag = $currentTag;

$this->combinedStatus = $combinedStatus;
Expand All @@ -69,13 +72,15 @@ private function __construct(
*/
public static function fromValues(
Project $project,
Branch $branch,
Tag $currentTag,
CombinedStatus $combinedStatus,
CheckRuns $checkRuns,
array $pullRequests
): self {
return new self(
$project,
$branch,
$currentTag,
$combinedStatus,
$checkRuns,
Expand All @@ -88,6 +93,11 @@ public function project(): Project
return $this->project;
}

public function branch(): Branch
{
return $this->branch;
}

public function currentTag(): Tag
{
return $this->currentTag;
Expand Down Expand Up @@ -185,6 +195,10 @@ public function stability(): Stability
return $pr->stability()->toString();
}, $this->pullRequests);

if (\in_array(Stability::major()->toString(), $stabilities, true)) {
return Stability::major();
}

if (\in_array(Stability::minor()->toString(), $stabilities, true)) {
return Stability::minor();
}
Expand Down
17 changes: 17 additions & 0 deletions src/Domain/Value/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace App\Domain\Value;

use App\Config\Exception\UnknownBranch;
use App\Domain\Exception\NoBranchesAvailable;
use Packagist\Api\Result\Package;
use function Symfony\Component\String\u;
Expand Down Expand Up @@ -133,6 +134,17 @@ public function package(): Package
return $this->package;
}

public function branch(string $name): Branch
{
foreach ($this->branches as $branch) {
if ($branch->name() === $name) {
return $branch;
}
}

throw UnknownBranch::forName($this, $name);
}

/**
* @return Branch[]
*/
Expand Down Expand Up @@ -305,6 +317,11 @@ public function stableBranch(): ?Branch
return $this->branches[1] ?? null;
}

public function isStable(): bool
{
return null !== $this->stableBranch();
}

private function getLatestPackagistVersion(): Package\Version
{
$versions = $this->package->getVersions();
Expand Down
Loading