You are here

class CircleCiFrontendEnvironment in Build Hooks 8.2

Same name and namespace in other branches
  1. 3.x modules/build_hooks_circleci/src/Plugin/FrontendEnvironment/CircleCiFrontendEnvironment.php \Drupal\build_hooks_circleci\Plugin\FrontendEnvironment\CircleCiFrontendEnvironment

Provides a 'CircleCI' frontend environment type.

Plugin annotation


@FrontendEnvironment(
 id = "circleci",
 label = "Circle CI (V1)",
 description = "An environment connected to Circle CI using Version 1 of their API"
)

Hierarchy

Expanded class hierarchy of CircleCiFrontendEnvironment

File

modules/build_hooks_circleci/src/Plugin/FrontendEnvironment/CircleCiFrontendEnvironment.php, line 22

Namespace

Drupal\build_hooks_circleci\Plugin\FrontendEnvironment
View source
class CircleCiFrontendEnvironment extends FrontendEnvironmentBase implements ContainerFactoryPluginInterface {
  use MessengerTrait;

  /**
   * Drupal\build_hooks_circleci\CircleCiManager definition.
   *
   * @var \Drupal\build_hooks_circleci\CircleCiManager
   */
  protected $circleCiManager;

  /**
   * Construct.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param string $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\build_hooks_circleci\CircleCiManager $circleCiManager
   *   The Circle CI Manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, CircleCiManager $circleCiManager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->circleCiManager = $circleCiManager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('build_hooks_circleci.circleci_manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function frontEndEnvironmentForm($form, FormStateInterface $form_state) {
    $form['project'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Project name'),
      '#maxlength' => 255,
      '#default_value' => isset($this->configuration['project']) ? $this->configuration['project'] : '',
      '#description' => $this
        ->t("Circle CI / Github Project name for this environment. Include the organization name."),
      '#required' => TRUE,
    ];
    $form['branch'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Git branch'),
      '#maxlength' => 255,
      '#default_value' => isset($this->configuration['branch']) ? $this->configuration['branch'] : '',
      '#description' => $this
        ->t("Git branch to deploy to for this environment."),
      '#required' => TRUE,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function frontEndEnvironmentSubmit($form, FormStateInterface $form_state) {
    $this->configuration['project'] = $form_state
      ->getValue('project');
    $this->configuration['branch'] = $form_state
      ->getValue('branch');
  }

  /**
   * {@inheritdoc}
   */
  public function getBuildHookDetails() {
    return $this->circleCiManager
      ->getBuildHookDetailsForPluginConfiguration($this
      ->getConfiguration());
  }

  /**
   * {@inheritdoc}
   */
  public function getAdditionalDeployFormElements(FormStateInterface $form_state) {

    // This plugin adds to the deployment form a fieldset displaying the
    // latest deployments:
    $form = [];
    $form['latestCircleCiDeployments'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Recent deployments'),
      '#description' => $this
        ->t('Here you can see the details for the latest deployments for this environment.'),
      '#open' => TRUE,
    ];
    try {
      $form['latestCircleCiDeployments']['table'] = $this
        ->getLastCircleCiDeploymentsTable($this
        ->getConfiguration());
      $form['latestCircleCiDeployments']['refresher'] = [
        '#type' => 'button',
        '#ajax' => [
          'callback' => [
            CircleCiFrontendEnvironment::class,
            'refreshDeploymentTable',
          ],
          'wrapper' => 'ajax-replace-table',
          'effect' => 'fade',
          'progress' => [
            'type' => 'throbber',
            'message' => $this
              ->t('Refreshing deployment status...'),
          ],
        ],
        '#value' => $this
          ->t('Refresh'),
      ];
    } catch (GuzzleException $e) {
      $this
        ->messenger()
        ->addError('Unable to retrieve information about the last deployments for this environment. Check configuration.');
    }
    return $form;
  }

