adachsoft / git-platform
Thin, test-friendly Git hosting abstraction for PHP with GitLab repository management.
Requires
- php: ^8.1
- adachsoft/collection: ^3.0
- guzzlehttp/guzzle: ^7.10
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
Requires (Dev)
- adachsoft/php-code-style: ^0.7
- friendsofphp/php-cs-fixer: ^3.94
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^13.0
- rector/rector: ^2.3
- symplify/phpstan-rules: ^14.9
- vlucas/phpdotenv: ^5.6
Suggests
- guzzlehttp/guzzle: Required as PSR-18 HTTP client implementation (^7.0)
Provides
None
Conflicts
None
Replaces
None
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:
guzzlehttp/guzzleas PSR-18 clientguzzlehttp/psr7as PSR-7 implementation and factories
Installation
Install the library via Composer:
composer require adachsoft/git-platform
The package itself only requires PSR interfaces:
psr/http-clientpsr/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 asgroup/project. - GitHub accepts
owner/name; a bare repository name is resolved using the owner configured inGitHubConfigDto.
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 = 1perPage = 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.
| Role | GitHub permission |
|---|---|
GUEST | pull |
REPORTER | triage |
DEVELOPER | push |
MAINTAINER | maintain |
OWNER | admin |
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.mdfor a human-readable list of changes between versions.
Error Handling
The facade may throw these domain-specific exceptions from AdachSoft\GitPlatform\Core\GitRepository\Exception:
InvalidRepositoryMemberExceptionRepositoryConflictExceptionRepositoryConnectionExceptionRepositoryInvitationNotFoundExceptionRepositoryNotFoundExceptionRepositoryUserNotFoundExceptionUnsupportedOperationException
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.