Skip to content

Commit

Permalink
Merge pull request #1 from styxit/publish-release
Browse files Browse the repository at this point in the history
Add support to spoof releases
  • Loading branch information
styxit authored Dec 12, 2017
2 parents 5713104 + 6450f74 commit c6f5d0a
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 10 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@ Tool to fake GitHub wehbook events.
- Adjust settings in `.env`

## Usage
Use `php spoof --help` to get more info about the commands.

### Pull request merge
To spoof a pull request merge event, use:
```
php spoof styxit/imposter myBranch develop
php spoof merge styxit/imposter myBranch develop
```

This will fake an event as if branch "myBranch" was merged into branch "develop", for repository "styxit/imposter".

Use `php spoof --help` to get more info about the command.
### Release published
To spoof a "release published" event, use:
```
php spoof release styxit/imposter v1.0.2
```

This will fake an event as if tag "v1.0.2" has been published for repository "styxit/imposter".

## Config
In the `.env` the following options can be specified:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "styxit/webhook-spoof",
"name": "styxit/imposter",
"description": "Fake GitHub webhooks for debugging and development.",
"authors": [
{
Expand Down
13 changes: 8 additions & 5 deletions spoof
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ require __DIR__.'/vendor/autoload.php';

use josegonzalez\Dotenv\Loader;
use Spoof\Commands\PrMergeCommand;
use Spoof\Commands\PublishReleaseCommand;
use Symfony\Component\Console\Application;

$envLoader = (new Loader('.env'))
->parse()
->expect('DESTINATION_URL', 'GITHUB_SECRET')
->toEnv(true);

(new Application('spoof', '0.0.1'))
->add(new PrMergeCommand())
->getApplication()
->setDefaultCommand('PrMerge', true)
->run();

$application = new Application('spoof', '0.0.1');

$application->add(new PrMergeCommand());
$application->add(new PublishReleaseCommand());

$application->run();
4 changes: 2 additions & 2 deletions src/Commands/PrMergeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PrMergeCommand extends Command
*/
protected function configure()
{
$this->setName('PrMerge')
$this->setName('merge')
->setDescription('Fake a pull request merge event from $from into $target.')
->addArgument(
'repository',
Expand Down Expand Up @@ -115,7 +115,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
private function confirm(InputInterface $input, OutputInterface $output, $question = 'Ok?')
{
$helper = $this->getHelper('question');
$confirmQuestion = new ConfirmationQuestion($question.' [y/n]: ', false);
$confirmQuestion = new ConfirmationQuestion($question.' [y/N]: ', false);

if (!$helper->ask($input, $output, $confirmQuestion)) {
return false;
Expand Down
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 c6f5d0a

Please sign in to comment.