Skip to content

Commit

Permalink
When executing the release, if the current branch is not the same as …
Browse files Browse the repository at this point in the history
…the default branch, user confirmation is required before proceeding
  • Loading branch information
kayw-geek committed Aug 14, 2023
1 parent 720af47 commit 9eb619b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/Release/Command/ReleaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->releaseWorkerReporter->printMetadata($releaseWorker);

if (! $isDryRun) {
if (method_exists($releaseWorker,'shouldConfirm')
&& $releaseWorker->shouldConfirm()['whenTrue']()
&& ! $this->symfonyStyle->confirm($releaseWorker->shouldConfirm()['message'])
){
return self::FAILURE;
}

$releaseWorker->work($version);
}
}
Expand Down
25 changes: 25 additions & 0 deletions packages/Release/ReleaseWorker/TagVersionReleaseWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Symplify\MonorepoBuilder\Release\ReleaseWorker;

use PharIo\Version\Version;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symplify\MonorepoBuilder\Release\Contract\ReleaseWorker\ReleaseWorkerInterface;
use Symplify\MonorepoBuilder\Release\Process\ProcessRunner;
use Symplify\MonorepoBuilder\ValueObject\Option;
Expand All @@ -22,6 +23,16 @@ public function __construct(
$this->branchName = $parameterProvider->provideStringParameter(Option::DEFAULT_BRANCH_NAME);
}

/**
* @return array<string,callable(): bool|string>
*/
public function shouldConfirm(): array
{
return [
'whenTrue' => fn(): bool => self::getCurrentBranch() !== self::getDefaultBranch(),
'message'=> sprintf('Do you want to release it on the [ %s ] branch?',self::getCurrentBranch())
];
}
public function work(Version $version): void
{
try {
Expand All @@ -42,4 +53,18 @@ public function getDescription(Version $version): string
{
return sprintf('Add local tag "%s"', $version->getOriginalString());
}

private function getCurrentBranch(): string
{
exec('git rev-parse --abbrev-ref HEAD',$outputs);

return $outputs[0];
}

private function getDefaultBranch(): string
{
exec("git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@'",$outputs);

return $outputs[0];
}
}

0 comments on commit 9eb619b

Please sign in to comment.