Writing a provider
A Git provider plugin reads from one kind of host, such as GitHub or GitLab. To support another kind of host, add a class in your module's src/Plugin/GitProvider directory with the #[GitProvider] attribute, implementing GitProviderInterface. The GitHub and GitLab plugins are complete examples.
namespace Drupal\my_module\Plugin\GitProvider;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\git_host\Attribute\GitProvider;
use Drupal\git_host\GitApi;
use Drupal\git_host\GitProviderInterface;
#[GitProvider(
id: 'example',
label: new TranslatableMarkup('Example'),
// Optional: a public host that works without a key.
public_host: 'git.example.org',
)]
class Example extends PluginBase implements GitProviderInterface {
public function getApiUrl(string $host): string {
return "https://$host/api/v1/";
}
public function getRequestHeaders(?string $token): array {
return $token ? ['Authorization' => 'token ' . $token] : [];
}
public function getFileContents(GitApi $api, string $host, string $repository, string $path, string $ref = ''): string {
return $api->getRaw($host, "repos/$repository/raw/$path", $ref !== '' ? ['ref' => $ref] : []);
}
// getReleases() and getProtectedBranches() return arrays in the shapes
// GitProviderInterface documents.
}
Once the plugin exists, its provider shows in the "Git host token" key type's provider list, and keys with it make their hosts usable.
- Send requests through the
GitApipassed to each method (get(),getRaw()orgetAllPages()), not your own HTTP client: it adds the host's token and caches the responses. getAllPages()usesper_pageandpagequery parameters, as GitHub and GitLab do. Read lists another way for hosts that page differently.- Other modules can change the definitions with
hook_git_provider_info_alter().