Skip to content

Commit

Permalink
♻️ refactor: migrate the gitlab pr command to sub-command MergeReques…
Browse files Browse the repository at this point in the history
…tCmd
  • Loading branch information
inhere committed Jun 14, 2022
1 parent c42288e commit d142c8a
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 150 deletions.
145 changes: 144 additions & 1 deletion app/Console/Attach/Gitlab/MergeRequestCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
namespace Inhere\Kite\Console\Attach\Gitlab;

use Inhere\Console\Command;
use Inhere\Console\Exception\PromptException;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Kite\Helper\AppHelper;
use Inhere\Kite\Helper\GitUtil;
use function array_merge;
use function http_build_query;
use function in_array;
use function is_string;
use function strtoupper;

/**
* class MergeRequestCmd
Expand All @@ -13,8 +21,40 @@
*/
class MergeRequestCmd extends Command
{
protected static string $name = 'pullRequest';
protected static string $desc = 'generate an PR/MR link for given project information';

public static function aliases(): array
{
return ['pr', 'mr', 'merge-request'];
}

/**
* Do execute command
* generate an PR link for given project information
*
* @options
* -s, --source The source branch name. will auto prepend branchPrefix
* --full-source The full source branch name
* -t, --target The target branch name
* -o, --open Open the generated PR link on browser
* -d, --direct bool;The PR is direct from fork to main repository
* --new bool;Open new pr page on browser. eg: http://my.gitlab.com/group/repo/merge_requests/new
*
* @argument
* project The project key in 'gitlab' config. eg: group-name, name
*
* @help
* Special:
* `@`, HEAD - Current branch.
* `@s` - Source branch.
* `@t` - Target branch.
*
* @example
* {binWithCmd} Will generate PR link for fork 'HEAD_BRANCH' to main 'HEAD_BRANCH'
* {binWithCmd} -o @ Will open PR link for fork 'HEAD_BRANCH' to main 'HEAD_BRANCH' on browser
* {binWithCmd} -o qa Will open PR link for main 'HEAD_BRANCH' to main 'qa' on browser
* {binWithCmd} -t qa Will generate PR link for main 'HEAD_BRANCH' to main 'qa'
* {binWithCmd} -t qa --direct Will generate PR link for fork 'HEAD_BRANCH' to main 'qa'
*
* @param Input $input
* @param Output $output
Expand All @@ -23,6 +63,109 @@ class MergeRequestCmd extends Command
*/
protected function execute(Input $input, Output $output): mixed
{
$fs = $this->flags;
$gl = AppHelper::newGitlab();

// 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
if (!$pjName = $gl->findProjectName()) {
$pjName = $fs->getArg('project');
}

$gl->loadProjectInfo($pjName);

$p = $gl->getCurProject();

// $brPrefix = $gitlab->getValue('branchPrefix', '');
// $fixedBrs = $gitlab->getValue('fixedBranch', []);
// 这里面的分支禁止作为源分支(source)来发起PR
$denyBrs = $gl->getValue('denyBranches', []);

$srcPjId = $p->getForkPid();
$tgtPjId = $p->getMainPid();

$output->info('auto fetch current branch name');
$curBranch = GitUtil::getCurrentBranchName();
$srcBranch = $fs->getOpt('source');
$tgtBranch = $fs->getOpt('target');

if ($fullSBranch = $fs->getOpt('full-source')) {
$srcBranch = $fullSBranch;
} elseif (!$srcBranch) {
$srcBranch = $curBranch;
}

$open = $fs->getOpt('open');
// if input '@', 'head', use current branch name.
if ($open) {
if ($open === '@' || strtoupper($open) === 'HEAD') {
$open = $curBranch;
} elseif ($open === '@s') {
$open = $srcBranch;
} elseif ($open === '@t') {
$open = $tgtBranch;
}
}

if (!$tgtBranch) {
if (is_string($open) && $open) {
$tgtBranch = $open;
} else {
$tgtBranch = $curBranch;
}
}

$srcBranch = $gl->getRealBranchName($srcBranch);
$tgtBranch = $gl->getRealBranchName($tgtBranch);

// deny as an source branch
if ($denyBrs && $srcBranch !== $tgtBranch && in_array($srcBranch, $denyBrs, true)) {
throw new PromptException("the branch '$srcBranch' dont allow as source-branch for PR to other branch");
}

$repo = $p->repo;
$group = $p->group;

// Is sync to remote
$isDirect = $fs->getOpt('direct');
if ($isDirect || $srcBranch === $tgtBranch) {
$group = $p->getForkGroup();
} else {
$srcPjId = $tgtPjId;
}

$prInfo = [
'source_project_id' => $srcPjId,
'source_branch' => $srcBranch,
'target_project_id' => $tgtPjId,
'target_branch' => $tgtBranch
];

$tipInfo = array_merge([
'name' => $pjName,
'glPath' => "$group/$repo",
], $prInfo);
$output->aList($tipInfo, '- project information', ['ucFirst' => false]);
$query = [
'utf8' => '',
'merge_request' => $prInfo
];

// $link = $this->config['hostUrl'];
$link = $gl->getHost();
$link .= "/$group/$repo/merge_requests/new?";
$link .= http_build_query($query);
// $link = UrlHelper::build($link, $query);

if ($open) {
// $output->info('will auto open link on browser');
AppHelper::openBrowser($link);
$output->success('Complete');
} else {
$output->colored("PR LINK: ");
$output->writeln(' ' . $link);
$output->colored('Complete, please open the link on browser');
}

return 0;
}
}
144 changes: 4 additions & 140 deletions app/Console/Controller/Gitx/GitLabController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,27 @@
use Inhere\Kite\Common\CmdRunner;
use Inhere\Kite\Common\GitLocal\GitLab;
use Inhere\Kite\Console\Attach\Gitlab\BranchCmd;
use Inhere\Kite\Console\Attach\Gitlab\MergeRequestCmd;
use Inhere\Kite\Console\Attach\Gitlab\ProjectCmd;
use Inhere\Kite\Console\Component\RedirectToGitGroup;
use Inhere\Kite\Console\SubCmd\Gitflow\BranchCreateCmd;
use Inhere\Kite\Helper\AppHelper;
use Inhere\Kite\Helper\GitUtil;
use Inhere\Kite\Kite;
use Throwable;
use Toolkit\PFlag\FlagsParser;
use Toolkit\Stdlib\Str;
use function array_merge;
use function chdir;
use function date;
use function explode;
use function http_build_query;
use function implode;
use function in_array;
use function is_string;
use function parse_str;
use function realpath;
use function sprintf;
use function str_contains;
use function str_starts_with;
use function strpos;
use function strtoupper;
use function trim;

