Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #139 from lartist/reproduce-webhook-behavior-as-cli
Browse files Browse the repository at this point in the history
Reproduce webhook behavior as cli
  • Loading branch information
jolelievre authored Apr 12, 2023
2 parents 98c344e + 3237366 commit b74d8dd
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
141 changes: 141 additions & 0 deletions src/AppBundle/Command/DispatchEventCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

namespace AppBundle\Command;

use AppBundle\Event\GitHubEvent;
use InvalidArgumentException;
use PrestaShop\Github\Event\GithubEventInterface;
use PrestaShop\Github\WebhookHandler;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* Generate report of repository activity to a group a defined users
* and send it by email.
*/
class DispatchEventCommand extends Command
{
private const PATH_ARGUMENT_NAME = 'path';
private const SUCCESS = 0;
private const EVENT_NOT_FOUND = 1;

/**
* @var WebhookHandler
*/
private $webhookHandler;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @var string
*/
private $repositoryOwner;

/**
* @var string
*/
private $repositoryName;

/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;

public function __construct(
WebhookHandler $webhookHandler,
LoggerInterface $logger,
EventDispatcherInterface $eventDispatcher,
string $repositoryOwner,
string $repositoryName
) {
parent::__construct();
$this->webhookHandler = $webhookHandler;
$this->logger = $logger;
$this->repositoryOwner = $repositoryOwner;
$this->repositoryName = $repositoryName;
$this->eventDispatcher = $eventDispatcher;
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('prestonbot:event:dispatch')
->setDescription('Dispatch the given event as json file argument.')
->addArgument(
self::PATH_ARGUMENT_NAME,
InputArgument::REQUIRED,
'The absolute path to the event json file.'
)
;
}

/**
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$path = $input->getArgument(self::PATH_ARGUMENT_NAME);
$io->title('Dispatch the event contained in '.$path);

$eventAsArray = json_decode(file_get_contents($path), true);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new InvalidArgumentException('Invalid JSON body');
}
$event = $this->createGithubEventFromEvent($eventAsArray);

if (null === $event) {
$io->error('[err] event not found.');

return self::EVENT_NOT_FOUND;
}

$eventName = strtolower($event->getName()).'_'.$event->getEvent()->getAction();

$this->logger->info(sprintf('[Event] %s (%s) received',
$event->getName(),
$event->getEvent()->getAction()
));

$this->eventDispatcher->dispatch($eventName, $event);

return self::SUCCESS;
}

private function createGithubEventFromEvent(array $event): ?GitHubEvent
{
$event = $this->webhookHandler->handle($event);
if (null === $event || null === $event->getAction() || !$this->isValid($event)) {
$this->logger->error(
sprintf(
'[Event] %s received from `%s` repository',
$event::name(),
$event->getRepository()->getFullName()
)
);

return null;
}

return new GitHubEvent($event::name(), $event);
}

private function isValid(GithubEventInterface $event): bool
{
[$repositoryUsername, $repositoryName] = explode('/', $event->getRepository()->getFullName());

return $repositoryUsername === $this->repositoryOwner && $repositoryName === $this->repositoryName;
}
}
1 change: 1 addition & 0 deletions src/AppBundle/PullRequests/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ public function checkForNewTranslations(PullRequest $pullRequest): bool
}
}
}
$this->logger->info('Find '.\count($newStrings).' new wording(s).');

if (!empty($newStrings)) {
$template = 'markdown/wordings.md.twig';
Expand Down

0 comments on commit b74d8dd

Please sign in to comment.