-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create command to fake a new release
- Loading branch information
Showing
2 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
|
||
namespace Spoof\Commands; | ||
|
||
use GuzzleHttp\Client; | ||
use Spoof\Template\Parser; | ||
use Spoof\Tools\Signer; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Question\ConfirmationQuestion; | ||
|
||
class PublishReleaseCommand extends Command | ||
{ | ||
/** | ||
* Configure this command. | ||
*/ | ||
protected function configure() | ||
{ | ||
$this->setName('release') | ||
->setDescription('Fake the release-published event for $tag.') | ||
->addArgument( | ||
'repository', | ||
InputArgument::REQUIRED, | ||
'Full repository name, including the owner. Example: styxit/deployments' | ||
) | ||
->addArgument( | ||
'tag', | ||
InputArgument::REQUIRED, | ||
'The tag to fake. Example: v1.0.2' | ||
); | ||
} | ||
|
||
/** | ||
* Execute the command. | ||
* | ||
* @param InputInterface $input | ||
* @param OutputInterface $output | ||
* | ||
* @return int The exit code. | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$output->writeLn(''); | ||
|
||
list($repositoryOwner, $repositoryName) = explode('/', $input->getArgument('repository')); | ||
|
||
$output->writeLn('<info>About to spoof a "release published" event with the following settings:</info>'); | ||
$output->writeLn(''); | ||
$output->writeLn(sprintf('Repository: <comment>%s/%s</comment>', $repositoryOwner, $repositoryName)); | ||
$output->writeln( | ||
sprintf( | ||
'Release tag: <comment>%s</comment>.', | ||
$input->getArgument('tag') | ||
) | ||
); | ||
$output->writeLn(''); | ||
|
||
// Ask confirmation. | ||
if (!$this->confirm($input, $output, 'Is this correct?')) { | ||
$output->writeLn('User did not confirm. Quit.'); | ||
|
||
return; | ||
} | ||
|
||
// Construct payload from template. | ||
$payload = (new Parser('release-published'))->parse( | ||
[ | ||
'repoName' => $repositoryName, | ||
'repoOwner' => $repositoryOwner, | ||
'repoFullName' => $repositoryOwner.'/'.$repositoryName, | ||
'tagName' => $input->getArgument('tag'), | ||
] | ||
); | ||
|
||
// Get the payload signature. | ||
$signature = (new Signer())->sign($payload); | ||
|
||
$output->writeLn('Spoofing the event...'); | ||
|
||
// Construct and execute the request. | ||
$client = new Client(); | ||
$client->post( | ||
$_ENV['DESTINATION_URL'], | ||
[ | ||
'body' => $payload, | ||
'headers' => [ | ||
'Content-Type' => 'application/json', | ||
'X-Hub-Signature' => 'sha1='.$signature, | ||
'X-GitHub-Event' => 'release', | ||
] | ||
] | ||
); | ||
|
||
$output->writeLn('Done.'); | ||
} | ||
|
||
/** | ||
* Ask a question the user must answer with 'y' or 'n'. | ||
* | ||
* @param InputInterface $input | ||
* @param OutputInterface $output | ||
* @param string $question The question to ask. | ||
* | ||
* @return bool True when the user entered 'y', False otherwise. | ||
*/ | ||
private function confirm(InputInterface $input, OutputInterface $output, $question = 'Ok?') | ||
{ | ||
$helper = $this->getHelper('question'); | ||
$confirmQuestion = new ConfirmationQuestion($question.' [y/n]: ', false); | ||
|
||
if (!$helper->ask($input, $output, $confirmQuestion)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"action": "published", | ||
"repository": { | ||
"name": "%s", | ||
"owner": { | ||
"login": "%s" | ||
}, | ||
"ssh_url": "http://github.com/%s" | ||
}, | ||
"release": { | ||
"tag_name": "%s" | ||
} | ||
} |