emri99 / gitlab-generic-api-client
PHP GitLab 通用 API 客户端(不绑定任何版本)
v3.0.0
2020-11-11 17:51 UTC
Requires
- php: >=5.4
- ext-json: *
- mashape/unirest-php: ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: 2.*
- phpunit/phpunit: ^4.8 || ^5.7
README
该库的构建考虑了 GitLab 发展迅速,难以在复杂代码库上应用更改和迁移指南的情况。
工作原理
此代码灵感来源于 npm 包 gitlab-api-client。
以下描述也是如此。
主要原则:所有路径都是通用构建的。
您不需要绑定任何特定 API 版本,因为您将能够访问所有 GitLab API 端点,即使尚未定义的端点也不例外。
安装
composer require emri99/gitlab-generic-api-client
用法
认证
- 使用 HTTP 令牌进行认证
$client->authenticate('SECRET-HTTP-TOKEN', GitlabApiClient::AUTH_HTTP_TOKEN);
- 使用 OAUTH 令牌进行认证
$client->authenticate('SECRET-OAUTH-TOKEN', GitlabApiClient::AUTH_OAUTH_TOKEN);
请求
- GET 请求
$client = new GitlabApiClient('https://my.gitlab.com/api/v4'); $branches = $client->projects(1) ->repository() ->branches() ->get() // will send GET request on // https://my.gitlab.com/api/v4/projects/1/repository/branches. foreach($branches as $branch) { echo $branch->name, "\n"; }
- POST 请求
# create a variable secret $variableDatas = $this->getClient() ->projects(2) ->variables() ->post([ 'key' => 'SECRET', 'value' => 'password' ]);
- PUT 请求
# protect a branch $branchUpdated = $this->getClient() ->projects(2) ->repository() ->branches('master') ->protect()->put([ 'developers_can_push' => false, 'developers_can_merge' => false ]); $done = $branchUpdated->protected;
- DELETE 请求
# delete a branch $branchUpdated = $this->getClient() ->projects(2) ->repository() ->branches('obsolet-feature') ->delete();
特殊情况
如果 URL 段与 GitlabApiClient
的公共方法相同,则可以正确构建路径。
例如,要构建路径 user/1/delete
,使用
$client->user(1, 'delete');
根据 GitLab API 版本进行 IDE 补全(可选)
可以使用空类来模拟通过安装 emri99/gitlab-generic-api-client-models
获取的对象的代码补全。此 可选包 根据 GitLab API 版本进行标记。
目前没有处理许多版本,只有我正在使用的版本。例如:9.1.4
使用此包时,获取的对象 不会 是模型类的实例。
获取的对象保持为stdclass
。这 仅 用于 IDE 补全。
composer require emri99/gitlab-generic-api-client-models:YOUR_GITLAB_VERSION --dev
您 必须 添加 phpdoc 以使用如下补全
- GET
$client = new GitlabApiClient('https://my.gitlab.com/api/v4'); /** * $branches aint really a Branch instance * @var Branch[] $branches */ $branches = $client->projects(1) ->repository() ->branches() ->get(array( // parameters )) // $branches is an array of stdclass having // the same properties than a Branch class foreach($branches as $branch) { echo $branch->name; }
贡献
感谢您的贡献!
请遵循以下规则
- 您 必须 运行
composer run-script cs
以应用提供的 CS-fixer - 您 必须 编写/更新测试
- 您应该编写文档
- 您 必须 在拉取请求描述中添加最小详细信息
将多个提交压缩成多个提交以避免在 Git 日志中产生噪音也是有意义的;()