Skip to content

Commit

Permalink
✨ feat: add new command gl:br:clean for clean branches
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 14, 2023
1 parent 67d84b8 commit 43b6190
Show file tree
Hide file tree
Showing 10 changed files with 952 additions and 30 deletions.
78 changes: 49 additions & 29 deletions app/Common/GitAPI/AbstractGitAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

namespace Inhere\Kite\Common\GitAPI;

use JsonException;
use PhpPkg\Http\Client\AbstractClient;
use PhpPkg\Http\Client\Client;
use PhpPkg\Http\Client\ClientInterface;
use Toolkit\Stdlib\Helper\JsonHelper;
use Toolkit\Stdlib\Obj\AbstractObj;
use function explode;
use function implode;
Expand All @@ -20,9 +17,11 @@ abstract class AbstractGitAPI extends AbstractObj
public const DEFAULT_UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36';

/**
* base API url
*
* @var string
*/
protected string $baseUrl = 'https://gitlab.example.com/api/v4';
protected string $baseApi = 'https://gitlab.example.com/api/v4';

/**
* The repository owner user/group
Expand Down Expand Up @@ -146,54 +145,75 @@ public function buildPathf(string $format, ...$args): string
}

/**
* @return ClientInterface|AbstractClient
* @return AbstractClient
*/
public function newClient(): AbstractClient
{
// $http = new HttpClient();
$http = Client::factory([]);
$http->setOptions([
// $cli = new HttpClient();
$cli = Client::factory([]);
$cli->setOptions([
'headers' => [
// github
// 'Authorization' => 'Basic ' . $this->token,
'Authorization' => 'Token ' . $this->token,
// 'Authorization' => 'Token ' . $this->token,
// gitlab
'Private-Token' => $this->token,
'User-Agent' => self::DEFAULT_UA,
'Content-Type' => 'application/json',
],
]);

return $http;
// $cli->setDebug(true);

return $cli;
}

/**
* @param string $method
* @param string $uri
* @param array $data
*
* @return AbstractClient
*/
public function sendThen(string $method, string $uri, array $data = []): AbstractClient
{
return $this->newClient()->request($this->baseApi . $uri, $data, $method);
}

/**
* @param string $method
* @param string $uri
* @param array $data
*
* @return array
*/
public function sendGET(string $uri): array
public function sendRequest(string $method, string $uri, array $data = []): array
{
/** @var AbstractClient $client */
$client = $this->newClient()->get($this->baseUrl . $uri);
return $this->sendThen($method, $uri, $data)->getJsonArray();
}

return $client->getJsonArray();
/**
* @param string $uri
* @param array $query
*
* @return array
*/
public function sendGET(string $uri, array $query = []): array
{
return $this->sendRequest('GET', $uri, $query);
}

/**
* @param string $uriPath
* @param string $uri
* @param array $data
*
* @return array
*/
public function sendPOST(string $uriPath, array $data): array
public function sendPOST(string $uri, array $data): array
{
// curl -u username:token https://api.github.com/user
// curl -H "Authorization: token OAUTH-TOKEN" https://api.github.com
$http = $this->newClient();
$resp = $http->byJson()->post($this->baseUrl . $uriPath, $data);

if (!$json = $resp->getBody()->getContents()) {
return [];
}

return JsonHelper::decode($json, true);
return $this->sendRequest('POST', $uri, $data);
}

/**
Expand Down Expand Up @@ -255,16 +275,16 @@ public function setGroup(string $group): void
/**
* @return string
*/
public function getBaseUrl(): string
public function getBaseApi(): string
{
return $this->baseUrl;
return $this->baseApi;
}

/**
* @param string $baseUrl
* @param string $baseApi
*/
public function setBaseUrl(string $baseUrl): void
public function setBaseApi(string $baseApi): void
{
$this->baseUrl = $baseUrl;
$this->baseApi = $baseApi;
}
}
2 changes: 1 addition & 1 deletion app/Common/GitAPI/GitHubV3API.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class GitHubV3API extends AbstractGitAPI
/**
* @var string
*/
protected string $baseUrl = self::BASE_API_URL;
protected string $baseApi = self::BASE_API_URL;

/**
* @param int $issueId
Expand Down
218 changes: 218 additions & 0 deletions app/Common/GitAPI/GitLabV4API.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,229 @@

namespace Inhere\Kite\Common\GitAPI;

use PhpPkg\Http\Client\AbstractClient;
use function array_filter;

/**
* class GitLabV4API
*/
class GitLabV4API extends AbstractGitAPI
{
/*
* response headers for query list.
*/

// X-Page: 1
public const RESP_HEADER_PAGE = 'X-Page';

// X-Next-Page: 2
public const RESP_HEADER_NEXT_PAGE = 'X-Next-Page';

// X-Total: 593
public const RESP_HEADER_TOTAL = 'X-Total';

// X-Total-Pages: 30
public const RESP_HEADER_TOTAL_PAGE = 'X-Total-Pages';

/**
* @param string $projectId
* @param bool $withStat
*
* @return array
*/
public function getProject(string $projectId, bool $withStat = false): array
{
$uri = "/projects/$projectId";
if ($withStat) {
$uri .= '?statistics=true';
}

return $this->sendRequest('GET', $uri);
}

/**
* @param string $projectId
* @param int $mergeId
*
* @return array
*/
public function mergePullRequest(string $projectId, int $mergeId): array
{
$apiUri = "/projects/$projectId/merge_requests/$mergeId/merge";

return $this->sendRequest('PUT', $apiUri);
}

/**
* 创建pull request
*
* @param string $projectId
* @param string $source
* @param string $target
* @param string $title
*
* @return array
*/
public function createPullRequest(string $projectId, string $source, string $target, string $title): array
{
return $this->sendRequest('POST', "/projects/$projectId/merge_requests/", [
'source_branch' => $source,
'target_branch' => $target,
'title' => $title
]);
}

/**
* 查询两个分支是否存在未合并的pull request
*
* @param string $projectId
* @param string $source
* @param string $target
*
* @return array
*/
public function getOnePRByBranches(string $projectId, string $source, string $target): array
{
$merges = $this->sendRequest('GET', '/projects/' . $projectId . '/merge_requests/', [
'source_branch' => $source,
'target_branch' => $target,
'state' => 'opened'
]);

return $merges[0] ?? [];
}

/**
* 对比两个分支代码
*
* @param string $projectId
* @param string $source
* @param string $target
*
* @return array = ['commit' => [], 'commits' => [], 'diffs' => []]
* @see http://gitlab.gongzl.com/help/api/repository.md
*/
public function compareBranches(string $projectId, string $source, string $target): array
{
return $this->sendRequest('GET', "/projects/$projectId/repository/compare/", [
'from' => $target,
'to' => $source,
]);
}

/**
* 获取所有分支
* - http://gitlab.gongzl.com/help/api/branches.md#list-repository-branches
*
* @param string $projectId
* @param string $search keywords
* @param array $params = [
* 'page' => 1,
* 'per_page' => 20,
* ]
*
* @return array
*/
public function getBranches(string $projectId, string $search = '', array $params = []): array
{
$uri = "/projects/$projectId/repository/branches";

$params['search'] = $search;

$cli = $this->sendThen('GET', $uri, array_filter($params));
$ret = $this->getPageInfo($cli);

$data = $cli->getJsonArray();
// set data
if (isset($data['error'])) {
$ret['errCode'] = $cli->getStatusCode();
$ret['error'] = $data['error'];
} else {
$ret['list'] = $data;
}

return $ret;
}

private function getPageInfo(AbstractClient $cli): array
{
return [
'list' => [],
'page' => (int)$cli->getResponseHeader(self::RESP_HEADER_PAGE, '0'),
'nextPage' => (int)$cli->getResponseHeader(self::RESP_HEADER_NEXT_PAGE, '0'),
'totalPage' => (int)$cli->getResponseHeader(self::RESP_HEADER_TOTAL_PAGE, '0'),
'totalNum' => (int)$cli->getResponseHeader(self::RESP_HEADER_TOTAL, '0'),
];
}

/**
* 获取分支信息
*
* @param string $projectId
* @param string $branch
*
* @return array
*/
public function getBranch(string $projectId, string $branch): array
{
return $this->sendRequest('GET', "/projects/$projectId/repository/branches/$branch");
}

/**
* delete-repository-branch
* - docs http://gitlab.gongzl.com/help/api/branches.md#delete-repository-branch
*
* @param string $projectId
* @param string $branch
*
* @return array = ['message' => '', 'errCode' => 404]
*/
public function delBranch(string $projectId, string $branch): array
{
// DELETE /projects/:id/repository/branches/:branch
return $this->sendRequest('DELETE', "/projects/$projectId/repository/branches/$branch");
}

/**
* 获取 tag 列表(每次返回20个)
* - http://gitlab.gongzl.com/help/api/tags.md#list-project-repository-tags
*
* @param string $projectId
* @param array $params = [
* 'page' => 1,
* 'per_page' => 20,
* 'order_by' => 'updated', // ordered by name or updated fields
* 'sort' => 'desc', // sorted in asc or desc order
* ]
*
* @return array
*/
public function getTags(string $projectId, array $params = []): array
{
$uri = "/projects/$projectId/repository/tags";
$cli = $this->sendThen('GET', $uri, array_filter($params));

$ret = $this->getPageInfo($cli);
// set data
$ret['list'] = $cli->getJsonArray();

return $ret;
}

/**
* Delete a tag
*
* @param string $projectId
* @param string $tag
*
* @return array = ['message' => '', 'errCode' => 404]
*/
public function delTag(string $projectId, string $tag): array
{
// DELETE /projects/:id/repository/tags/:tag_name
return $this->sendRequest('DELETE', "/projects/$projectId/repository/tags/$tag");
}

/**
* GET /groups/:id/members
*
Expand Down

0 comments on commit 43b6190

Please sign in to comment.