  /**
   * Gets info about the latest circle ci deployments for this environment.
   *
   * @param array $settings
   *   The plugin settings array.
   *
   * @return array
   *   Renderable array.
   *
   * @throws \GuzzleHttp\Exception\GuzzleException
   */
  private function getLastCircleCiDeploymentsTable(array $settings) {
    $circleCiData = $this->circleCiManager
      ->retrieveLatestBuildsFromCircleciForEnvironment($settings, 8);
    $element = [
      '#type' => 'table',
      '#attributes' => [
        'id' => 'ajax-replace-table',
      ],
      '#header' => [
        $this
          ->t('Status'),
        $this
          ->t('Started at'),
        $this
          ->t('Finished at'),
      ],
    ];
    if (!empty($circleCiData)) {
      foreach ($circleCiData as $circleCiDeployment) {

        // TODO : HACK: We do not want to show the "validate" jobs:
        if ($circleCiDeployment['build_parameters']['CIRCLE_JOB'] == 'validate') {
          continue;
        }
        $element[$circleCiDeployment['build_num']]['status'] = [
          '#type' => 'item',
          '#markup' => '<strong>' . $circleCiDeployment['status'] . '</strong>',
        ];
        $started_time = $circleCiDeployment['start_time'] ? $this->circleCiManager
          ->formatCircleCiDateTime($circleCiDeployment['start_time']) : '';
        $element[$circleCiDeployment['build_num']]['started_at'] = [
          '#type' => 'item',
          '#markup' => $started_time,
        ];
        $stopped_time = $circleCiDeployment['stop_time'] ? $this->circleCiManager
          ->formatCircleCiDateTime($circleCiDeployment['stop_time']) : '';
        $element[$circleCiDeployment['build_num']]['finished_at'] = [
          '#type' => 'item',
          '#markup' => $stopped_time,
        ];
      }
    }
    return $element;
  }

  /**
   * Ajax form callback to rebuild the latest deployments table.
   *
   * @param array $form
   *   The form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state of the form.
   *
   * @return array
   *   The form array to add back to the form.
   */
  public static function refreshDeploymentTable(array $form, FormStateInterface $form_state) {
    return $form['latestCircleCiDeployments']['table'];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CircleCiFrontendEnvironment::$circleCiManager protected property Drupal\build_hooks_circleci\CircleCiManager definition.
CircleCiFrontendEnvironment::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
CircleCiFrontendEnvironment::frontEndEnvironmentForm public function Overrides FrontendEnvironmentBase::frontEndEnvironmentForm
CircleCiFrontendEnvironment::frontEndEnvironmentSubmit public function Overrides FrontendEnvironmentBase::frontEndEnvironmentSubmit
CircleCiFrontendEnvironment::getAdditionalDeployFormElements public function Allows the plugin to add elements to the deployment form. Overrides FrontendEnvironmentInterface::getAdditionalDeployFormElements
CircleCiFrontendEnvironment::getBuildHookDetails public function Get the info to trigger the hook based on the configuration of the plugin. Overrides FrontendEnvironmentInterface::getBuildHookDetails
CircleCiFrontendEnvironment::getLastCircleCiDeploymentsTable private function Gets info about the latest circle ci deployments for this environment.
CircleCiFrontendEnvironment::refreshDeploymentTable public static function Ajax form callback to rebuild the latest deployments table.
CircleCiFrontendEnvironment::__construct public function Construct. Overrides FrontendEnvironmentBase::__construct
FrontendEnvironmentBase::$transliteration protected property The transliteration service.
FrontendEnvironmentBase::baseConfigurationDefaults protected function Returns generic default configuration for frontend environment plugins.
FrontendEnvironmentBase::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
FrontendEnvironmentBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
FrontendEnvironmentBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration 3
FrontendEnvironmentBase::deploymentWasTriggered public function Determine if the deployment was triggered successfully. Overrides FrontendEnvironmentInterface::deploymentWasTriggered 1
FrontendEnvironmentBase::frontEndEnvironmentFormValidate public function 1
FrontendEnvironmentBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
FrontendEnvironmentBase::getMachineNameSuggestion public function
FrontendEnvironmentBase::label public function
FrontendEnvironmentBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
FrontendEnvironmentBase::setConfigurationValue public function
FrontendEnvironmentBase::setTransliteration public function Sets the transliteration service.
FrontendEnvironmentBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
FrontendEnvironmentBase::transliteration protected function Wraps the transliteration service.
FrontendEnvironmentBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginWithFormsTrait::getFormClass public function
PluginWithFormsTrait::hasFormClass public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.