Skip to content

Commit

Permalink
optimize gitlab command usage
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 5, 2020
1 parent d31b272 commit 56381ac
Show file tree
Hide file tree
Showing 3 changed files with 268 additions and 24 deletions.
29 changes: 25 additions & 4 deletions app/Common/GitLocal/GitLab.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Inhere\Kite\Common\GitLocal;

use Inhere\Console\Exception\PromptException;
use Inhere\Kite\Common\GitLocal\GitLab\Project;
use RuntimeException;
use function array_merge;

/**
* Class GitLab
Expand Down Expand Up @@ -60,17 +61,29 @@ protected function init(array $config): void
}

/**
* @param string $pjName
*
* @return $this
*/
public function loadCurPjInfo(): self
public function loadCurPjInfo(string $pjName = ''): self
{
$pjName = $this->curPjName;
if ($pjName) {
$this->setCurPjName($pjName);
}

$pjName = $this->curPjName;
if (!isset($this->projects[$pjName])) {
throw new RuntimeException("project '{$pjName}' is not found in the projects");
}

$this->curPjInfo = $this->projects[$pjName];
$defaultInfo = [
'name' => $pjName,
'group' => $this->getValue('defaultGroup', ''),
'forkGroup' => $this->getValue('defaultForkGroup', ''),
];

$this->curPjInfo = array_merge($defaultInfo, $this->projects[$pjName]);

return $this;
}

Expand Down Expand Up @@ -162,4 +175,12 @@ public function getCurPjInfo(): array
{
return $this->curPjInfo;
}

/**
* @return Project
*/
public function getCurProject(): Project
{
return Project::new($this->curPjInfo);
}
}
217 changes: 217 additions & 0 deletions app/Common/GitLocal/GitLab/Project.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
<?php declare(strict_types=1);

namespace Inhere\Kite\Common\GitLocal\GitLab;

use Toolkit\Stdlib\Obj;

/**
* Class Project
*
* @package Inhere\Kite\Common\GitLocal\GitLab
*/
class Project
{
// '/' => '%2F'
public const PID_SEP = '%2F';

/**
* @var string
*/
private $name;

/**
* The main group
*
* @var string
*/
private $group;

/**
* The repository name
*
* @var string
*/
private $repo;

/**
* Project Id for main group/repo
* - int 123
* - string "group%2Frepo"
*
* @var string
*/
private $mainPid;

/**
* @var string
*/
private $forkGroup;

/**
* Project Id for forked-group/repo
* - int 123
* - string "group%2Frepo"
*
* @var string
*/
private $forkPid;

/**
* @param array $data
*
* @return static
*/
public static function new(array $data): self
{
return new self($data);
}

/**
* @return array
*/
public function toArray(): array
{
return [
'name' => $this->name,
'group' => $this->group,
'repo' => $this->repo,
'mainPid' => $this->mainPid,
'forkGroup' => $this->forkGroup,
'forkPid' => $this->forkPid,
];
}

/**
* Class constructor.
*
* @param array $data
*/
public function __construct(array $data = [])
{
Obj::init($this, $data);
}

/**
* @return string
*/
public function getName(): string
{
return $this->name;
}

/**
* @param string $name
*
* @return Project
*/
public function setName(string $name): Project
{
$this->name = $name;
return $this;
}

/**
* @return string
*/
public function getGroup(): string
{
return $this->group;
}

/**
* @param string $group
*
* @return Project
*/
public function setGroup(string $group): Project
{
$this->group = $group;
return $this;
}

/**
* @return string
*/
public function getRepo(): string
{
return $this->repo;
}

/**
* @param string $repo
*
* @return Project
*/
public function setRepo(string $repo): Project
{
$this->repo = $repo;
return $this;
}

/**
* @param string|int $mainPid
*
* @return Project
*/
public function setMainPid($mainPid): Project
{
$this->mainPid = (string)$mainPid;
return $this;
}

/**
* @return string
*/
public function getMainPid(): string
{
if (!$this->mainPid) {
// string pid: group + %2F + repo
$this->mainPid = $this->group . self::PID_SEP . $this->repo;
}

return $this->mainPid;
}

/**
* @return string
*/
public function getForkGroup(): string
{
return $this->forkGroup;
}

/**
* @param string $forkGroup
*
* @return Project
*/
public function setForkGroup(string $forkGroup): Project
{
$this->forkGroup = $forkGroup;
return $this;
}

/**
* @return string
*/
public function getForkPid(): string
{
if (!$this->forkPid) {
// string pid: group + %2F + repo
$this->forkPid = $this->forkGroup . self::PID_SEP . $this->repo;
}

return $this->forkPid;
}

/**
* @param string|int $forkPid
*
* @return Project
*/
public function setForkPid($forkPid): Project
{
$this->forkPid = (string)$forkPid;
return $this;
}
}
46 changes: 26 additions & 20 deletions app/Console/Controller/GitLabController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
use function in_array;
use function parse_str;
use function parse_url;
use function strlen;
use function strpos;
use function substr;
use function trim;

