Search by

adachsoft / git-platform

Arkadiusz Adach

Thin, test-friendly Git hosting abstraction for PHP with GitLab repository management.

Package info

gitlab.com/a.adach/git-platform

Issues

pkg:composer/adachsoft/git-platform

Statistics

Installs: 52

Dependents: 1

Suggesters: 0

Stars: 0

v0.3.3 2026-11-25 11:25 UTC

This package is auto-updated.

Last update: 2026-11-25 10:26:40 UTC


README

adachsoft/git-platform is a small PHP library that provides a thin, test-friendly abstraction over Git hosting providers.

It supports GitLab (REST API v4) and GitHub (REST API) for repository management and listing repositories with pagination.

The only layer you need to use as a consumer is the PublicApi layer, in particular AdachSoft\GitPlatform\PublicApi\GitRepository\GitRepositoryFacadeInterface and AdachSoft\GitPlatform\PublicApi\GitRepository\Builder\GitRepositoryFacadeBuilder.

This package is intended to be used as a library in other applications (not as a standalone service).

Requirements

  • PHP ^8.1
  • ext-json
  • A PSR-18 HTTP client implementation
  • PSR-17 request & stream factories

This library is tested with:

Installation

Install the library via Composer:

composer require adachsoft/git-platform

The package itself only requires PSR interfaces:

  • psr/http-client
  • psr/http-factory

In a typical setup you will also install a concrete implementation, e.g.:

composer require guzzlehttp/guzzle
composer require guzzlehttp/psr7

If you already have a PSR-18 client and PSR-17 factories in your project, you can reuse them – the library will use them under the hood when you build the facade.

Usage

The recommended entry point is the GitRepositoryFacadeBuilder, which creates a ready-to-use GitRepositoryFacadeInterface for either supported platform.

GitLab

<?php

use AdachSoft\GitPlatform\Infrastructure\GitLab\GitLabConfigDto;
use AdachSoft\GitPlatform\PublicApi\GitRepository\Builder\GitRepositoryFacadeBuilder;
use AdachSoft\GitPlatform\PublicApi\GitRepository\GitRepositoryFacadeInterface;

$baseUrl = 'https://gitlab.com';
$accessToken = 'your-gitlab-personal-access-token';

$gitLabConfig = new GitLabConfigDto($baseUrl, $accessToken);

$builder = new GitRepositoryFacadeBuilder();

/** @var GitRepositoryFacadeInterface $gitRepositoryFacade */
$gitRepositoryFacade = $builder->build($gitLabConfig);

GitHub

Use GitHubOwnerTypeEnum::USER for a personal account or GitHubOwnerTypeEnum::ORGANIZATION for an organization. apiBaseUrl defaults to https://api.github.com.

<?php

use AdachSoft\GitPlatform\Infrastructure\GitHub\Enum\GitHubOwnerTypeEnum;
use AdachSoft\GitPlatform\Infrastructure\GitHub\GitHubConfigDto;
use AdachSoft\GitPlatform\PublicApi\GitRepository\Builder\GitRepositoryFacadeBuilder;
use AdachSoft\GitPlatform\PublicApi\GitRepository\GitRepositoryFacadeInterface;

$gitHubConfig = new GitHubConfigDto(
    accessToken: 'your-github-token',
    owner: 'your-github-user-or-organization',
    ownerType: GitHubOwnerTypeEnum::USER,
);

$builder = new GitRepositoryFacadeBuilder();

/** @var GitRepositoryFacadeInterface $gitRepositoryFacade */
$gitRepositoryFacade = $builder->build($gitHubConfig);

From now on you only interact with the GitRepositoryFacadeInterface and PublicApi DTOs.

Repository Identifiers

Repository identifiers depend on the configured platform:

  • GitLab accepts a numeric project ID (for example, 123456) or a project path such as group/project.
  • GitHub accepts owner/name; a bare repository name is resolved using the owner configured in GitHubConfigDto.

Listing Repositories (Pagination)

The facade's listRepositories() method provides paginated repository listings for both providers.

Default pagination

Calling listRepositories() without arguments uses the default pagination:

  • page = 1
  • perPage = 20
use AdachSoft\GitPlatform\PublicApi\GitRepository\Dto\ListRepositoriesQueryDto;
use AdachSoft\GitPlatform\PublicApi\GitRepository\GitRepositoryFacadeInterface;
use AdachSoft\GitPlatform\PublicApi\GitRepository\Dto\PaginatedRepositoryCollectionDto;
use AdachSoft\GitPlatform\PublicApi\GitRepository\Collection\RepositoryCollection;
use AdachSoft\GitPlatform\PublicApi\GitRepository\Dto\RepositoryDto;

/** @var GitRepositoryFacadeInterface $gitRepositoryFacade */
$paginated = $gitRepositoryFacade->listRepositories();

// $paginated is an instance of PaginatedRepositoryCollectionDto

$items = $paginated->items; // RepositoryCollection

foreach ($items as $repository) {
    /** @var RepositoryDto $repository */
    echo $repository->id . ' ' . $repository->fullName . PHP_EOL;
}

// Pagination metadata
$page = $paginated->page;        // int
$perPage = $paginated->perPage;  // int
$totalItems = $paginated->totalItems; // ?int (always null on GitHub; nullable on GitLab)
$totalPages = $paginated->totalPages; // ?int

On GitHub, totalItems is always null. totalPages is derived from the Link response header and is null when the last page cannot be determined.

Custom pagination

To request a specific page and page size, construct ListRepositoriesQueryDto and pass it to listRepositories():

use AdachSoft\GitPlatform\PublicApi\GitRepository\Dto\ListRepositoriesQueryDto;
use AdachSoft\GitPlatform\PublicApi\GitRepository\GitRepositoryFacadeInterface;

