From d142c8a769875d3efd91d21281de0e0f6e1b0f0c Mon Sep 17 00:00:00 2001 From: Inhere Date: Tue, 14 Jun 2022 11:20:16 +0800 Subject: [PATCH] :recycle: refactor: migrate the gitlab pr command to sub-command MergeRequestCmd --- app/Console/Attach/Gitlab/MergeRequestCmd.php | 145 +++++++++++++++++- .../Controller/Gitx/GitLabController.php | 144 +---------------- app/Helper/AppHelper.php | 20 +-- 3 files changed, 159 insertions(+), 150 deletions(-) diff --git a/app/Console/Attach/Gitlab/MergeRequestCmd.php b/app/Console/Attach/Gitlab/MergeRequestCmd.php index 2e21162..0a0c130 100644 --- a/app/Console/Attach/Gitlab/MergeRequestCmd.php +++ b/app/Console/Attach/Gitlab/MergeRequestCmd.php @@ -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 @@ -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 @@ -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; } } diff --git a/app/Console/Controller/Gitx/GitLabController.php b/app/Console/Controller/Gitx/GitLabController.php index 3beb9d1..c7aba9a 100644 --- a/app/Console/Controller/Gitx/GitLabController.php +++ b/app/Console/Controller/Gitx/GitLabController.php @@ -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; /** @@ -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', @@ -82,6 +78,7 @@ protected static function commandAliases(): array 'project' => ['pj', 'info'], 'checkout' => ['co'], 'branch' => BranchCmd::aliases(), + MergeRequestCmd::getName() => MergeRequestCmd::aliases(), ]; } @@ -93,6 +90,7 @@ protected function subCommands(): array return [ BranchCmd::class, ProjectCmd::class, + MergeRequestCmd::class, ]; } @@ -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'); } /** diff --git a/app/Helper/AppHelper.php b/app/Helper/AppHelper.php index 960577c..8c258dc 100644 --- a/app/Helper/AppHelper.php +++ b/app/Helper/AppHelper.php @@ -5,12 +5,9 @@ use ArrayAccess; use Closure; use Inhere\Console\Util\Show; -use Inhere\Kite\Console\Component\Clipboard; +use Inhere\Kite\Common\GitLocal\GitLab; use Inhere\Kite\Console\Component\ContentsAutoReader; use Inhere\Kite\Kite; -use Toolkit\Cli\Cli; -use Toolkit\FsUtil\File; -use Toolkit\Stdlib\OS; use Toolkit\Sys\Sys; use function array_filter; use function array_shift; @@ -21,16 +18,11 @@ use function getenv; use function implode; use function is_array; -use function is_file; use function is_object; use function random_int; use function strlen; use function strpos; -use function substr; -use function trim; -use function vdump; use const IN_PHAR; -use const STDIN; /** * Class AppHelper @@ -263,4 +255,14 @@ public static function json5lineParser(): Closure return $values; }; } + + /** + * @return GitLab + */ + public static function newGitlab(): GitLab + { + $settings = Kite::config()->getArray('gitlab'); + + return GitLab::new(Kite::cliApp()->getOutput(), $settings); + } }