Skip to content

Commit

Permalink
Prevent release on wrong branch (#43)
Browse files Browse the repository at this point in the history
* When executing the release, if the current branch is not the same as the default branch, user confirmation is required before proceeding
  • Loading branch information
kayw-geek authored Aug 14, 2023
1 parent 05c8158 commit 4a49ff4
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 @@ -22,6 +22,17 @@ 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 4a49ff4

Please sign in to comment.