From 9d4cbbdac1731a33be2e15c23e8ea4bd3b605042 Mon Sep 17 00:00:00 2001 From: caoyanyi Date: Tue, 19 Mar 2024 15:31:55 +0800 Subject: [PATCH] * Adjust mr create logic. --- lib/scm/gitea.class.php | 33 ++++++++++++++++++++++++ lib/scm/gitfox.class.php | 33 ++++++++++++++++++++++++ lib/scm/gitlab.class.php | 30 ++++++++++++++++++++++ lib/scm/gogs.class.php | 33 ++++++++++++++++++++++++ lib/scm/scm.class.php | 14 +++++++++++ module/ci/model.php | 2 +- module/mr/js/create.ui.js | 2 +- module/mr/lang/de.php | 4 +++ module/mr/lang/en.php | 4 +++ module/mr/lang/fr.php | 4 +++ module/mr/lang/zh-cn.php | 36 ++++++++++++++------------ module/mr/model.php | 53 +++++++++------------------------------ 12 files changed, 189 insertions(+), 59 deletions(-) diff --git a/lib/scm/gitea.class.php b/lib/scm/gitea.class.php index b450319d94..9a37d98248 100644 --- a/lib/scm/gitea.class.php +++ b/lib/scm/gitea.class.php @@ -792,4 +792,37 @@ class giteaRepo return $lists; } + + /** + * 通过API创建合并请求。 + * Create mr by api. + * + * @param object $MR + * @param string $openID + * @param string $assignee + * @access public + * @return object|null + */ + public function createMR(object $MR, string $openID, string $assignee): object|null + { + $MRObject = new stdclass(); + $MRObject->title = $MR->title; + $MRObject->head = $MR->sourceBranch; + $MRObject->base = $MR->targetBranch; + $MRObject->body = $MR->description; + if(!empty($assignee)) $MRObject->assignee = $assignee; + + global $app; + $url = sprintf($app->control->loadModel('gitea')->getApiRoot($MR->hostID), "/repos/{$MR->sourceProject}/pulls"); + $MR = json_decode(commonModel::http($url, $MRObject)); + if(isset($MR->number)) $MR->iid = $MR->number; + if(isset($MR->mergeable)) + { + if($MR->mergeable) $MR->merge_status = 'can_be_merged'; + if(!$MR->mergeable) $MR->merge_status = 'cannot_be_merged'; + } + if(isset($MR->state) && $MR->state == 'open') $MR->state = 'opened'; + if(isset($MR->merged) && $MR->merged) $MR->state = 'merged'; + return $MR; + } } diff --git a/lib/scm/gitfox.class.php b/lib/scm/gitfox.class.php index eb0ab16fc4..7ef66447ca 100644 --- a/lib/scm/gitfox.class.php +++ b/lib/scm/gitfox.class.php @@ -896,4 +896,37 @@ class gitfoxRepo if(isset($project->git_url)) $url->http = $project->git_url; return $url; } + + /** + * 通过API创建合并请求。 + * Create mr by api. + * + * @param object $MR + * @param string $openID + * @access public + * @return object|null + */ + public function createMR(object $MR, string $openID): object|null + { + $MRObject = new stdclass(); + $MRObject->title = $MR->title; + $MRObject->source_branch = $MR->sourceBranch; + $MRObject->target_branch = $MR->targetBranch; + $MRObject->description = $MR->description; + + global $app; + $url = "{$this->root}pullreq"; + if(!$app->user->admin) $url .= "?sudo={$openID}"; + + $MR = json_decode(commonModel::http($url, $MRObject, array(), array("Authorization: Bearer {$this->token}"), 'json')); + if(isset($MR->number)) $MR->iid = $MR->number; + if(isset($MR->merge_check_status)) + { + if(!$MR->merge_check_status) $MR->merge_status = 'can_be_merged'; + if($MR->merge_check_status) $MR->merge_status = 'cannot_be_merged'; + } + if(isset($MR->state) && $MR->state == 'open') $MR->state = 'opened'; + if(isset($MR->merged) && $MR->merged) $MR->state = 'merged'; + return $MR; + } } diff --git a/lib/scm/gitlab.class.php b/lib/scm/gitlab.class.php index c95a15a608..2196593806 100644 --- a/lib/scm/gitlab.class.php +++ b/lib/scm/gitlab.class.php @@ -994,4 +994,34 @@ class gitlabRepo $api = $this->root . $target . '?' . http_build_query($params); return $api; } + + /** + * 通过API创建合并请求。 + * Create mr by api. + * + * @param object $MR + * @param string $openID + * @param string $assignee + * @access public + * @return object|null + */ + public function createMR(object $MR, string $openID, string $assignee): object|null + { + $MRObject = new stdclass(); + $MRObject->title = $MR->title; + $MRObject->target_project_id = $MR->targetProject; + $MRObject->source_branch = $MR->sourceBranch; + $MRObject->target_branch = $MR->targetBranch; + $MRObject->description = $MR->description; + $MRObject->remove_source_branch = $MR->removeSourceBranch == '1' ? true : false; + $MRObject->squash = $MR->squash == '1' ? 1 : 0; + if(!empty($assignee)) $MRObject->assignee_ids = $assignee; + + global $app; + $url = str_replace('repository/', '', $this->root); + $url .= "merge_requests?private_token={$this->token}"; + if(!$app->user->admin) $url .= "&sudo={$openID}"; + return json_decode(commonModel::http($url, $MRObject)); + + } } diff --git a/lib/scm/gogs.class.php b/lib/scm/gogs.class.php index 4bf8a7e0ed..c9f9693eb5 100644 --- a/lib/scm/gogs.class.php +++ b/lib/scm/gogs.class.php @@ -823,4 +823,37 @@ class gogsRepo return $lists; } + + /** + * 通过API创建合并请求。 + * Create mr by api. + * + * @param object $MR + * @param string $openID + * @param string $assignee + * @access public + * @return object|null + */ + public function createMR(object $MR, string $openID, string $assignee): object|null + { + $MRObject = new stdclass(); + $MRObject->title = $MR->title; + $MRObject->head = $MR->sourceBranch; + $MRObject->base = $MR->targetBranch; + $MRObject->body = $MR->description; + if(!empty($assignee)) $MRObject->assignee = $assignee; + + global $app; + $url = sprintf($app->control->loadModel('gogs')->getApiRoot($MR->hostID), "/repos/{$MR->sourceProject}/pulls"); + $MR = json_decode(commonModel::http($url, $MRObject)); + if(isset($MR->number)) $MR->iid = $MR->number; + if(isset($MR->mergeable)) + { + if($MR->mergeable) $MR->merge_status = 'can_be_merged'; + if(!$MR->mergeable) $MR->merge_status = 'cannot_be_merged'; + } + if(isset($MR->state) && $MR->state == 'open') $MR->state = 'opened'; + if(isset($MR->merged) && $MR->merged) $MR->state = 'merged'; + return $MR; + } } diff --git a/lib/scm/scm.class.php b/lib/scm/scm.class.php index acd0cb2e44..611715da45 100644 --- a/lib/scm/scm.class.php +++ b/lib/scm/scm.class.php @@ -311,6 +311,20 @@ class scm { return $this->engine->getFilesByCommit($revision); } + + /** + * Create mr by api. + * + * @param object $MR + * @param string $openID + * @param string $assignee + * @access public + * @return null|object + */ + public function createMR($MR, $openID = '', $assignee = '') + { + return $this->engine->createMR($MR, $openID, $assignee); + } } /** diff --git a/module/ci/model.php b/module/ci/model.php index a658891466..735f88cb54 100644 --- a/module/ci/model.php +++ b/module/ci/model.php @@ -294,7 +294,7 @@ class ciModel extends model } elseif(isset($relateMR->synced) && $relateMR->synced == '0') { - $rawMR = $this->loadModel('mr')->apiCreateMR($relateMR->hostID, $relateMR->sourceProject, $relateMR); + $rawMR = $this->loadModel('mr')->apiCreateMR($relateMR->hostID, $relateMR); if(!empty($rawMR->iid)) { $newMR = new stdclass(); diff --git a/module/mr/js/create.ui.js b/module/mr/js/create.ui.js index fe5db276cd..aad30b44e2 100644 --- a/module/mr/js/create.ui.js +++ b/module/mr/js/create.ui.js @@ -47,7 +47,7 @@ function onProjectChange() }, }); - getBranchPriv(projectID); + if(repo.SCM != 'GitFox') getBranchPriv(projectID); } function onSourceProjectChange() diff --git a/module/mr/lang/de.php b/module/mr/lang/de.php index 1a7d12252b..995430837d 100644 --- a/module/mr/lang/de.php +++ b/module/mr/lang/de.php @@ -129,6 +129,8 @@ $lang->mr->apiErrorMap[6] = "Invalid PullRequest: There are no changes between t $lang->mr->apiErrorMap[7] = "/(user doesn't have access to repo).*/"; $lang->mr->apiErrorMap[8] = "/(git apply).*/"; + + $lang->mr->errorLang[1] = 'The source project branch cannot be the same as the target project branch'; $lang->mr->errorLang[2] = 'Another open merge request already exists for this source branch: ID%u'; $lang->mr->errorLang[3] = "Unauthorized"; @@ -138,6 +140,8 @@ $lang->mr->errorLang[6] = 'The source project branch cannot be the same as the t $lang->mr->errorLang[7] = "user doesn't have access to repo"; $lang->mr->errorLang[8] = 'The source branch and target branch cannot be merged'; + + $lang->mr->from = "from"; $lang->mr->to = "to"; $lang->mr->at = "at"; diff --git a/module/mr/lang/en.php b/module/mr/lang/en.php index 1a7d12252b..995430837d 100644 --- a/module/mr/lang/en.php +++ b/module/mr/lang/en.php @@ -129,6 +129,8 @@ $lang->mr->apiErrorMap[6] = "Invalid PullRequest: There are no changes between t $lang->mr->apiErrorMap[7] = "/(user doesn't have access to repo).*/"; $lang->mr->apiErrorMap[8] = "/(git apply).*/"; + + $lang->mr->errorLang[1] = 'The source project branch cannot be the same as the target project branch'; $lang->mr->errorLang[2] = 'Another open merge request already exists for this source branch: ID%u'; $lang->mr->errorLang[3] = "Unauthorized"; @@ -138,6 +140,8 @@ $lang->mr->errorLang[6] = 'The source project branch cannot be the same as the t $lang->mr->errorLang[7] = "user doesn't have access to repo"; $lang->mr->errorLang[8] = 'The source branch and target branch cannot be merged'; + + $lang->mr->from = "from"; $lang->mr->to = "to"; $lang->mr->at = "at"; diff --git a/module/mr/lang/fr.php b/module/mr/lang/fr.php index 6cc2611d71..26bf46f789 100644 --- a/module/mr/lang/fr.php +++ b/module/mr/lang/fr.php @@ -129,6 +129,8 @@ $lang->mr->apiErrorMap[6] = "Invalid PullRequest: There are no changes between t $lang->mr->apiErrorMap[7] = "/(user doesn't have access to repo).*/"; $lang->mr->apiErrorMap[8] = "/(git apply).*/"; + + $lang->mr->errorLang[1] = 'The source project branch cannot be the same as the target project branch'; $lang->mr->errorLang[2] = 'Another open merge request already exists for this source branch: ID%u'; $lang->mr->errorLang[3] = "Unauthorized"; @@ -138,6 +140,8 @@ $lang->mr->errorLang[6] = 'The source project branch cannot be the same as the t $lang->mr->errorLang[7] = "user doesn't have access to repo"; $lang->mr->errorLang[8] = 'The source branch and target branch cannot be merged'; + + $lang->mr->from = "from"; $lang->mr->to = "to"; $lang->mr->at = "at"; diff --git a/module/mr/lang/zh-cn.php b/module/mr/lang/zh-cn.php index 3609b2d6e7..1538023536 100644 --- a/module/mr/lang/zh-cn.php +++ b/module/mr/lang/zh-cn.php @@ -120,23 +120,27 @@ $lang->mr->accessGitlabFailed = "当前无法连接到GitLab服务器。"; $lang->mr->reopenSuccess = "已重新打开合并请求。"; $lang->mr->closeSuccess = "已关闭合并请求。"; -$lang->mr->apiErrorMap[1] = "You can't use same project/branch for source and target"; -$lang->mr->apiErrorMap[2] = "/Another open merge request already exists for this source branch: !([0-9]+)/"; -$lang->mr->apiErrorMap[3] = "401 Unauthorized"; -$lang->mr->apiErrorMap[4] = "403 Forbidden"; -$lang->mr->apiErrorMap[5] = "/(pull request already exists for these targets).*/"; -$lang->mr->apiErrorMap[6] = "Invalid PullRequest: There are no changes between the head and the base"; -$lang->mr->apiErrorMap[7] = "/(user doesn't have access to repo).*/"; -$lang->mr->apiErrorMap[8] = "/(git apply).*/"; +$lang->mr->apiErrorMap[1] = "You can't use same project/branch for source and target"; +$lang->mr->apiErrorMap[2] = "/Another open merge request already exists for this source branch: !([0-9]+)/"; +$lang->mr->apiErrorMap[3] = "401 Unauthorized"; +$lang->mr->apiErrorMap[4] = "403 Forbidden"; +$lang->mr->apiErrorMap[5] = "/(pull request already exists for these targets).*/"; +$lang->mr->apiErrorMap[6] = "Invalid PullRequest: There are no changes between the head and the base"; +$lang->mr->apiErrorMap[7] = "/(user doesn't have access to repo).*/"; +$lang->mr->apiErrorMap[8] = "/(git apply).*/"; +$lang->mr->apiErrorMap[9] = "a pull request for this target and source branch already exists"; +$lang->mr->apiErrorMap[10] = 'Internal error occurred'; -$lang->mr->errorLang[1] = '源项目分支与目标项目分支不能相同'; -$lang->mr->errorLang[2] = '存在另外一个同样的合并请求在源项目分支中: ID%u'; -$lang->mr->errorLang[3] = '权限不足'; -$lang->mr->errorLang[4] = '权限不足'; -$lang->mr->errorLang[5] = '存在另外一个同样的合并请求在源项目分支中'; -$lang->mr->errorLang[6] = '源项目分支与目标项目分支不能相同'; -$lang->mr->errorLang[7] = '您无权合并改版本库'; -$lang->mr->errorLang[8] = '当前源分支和目标分支无法合并'; +$lang->mr->errorLang[1] = '源项目分支与目标项目分支不能相同'; +$lang->mr->errorLang[2] = '存在另外一个同样的合并请求在源项目分支中: ID%u'; +$lang->mr->errorLang[3] = '权限不足'; +$lang->mr->errorLang[4] = '权限不足'; +$lang->mr->errorLang[5] = '存在另外一个同样的合并请求在源项目分支中'; +$lang->mr->errorLang[6] = '源项目分支与目标项目分支不能相同'; +$lang->mr->errorLang[7] = '您无权合并改版本库'; +$lang->mr->errorLang[8] = '当前源分支和目标分支无法合并'; +$lang->mr->errorLang[9] = '已存在相同的合并请求'; +$lang->mr->errorLang[10] = '服务器错误'; $lang->mr->from = "从"; $lang->mr->to = "合并到"; diff --git a/module/mr/model.php b/module/mr/model.php index aa76da7bdb..e19a4a618d 100644 --- a/module/mr/model.php +++ b/module/mr/model.php @@ -215,7 +215,7 @@ class mrModel extends model $MRID = $this->dao->lastInsertID(); $this->loadModel('action')->create('mr', $MRID, 'opened'); - $rawMR = $this->apiCreateMR($MR->hostID, $MR->sourceProject, $MR); + $rawMR = $this->apiCreateMR($MR->hostID, $MR); /** * Another open merge request already exists for this source branch. @@ -521,53 +521,24 @@ class mrModel extends model * Create MR by API. * * @param int $hostID - * @param string $projectID * @param object $MR * @access public * @return object|null */ - public function apiCreateMR(int $hostID, string $projectID, object $MR): object|null + public function apiCreateMR(int $hostID, object $MR): object|null { - $host = $this->loadModel('pipeline')->getByID($hostID); - if(!$host) return null; + $repo = $this->loadModel('repo')->getByID($MR->repoID); + if(!$repo) return null; - if($MR->assignee) $assignee = $this->pipeline->getOpenIdByAccount($hostID, $host->type, $MR->assignee); + $openID = $this->loadModel('pipeline')->getOpenIdByAccount($hostID, strtolower($repo->SCM), $this->app->user->account); + $assignee = ''; + if($MR->assignee) $assignee = $this->pipeline->getOpenIdByAccount($hostID, strtolower($repo->SCM), $MR->assignee); - $MRObject = new stdclass(); - $MRObject->title = $MR->title; - if($host->type == 'gitlab') - { - $url = sprintf($this->loadModel('gitlab')->getApiRoot($hostID), "/projects/$projectID/merge_requests"); - - $MRObject->target_project_id = $MR->targetProject; - $MRObject->source_branch = $MR->sourceBranch; - $MRObject->target_branch = $MR->targetBranch; - $MRObject->description = $MR->description; - $MRObject->remove_source_branch = $MR->removeSourceBranch == '1' ? true : false; - $MRObject->squash = $MR->squash == '1' ? 1 : 0; - if(!empty($assignee)) $MRObject->assignee_ids = $assignee; - return json_decode(commonModel::http($url, $MRObject)); - } - elseif(in_array($host->type, array('gitea', 'gogs'))) - { - $url = sprintf($this->loadModel($host->type)->getApiRoot($hostID), "/repos/$projectID/pulls"); - - $MRObject->head = $MR->sourceBranch; - $MRObject->base = $MR->targetBranch; - $MRObject->body = $MR->description; - if(!empty($assignee)) $MRObject->assignee = $assignee; - - $mergeResult = json_decode(commonModel::http($url, $MRObject)); - if(isset($mergeResult->number)) $mergeResult->iid = $mergeResult->number; - if(isset($mergeResult->mergeable)) - { - if($mergeResult->mergeable) $mergeResult->merge_status = 'can_be_merged'; - if(!$mergeResult->mergeable) $mergeResult->merge_status = 'cannot_be_merged'; - } - if(isset($mergeResult->state) && $mergeResult->state == 'open') $mergeResult->state = 'opened'; - if(isset($mergeResult->merged) && $mergeResult->merged) $mergeResult->state = 'merged'; - return $mergeResult; - } + $scm = $this->app->loadClass('scm'); + $scm->setEngine($repo); + $result = $scm->createMR($MR, $assignee, $openID); + if(!empty($result->message)) $result->message = $this->convertApiError($result->message); + return $result; } /**