View source
<?php
namespace Drupal\build_hooks_bitbucket\Plugin\FrontendEnvironment;
use Drupal\build_hooks\Plugin\FrontendEnvironmentBase;
use Drupal\build_hooks_bitbucket\BitbucketManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BitbucketFrontendEnvironment extends FrontendEnvironmentBase implements ContainerFactoryPluginInterface {
use MessengerTrait;
protected $bitbucketManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, BitbucketManager $bitbucketManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->bitbucketManager = $bitbucketManager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('build_hooks_bitbucket.bitbucket_manager'));
}
public function getBuildHookDetails() {
return $this->bitbucketManager
->getBuildHookDetailsForPluginConfiguration($this
->getConfiguration());
}
public function frontEndEnvironmentForm($form, FormStateInterface $form_state) {
$form['repo'] = [
'#type' => 'fieldset',
];
$form['repo']['workspace'] = [
'#type' => 'textfield',
'#title' => $this
->t('Repo workspace'),
'#maxlength' => 255,
'#default_value' => isset($this->configuration['repo']['workspace']) ? $this->configuration['repo']['workspace'] : '',
'#required' => TRUE,
];
$form['repo']['slug'] = [
'#type' => 'textfield',
'#title' => $this
->t('Repo Slug'),
'#maxlength' => 255,
'#default_value' => isset($this->configuration['repo']['slug']) ? $this->configuration['repo']['slug'] : '',
'#description' => $this
->t('Found in the URL https://bitbucket.org/{workspace}/{repo_slug}'),
'#required' => TRUE,
];
$form['ref'] = [
'#type' => 'fieldset',
];
$form['ref']['type'] = [
'#type' => 'select',
'#title' => $this
->t('Reference type'),
'#options' => [
'branch' => 'Branch',
'tag' => 'Tag',
],
'#default_value' => isset($this->configuration['ref']['type']) ? $this->configuration['ref']['type'] : '',
'#description' => $this
->t('The type of thing we want bitbucket to build.'),
'#required' => TRUE,
];
$form['ref']['name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Reference name'),
'#default_value' => isset($this->configuration['ref']['name']) ? $this->configuration['ref']['name'] : '',
'#description' => $this
->t('The name of the branch or tag to build.'),
'#required' => TRUE,
];
$form['selector'] = [
'#type' => 'fieldset',
];
$form['selector']['type'] = [
'#type' => 'select',
'#title' => $this
->t('Pipeline type'),
'#options' => [
'custom' => $this
->t('Custom'),
'pull-requests' => $this
->t('Pull Request'),
],
'#default_value' => isset($this->configuration['selector_type']) ? $this->configuration['selector_type'] : '',
'#description' => $this
->t('The type of pipeline.'),
'#required' => TRUE,
];
$form['selector']['name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Pipeline name'),
'#default_value' => isset($this->configuration['selector']['name']) ? $this->configuration['selector']['name'] : '',
'#description' => $this
->t('The name of the pipeline to trigger for this branch.'),
'#required' => TRUE,
];
return $form;
}
public function frontEndEnvironmentSubmit($form, FormStateInterface $form_state) {
$this->configuration['repo'] = $form_state
->getValue('repo');
$this->configuration['ref'] = $form_state
->getValue('ref');
$this->configuration['selector'] = $form_state
->getValue('selector');
}
public function getAdditionalDeployFormElements(FormStateInterface $form_state) {
$form = [];
$form['bitbucketDeployments'] = [
'#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['bitbucketDeployments']['table'] = $this
->getLastBitbucketDeploymentsTable($this
->getConfiguration());
$form['bitbucketDeployments']['refresher'] = [
'#type' => 'button',
'#ajax' => [
'callback' => [
self::class,
'refreshDeploymentTable',
],
'wrapper' => 'ajax-replace-table',
'effect' => 'fade',
'progress' => [
'type' => 'throbber',
'message' => $this
->t('Refreshing deployment status...'),
],
],
'#value' => $this
->t('Refresh'),
];
} catch (\Exception $e) {
$this
->messenger()
->addError('Unable to retrieve information about the last deployments for this environment. Check configuration.')
->addError($e
->getMessage());
}
return $form;
}
private function getLastBitbucketDeploymentsTable(array $settings) {
$latestBuilds = $this->bitbucketManager
->retrieveLatestBuilds($settings, 15);
$element = [
'#type' => 'table',
'#attributes' => [
'id' => 'ajax-replace-table',
],
'#header' => [
$this
->t('Build'),
$this
->t('Status'),
$this
->t('Started at'),
$this
->t('Finished at'),
],
];
if (!empty($latestBuilds)) {
foreach ($latestBuilds['values'] as $pipelineId => $pipelineData) {
$element[$pipelineId]['build'] = [
'#type' => 'link',
'#url' => Url::fromUri($pipelineData['repository']['links']['html']['href'] . '/addon/pipelines/home', [
'fragment' => '!/results/' . $pipelineData['build_number'],
]),
'#title' => 'Build ' . $pipelineData['build_number'],
'#attributes' => [
'target' => '_blank',
],
];
$element[$pipelineId]['name'] = [
'#plain_text' => $pipelineData['state']['name'] . ' - ' . $pipelineData['state']['result']['name'],
];
$element[$pipelineId]['started_at'] = [
'#type' => 'item',
'#markup' => $pipelineData['created_on'],
];
$element[$pipelineId]['completed_on'] = [
'#type' => 'item',
'#markup' => $pipelineData['completed_on'],
];
}
}
return $element;
}
public static function refreshDeploymentTable(array $form, FormStateInterface $form_state) {
return $form['bitbucketDeployments']['table'];
}
}