You are here

class BitbucketManager in Build Hooks 8.2

Same name and namespace in other branches
  1. 3.x modules/build_hooks_bitbucket/src/BitbucketManager.php \Drupal\build_hooks_bitbucket\BitbucketManager

Service for managing BitBucket connection details.

Hierarchy

Expanded class hierarchy of BitbucketManager

2 files declare their use of BitbucketManager
BitbucketFrontendEnvironment.php in modules/build_hooks_bitbucket/src/Plugin/FrontendEnvironment/BitbucketFrontendEnvironment.php
BitbucketManagerTest.php in modules/build_hooks_bitbucket/tests/src/Unit/BitbucketManagerTest.php
1 string reference to 'BitbucketManager'
build_hooks_bitbucket.services.yml in modules/build_hooks_bitbucket/build_hooks_bitbucket.services.yml
modules/build_hooks_bitbucket/build_hooks_bitbucket.services.yml
1 service uses BitbucketManager
build_hooks_bitbucket.bitbucket_manager in modules/build_hooks_bitbucket/build_hooks_bitbucket.services.yml
Drupal\build_hooks_bitbucket\BitbucketManager

File

modules/build_hooks_bitbucket/src/BitbucketManager.php, line 12

Namespace

Drupal\build_hooks_bitbucket
View source
class BitbucketManager {

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $config;

  /**
   * GuzzleHttp\ClientInterface definition.
   *
   * @var \GuzzleHttp\ClientInterface
   */
  protected $httpClient;
  const API_PREFIX = "https://api.bitbucket.org/2.0/repositories/";
  const PIPELINES_PATH = "/pipelines/";

  /**
   * Constructs a BitbucketManager object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \GuzzleHttp\ClientInterface $http_client
   *   HTTP client.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ClientInterface $http_client) {
    $this->config = $config_factory
      ->get('build_hooks_bitbucket.settings');
    $this->httpClient = $http_client;
  }

  /**
   * Get the full URL for the pipelines api call.
   *
   * @param array $settings
   *   The configuration for this environment.
   *
   * @return string
   *   The api endpoint we should hit.
   */
  protected function getPipelinesApiPath(array $settings) {
    return self::API_PREFIX . $settings['repo']['workspace'] . '/' . $settings['repo']['slug'] . self::PIPELINES_PATH;
  }

  /**
   * Get the authentication headers for the request.
   *
   * @return array
   *   A list of username,password suitable for passing to guzzle.
   */
  protected function getAuth() {
    return [
      $this->config
        ->get('username'),
      $this->config
        ->get('password'),
    ];
  }

  /**
   * Converts hook configuration into an api call.
   *
   * @param array $settings
   *   The configuration for this hook.
   *
   * @return \Drupal\build_hooks\BuildHookDetails
   *   An object that will trigger a pipeline based on config.
   */
  public function getBuildHookDetailsForPluginConfiguration($settings) {
    $buildHookDetails = new BuildHookDetails();
    $buildHookDetails
      ->setUrl($this
      ->getPipelinesApiPath($settings));
    $buildHookDetails
      ->setMethod('POST');
    $body = [
      'target' => [
        "type" => "pipeline_ref_target",
        "ref_name" => $settings['ref']['name'],
        "ref_type" => $settings['ref']['type'],
        "selector" => [
          "type" => $settings['selector']['type'],
          "pattern" => $settings['selector']['name'],
        ],
      ],
    ];
    $buildHookDetails
      ->setOptions([
      'json' => $body,
      'auth' => $this
        ->getAuth(),
    ]);
    return $buildHookDetails;
  }

  /**
   * Get the latest builds from bitbucket pipelines.
   *
   * @param array $settings
   *   The plugin settings array.
   * @param int $limit
   *   Number of desired builds to retrieve.
   *
   * @return array
   *   An array with info about the builds.
   *
   * @throws \GuzzleHttp\Exception\GuzzleException
   */
  public function retrieveLatestBuilds(array $settings, $limit = 10) {
    $url = $this
      ->getPipelinesApiPath($settings);
    $url .= '?sort=' . urlencode('-created_on') . '&pagelen=' . (int) $limit;
    if ($settings['ref']['type'] == 'branch') {
      $url .= '&target.branch=' . urlencode($settings['ref']['name']);
    }
    elseif ($settings['ref']['type'] == 'tag') {
      $url .= '&target.tag=' . urlencode($settings['ref']['name']);
    }
    $options = [
      'auth' => $this
        ->getAuth(),
    ];
    $response = $this->httpClient
      ->request('GET', $url, $options);
    return json_decode($response
      ->getBody()
      ->getContents(), TRUE);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BitbucketManager::$config protected property The config factory.
BitbucketManager::$httpClient protected property GuzzleHttp\ClientInterface definition.
BitbucketManager::API_PREFIX constant
BitbucketManager::getAuth protected function Get the authentication headers for the request.
BitbucketManager::getBuildHookDetailsForPluginConfiguration public function Converts hook configuration into an api call.
BitbucketManager::getPipelinesApiPath protected function Get the full URL for the pipelines api call.
BitbucketManager::PIPELINES_PATH constant
BitbucketManager::retrieveLatestBuilds public function Get the latest builds from bitbucket pipelines.
BitbucketManager::__construct public function Constructs a BitbucketManager object.