src/Subscriber/BitbucketEventSubscriber.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber;
  3. use App\Services\Discord\DiscordMessageService;
  4. use Smalot\Bitbucket\Webhook\Event\EventBase;
  5. use Smalot\Bitbucket\Webhook\Event\RepoCommitStatusCreatedEvent;
  6. use Smalot\Bitbucket\Webhook\Event\RepoCommitStatusUpdatedEvent;
  7. use Smalot\Bitbucket\Webhook\Event\RepoPushEvent;
  8. use Smalot\Bitbucket\Webhook\Model\RepoPushModel;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class BitbucketEventSubscriber implements EventSubscriberInterface
  11. {
  12.     const COMMIT_STATUS_INPROGRESS 'INPROGRESS';
  13.     const COMMIT_STATUS_SUCCESSFUL 'SUCCESSFUL';
  14.     const COMMIT_STATUS_FAILED 'FAILED';
  15.     const COMMIT_TAGGED_SUCCESSFUL 'has new version';
  16.     const COLOR_RED '#ED4245';
  17.     const COLOR_BLUE '#5865F2';
  18.     const COLOR_GREEN '#57F287';
  19.     private DiscordMessageService $discordMessageService;
  20.     public function __construct(DiscordMessageService $discordMessageService)
  21.     {
  22.         $this->discordMessageService $discordMessageService;
  23.     }
  24.     public static function getSubscribedEvents()
  25.     {
  26.         // return the subscribed events, their methods and priorities
  27.         return [
  28.             RepoCommitStatusUpdatedEvent::class => 'onCommitStatusChanged',
  29.             RepoCommitStatusCreatedEvent::class => 'onCommitStatusChanged',
  30.             RepoPushEvent::class => 'onRepoPush',
  31.         ];
  32.     }
  33.     public function onRepoPush(EventBase $event): void
  34.     {
  35.         /** @var RepoPushModel $model */
  36.         $model $event->getData();
  37.         $repository $model->getRepository();
  38.         $push $model->getPush();
  39.         if (
  40.             !empty($repository['project']['name']) &&
  41.             !empty($repository['name']) &&
  42.             !empty($push['changes'])
  43.         ) {
  44.             foreach ($push['changes'] as $change) {
  45.                 if (!empty($change['new']['type']) && $change['new']['type'] === 'tag' && !empty($change['new']['name'])) {
  46.                     $this->sendDiscordNotification(
  47.                         $repository['project']['name'],
  48.                         $repository['name'],
  49.                         null,
  50.                         self::COMMIT_TAGGED_SUCCESSFUL,
  51.                         $change['new']['name']
  52.                     );
  53.                 }
  54.             }
  55.         }
  56.     }
  57.     public function onCommitStatusChanged(EventBase $event): void
  58.     {
  59.         $repository $event->getData()->getRepository();
  60.         $commitStatus $event->getData()->getCommitStatus();
  61.         if (
  62.             !empty($repository['project']['name']) &&
  63.             !empty($repository['name']) &&
  64.             !empty($commitStatus['refname']) &&
  65.             !empty($commitStatus['name']) &&
  66.             !empty($commitStatus['state'])
  67.         ) {
  68.             $this->sendDiscordNotification(
  69.                 $repository['project']['name'],
  70.                 $repository['name'],
  71.                 $commitStatus['refname'],
  72.                 $commitStatus['name'],
  73.                 $commitStatus['state']
  74.             );
  75.         }
  76.     }
  77.     private function isAcceptableBranch(string $branch): bool
  78.     {
  79.         return $branch === "master" || preg_match('/^v\d+\.x$/',$branch);
  80.     }
  81.     private function sendDiscordNotification(string $projectstring $repository, ?string $branchstring $statusstring $state): void
  82.     {
  83.         if ( $branch === null || $this->isAcceptableBranch($branch) ) {
  84.             $message ':bulb: ';
  85.             if ( $state === self::COMMIT_STATUS_SUCCESSFUL ) {
  86.                 $message .= ':white_check_mark: ';
  87.             }
  88.             elseif ( $state === self::COMMIT_STATUS_FAILED ) {
  89.                 $message .= ':no_entry_sign: ';
  90.             }
  91.             else {
  92.                 $message .= ':information_source: ';
  93.             }
  94.             $message .= '**' $project ' / ' $repository ':** '.$status.' `'.$state.'`';
  95.             $this->discordMessageService->send($message);
  96.         }
  97.     }
  98. }