/** @var GitRepositoryFacadeInterface $gitRepositoryFacade */
$query = new ListRepositoriesQueryDto(page: 2, perPage: 50);

$paginated = $gitRepositoryFacade->listRepositories($query);

// You can now assert or use pagination metadata
assert($paginated->page === 2);
assert($paginated->perPage === 50);

Creating a Repository

Use CreateRepositoryDto to create a new repository:

use AdachSoft\GitPlatform\PublicApi\GitRepository\Dto\CreateRepositoryDto;
use AdachSoft\GitPlatform\PublicApi\GitRepository\GitRepositoryFacadeInterface;
use AdachSoft\GitPlatform\PublicApi\GitRepository\Dto\RepositoryDto;

/** @var GitRepositoryFacadeInterface $gitRepositoryFacade */
$dto = new CreateRepositoryDto(
    name: 'my-new-repository',
    description: 'Example project managed via git-platform',
    private: null,           // optional, let GitLab defaults decide
    visibility: 'private',   // or 'public', 'internal' depending on your GitLab setup
);

/** @var RepositoryDto $repository */
$repository = $gitRepositoryFacade->createRepository($dto);

echo $repository->id;       // string
echo $repository->fullName; // string

Updating a Repository

Use UpdateRepositoryDto to update an existing repository. You only need to provide the fields you want to change. For GitLab, defaultBranch accepts a validated BranchNameVo; omitting it leaves the current default branch unchanged:

use AdachSoft\GitPlatform\Core\GitRepository\Vo\BranchNameVo;
use AdachSoft\GitPlatform\PublicApi\GitRepository\GitRepositoryFacadeInterface;
use AdachSoft\GitPlatform\PublicApi\GitRepository\Dto\RepositoryDto;
use AdachSoft\GitPlatform\PublicApi\GitRepository\Dto\UpdateRepositoryDto;

/** @var GitRepositoryFacadeInterface $gitRepositoryFacade */
$dto = new UpdateRepositoryDto(
    repositoryId: '123456',
    name: 'renamed-repository',
    description: 'Updated description',
    private: null,             // keep previous value
    visibility: 'private',     // change visibility
    hasIssues: true,
    hasWiki: false,
    defaultBranch: new BranchNameVo('main'),
);

/** @var RepositoryDto $repository */
$repository = $gitRepositoryFacade->updateRepository($dto);

echo $repository->name; // "renamed-repository"
echo $repository->defaultBranch; // "main"

Getting a Single Repository

use AdachSoft\GitPlatform\PublicApi\GitRepository\GitRepositoryFacadeInterface;
use AdachSoft\GitPlatform\PublicApi\GitRepository\Dto\RepositoryDto;

/** @var GitRepositoryFacadeInterface $gitRepositoryFacade */
$repositoryId = '123456';

/** @var RepositoryDto $repository */
$repository = $gitRepositoryFacade->getRepository($repositoryId);

echo $repository->fullName;

Deleting a Repository

use AdachSoft\GitPlatform\PublicApi\GitRepository\GitRepositoryFacadeInterface;

/** @var GitRepositoryFacadeInterface $gitRepositoryFacade */
$repositoryId = '123456';

$gitRepositoryFacade->deleteRepository($repositoryId);

Repository Members

GitHub role mapping

The platform-independent RepositoryMemberRoleEnum roles map to GitHub repository permissions for organization-owned repositories as follows. For user-owned repositories, the collaborator endpoint does not accept a permission value, so GitHub grants its default write access regardless of the requested role.

RoleGitHub permission
GUESTpull
REPORTERtriage
DEVELOPERpush
MAINTAINERmaintain
OWNERadmin

Adding members and accepting invitations

addMember() returns an AddRepositoryMemberResultDto. Its status is MemberAdditionStatusEnum::ADDED when the user is added directly or MemberAdditionStatusEnum::INVITED when an invitation is created. invitationId contains the created invitation ID when present, otherwise null.

On GitHub, a user who is not yet a collaborator receives an invitation and must accept it. Build the facade with the invited user's token and the repository owner's account details; use GitHubOwnerTypeEnum::ORGANIZATION when the repository owner is an organization:

<?php

use AdachSoft\GitPlatform\Infrastructure\GitHub\Enum\GitHubOwnerTypeEnum;
use AdachSoft\GitPlatform\Infrastructure\GitHub\GitHubConfigDto;
use AdachSoft\GitPlatform\PublicApi\GitRepository\Builder\GitRepositoryFacadeBuilder;

$invitedUserFacade = (new GitRepositoryFacadeBuilder())->build(
    new GitHubConfigDto(
        accessToken: 'invited-user-token',
        owner: 'owner',
        ownerType: GitHubOwnerTypeEnum::USER,
    ),
);

$invitedUserFacade->acceptRepositoryInvitation('owner/name');

On GitLab, calling acceptRepositoryInvitation() throws UnsupportedOperationException.

Versioning

This library follows semantic versioning (SemVer).

  • Current stable version: 0.1.0 (initial release)
  • See CHANGELOG.md for a human-readable list of changes between versions.

Error Handling

The facade may throw these domain-specific exceptions from AdachSoft\GitPlatform\Core\GitRepository\Exception:

  • InvalidRepositoryMemberException
  • RepositoryConflictException
  • RepositoryConnectionException
  • RepositoryInvitationNotFoundException
  • RepositoryNotFoundException
  • RepositoryUserNotFoundException
  • UnsupportedOperationException

RepositoryInvitationNotFoundException indicates that no pending GitHub invitation matches the repository identifier. UnsupportedOperationException is thrown when the configured provider does not support an operation, such as accepting an invitation on GitLab.

Make sure to wrap calls in your own error handling or translate these exceptions to your application-level errors.

License

This library is released under the MIT License.