Skip to content

Commit

Permalink
Create command to fake a new release
Browse files Browse the repository at this point in the history
  • Loading branch information
styxit committed Dec 11, 2017
1 parent af09c60 commit 701f1d4
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
119 changes: 119 additions & 0 deletions src/Commands/PublishReleaseCommand.php
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;
}
}
13 changes: 13 additions & 0 deletions templates/release-published.json
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"
}
}

0 comments on commit 701f1d4

Please sign in to comment.