|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\va_gov_content_release\GitHub; |
| 4 | + |
| 5 | +use Drupal\va_gov_consumers\GitHub\GitHubClientInterface; |
| 6 | +use Drupal\va_gov_content_release\Exception\ContentReleaseInProgressException; |
| 7 | +use Drupal\va_gov_content_release\Exception\GitHubRepositoryDispatchException; |
| 8 | + |
| 9 | +/** |
| 10 | + * The GitHub repository dispatch service. |
| 11 | + * |
| 12 | + * This service is used to dispatch repository dispatch events to GitHub, to |
| 13 | + * check whether a current workflow is pending, and to make these operations |
| 14 | + * testable. |
| 15 | + * |
| 16 | + * @see \Drupal\va_gov_content_release\GitHub\GitHubRepositoryDispatchInterface |
| 17 | + */ |
| 18 | +class GitHubRepositoryDispatch implements GitHubRepositoryDispatchInterface { |
| 19 | + |
| 20 | + /** |
| 21 | + * The GitHub client. |
| 22 | + * |
| 23 | + * @var \Drupal\va_gov_consumers\GitHub\GitHubClientInterface |
| 24 | + */ |
| 25 | + protected $client; |
| 26 | + |
| 27 | + /** |
| 28 | + * Constructor. |
| 29 | + * |
| 30 | + * @param \Drupal\va_gov_consumers\GitHub\GitHubClientInterface $client |
| 31 | + * The GitHub client. |
| 32 | + */ |
| 33 | + public function __construct(GitHubClientInterface $client) { |
| 34 | + $this->client = $client; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Build parameters for determining whether a workflow is pending. |
| 39 | + * |
| 40 | + * @param int|null $time |
| 41 | + * The time to use for the 'created' parameter. Defaults to the current |
| 42 | + * time. |
| 43 | + * |
| 44 | + * @return array |
| 45 | + * The parameters. |
| 46 | + */ |
| 47 | + public function buildPendingWorkflowParams(int $time = NULL) : array { |
| 48 | + $time = $time ?? time(); |
| 49 | + $sinceTimestamp = $time - (2 * 60 * 60); |
| 50 | + return [ |
| 51 | + 'status' => 'pending', |
| 52 | + 'created' => '>=' . date('c', $sinceTimestamp), |
| 53 | + ]; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * {@inheritDoc} |
| 58 | + */ |
| 59 | + public function dispatch() : void { |
| 60 | + try { |
| 61 | + if ($this->isPending()) { |
| 62 | + throw new ContentReleaseInProgressException('A workflow is already pending.'); |
| 63 | + } |
| 64 | + $this->client->repositoryDispatchWorkflow(static::EVENT_TYPE); |
| 65 | + } |
| 66 | + catch (ContentReleaseInProgressException $exception) { |
| 67 | + throw $exception; |
| 68 | + } |
| 69 | + catch (\Throwable $throwable) { |
| 70 | + throw new GitHubRepositoryDispatchException('Repository dispatch failed.', $throwable->getCode(), $throwable); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * {@inheritDoc} |
| 76 | + */ |
| 77 | + public function isPending() : bool { |
| 78 | + try { |
| 79 | + $parameters = $this->buildPendingWorkflowParams(); |
| 80 | + $workflowRuns = $this->client->listWorkflowRuns(static::EVENT_TYPE . '.yml', $parameters); |
| 81 | + return !empty($workflowRuns['total_count']) && $workflowRuns['total_count'] > 0; |
| 82 | + } |
| 83 | + catch (\Throwable $throwable) { |
| 84 | + throw new GitHubRepositoryDispatchException('Failed to get workflow runs.', $throwable->getCode(), $throwable); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments