-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VACMS-13847: Create ContentReleaseStrategy plugin system. (#14160)
* VACMS-13847: Create ContentReleaseStrategy plugin system. * Adds strategy plugin system. * Expands tests, fixes issues from copypasta mistakes. * GitHubRepositoryDispatch stuff, tests, etc. * Adds LocalFilesystemBuildFile strategy plugin and tests. * Adds stuff to support watchdog_exception() call.
- Loading branch information
Showing
32 changed files
with
1,459 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
docroot/modules/custom/va_gov_consumers/src/Exception/GitHubRepositoryDispatchException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Drupal\va_gov_consumers\Exception; | ||
|
||
/** | ||
* Exception thrown when GitHub repository dispatch fails. | ||
*/ | ||
class GitHubRepositoryDispatchException extends \Exception {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
docroot/modules/custom/va_gov_content_release/src/Annotation/ContentReleaseStrategy.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Drupal\va_gov_content_release\Annotation; | ||
|
||
use Drupal\Component\Annotation\Plugin; | ||
|
||
/** | ||
* Defines the annotation object for content release strategy plugins. | ||
* | ||
* @see plugin_api | ||
* @see \Drupal\va_gov_content_release\Strategy\Plugin\StrategyPluginInterface | ||
* @see \Drupal\va_gov_content_release\Strategy\Plugin\StrategyPluginManager | ||
* | ||
* @Annotation | ||
*/ | ||
class ContentReleaseStrategy extends Plugin { | ||
|
||
/** | ||
* The plugin ID. | ||
* | ||
* @var string | ||
*/ | ||
public $id; | ||
|
||
/** | ||
* The human-readable name of the environemnt. | ||
* | ||
* @var \Drupal\Core\Annotation\Translation | ||
*/ | ||
public $label; | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
...modules/custom/va_gov_content_release/src/Exception/ContentReleaseInProgressException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Drupal\va_gov_content_release\Exception; | ||
|
||
/** | ||
* Exception thrown when a content release is already in progress. | ||
*/ | ||
class ContentReleaseInProgressException extends \Exception {} |
8 changes: 8 additions & 0 deletions
8
...modules/custom/va_gov_content_release/src/Exception/GitHubRepositoryDispatchException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Drupal\va_gov_content_release\Exception; | ||
|
||
/** | ||
* Exception thrown when GitHub repository dispatch fails. | ||
*/ | ||
class GitHubRepositoryDispatchException extends \Exception {} |
8 changes: 8 additions & 0 deletions
8
docroot/modules/custom/va_gov_content_release/src/Exception/StrategyErrorException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Drupal\va_gov_content_release\Exception; | ||
|
||
/** | ||
* Exception thrown when the strategy encounters an error and cannot continue. | ||
*/ | ||
class StrategyErrorException extends \Exception {} |
8 changes: 8 additions & 0 deletions
8
docroot/modules/custom/va_gov_content_release/src/Exception/UnknownStrategyException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Drupal\va_gov_content_release\Exception; | ||
|
||
/** | ||
* Exception thrown when the strategy cannot be found. | ||
*/ | ||
class UnknownStrategyException extends \Exception {} |
88 changes: 88 additions & 0 deletions
88
docroot/modules/custom/va_gov_content_release/src/GitHub/GitHubRepositoryDispatch.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace Drupal\va_gov_content_release\GitHub; | ||
|
||
use Drupal\va_gov_consumers\GitHub\GitHubClientInterface; | ||
use Drupal\va_gov_content_release\Exception\ContentReleaseInProgressException; | ||
use Drupal\va_gov_content_release\Exception\GitHubRepositoryDispatchException; | ||
|
||
/** | ||
* The GitHub repository dispatch service. | ||
* | ||
* This service is used to dispatch repository dispatch events to GitHub, to | ||
* check whether a current workflow is pending, and to make these operations | ||
* testable. | ||
* | ||
* @see \Drupal\va_gov_content_release\GitHub\GitHubRepositoryDispatchInterface | ||
*/ | ||
class GitHubRepositoryDispatch implements GitHubRepositoryDispatchInterface { | ||
|
||
/** | ||
* The GitHub client. | ||
* | ||
* @var \Drupal\va_gov_consumers\GitHub\GitHubClientInterface | ||
*/ | ||
protected $client; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param \Drupal\va_gov_consumers\GitHub\GitHubClientInterface $client | ||
* The GitHub client. | ||
*/ | ||
public function __construct(GitHubClientInterface $client) { | ||
$this->client = $client; | ||
} | ||
|
||
/** | ||
* Build parameters for determining whether a workflow is pending. | ||
* | ||
* @param int|null $time | ||
* The time to use for the 'created' parameter. Defaults to the current | ||
* time. | ||
* | ||
* @return array | ||
* The parameters. | ||
*/ | ||
public function buildPendingWorkflowParams(int $time = NULL) : array { | ||
$time = $time ?? time(); | ||
$sinceTimestamp = $time - (2 * 60 * 60); | ||
return [ | ||
'status' => 'pending', | ||
'created' => '>=' . date('c', $sinceTimestamp), | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function dispatch() : void { | ||
try { | ||
if ($this->isPending()) { | ||
throw new ContentReleaseInProgressException('A workflow is already pending.'); | ||
} | ||
$this->client->repositoryDispatchWorkflow(static::EVENT_TYPE); | ||
} | ||
catch (ContentReleaseInProgressException $exception) { | ||
throw $exception; | ||
} | ||
catch (\Throwable $throwable) { | ||
throw new GitHubRepositoryDispatchException('Repository dispatch failed.', $throwable->getCode(), $throwable); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function isPending() : bool { | ||
try { | ||
$parameters = $this->buildPendingWorkflowParams(); | ||
$workflowRuns = $this->client->listWorkflowRuns(static::EVENT_TYPE . '.yml', $parameters); | ||
return !empty($workflowRuns['total_count']) && $workflowRuns['total_count'] > 0; | ||
} | ||
catch (\Throwable $throwable) { | ||
throw new GitHubRepositoryDispatchException('Failed to get workflow runs.', $throwable->getCode(), $throwable); | ||
} | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
...ot/modules/custom/va_gov_content_release/src/GitHub/GitHubRepositoryDispatchInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Drupal\va_gov_content_release\GitHub; | ||
|
||
/** | ||
* An interface for the GitHub repository dispatch service. | ||
* | ||
* This service is used to dispatch repository dispatch events to GitHub, to | ||
* check whether a current workflow is pending, and to make these operations | ||
* testable. | ||
* | ||
* @see \Drupal\va_gov_content_release\GitHub\GitHubRepositoryDispatch | ||
*/ | ||
interface GitHubRepositoryDispatchInterface { | ||
|
||
// The event type for content release. | ||
const EVENT_TYPE = 'content-release'; | ||
|
||
/** | ||
* Dispatch a repository dispatch event to trigger content release. | ||
* | ||
* @throws \Drupal\va_gov_content_release\Exception\GitHubRepositoryDispatchException | ||
* If the repository dispatch fails. | ||
*/ | ||
public function dispatch() : void; | ||
|
||
/** | ||
* Check whether a workflow is pending. | ||
* | ||
* @return bool | ||
* TRUE if a workflow is pending, FALSE otherwise. | ||
* | ||
* @throws \Drupal\va_gov_content_release\Exception\GitHubRepositoryDispatchException | ||
* If the request fails. | ||
*/ | ||
public function isPending() : bool; | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
docroot/modules/custom/va_gov_content_release/src/Plugin/Strategy/ExceptionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Drupal\va_gov_content_release\Plugin\Strategy; | ||
|
||
use Drupal\va_gov_content_release\Exception\StrategyErrorException; | ||
use Drupal\va_gov_content_release\Strategy\Plugin\StrategyPluginBase; | ||
|
||
/** | ||
* Exception test strategy. | ||
* | ||
* This always throws an exception. | ||
* | ||
* @ContentReleaseStrategy( | ||
* id = "test_exception", | ||
* label = @Translation("Exception Test") | ||
* ) | ||
*/ | ||
class ExceptionTest extends StrategyPluginBase { | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function triggerContentRelease() : void { | ||
throw new StrategyErrorException('This is a test exception.'); | ||
} | ||
|
||
} |
Oops, something went wrong.