* Adjust mr create logic.

This commit is contained in:
caoyanyi
2024-03-19 15:31:55 +08:00
parent 1ad644b1ee
commit 9d4cbbdac1
12 changed files with 189 additions and 59 deletions
+33
View File
@@ -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;
}
}
+33
View File
@@ -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;
}
}
+30
View File
@@ -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));
}
}
+33
View File
@@ -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;
}
}
+14
View File
@@ -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);
}
}
/**
+1 -1
View File
@@ -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();
+1 -1
View File
@@ -47,7 +47,7 @@ function onProjectChange()
},
});
getBranchPriv(projectID);
if(repo.SCM != 'GitFox') getBranchPriv(projectID);
}
function onSourceProjectChange()
+4
View File
@@ -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";
+4
View File
@@ -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";
+4
View File
@@ -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";
+20 -16
View File
@@ -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 = "合并到";
+12 -41
View File
@@ -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;
}
/**