config_preview_deploy-1.0.0-alpha3/src/Form/DeployForm.php

src/Form/DeployForm.php
<?php

declare(strict_types=1);

namespace Drupal\config_preview_deploy\Form;

use Drupal\config_preview_deploy\ConfigDiff;
use Drupal\config_preview_deploy\ConfigVerifier;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Deployment confirmation form.
 */
class DeployForm extends FormBase {

  /**
   * The configuration diff service.
   */
  protected ConfigDiff $configDiff;

  /**
   * The config verifier service.
   */
  protected ConfigVerifier $configVerifier;

  /**
   * The private tempstore factory.
   */
  protected PrivateTempStoreFactory $privateTempStoreFactory;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $instance = new static();
    $instance->configDiff = $container->get('config_preview_deploy.config_diff');
    $instance->configVerifier = $container->get('config_preview_deploy.config_verifier');
    $instance->privateTempStoreFactory = $container->get('tempstore.private');
    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId(): string {
    return 'config_preview_deploy_deploy_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state): array {
    $form['#title'] = $this->t('Deploy Configuration to Production');

    // Check if OAuth access token is available and valid.
    $tempstore = $this->privateTempStoreFactory->get('config_preview_deploy');
    $access_token = $tempstore->get('access_token');
    $tokenReceivedAt = $tempstore->get('token_received_at');
    // 4 minutes
    $maxAge = 240;

    $has_auth = !empty($access_token) && $tokenReceivedAt && (time() - $tokenReceivedAt) <= $maxAge;

    try {
      $changedConfigs = $this->configDiff->getChangedConfigs(TRUE);

      if (empty($changedConfigs)) {
        $form['no_changes'] = [
          '#markup' => '<div class="messages messages--warning">' .
          $this->t('No configuration changes to deploy.') .
          '</div>',
        ];

        $form['back'] = [
          '#type' => 'link',
          '#title' => $this->t('Back to Dashboard'),
          '#url' => Url::fromRoute('config_preview_deploy.dashboard'),
          '#attributes' => ['class' => ['button']],
        ];

        return $form;
      }

      // If no OAuth token available or expired, show authorization form.
      if (!$has_auth) {
        $tokenExpired = !empty($access_token) && (!$tokenReceivedAt || (time() - $tokenReceivedAt) > $maxAge);
        return $this->buildAuthorizationForm($form, $changedConfigs, $tokenExpired);
      }

      // If we have OAuth token, show the deployment confirmation.
      return $this->buildDeploymentForm($form, $changedConfigs);

    }
    catch (\Exception $e) {
      $form['error'] = [
        '#markup' => '<div class="messages messages--error">' .
        $this->t('Error preparing deployment: @message', ['@message' => $e->getMessage()]) .
        '</div>',
      ];
    }

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {
    // Default submit does nothing - actual work is in verifyAndDeploy().
  }

  /**
   * Deploys configuration to production (includes verification).
   */
  public function verifyAndDeploy(array &$form, FormStateInterface $form_state): void {
    try {
      // Generate diff for deployment.
      $diff = $this->configDiff->generateDiff();

      if (empty($diff)) {
        $this->messenger()->addWarning($this->t('No configuration changes to deploy.'));
        return;
      }

      // Send deployment request to production (includes all verification).
      $result = $this->configVerifier->deployToProduction($diff);

      // Check deployment result and show success message only if successful.
      if (!empty($result['success'])) {
        $message = $this->t('Configuration successfully deployed to production at @time', [
          '@time' => date('Y-m-d H:i:s'),
        ]);

        // Add checkpoint ID if available.
        if (!empty($result['checkpoint_id'])) {
          $message = $this->t('Configuration successfully deployed to production at @time (checkpoint: @id)', [
            '@time' => date('Y-m-d H:i:s'),
            '@id' => $result['checkpoint_id'],
          ]);
        }

        $this->messenger()->addStatus($message);
      }
      else {
        // This shouldn't happen as deployToProduction throws exceptions on
        // failure, but handle it gracefully just in case.
        throw new \Exception(
          $result['error'] ?? $this->t('Deployment failed without specific error message')->render()
        );
      }

      // Re-initialize preview environment after successful deployment.
      try {
        $checkpointId = $this->configDiff->createBaseCheckpoint();
        $this->getLogger('config_preview_deploy')->notice('Automatic re-initialization completed after deployment with checkpoint: @id', [
          '@id' => $checkpointId,
        ]);
      }
      catch (\Exception $e) {
        $this->messenger()->addWarning($this->t('Deployment succeeded but failed to re-initialize preview environment: @message', [
          '@message' => $e->getMessage(),
        ]));
        $this->getLogger('config_preview_deploy')->error('Failed to automatically re-initialize after deployment: @message', [
          '@message' => $e->getMessage(),
        ]);
      }

      // Clear the OAuth token after successful deployment.
      $tempstore = $this->privateTempStoreFactory->get('config_preview_deploy');
      $tempstore->delete('access_token');
      $tempstore->delete('token_received_at');

      // Redirect to dashboard after successful deployment.
      $form_state->setRedirect('config_preview_deploy.dashboard');

    }
    catch (\Exception $e) {
      $this->messenger()->addError($this->t('Deployment failed: @message', ['@message' => $e->getMessage()]));

      // Offer rebase option if the error suggests production has changed.
      if (strpos($e->getMessage(), 'authentication') === FALSE) {
        $this->messenger()->addWarning($this->t('If production has changed since your last sync, you may need to <a href="@url">rebase your environment</a>.', [
          '@url' => Url::fromRoute('config_preview_deploy.rebase_form')->toString(),
        ]));
      }
    }
  }

  /**
   * Builds the setup instructions form.
   */
  protected function buildSetupForm(array $form, array $changedConfigs): array {
    $form['setup_info'] = [
      '#markup' => '<div class="messages messages--warning">' .
      $this->t('Deployment secret not configured. Run the module update to create deployment keys.') .
      '</div>',
    ];

    $form['changes_summary'] = [
      '#type' => 'details',
      '#title' => $this->t('Changes ready to deploy (@count configurations)', ['@count' => count($changedConfigs)]),
      '#open' => FALSE,
    ];

    if (count($changedConfigs) <= 20) {
      $form['changes_summary']['list'] = [
        '#theme' => 'item_list',
        '#items' => $changedConfigs,
        '#attributes' => ['class' => ['config-preview-deploy-changes-list']],
      ];
    }
    else {
      $form['changes_summary']['summary'] = [
        '#markup' => '<p>' . $this->t('Too many changes to list (@count configurations).', ['@count' => count($changedConfigs)]) . '</p>',
      ];
    }

    $form['actions'] = [
      '#type' => 'actions',
    ];

    $form['actions']['back'] = [
      '#type' => 'link',
      '#title' => $this->t('Back to Dashboard'),
      '#url' => Url::fromRoute('config_preview_deploy.dashboard'),
      '#attributes' => ['class' => ['button']],
    ];

    return $form;
  }

  /**
   * Builds the OAuth authorization form.
   */
  protected function buildAuthorizationForm(array $form, array $changedConfigs, bool $tokenExpired = FALSE): array {
    $message = $tokenExpired
      ? $this->t('Your authorization has expired. Please re-authorize with the production environment.')
      : $this->t('To deploy configuration changes, you must first authorize with the production environment.');

    $form['oauth_info'] = [
      '#markup' => '<div class="messages messages--status">' . $message . '</div>',
    ];

    $form['changes_summary'] = [
      '#type' => 'details',
      '#title' => $this->t('Changes to deploy (@count configurations)', ['@count' => count($changedConfigs)]),
      '#open' => TRUE,
    ];

    if (count($changedConfigs) <= 20) {
      $form['changes_summary']['list'] = [
        '#theme' => 'item_list',
        '#items' => $changedConfigs,
        '#attributes' => ['class' => ['config-preview-deploy-changes-list']],
      ];
    }
    else {
      $form['changes_summary']['summary'] = [
        '#markup' => '<p>' . $this->t('Too many changes to list (@count configurations).', ['@count' => count($changedConfigs)]) . '</p>',
      ];
    }

    $form['actions'] = [
      '#type' => 'actions',
    ];

    $form['actions']['authorize'] = [
      '#type' => 'link',
      '#title' => $this->t('Authorize with Production'),
      '#url' => Url::fromRoute('config_preview_deploy.oauth_authorize'),
      '#attributes' => ['class' => ['button', 'button--primary']],
    ];

    $form['actions']['cancel'] = [
      '#type' => 'link',
      '#title' => $this->t('Cancel'),
      '#url' => Url::fromRoute('config_preview_deploy.dashboard'),
      '#attributes' => ['class' => ['button']],
    ];

    return $form;
  }

  /**
   * Builds the deployment confirmation form.
   */
  protected function buildDeploymentForm(array $form, array $changedConfigs): array {
    $form['auth_ready'] = [
      '#markup' => '<div class="messages messages--status">' .
      $this->t('Authorized with production environment.') .
      '</div>',
    ];

    $form['changes_summary'] = [
      '#type' => 'details',
      '#title' => $this->t('Changes to deploy (@count configurations)', ['@count' => count($changedConfigs)]),
      '#open' => TRUE,
    ];

    if (count($changedConfigs) <= 20) {
      $form['changes_summary']['list'] = [
        '#theme' => 'item_list',
        '#items' => $changedConfigs,
        '#attributes' => ['class' => ['config-preview-deploy-changes-list']],
      ];
    }
    else {
      $form['changes_summary']['summary'] = [
        '#markup' => '<p>' . $this->t('Too many changes to list (@count configurations). A checkpoint will be created before deployment.', ['@count' => count($changedConfigs)]) . '</p>',
      ];
    }

    $form['verification_info'] = [
      '#type' => 'details',
      '#title' => $this->t('Deployment Process'),
      '#open' => FALSE,
    ];

    $form['verification_info']['steps'] = [
      '#theme' => 'item_list',
      '#title' => $this->t('The following steps will be performed:'),
      '#items' => [
        $this->t('Verify that production configuration has not changed since your base sync'),
        $this->t('Create a checkpoint on production before applying changes'),
        $this->t('Apply your configuration changes to production'),
        $this->t('Re-initialize preview environment for the next iteration of changes'),
      ],
    ];

    $form['actions'] = [
      '#type' => 'actions',
    ];

    $form['actions']['deploy'] = [
      '#type' => 'submit',
      '#value' => $this->t('Deploy to Production'),
      '#button_type' => 'primary',
      '#submit' => ['::verifyAndDeploy'],
    ];

    $form['actions']['reauthorize'] = [
      '#type' => 'link',
      '#title' => $this->t('Re-authorize'),
      '#url' => Url::fromRoute('config_preview_deploy.oauth_authorize'),
      '#attributes' => ['class' => ['button']],
    ];

    $form['actions']['cancel'] = [
      '#type' => 'link',
      '#title' => $this->t('Cancel'),
      '#url' => Url::fromRoute('config_preview_deploy.dashboard'),
      '#attributes' => ['class' => ['button']],
    ];

    return $form;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc