Skip to content

Using the API

The git_host.api service (GitApi) reads from any host that has a "Git host token" key, and from the providers' public hosts (github.com and gitlab.com) without one. Inject it into your services, plugins or controllers:

services:
  my_module.repository_checker:
    class: Drupal\my_module\RepositoryChecker
    arguments: ['@git_host.api']

Repositories are given as their host and their path on the host, such as github.com and owner/name, or gitlab.example.com and group/subgroup/project.

Reading data

use Drupal\git_host\GitApi;

/** @var \Drupal\git_host\GitApi $api */
$api = \Drupal::service('git_host.api');

// A file's contents, from the default branch or a branch, tag or commit.
$composer_json = $api->getFileContents('github.com', 'drupal/drupal', 'composer.json');
$lock = $api->getFileContents('gitlab.example.com', 'team/site', 'composer.lock', '1.x');

// Published releases, newest first, each with its 'tag', 'url',
// 'download_url' and 'date'. Draft and upcoming releases are left out.
$releases = $api->getReleases('github.com', 'owner/name');

// Protected branches, each with its 'name', 'url', 'download_url' and the
// 'date' of its latest commit.
$branches = $api->getProtectedBranches('gitlab.com', 'group/project');

// Any other endpoint, relative to the host's API URL, as decoded JSON.
$alerts = $api->get('github.com', 'repos/owner/name/dependabot/alerts', ['state' => 'open']);

// Every page of a list, up to GitApi::MAX_PAGES pages of 100.
$tags = $api->getAllPages('github.com', 'repos/owner/name/tags');

get() and getAllPages() take the provider's own endpoints, so check the host's provider first when an endpoint only exists on one of them:

if ($api->getProviderId($host) === 'github') {
  $alerts = $api->get($host, "repos/$repository/dependabot/alerts");
}

Hosts

getHosts() lists the hosts that can be used, keyed by host, with each one's provider plugin ID and key ID. Use it for a host select list, as Git Field's widget does. getProviderId() returns NULL for a host that can't be used.

Keys in code and configuration

A host's token is a Key entity with the git_host_token key type (GitHostTokenKeyType), whose key_type_settings are:

  • provider: the provider plugin ID, such as github or gitlab.
  • host: the host name, without https://, with a port if the host needs one, such as gitlab.example.com:8443.

In exported configuration, or a recipe, a key that reads its token from the GITLAB_TOKEN environment variable with Key's "Environment" provider, so the token itself stays out of the configuration, looks like this (config/sync/key.key.gitlab_example_com.yml):

langcode: en
status: true
dependencies: {  }
id: gitlab_example_com
label: 'gitlab.example.com'
description: ''
key_type: git_host_token
key_type_settings:
  provider: gitlab
  host: gitlab.example.com
key_provider: env
key_provider_settings:
  env_variable: GITLAB_TOKEN
  base64_encoded: false
  strip_line_breaks: true
key_input: none
key_input_settings: {  }

In tests, Key::create() takes the same values, with Key's config provider and the token in key_provider_settings.key_value.

Each host needs exactly one key. The key form refuses a second key for a host, but configuration imports and code don't go through the form: with two keys for one host, getHosts() uses the one it reads last.

Errors

Requests throw exceptions instead of returning empty data:

  • \RuntimeException when no key is set up for the host;
  • \GuzzleHttp\Exception\ClientException for the host's error responses, such as a missing repository (404) or a bad token (401). GitHub and GitLab put a readable reason in the response body's message;
  • \GuzzleHttp\Exception\GuzzleException for other request failures, and \JsonException for responses that aren't JSON.

Guzzle's exceptions are runtime exceptions too, so catch them before \RuntimeException.

Caching

Successful responses are cached in the default cache bin for GitApi::CACHE_LIFETIME (an hour), keyed by their URL, and tagged with the host's key, so changing a key clears its hosts' responses. Error responses aren't cached.

Output built from the API's data should expire too: set its render cache's max-age to GitApi::CACHE_LIFETIME, and add the config:key_list tag, since adding or removing a key changes which hosts can be used.