/**
Expand Down Expand Up @@ -64,6 +61,7 @@ protected static function commandAliases(): array
'li' => 'linkInfo',
'cf' => 'config',
'conf' => 'config',
'pj' => 'project',
];
}

Expand Down Expand Up @@ -185,16 +183,28 @@ public function acpCommand(Input $input, Output $output): void
* @options
* -l, --list List all project information
*
* @argument
* name Display project information for given name
*
* @param Input $input
* @param Output $output
*/
public function projectCommand(Input $input, Output $output): void
{
$gitlab = $this->gitlab();
if ($input->getSameBoolOpt(['l', 'list'])) {
$output->json($this->projects);
$output->json($gitlab->getProjects());
return;
}

if (!$pjName = $gitlab->findPjName()) {
$pjName = $input->getArg(0);
}

$gitlab->loadCurPjInfo($pjName);
$project = $gitlab->getCurProject();

$output->json($project->toArray());
$output->success('Complete');
}

Expand Down Expand Up @@ -268,28 +278,27 @@ protected function pullRequestConfigure(Input $input): void
public function pullRequestCommand(Input $input, Output $output): void
{
// http://gitlab.my.com/group/repo/merge_requests/new?utf8=%E2%9C%93&merge_request%5Bsource_project_id%5D=319&merge_request%5Bsource_branch%5D=fea_4_16&merge_request%5Btarget_project_id%5D=319&merge_request%5Btarget_branch%5D=qa
$git = $this->gitlab();

if (!$pjName = $git->findPjName()) {
$gitlab = $this->gitlab();
if (!$pjName = $gitlab->findPjName()) {
$pjName = $input->getRequiredArg('project');
}

if (!$git->hasProject($pjName)) {
if (!$gitlab->hasProject($pjName)) {
throw new PromptException("project '{$pjName}' is not found in the config");
}

$git->setCurPjName($pjName)->loadCurPjInfo();
$gitlab->loadCurPjInfo($pjName);

$pjInfo = $git->getCurPjInfo();
$project = $gitlab->getCurProject();

$group = $pjInfo['group'] ?? $this->config['defaultGroup'];
$repo = $pjInfo['repo'];
$repo = $project->getRepo();
$group = $project->getGroup();

$brPrefix = $this->config['branchPrefix'];
$fixedBrs = $this->config['fixedBranch'];
$brPrefix = $gitlab->getValue('branchPrefix', '');
$fixedBrs = $gitlab->getValue('fixedBranch', []);

$srcPjId = $pjInfo['forkProjectId'];
$tgtPjId = $pjInfo['mainProjectId'];
$srcPjId = $project->getForkPid();
$tgtPjId = $project->getMainPid();

$output->info('auto fetch current branch name');
$curBranch = GitUtil::getCurrentBranchName();
Expand All @@ -315,7 +324,7 @@ public function pullRequestCommand(Input $input, Output $output): void
// Is sync to remote
$isDirect = $input->getBoolOpt('direct');
if ($isDirect || $srcBranch === $tgtBranch) {
$group = $pjInfo['forkGroup'] ?? $this->config['defaultForkGroup'];
$group = $project->getForkGroup();
} else {
$srcPjId = $tgtPjId;
}
Expand Down Expand Up @@ -368,9 +377,6 @@ protected function linkInfoConfigure(Input $input): void
* @arguments
* link Please input an gitlab link
*
* @options
* --config Convert to config data TODO
*
* @param Input $input
* @param Output $output
*/
Expand Down

0 comments on commit 56381ac

Please sign in to comment.