diff --git a/module/ci/model.php b/module/ci/model.php index 5e284c4abe..e9b06fd86c 100644 --- a/module/ci/model.php +++ b/module/ci/model.php @@ -29,7 +29,7 @@ class ciModel extends model if($this->session->repoID) { $repo = $this->loadModel('repo')->getRepoByID($this->session->repoID); - if($repo->SCM != 'Gitlab') unset($this->lang->devops->menu->mr); + if(!in_array(strtolower($repo->SCM), $this->config->mr->gitServiceList)) unset($this->lang->devops->menu->mr); $this->lang->switcherMenu = $this->loadModel('repo')->getSwitcher($this->session->repoID); } diff --git a/module/common/model.php b/module/common/model.php index bb85ca6c40..4dc18451b5 100644 --- a/module/common/model.php +++ b/module/common/model.php @@ -3084,11 +3084,13 @@ EOD; * @param string|array $data * @param array $options This is option and value pair, like CURLOPT_HEADER => true. Use curl_setopt function to set options. * @param array $headers Set request headers. + * @param string $dataType + * @param string $method POST|PATCH|PUT * @static * @access public * @return string */ - public static function http($url, $data = null, $options = array(), $headers = array(), $dataType = 'data') + public static function http($url, $data = null, $options = array(), $headers = array(), $dataType = 'data', $method = 'POST') { global $lang, $app; if(!extension_loaded('curl')) @@ -3125,7 +3127,8 @@ EOD; if(!empty($data)) { if(is_object($data)) $data = (array) $data; - curl_setopt($curl, CURLOPT_POST, true); + if($method == 'POST') curl_setopt($curl, CURLOPT_POST, true); + if(in_array($method, array('PATCH', 'PUT'))) curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } diff --git a/module/gitea/control.php b/module/gitea/control.php index be079cb338..cd18b89de5 100644 --- a/module/gitea/control.php +++ b/module/gitea/control.php @@ -210,6 +210,7 @@ class gitea extends control { if(!$giteaID or !$project) return $this->send(array('message' => array())); + $project = urldecode(base64_decode($project)); $branches = $this->gitea->apiGetBranches($giteaID, $project); $options = ""; foreach($branches as $branch) diff --git a/module/mr/control.php b/module/mr/control.php index 757db431c9..1a1ecd3c94 100644 --- a/module/mr/control.php +++ b/module/mr/control.php @@ -40,6 +40,7 @@ class mr extends control $repoID = $this->repo->saveState($repoID, $objectID); $repo = $this->repo->getRepoByID($repoID); if(!in_array(strtolower($repo->SCM), $this->config->mr->gitServiceList)) $repo = $repos[0]; + if($repo->SCM != 'Gitlab') unset($this->lang->mr->statusList['merged']); $this->loadModel('ci')->setMenu($repo->id); $projects = $this->mr->getAllProjects($repoID, $repo->SCM); @@ -49,7 +50,7 @@ class mr extends control $this->session->set('mrList', $this->app->getURI(true), 'repo'); /* Sync GitLab MR to ZenTao Database. */ - $MRList = $this->mr->batchSyncMR($MRList); + $MRList = $this->mr->batchSyncMR($MRList, $repo->SCM); /* Check whether Mr is linked with the product. */ foreach($MRList as $MR) @@ -217,7 +218,7 @@ class mr extends control $res = $this->mr->apiDeleteMR($MR->hostID, $MR->targetProject, $MR->mriid); if(isset($res->message)) return print(js::alert($this->mr->convertApiError($res->message))); } - //$this->dao->delete()->from(TABLE_MR)->where('id')->eq($id)->exec(); + $this->dao->delete()->from(TABLE_MR)->where('id')->eq($id)->exec(); echo js::reload('parent'); } @@ -903,8 +904,9 @@ class mr extends control { $this->loadModel($scm); + if($scm != 'gitlab') $projectID = urldecode(base64_decode($projectID)); /* First step: get forks. Only get first level forks(not recursively). */ - $projects = $this->$scm->apiGetForks($hostID, $projectID); + $projects = $scm == 'gitlab' ? $this->$scm->apiGetForks($hostID, $projectID) : array(); /* Second step: get project itself. */ $projects[] = $this->$scm->apiGetSingleProject($hostID, $projectID); @@ -957,8 +959,10 @@ class mr extends control */ public function ajaxGetRepoList($hostID, $projectID) { - $this->loadModel('repo'); - $repoList = $this->repo->getRepoListByClient($hostID, $projectID); + $host = $this->loadModel('pipeline')->getByID($hostID); + if($host->type != 'gitlab') $projectID =urldecode(base64_decode($projectID)); + + $repoList = $this->loadModel('repo')->getRepoListByClient($hostID, $projectID); if(!$repoList) return $this->send(array('message' => array())); $options = ""; diff --git a/module/mr/js/create.js b/module/mr/js/create.js index 584caad186..0a15f093f5 100644 --- a/module/mr/js/create.js +++ b/module/mr/js/create.js @@ -1,3 +1,18 @@ +/** + * Urlencode param. + * + * @param param $param + * @access public + * @return string + */ +function urlencode(param) +{ + var hostID = $('#hostID').val(); + if(hosts[hostID].type != 'gitlab') return Base64.encode(encodeURIComponent(param)); + + return param; +} + $(function() { $('#hostID').change(function() @@ -22,8 +37,8 @@ $(function() $('#sourceProject,#targetProject').change(function() { - var hostID = $('#hostID').val(); - var sourceProject = $(this).val(); + var hostID = $('#hostID').val(); + var sourceProject = urlencode($(this).val()); var branchSelect = $(this).parents('td').find('select[name*=Branch]'); var branchUrl = createLink(hosts[hostID].type, 'ajaxGetProjectBranches', "hostID=" + hostID + "&projectID=" + sourceProject); $.get(branchUrl, function(response) @@ -36,8 +51,8 @@ $(function() $('#sourceProject').change(function() { - var hostID = $('#hostID').val(); - var sourceProject = $(this).val(); + var hostID = $('#hostID').val(); + var sourceProject = urlencode($(this).val()); var projectUrl = createLink('mr', 'ajaxGetMRTargetProjects', "hostID=" + hostID + "&projectID=" + sourceProject + "&scm=" + hosts[hostID].type); $.get(projectUrl, function(response) { @@ -55,10 +70,10 @@ $(function() $('#sourceBranch,#targetBranch').change(function() { - var sourceProject = $('#sourceProject').val(); - var sourceBranch = $('#sourceBranch').val(); - var targetProject = $('#targetProject').val(); - var targetBranch = $('#targetBranch').val(); + var sourceProject = urlencode($('#sourceProject').val()); + var sourceBranch = urlencode($('#sourceBranch').val()); + var targetProject = urlencode($('#targetProject').val()); + var targetBranch = urlencode($('#targetBranch').val()); if(!sourceProject || !sourceBranch || !targetProject || !targetBranch) return false; var $this = $(this); diff --git a/module/mr/model.php b/module/mr/model.php index f012ff8c39..2864687159 100644 --- a/module/mr/model.php +++ b/module/mr/model.php @@ -138,7 +138,7 @@ class mrModel extends model $allProjects = array(); $allGroups = array(); $gitlabUsers = $this->loadModel('gitlab')->getGitLabListByAccount(); - if(!$this->app->user->admin and !isset($gitlabUsers[$hostID])) continue; + if(!$this->app->user->admin and !isset($gitlabUsers[$hostID])) return array(); $minProject = $maxProject = 0; /* Mysql string to int. */ @@ -219,7 +219,7 @@ class mrModel extends model $MRID = $this->dao->lastInsertId(); $this->loadModel('action')->create('mr', $MRID, 'opened'); - $rawMR = $this->apiCreateMR($this->post->hostID, $this->post->sourceProject, $MR); + $rawMR = $this->apiCreateMR($this->post->hostID, $this->post->sourceProject, $MR); /** * Another open merge request already exists for this source branch. @@ -465,18 +465,21 @@ class mrModel extends model * Batch Sync GitLab MR Database. * * @param object $MRList + * @param string $scm * @access public * @return array */ - public function batchSyncMR($MRList) + public function batchSyncMR($MRList, $scm = 'Gitlab') { if(empty($MRList)) return array(); + $this->loadModel('gitlab'); + $this->loadModel('gitea'); foreach($MRList as $key => $MR) { if($MR->status != 'opened') continue; - if(!isset($rawMRList[$MR->hostID][$MR->targetProject])) $rawMRList[$MR->hostID][$MR->targetProject] = $this->apiGetMRList($MR->hostID, $MR->targetProject); + if(!isset($rawMRList[$MR->hostID][$MR->targetProject])) $rawMRList[$MR->hostID][$MR->targetProject] = $this->apiGetMRList($MR->hostID, $MR->targetProject, $scm); $rawMR = new stdClass(); foreach($rawMRList[$MR->hostID][$MR->targetProject] as $projcetRawMR) { @@ -490,10 +493,17 @@ class mrModel extends model if(isset($rawMR->iid)) { /* create gitlab mr todo to zentao todo */ - $this->batchSyncTodo($MR->hostID, $MR->targetProject); + if($scm == 'Gitlab') $this->batchSyncTodo($MR->hostID, $MR->targetProject); - $map = $this->config->mr->maps->sync; - $gitlabUsers = $this->gitlab->getUserIdAccountPairs($MR->hostID); + $map = $this->config->mr->maps->sync; + if($scm == 'Gitlab') + { + $users = $this->gitlab->getUserIdAccountPairs($MR->hostID); + } + else + { + $users = $this->gitea->getUserAccountIdPairs($MR->hostID, 'openID,account'); + } $newMR = new stdclass; @@ -511,7 +521,7 @@ class mrModel extends model $values = $rawMR->$field; if(isset($values[0])) $gitlabUserID = $values[0]->$options; } - $value = zget($gitlabUsers, $gitlabUserID, ''); + $value = zget($users, $gitlabUserID, ''); } if($value) $newMR->$syncField = $value; @@ -654,8 +664,8 @@ class mrModel extends model { $url = sprintf($this->loadModel('gitea')->getApiRoot($hostID), "/repos/$projectID/pulls"); - $MRObject->base = $MR->sourceBranch; - $MRObject->head = $MR->targetBranch; + $MRObject->head = $MR->sourceBranch; + $MRObject->base = $MR->targetBranch; $MRObject->body = $MR->description; if($MR->assignee) { @@ -684,12 +694,33 @@ class mrModel extends model * @access public * @return object */ - public function apiGetMRList($hostID, $projectID) + public function apiGetMRList($hostID, $projectID, $scm = 'Gitlab') { - $url = sprintf($this->gitlab->getApiRoot($hostID), "/projects/$projectID/merge_requests"); + if($scm == 'Gitlab') + { + $url = sprintf($this->loadModel('gitlab')->getApiRoot($hostID), "/projects/$projectID/merge_requests"); + } + else + { + $url = sprintf($this->loadModel('gitea')->getApiRoot($hostID), "/repos/$projectID/pulls"); + } $response = json_decode(commonModel::http($url)); if(empty($response)) $response = array(); + if($scm == 'Gitea') + { + foreach($response as $mr) + { + $mr->iid = $mr->number; + $mr->state = $mr->state == 'open' ? 'opened' : $mr->state; + $mr->merge_status = $mr->mergeable ? 'can_be_merged' : 'cannot_be_merged'; + $mr->description = $mr->body; + $mr->target_branch = $mr->base->ref; + $mr->source_branch = $mr->head->ref; + $mr->source_project_id = $projectID; + $mr->target_project_id = $projectID; + } + } return $response; } @@ -792,8 +823,8 @@ class mrModel extends model } else { - $url = sprintf($this->loadModel('gitea')->getApiRoot($hostID), "/repos/$projectID/pulls/$MRID"); - return json_decode(commonModel::http($url, array('state' => 'closed'), array(CURLOPT_CUSTOMREQUEST => 'PATCH'))); + $url = sprintf($this->loadModel('gitea')->getApiRoot($hostID), "/repos/$projectID/pulls/$MRID"); + return json_decode(commonModel::http($url, array('state' => 'closed'), array(), array(), 'json', 'PATCH')); } } diff --git a/module/mr/view/browse.html.php b/module/mr/view/browse.html.php index 08b725d297..ef6e4a9de4 100644 --- a/module/mr/view/browse.html.php +++ b/module/mr/view/browse.html.php @@ -58,8 +58,18 @@ - hostID][$MR->sourceProject]) ? $projects[$MR->hostID][$MR->sourceProject]->name_with_namespace . ':' . $MR->sourceBranch : $MR->sourceProject . ':' . $MR->sourceBranch; ?> - hostID][$MR->targetProject]) ? $projects[$MR->hostID][$MR->targetProject]->name_with_namespace . ':' . $MR->targetBranch : $MR->targetProject . ':' . $MR->targetBranch; ?> + SCM == 'Gitlab') + { + $sourceProject = isset($projects[$MR->hostID][$MR->sourceProject]) ? $projects[$MR->hostID][$MR->sourceProject]->name_with_namespace . ':' . $MR->sourceBranch : $MR->sourceProject . ':' . $MR->sourceBranch; + $targetProject = isset($projects[$MR->hostID][$MR->targetProject]) ? $projects[$MR->hostID][$MR->targetProject]->name_with_namespace . ':' . $MR->targetBranch : $MR->targetProject . ':' . $MR->targetBranch; + } + else + { + $sourceProject = isset($projects[$MR->hostID][$MR->sourceProject]) ? $projects[$MR->hostID][$MR->sourceProject] . ':' . $MR->sourceBranch : $MR->sourceProject . ':' . $MR->sourceBranch; + $targetProject = isset($projects[$MR->hostID][$MR->targetProject]) ? $projects[$MR->hostID][$MR->targetProject] . ':' . $MR->targetBranch : $MR->targetProject . ':' . $MR->targetBranch; + } + ?> id;?> id}"), $MR->title);?> diff --git a/module/mr/view/create.html.php b/module/mr/view/create.html.php index 6293defe67..9175d19c17 100644 --- a/module/mr/view/create.html.php +++ b/module/mr/view/create.html.php @@ -10,6 +10,7 @@ */ ?> +