You are here

public function CircleV2::getAdditionalDeployFormElements in Build Hooks 8.2

Same name and namespace in other branches
  1. 3.x modules/build_hooks_circleci/src/Plugin/FrontendEnvironment/CircleV2.php \Drupal\build_hooks_circleci\Plugin\FrontendEnvironment\CircleV2::getAdditionalDeployFormElements()

Allows the plugin to add elements to the deployment form.

Parameters

\Drupal\Core\Form\FormStateInterface $form_state: The current form state.

Return value

array A form array to add to the deployment form.

Overrides FrontendEnvironmentInterface::getAdditionalDeployFormElements

File

modules/build_hooks_circleci/src/Plugin/FrontendEnvironment/CircleV2.php, line 284

Class

CircleV2
Defines a circle v2 environment.

Namespace

Drupal\build_hooks_circleci\Plugin\FrontendEnvironment

Code

public function getAdditionalDeployFormElements(FormStateInterface $form_state) {
  try {
    $response = $this->httpClient
      ->request('GET', sprintf('https://circleci.com/api/v2/project/gh/%s/pipeline/mine?branch=%s', $this->configuration['project'], $this->configuration['reference']), [
      'auth' => [
        $this->configuration['token'],
        '',
      ],
    ]);
  } catch (GuzzleException $e) {
    return [
      'error' => [
        '#markup' => $this
          ->t('Could not get list of recent builds'),
      ],
    ];
  }
  $body = Json::decode($response
    ->getBody());
  $build['builds'] = [
    '#type' => 'table',
    '#attributes' => [
      'id' => 'ajax-replace-table',
    ],
    '#header' => [
      $this
        ->t('Created'),
      $this
        ->t('Updated'),
      $this
        ->t('Status'),
      $this
        ->t('Link'),
    ],
    '#empty' => $this
      ->t('No recent builds for reference %reference', [
      '%reference' => $this->configuration['reference'],
    ]),
    '#caption' => $this
      ->t('Last 5 builds'),
  ];
  foreach (array_slice(array_filter($body['items'], function (array $item) {
    return ($item['vcs']['branch'] ?? FALSE) === $this->configuration['reference'];
  }), 0, 5) as $item) {
    try {
      $workflow_response = $this->httpClient
        ->request('GET', 'https://circleci.com/api/v2/pipeline/' . $item['id'] . '/workflow', [
        'auth' => [
          $this->configuration['token'],
          '',
        ],
      ]);
      $workflows = Json::decode($workflow_response
        ->getBody());
    } catch (GuzzleException $e) {
      $build['builds']['#rows'][] = [
        $this->formatter
          ->format(\DateTime::createFromFormat(\DateTime::RFC3339_EXTENDED, $item['created_at'])
          ->getTimestamp(), 'medium'),
        $this->formatter
          ->format(\DateTime::createFromFormat(\DateTime::RFC3339_EXTENDED, $item['updated_at'])
          ->getTimestamp(), 'medium'),
        $this
          ->t('Could not get workflows'),
        '-',
      ];
      continue;
    }
    $worflow = reset($workflows['items']);
    $build['builds']['#rows'][] = [
      $this->formatter
        ->format(\DateTime::createFromFormat(\DateTime::RFC3339_EXTENDED, $item['created_at'])
        ->getTimestamp(), 'medium'),
      $this->formatter
        ->format(\DateTime::createFromFormat(\DateTime::RFC3339_EXTENDED, $item['updated_at'])
        ->getTimestamp(), 'medium'),
      Unicode::ucfirst($worflow['status']),
      [
        'data' => [
          '#type' => 'link',
          '#url' => Url::fromUri(sprintf('https://app.circleci.com/pipelines/github/%s/%d/workflows/%s', $this->configuration['project'], $item['number'], $worflow['id'])),
          '#title' => $this
            ->t('View'),
        ],
      ],
    ];
  }
  $build['refresher'] = [
    '#type' => 'button',
    '#ajax' => [
      'callback' => [
        self::class,
        'refresh',
      ],
      'wrapper' => 'ajax-replace-table',
      'effect' => 'fade',
      'progress' => [
        'type' => 'throbber',
        'message' => $this
          ->t('Refreshing status...'),
      ],
    ],
    '#value' => $this
      ->t('Refresh'),
  ];
  return $build;
}