From 97de240b3cfd8426ed1e4e450080f0b287ec3c0c Mon Sep 17 00:00:00 2001 From: Nathan Douglas Date: Tue, 3 Oct 2023 15:31:41 -0400 Subject: [PATCH] A lot of stuff. --- .../css/build_trigger_form.css | 42 ---- .../js/content_release_status_block.es6.js | 23 --- .../js/content_release_status_block.js | 22 --- .../src/Commands/ContentReleaseCommands.php | 25 --- .../FrontEndBranchAutocompleteController.php | 174 ---------------- .../va_gov_build_trigger.routing.yml | 8 - .../src}/Block/ContentReleaseStatusBlock.php | 9 +- .../src/Commands/FrontendVersionCommands.php | 27 ++- .../FrontendVersionAutocompleteController.php | 80 ++++++++ .../Strategy/Plugin/StrategyPluginBase.php | 2 +- .../src/Form/GitForm.php | 124 +++++++++--- .../src/Frontend/Frontend.php | 55 ++++++ .../src/Frontend/FrontendInterface.php | 42 ++++ .../src/FrontendVersion/FrontendVersion.php | 37 ++-- .../FrontendVersionInterface.php | 18 +- .../FrontendVersionSearch.php | 187 ++++++++++++++++++ .../FrontendVersionSearchInterface.php | 56 ++++++ .../Strategy/Plugin/StrategyPluginBase.php | 2 +- .../src/Strategy/Resolver/Resolver.php | 2 +- .../va_gov_content_release.routing.yml | 8 + .../va_gov_content_release.services.yml | 8 + docroot/sites/default/settings.php | 2 +- .../sites/default/settings/settings.local.php | 3 +- scripts/remove-git-dirs.sh | 1 + 24 files changed, 601 insertions(+), 356 deletions(-) delete mode 100644 docroot/modules/custom/va_gov_build_trigger/css/build_trigger_form.css delete mode 100644 docroot/modules/custom/va_gov_build_trigger/js/content_release_status_block.es6.js delete mode 100644 docroot/modules/custom/va_gov_build_trigger/js/content_release_status_block.js delete mode 100644 docroot/modules/custom/va_gov_build_trigger/src/Controller/FrontEndBranchAutocompleteController.php rename docroot/modules/custom/{va_gov_build_trigger/src/Plugin => va_gov_content_release/src}/Block/ContentReleaseStatusBlock.php (95%) create mode 100644 docroot/modules/custom/va_gov_content_release/src/Controller/FrontendVersionAutocompleteController.php create mode 100644 docroot/modules/custom/va_gov_content_release/src/Frontend/Frontend.php create mode 100644 docroot/modules/custom/va_gov_content_release/src/Frontend/FrontendInterface.php create mode 100644 docroot/modules/custom/va_gov_content_release/src/FrontendVersionSearch/FrontendVersionSearch.php create mode 100644 docroot/modules/custom/va_gov_content_release/src/FrontendVersionSearch/FrontendVersionSearchInterface.php diff --git a/docroot/modules/custom/va_gov_build_trigger/css/build_trigger_form.css b/docroot/modules/custom/va_gov_build_trigger/css/build_trigger_form.css deleted file mode 100644 index 5f608931b5..0000000000 --- a/docroot/modules/custom/va_gov_build_trigger/css/build_trigger_form.css +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Content release form styling. - */ - -form.va-gov-build-trigger-build-trigger-form { - margin-top: 2em; -} - -form h2 { - margin-top: 2em; -} - -.form-actions .form-item-confirm { - margin-top: 2em; -} - -.form-item.form-item-details { - margin-top: 2em; -} - -fieldset.form-item { - margin-top: 1em; -} - -#edit-content-release-status-block--2 { - margin-top: 1.5em; -} - -#edit-content-release-status-block--2 [colspan]:not([colspan='1']) { - font-style: italic; - padding: 1.5em; - text-align: center; -} - -.form-item-cta.button--primary a { - color: #fff; -} - -/* ensures button to release content is on its own line */ -.va-gov-build-trigger-build-trigger-form .form-actions { - flex-direction: column; -} diff --git a/docroot/modules/custom/va_gov_build_trigger/js/content_release_status_block.es6.js b/docroot/modules/custom/va_gov_build_trigger/js/content_release_status_block.es6.js deleted file mode 100644 index 794bd67d1d..0000000000 --- a/docroot/modules/custom/va_gov_build_trigger/js/content_release_status_block.es6.js +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file - */ - -(($, Drupal, once) => { - function refreshStatusBlock(url) { - $.get(url, (data) => { - $(".content-release-status-block").html(data); - }); - } - - Drupal.behaviors.contentReleaseStatusBlock = { - attach: (context, settings) => { - $(once("contentReleaseStatusBlock", "body", context)).on("load", () => { - window.setInterval(() => { - refreshStatusBlock( - settings.contentReleaseStatusBlock.blockRefreshPath - ); - }, 10000); - }); - }, - }; -})(jQuery, window.Drupal, window.once); diff --git a/docroot/modules/custom/va_gov_build_trigger/js/content_release_status_block.js b/docroot/modules/custom/va_gov_build_trigger/js/content_release_status_block.js deleted file mode 100644 index 799c8f8841..0000000000 --- a/docroot/modules/custom/va_gov_build_trigger/js/content_release_status_block.js +++ /dev/null @@ -1,22 +0,0 @@ -/** -* DO NOT EDIT THIS FILE. -* See the following change record for more information, -* https://www.drupal.org/node/2815083 -* @preserve -**/ -(function ($, Drupal, once) { - function refreshStatusBlock(url) { - $.get(url, function (data) { - $(".content-release-status-block").html(data); - }); - } - Drupal.behaviors.contentReleaseStatusBlock = { - attach: function attach(context, settings) { - $(once("contentReleaseStatusBlock", "body", context)).on("load", function () { - window.setInterval(function () { - refreshStatusBlock(settings.contentReleaseStatusBlock.blockRefreshPath); - }, 10000); - }); - } - }; -})(jQuery, window.Drupal, window.once); \ No newline at end of file diff --git a/docroot/modules/custom/va_gov_build_trigger/src/Commands/ContentReleaseCommands.php b/docroot/modules/custom/va_gov_build_trigger/src/Commands/ContentReleaseCommands.php index f7c74cb74b..191e2e1d50 100644 --- a/docroot/modules/custom/va_gov_build_trigger/src/Commands/ContentReleaseCommands.php +++ b/docroot/modules/custom/va_gov_build_trigger/src/Commands/ContentReleaseCommands.php @@ -6,7 +6,6 @@ use Drupal\Core\State\StateInterface; use Drupal\va_gov_build_trigger\Controller\ContentReleaseNotificationController; use Drupal\va_gov_build_trigger\EventSubscriber\ContinuousReleaseSubscriber; -use Drupal\va_gov_build_trigger\Service\BuildRequester; use Drupal\va_gov_build_trigger\Service\BuildRequesterInterface; use Drupal\va_gov_build_trigger\Service\BuildSchedulerInterface; use Drupal\va_gov_build_trigger\Service\ReleaseStateManager; @@ -91,19 +90,6 @@ public function resetState() { $this->logger()->info('Content release state has been reset to \'ready\'.'); } - /** - * Reset the content release frontend version. - * - * @command va-gov:content-release:reset-frontend-version - * @aliases va-gov-content-release-reset-frontend-version - */ - public function resetFrontendVersion() { - $this->state->delete(BuildRequester::VA_GOV_FRONTEND_VERSION); - $this->logger()->info('Content release state has been reset to @state.', [ - '@state' => ReleaseStateManager::STATE_DEFAULT, - ]); - } - /** * Advance the state like an external system would do through HTTP. * @@ -131,17 +117,6 @@ public function advanceState($state) { ]); } - /** - * Get the frontend version that was requested by the user. - * - * @command va-gov:content-release:get-frontend-version - * @aliases va-gov-content-release-get-frontend-version - */ - public function getFrontendVersion() { - $state = $this->state->get(BuildRequester::VA_GOV_FRONTEND_VERSION, '__default'); - $this->io()->write($state); - } - /** * Get the current release state. * diff --git a/docroot/modules/custom/va_gov_build_trigger/src/Controller/FrontEndBranchAutocompleteController.php b/docroot/modules/custom/va_gov_build_trigger/src/Controller/FrontEndBranchAutocompleteController.php deleted file mode 100644 index d909d2e1ba..0000000000 --- a/docroot/modules/custom/va_gov_build_trigger/src/Controller/FrontEndBranchAutocompleteController.php +++ /dev/null @@ -1,174 +0,0 @@ -cbGitHubClient = $cbGitHubClient; - $this->cbBranchSearch = $cbBranchSearch; - $this->logger = $logger->get('va_gov_build_trigger'); - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container) { - return new static( - $container->get('va_gov_git.branch_search.content_build'), - $container->get('va_gov_github.api_client.content_build'), - $container->get('logger.factory') - ); - } - - /** - * Handler for autocomplete request. - * - * @param \Symfony\Component\HttpFoundation\Request $request - * The current request. - * @param int $count - * Number of results to return. - * - * @return \Symfony\Component\HttpFoundation\Response - * The json response object. - */ - public function handleAutocomplete(Request $request, $count) : Response { - $results = []; - - if ($input = $request->query->get('q')) { - $string = mb_strtolower($input); - $results = $this->getMatchingRefs($string, $count); - } - - return JsonResponse::create($results); - } - - /** - * Get branch and PR git references matching the given search string. - * - * @param string $string - * Search string. - * @param int $count - * Number of references to return. - * - * @return string[] - * Array of labeled git references - */ - private function getMatchingRefs(string $string, int $count) : array { - // @todo parallelize with https://github.com/spatie/async? - $results = []; - - $individual_count = (int) ($count / 2); - - $branches = $this->searchFrontEndBranches($string); - for ($i = 0; $i < $individual_count; $i++) { - if (!empty($branches[$i])) { - $results[] = [ - 'label' => "BRANCH {$branches[$i]}", - 'value' => "BRANCH {$branches[$i]} ({$branches[$i]})", - ]; - } - } - - $frontEndPrs = $this->searchFrontEndPrs($string); - for ($i = 0; $i < $individual_count; $i++) { - if (!empty($frontEndPrs['items'][$i])) { - $item = $frontEndPrs['items'][$i]; - $results[] = [ - 'label' => "PR {$item['number']} ({$item['title']})", - 'value' => "PR {$item['number']} - {$item['title']} ({$item['number']})", - ]; - } - } - - return $results; - } - - /** - * Return front end branch names matching the given string. - * - * @param string $string - * Search string. - * - * @return string[] - * Array of branch names. - */ - private function searchFrontEndBranches(string $string) : array { - try { - return $this->cbBranchSearch->getRemoteBranchNamesContaining($string); - } - catch (\Throwable $exception) { - $this->logger->error('Error searching for branches: @message', [ - '@message' => $exception->getMessage(), - ]); - return []; - } - } - - /** - * Search Front End PRs. - * - * @param string $string - * Search string. - * @param int $count - * Number of PRs to return. - * - * @return string[] - * Array of PRs - */ - private function searchFrontEndPrs(string $string, int $count = 20) { - $results = []; - - try { - $results = $this->cbGitHubClient->searchPullRequests($string); - } - catch (\Exception $e) { - $variables = Error::decodeException($e); - $this->logger->error('%type: @message in %function (line %line of %file).', $variables); - } - - return $results; - } - -} diff --git a/docroot/modules/custom/va_gov_build_trigger/va_gov_build_trigger.routing.yml b/docroot/modules/custom/va_gov_build_trigger/va_gov_build_trigger.routing.yml index 015ff3c77a..9f2740ef3c 100644 --- a/docroot/modules/custom/va_gov_build_trigger/va_gov_build_trigger.routing.yml +++ b/docroot/modules/custom/va_gov_build_trigger/va_gov_build_trigger.routing.yml @@ -1,11 +1,3 @@ -va_gov_build_trigger.front_end_branches_autocomplete: - path: '/admin/content/deploy/front_end_branches_autocomplete/{count}' - defaults: - _controller: '\Drupal\va_gov_build_trigger\Controller\FrontEndBranchAutocompleteController::handleAutocomplete' - _format: json - requirements: - _permission: "va gov deploy content build" - va_gov_build_trigger.content_release_status_block_controller_get_block: path: '/admin/content_release_status_block/ajax' defaults: diff --git a/docroot/modules/custom/va_gov_build_trigger/src/Plugin/Block/ContentReleaseStatusBlock.php b/docroot/modules/custom/va_gov_content_release/src/Block/ContentReleaseStatusBlock.php similarity index 95% rename from docroot/modules/custom/va_gov_build_trigger/src/Plugin/Block/ContentReleaseStatusBlock.php rename to docroot/modules/custom/va_gov_content_release/src/Block/ContentReleaseStatusBlock.php index 2986edc27b..c46e8f42c8 100644 --- a/docroot/modules/custom/va_gov_build_trigger/src/Plugin/Block/ContentReleaseStatusBlock.php +++ b/docroot/modules/custom/va_gov_content_release/src/Block/ContentReleaseStatusBlock.php @@ -1,6 +1,6 @@ environmentDiscovery->shouldDisplayBuildDetails()) { $current_frontend_version = $this->state->get(BuildRequester::VA_GOV_FRONTEND_VERSION, '[default]'); - $build_log_link = $items['front_end_version'] = [ 'title' => $this->t('Front end version'), diff --git a/docroot/modules/custom/va_gov_content_release/src/Commands/FrontendVersionCommands.php b/docroot/modules/custom/va_gov_content_release/src/Commands/FrontendVersionCommands.php index 810bed4eca..b2f444e18d 100644 --- a/docroot/modules/custom/va_gov_content_release/src/Commands/FrontendVersionCommands.php +++ b/docroot/modules/custom/va_gov_content_release/src/Commands/FrontendVersionCommands.php @@ -2,6 +2,7 @@ namespace Drupal\va_gov_content_release\Commands; +use Drupal\va_gov_content_release\Frontend\Frontend; use Drupal\va_gov_content_release\FrontendVersion\FrontendVersionInterface; use Drush\Commands\DrushCommands; @@ -32,33 +33,47 @@ public function __construct( /** * Get the frontend version. * + * @param string $frontend + * The frontend whose version we are getting. + * * @command va-gov-content-release:frontend-version:get * @aliases va-gov-content-release-frontend-version-get */ - public function get() { - $value = $this->frontendVersion->get(); + public function get(string $frontend = 'content_build') { + $frontend = Frontend::fromRawValue($frontend); + $value = $this->frontendVersion->getVersion($frontend); $this->io()->write($value); } /** * Reset the frontend version. * + * @param string $frontend + * The frontend whose version we are resetting. + * * @command va-gov-content-release:frontend-version:reset * @aliases va-gov-content-release-frontend-version-reset */ - public function reset() { - $this->frontendVersion->reset(); + public function reset(string $frontend = 'content_build') { + $frontend = Frontend::fromRawValue($frontend); + $this->frontendVersion->resetVersion($frontend); $this->io()->success('Frontend version reset.'); } /** * Set the frontend version. * + * @param string $frontend + * The frontend whose version we are getting. + * @param string $version + * The version to set. + * * @command va-gov-content-release:frontend-version:set * @aliases va-gov-content-release-frontend-version-set */ - public function set($version) { - $this->frontendVersion->set($version); + public function setVersion(string $frontend = 'content_build', $version = '_default') { + $frontend = Frontend::fromRawValue($frontend); + $this->frontendVersion->setVersion($frontend, $version); $this->io()->success('Frontend version set to ' . $version); } diff --git a/docroot/modules/custom/va_gov_content_release/src/Controller/FrontendVersionAutocompleteController.php b/docroot/modules/custom/va_gov_content_release/src/Controller/FrontendVersionAutocompleteController.php new file mode 100644 index 0000000000..a752538719 --- /dev/null +++ b/docroot/modules/custom/va_gov_content_release/src/Controller/FrontendVersionAutocompleteController.php @@ -0,0 +1,80 @@ +frontendVersionSearch = $frontendVersionSearch; + $this->logger = $logger->get('va_gov_content_release'); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('va_gov_content_release.frontend_version_search'), + $container->get('logger.factory') + ); + } + + /** + * Handler for autocomplete request. + * + * @param \Symfony\Component\HttpFoundation\Request $request + * The current request. + * @param string $frontend + * The frontend type. + * @param int $count + * Number of results to return. + * + * @return \Symfony\Component\HttpFoundation\Response + * The json response object. + */ + public function handleAutocomplete(Request $request, string $frontend, int $count) : Response { + $results = []; + try { + $frontend = Frontend::from($frontend); + } + catch (\InvalidArgumentException $e) { + return JsonResponse::create(['error' => 'Invalid frontend type provided.']); + } + $input = $request->query->get('q') ?? 'main'; + $query = mb_strtolower($input); + $results = $this->frontendVersionSearch->getMatchingReferences($frontend, $query, $count); + return JsonResponse::create($results); + } + +} diff --git a/docroot/modules/custom/va_gov_content_release/src/EntityEvent/Strategy/Plugin/StrategyPluginBase.php b/docroot/modules/custom/va_gov_content_release/src/EntityEvent/Strategy/Plugin/StrategyPluginBase.php index aada220038..9a83462350 100644 --- a/docroot/modules/custom/va_gov_content_release/src/EntityEvent/Strategy/Plugin/StrategyPluginBase.php +++ b/docroot/modules/custom/va_gov_content_release/src/EntityEvent/Strategy/Plugin/StrategyPluginBase.php @@ -5,8 +5,8 @@ use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\PluginBase; -use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\va_gov_content_types\Entity\VaNodeInterface; use Symfony\Component\DependencyInjection\ContainerInterface; diff --git a/docroot/modules/custom/va_gov_content_release/src/Form/GitForm.php b/docroot/modules/custom/va_gov_content_release/src/Form/GitForm.php index 220a9ea4d3..8becaddb0c 100644 --- a/docroot/modules/custom/va_gov_content_release/src/Form/GitForm.php +++ b/docroot/modules/custom/va_gov_content_release/src/Form/GitForm.php @@ -4,10 +4,12 @@ use Drupal\Core\Block\BlockManagerInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\va_gov_build_trigger\Service\ReleaseStateManagerInterface; +use Drupal\va_gov_content_release\Frontend\Frontend; +use Drupal\va_gov_content_release\Frontend\FrontendInterface; use Drupal\va_gov_content_release\FrontendVersion\FrontendVersionInterface; -use Drupal\va_gov_content_release\Request\RequestInterface; use Drupal\va_gov_content_release\Reporter\ReporterInterface; -use Drupal\va_gov_build_trigger\Service\ReleaseStateManagerInterface; +use Drupal\va_gov_content_release\Request\RequestInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -100,29 +102,57 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#suffix' => '', ]; - $form['build_request']['selection'] = [ - '#title' => $this->t('Which version of the VA.gov front end would you like to use?'), + $form['build_request']['content_build_selection'] = [ + '#title' => $this->t('Which version of content-build would you like to use?'), + '#type' => 'radios', + '#options' => [ + 'default' => $this->t('Use default - the content-build version from the time this demo environment was created.'), + 'choose' => $this->t('Select a different content-build branch/pull request - for example, to see your content in a newer frontend design.'), + ], + '#default_value' => 'default', + ]; + + $form['build_request']['content_build_git_ref'] = [ + '#type' => 'textfield', + '#title' => $this->t('Select branch/pull request'), + '#description' => $this->t('Start typing to select a branch for the content-build version you want to use.'), + '#autocomplete_route_name' => 'va_gov_content_release.frontend_version_autocomplete', + '#autocomplete_route_parameters' => [ + 'frontend' => 'content_build', + 'count' => 10, + ], + '#size' => 72, + '#maxlength' => 1024, + '#hidden' => TRUE, + '#states' => [ + 'visible' => [':input[name="content_build_selection"]' => ['value' => 'choose']], + ], + ]; + + $form['build_request']['vets_website_selection'] = [ + '#title' => $this->t('Which version of vets-website would you like to use?'), '#type' => 'radios', '#options' => [ - 'default' => $this->t('Use default - the frontend version from the time this demo environment was created.'), - 'choose' => $this->t('Select a different frontend branch/pull request - for example, to see your content in a newer frontend design.'), + 'default' => $this->t('Use default - the vets-website version from the time this demo environment was created.'), + 'choose' => $this->t('Select a different vets-website branch/pull request - for example, to see your content in a newer frontend design.'), ], '#default_value' => 'default', ]; - $form['build_request']['git_ref'] = [ + $form['build_request']['vets_website_git_ref'] = [ '#type' => 'textfield', '#title' => $this->t('Select branch/pull request'), - '#description' => $this->t('Start typing to select a branch for the frontend version you want to use.'), - '#autocomplete_route_name' => 'va_gov_build_trigger.front_end_branches_autocomplete', + '#description' => $this->t('Start typing to select a branch for the vets-website version you want to use.'), + '#autocomplete_route_name' => 'va_gov_content_release.frontend_version_autocomplete', '#autocomplete_route_parameters' => [ + 'frontend' => 'vets_website', 'count' => 10, ], '#size' => 72, '#maxlength' => 1024, '#hidden' => TRUE, '#states' => [ - 'visible' => [':input[name="selection"]' => ['value' => 'choose']], + 'visible' => [':input[name="vets_website_selection"]' => ['value' => 'choose']], ], ]; @@ -152,70 +182,108 @@ protected function getContentReleaseStatusBlock() { * Object containing current form state. */ public function submitForm(array &$form, FormStateInterface $form_state) { - if ($form_state->getValue('selection') === 'default') { - $this->resetFrontendVersion($form_state); + $this->submitFormForFrontend(Frontend::ContentBuild, $form_state); + $this->submitFormForFrontend(Frontend::VetsWebsite, $form_state); + parent::submitForm($form, $form_state); + } + + /** + * Submit the form. + * + * @param \Drupal\va_gov_content_release\Frontend\FrontendInterface $frontend + * The frontend whose version we are managing. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * Object containing current form state. + */ + protected function submitFormForFrontend(FrontendInterface $frontend, FormStateInterface $form_state) { + $selectionName = $frontend->getRawValue() . '_selection'; + if ($form_state->getValue($selectionName) === 'default') { + $this->resetFrontendVersion($frontend, $form_state); } else { - $this->setFrontendVersion($form_state); + $this->setFrontendVersion($frontend, $form_state); } - - parent::submitForm($form, $form_state); } /** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { - if ($form_state->getValue('selection') === 'default') { - return; - } - if (empty($this->getGitRef($form_state))) { - $form_state->setErrorByName('git_ref', $this->t('Invalid selection.')); + $this->validateForm(Frontend::ContentBuild, $form_state); + $this->validateForm(Frontend::VetsWebsite, $form_state); + } + + /** + * Validate the form. + * + * @param \Drupal\va_gov_content_release\Frontend\FrontendInterface $frontend + * The frontend whose version we are managing. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * Object containing current form state. + */ + protected function validateFormForFrontend(FrontendInterface $frontend, FormStateInterface $form_state) { + $selectionName = $frontend->getRawValue() . '_selection'; + $gitRefName = $frontend->getRawValue() . '_git_ref'; + if ($form_state->getValue($selectionName) !== 'default') { + if (empty($this->getGitRef($frontend, $form_state))) { + $form_state->setErrorByName($gitRefName, $this->t('Invalid selection.')); + } } } /** * Reset the frontend version. * + * @param \Drupal\va_gov_content_release\Frontend\FrontendInterface $frontend + * The frontend whose version we are resetting. * @param \Drupal\Core\Form\FormStateInterface $form_state * Object containing current form state. */ - public function resetFrontendVersion(FormStateInterface $form_state) { + public function resetFrontendVersion(FrontendInterface $frontend, FormStateInterface $form_state) { if (!$this->isUnderTest($form_state)) { - $this->frontendVersion->reset(); + $this->frontendVersion->resetVersion($frontend); } else { - $this->reporter->reportInfo($this->t('Reset frontend version skipped; form is under test.')); + $this->reporter->reportInfo($this->t('Reset :frontend version skipped; form is under test.', [ + ':frontend' => $frontend->getRawValue(), + ])); } } /** * Set the frontend version according to the form. * + * @param \Drupal\va_gov_content_release\Frontend\FrontendInterface $frontend + * The frontend whose version we are setting. * @param \Drupal\Core\Form\FormStateInterface $form_state * Object containing current form state. */ - public function setFrontendVersion(FormStateInterface $form_state) { + public function setFrontendVersion(FrontendInterface $frontend, FormStateInterface $form_state) { if (!$this->isUnderTest($form_state)) { - $this->frontendVersion->set($this->getGitRef($form_state)); + $this->frontendVersion->setVersion($frontend, $this->getGitRef($frontend, $form_state)); } else { - $this->reporter->reportInfo($this->t('Set frontend version skipped; form is under test.')); + $this->reporter->reportInfo($this->t('Set :frontend version skipped; form is under test.', [ + ':frontend' => $frontend->getRawValue(), + ])); } } /** * Parse a git ref out of the `git_ref` field value. * + * @param \Drupal\va_gov_content_release\Frontend\FrontendInterface $frontend + * The frontend whose version we are setting. * @param \Drupal\Core\Form\FormStateInterface $form_state * Object containing current form state. * * @return string * A standalone git ref, or an empty string. */ - public function getGitRef(FormStateInterface $form_state) : string { + public function getGitRef(FrontendInterface $frontend, FormStateInterface $form_state) : string { // If they selected a specific git ref, use that. - $formValue = $form_state->getValue('git_ref'); + $gitRefName = $frontend->getRawValue() . '_git_ref'; + $formValue = $form_state->getValue($gitRefName); $result = ''; if (preg_match("/.+\\s\\(([^\\)]+)\\)/", $formValue, $matches)) { $result = $matches[1]; diff --git a/docroot/modules/custom/va_gov_content_release/src/Frontend/Frontend.php b/docroot/modules/custom/va_gov_content_release/src/Frontend/Frontend.php new file mode 100644 index 0000000000..ce935f8188 --- /dev/null +++ b/docroot/modules/custom/va_gov_content_release/src/Frontend/Frontend.php @@ -0,0 +1,55 @@ +value; + } + + /** + * {@inheritDoc} + */ + public function isContentBuild() : bool { + return match ($this) { + self::ContentBuild => TRUE, + default => FALSE, + }; + } + + /** + * {@inheritDoc} + */ + public function isVetsWebsite() : bool { + return match ($this) { + self::VetsWebsite => TRUE, + default => FALSE, + }; + } + + /** + * {@inheritDoc} + */ + public function isNextBuild() : bool { + return match ($this) { + self::NextBuild => TRUE, + default => FALSE, + }; + } + +} diff --git a/docroot/modules/custom/va_gov_content_release/src/Frontend/FrontendInterface.php b/docroot/modules/custom/va_gov_content_release/src/Frontend/FrontendInterface.php new file mode 100644 index 0000000000..bc6a70e31f --- /dev/null +++ b/docroot/modules/custom/va_gov_content_release/src/Frontend/FrontendInterface.php @@ -0,0 +1,42 @@ +state->get(FrontendVersionInterface::FRONTEND_VERSION, FrontendVersionInterface::FRONTEND_VERSION_DEFAULT); + protected function getKey(FrontendInterface $frontend) : string { + return FrontendVersionInterface::FRONTEND_VERSION_PREFIX . $frontend->getRawValue(); } /** - * Set the current version of the frontend. - * - * @param string $version - * The version to set. + * {@inheritDoc} + */ + public function getVersion(FrontendInterface $frontend) : string { + $key = $this->getKey($frontend); + return $this->state->get($key, FrontendVersionInterface::FRONTEND_VERSION_DEFAULT); + } + + /** + * {@inheritDoc} */ - public function set(string $version) : void { - $this->state->set(FrontendVersionInterface::FRONTEND_VERSION, $version); + public function setVersion(FrontendInterface $frontend, string $version) : void { + $key = $this->getKey($frontend); + $this->state->set($key, $version); } /** - * Reset the current version of the frontend. + * {@inheritDoc} */ - public function reset() : void { - $this->state->delete(FrontendVersionInterface::FRONTEND_VERSION); + public function resetVersion(FrontendInterface $frontend) : void { + $key = $this->getKey($frontend); + $this->state->delete($key); } } diff --git a/docroot/modules/custom/va_gov_content_release/src/FrontendVersion/FrontendVersionInterface.php b/docroot/modules/custom/va_gov_content_release/src/FrontendVersion/FrontendVersionInterface.php index 1e8e014e9a..32867c4149 100644 --- a/docroot/modules/custom/va_gov_content_release/src/FrontendVersion/FrontendVersionInterface.php +++ b/docroot/modules/custom/va_gov_content_release/src/FrontendVersion/FrontendVersionInterface.php @@ -2,6 +2,8 @@ namespace Drupal\va_gov_content_release\FrontendVersion; +use Drupal\va_gov_content_release\Frontend\FrontendInterface; + /** * An interface for the FrontendVersion service. * @@ -10,28 +12,36 @@ */ interface FrontendVersionInterface { - const FRONTEND_VERSION = 'va_gov_content_release.frontend_version'; + const FRONTEND_VERSION_PREFIX = 'va_gov_content_release.frontend_version.'; const FRONTEND_VERSION_DEFAULT = '__default'; /** * Get the current version of the frontend. * + * @param \Drupal\va_gov_content_release\Frontend\FrontendInterface $frontend + * The frontend whose version we are requesting. + * * @return string * The current version of the frontend. */ - public function get() : string; + public function getVersion(FrontendInterface $frontend) : string; /** * Set the current version of the frontend. * + * @param \Drupal\va_gov_content_release\Frontend\FrontendInterface $frontend + * The frontend whose version we are setting. * @param string $version * The version to set. */ - public function set(string $version) : void; + public function setVersion(FrontendInterface $frontend, string $version) : void; /** * Reset the current version of the frontend. + * + * @param \Drupal\va_gov_content_release\Frontend\FrontendInterface $frontend + * The frontend whose version we are resetting. */ - public function reset() : void; + public function resetVersion(FrontendInterface $frontend) : void; } diff --git a/docroot/modules/custom/va_gov_content_release/src/FrontendVersionSearch/FrontendVersionSearch.php b/docroot/modules/custom/va_gov_content_release/src/FrontendVersionSearch/FrontendVersionSearch.php new file mode 100644 index 0000000000..d2d6145d39 --- /dev/null +++ b/docroot/modules/custom/va_gov_content_release/src/FrontendVersionSearch/FrontendVersionSearch.php @@ -0,0 +1,187 @@ +cbBranchSearch = $cbBranchSearch; + $this->cbApiClient = $cbApiClient; + $this->vwBranchSearch = $vwBranchSearch; + $this->vwApiClient = $vwApiClient; + $this->logger = $loggerFactory->get('va_gov_content_release'); + } + + /** + * Get the branch search service for the given frontend. + * + * @param \Drupal\va_gov_content_release\Frontend\FrontendInterface $frontend + * The frontend. + * + * @return \Drupal\va_gov_git\BranchSearch\BranchSearchInterface + * The branch search service. + */ + protected function getBranchSearch(FrontendInterface $frontend) : BranchSearchInterface { + switch (TRUE) { + case $frontend->isContentBuild(): + return $this->cbBranchSearch; + + case $frontend->isVetsWebsite(): + return $this->vwBranchSearch; + + default: + throw new \InvalidArgumentException('Invalid frontend: ' . $frontend->getRawValue()); + } + } + + /** + * Get the API client for the given frontend. + * + * @param \Drupal\va_gov_content_release\Frontend\FrontendInterface $frontend + * The frontend. + * + * @return \Drupal\va_gov_github\Api\Client\ApiClientInterface + * The API client. + */ + protected function getApiClient(FrontendInterface $frontend) : ApiClientInterface { + switch (TRUE) { + case $frontend->isContentBuild(): + return $this->cbApiClient; + + case $frontend->isVetsWebsite(): + return $this->vwApiClient; + + default: + throw new \InvalidArgumentException('Invalid frontend: ' . $frontend->getRawValue()); + } + } + + /** + * {@inheritDoc} + */ + public function getMatchingReferences(FrontendInterface $frontend, string $query, int $count) : array { + $results = []; + $branches = $this->getMatchingBranches($frontend, $query); + for ($i = 0; $i < $count; $i++) { + if (!empty($branches[$i])) { + $results[] = [ + 'label' => "BRANCH {$branches[$i]}", + 'value' => "BRANCH {$branches[$i]} ({$branches[$i]})", + ]; + } + } + + $pullRequests = $this->getMatchingPullRequests($frontend, $query); + for ($i = 0; $i < $count; $i++) { + if (!empty($pullRequests['items'][$i])) { + $item = $pullRequests['items'][$i]; + $results[] = [ + 'label' => "PR {$item['number']} ({$item['title']})", + 'value' => "PR {$item['number']} - {$item['title']} ({$item['number']})", + ]; + } + } + + return $results; + } + + /** + * {@inheritDoc} + */ + public function getMatchingBranches(FrontendInterface $frontend, string $query) : array { + try { + return $this->getBranchSearch($frontend)->getRemoteBranchNamesContaining($query); + } + catch (\Throwable $exception) { + $this->logger->error('Error searching for branches: @message', [ + '@message' => $exception->getMessage(), + ]); + return []; + } + } + + /** + * {@inheritDoc} + */ + public function getMatchingPullRequests(FrontendInterface $frontend, string $query) : array { + $results = []; + + try { + $results = $this->getApiClient($frontend)->searchPullRequests($query); + } + catch (\Throwable $exception) { + $this->logger->error('Error searching for pull requests: @message', [ + '@message' => $exception->getMessage(), + ]); + } + + return $results; + } + +} diff --git a/docroot/modules/custom/va_gov_content_release/src/FrontendVersionSearch/FrontendVersionSearchInterface.php b/docroot/modules/custom/va_gov_content_release/src/FrontendVersionSearch/FrontendVersionSearchInterface.php new file mode 100644 index 0000000000..6e9861d673 --- /dev/null +++ b/docroot/modules/custom/va_gov_content_release/src/FrontendVersionSearch/FrontendVersionSearchInterface.php @@ -0,0 +1,56 @@ + 'default', diff --git a/scripts/remove-git-dirs.sh b/scripts/remove-git-dirs.sh index d23b9d0adf..9c9d4ba629 100755 --- a/scripts/remove-git-dirs.sh +++ b/scripts/remove-git-dirs.sh @@ -13,6 +13,7 @@ find . \ -not \ \( \ -path './docroot/vendor/va-gov/content-build/.git' \ + -path './docroot/vendor/va-gov/vets-website/.git' \ \) \ -print \ -exec rm -rf {} +