/**
Expand Down Expand Up @@ -69,7 +66,6 @@ public static function aliases(): array
protected static function commandAliases(): array
{
return [
'pullRequest' => ['pr', 'mr', 'merge-request'],
'deleteBranch' => ['del-br', 'delbr', 'dbr', 'db'],
'newBranch' => ['new-br', 'newbr', 'nbr', 'nb'],
'li' => 'linkInfo',
Expand All @@ -82,6 +78,7 @@ protected static function commandAliases(): array
'project' => ['pj', 'info'],
'checkout' => ['co'],
'branch' => BranchCmd::aliases(),
MergeRequestCmd::getName() => MergeRequestCmd::aliases(),
];
}

Expand All @@ -93,6 +90,7 @@ protected function subCommands(): array
return [
BranchCmd::class,
ProjectCmd::class,
MergeRequestCmd::class,
];
}

Expand Down Expand Up @@ -598,141 +596,7 @@ public function resolveCommand(FlagsParser $fs, Output $output): void

$output->success('Complete. please resolve conflicts by tools or manual');
$output->note('TIPS can exec this command after resolved for quick commit:');
$output->colored("git add . && git commit && git push && kite gl pr -o head && git checkout $curBranch", 'mga');
}

/**
* generate an PR link for given project information
*
* @options
* -s, --source The source branch name. will auto prepend branchPrefix
* --full-source The full source branch name
* -t, --target The target branch name
* -o, --open Open the generated PR link on browser
* -d, --direct bool;The PR is direct from fork to main repository
* --new bool;Open new pr page on browser. eg: http://my.gitlab.com/group/repo/merge_requests/new
*
* @argument
* project The project key in 'gitlab' config. eg: group-name, name
*
* @param FlagsParser $fs
* @param Output $output
*
* @help
* Special:
* `@`, HEAD - Current branch.
* `@s` - Source branch.
* `@t` - Target branch.
*
* @example
* {binWithCmd} Will generate PR link for fork 'HEAD_BRANCH' to main 'HEAD_BRANCH'
* {binWithCmd} -o @ Will open PR link for fork 'HEAD_BRANCH' to main 'HEAD_BRANCH' on browser
* {binWithCmd} -o qa Will open PR link for main 'HEAD_BRANCH' to main 'qa' on browser
* {binWithCmd} -t qa Will generate PR link for main 'HEAD_BRANCH' to main 'qa'
* {binWithCmd} -t qa --direct Will generate PR link for fork 'HEAD_BRANCH' to main 'qa'
*/
public function pullRequestCommand(FlagsParser $fs, 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
$gitlab = $this->getGitlab();
if (!$pjName = $gitlab->findProjectName()) {
$pjName = $fs->getArg('project');
}

$gitlab->loadProjectInfo($pjName);

$p = $gitlab->getCurProject();

// $brPrefix = $gitlab->getValue('branchPrefix', '');
// $fixedBrs = $gitlab->getValue('fixedBranch', []);
// 这里面的分支禁止作为源分支(source)来发起PR
$denyBrs = $gitlab->getValue('denyBranches', []);

$srcPjId = $p->getForkPid();
$tgtPjId = $p->getMainPid();

$output->info('auto fetch current branch name');
$curBranch = GitUtil::getCurrentBranchName();
$srcBranch = $fs->getOpt('source');
$tgtBranch = $fs->getOpt('target');

if ($fullSBranch = $fs->getOpt('full-source')) {
$srcBranch = $fullSBranch;
} elseif (!$srcBranch) {
$srcBranch = $curBranch;
}

$open = $fs->getOpt('open');
// if input '@', 'head', use current branch name.
if ($open) {
if ($open === '@' || strtoupper($open) === 'HEAD') {
$open = $curBranch;
} elseif ($open === '@s') {
$open = $srcBranch;
} elseif ($open === '@t') {
$open = $tgtBranch;
}
}

if (!$tgtBranch) {
if (is_string($open) && $open) {
$tgtBranch = $open;
} else {
$tgtBranch = $curBranch;
}
}

$srcBranch = $gitlab->getRealBranchName($srcBranch);
$tgtBranch = $gitlab->getRealBranchName($tgtBranch);

// deny as an source branch
if ($denyBrs && $srcBranch !== $tgtBranch && in_array($srcBranch, $denyBrs, true)) {
throw new PromptException("the branch '$srcBranch' dont allow as source-branch for PR to other branch");
}

$repo = $p->repo;
$group = $p->group;

// Is sync to remote
$isDirect = $fs->getOpt('direct');
if ($isDirect || $srcBranch === $tgtBranch) {
$group = $p->getForkGroup();
} else {
$srcPjId = $tgtPjId;
}

$prInfo = [
'source_project_id' => $srcPjId,
'source_branch' => $srcBranch,
'target_project_id' => $tgtPjId,
'target_branch' => $tgtBranch
];

$tipInfo = array_merge([
'name' => $pjName,
'glPath' => "$group/$repo",
], $prInfo);
$output->aList($tipInfo, '- project information', ['ucFirst' => false]);
$query = [
'utf8' => '',
'merge_request' => $prInfo
];

// $link = $this->config['hostUrl'];
$link = $gitlab->getHost();
$link .= "/$group/$repo/merge_requests/new?";
$link .= http_build_query($query);
// $link = UrlHelper::build($link, $query);

if ($open) {
// $output->info('will auto open link on browser');
AppHelper::openBrowser($link);
$output->success('Complete');
} else {
$output->colored("PR LINK: ");
$output->writeln(' ' . $link);
$output->colored('Complete, please open the link on browser');
}
$output->colored(" git add . && git commit && git push && kite gl pr -o head && git checkout $curBranch", 'mga');
}

/**
Expand Down
Loading

0 comments on commit d142c8a

Please sign in to comment.