* Modify edit merge request for gitea.

This commit is contained in:
caoyanyi
2022-07-19 14:00:59 +08:00
parent 8163199cf1
commit b3a83fc407
5 changed files with 130 additions and 45 deletions
+36
View File
@@ -506,4 +506,40 @@ class giteaModel extends model
if(isset($currentProject->parent->full_name)) return $currentProject->parent->full_name;
return array();
}
/**
* Get branches.
*
* @param int $giteaID
* @param string $project
* @access public
* @return array
*/
public function getBranches($giteaID, $project)
{
$rawBranches = $this->apiGetBranches($giteaID, $project);
$branches = array();
foreach($rawBranches as $branch) $branches[] = $branch->name;
return $branches;
}
/**
* Get gitea user id and realname pairs of one gitea.
*
* @param int $giteaID
* @access public
* @return array
*/
public function getUserIdRealnamePairs($giteaID)
{
return $this->dao->select('oauth.openID as openID,user.realname as realname')
->from(TABLE_OAUTH)->alias('oauth')
->leftJoin(TABLE_USER)->alias('user')
->on("oauth.account = user.account")
->where('providerType')->eq('gitea')
->andWhere('providerID')->eq($giteaID)
->fetchPairs();
}
}
+23 -18
View File
@@ -63,7 +63,17 @@ class mr extends control
$this->app->loadLang('compile');
$openIDList = array();
if(!$this->app->user->admin) $openIDList = $this->loadModel('gitlab')->getGitLabListByAccount($this->app->user->account);
if(!$this->app->user->admin)
{
if($repo->SCM == 'Gitlab')
{
$openIDList = $this->loadModel('gitlab')->getGitLabListByAccount($this->app->user->account);
}
else
{
$openIDList = $this->loadModel('gitea')->getGiteaListByAccount($this->app->user->account);
}
}
$this->view->title = $this->lang->mr->common . $this->lang->colon . $this->lang->mr->browse;
$this->view->MRList = $MRList;
@@ -143,32 +153,26 @@ class mr extends control
$this->view->rawMR = isset($rawMR) ? $rawMR : false;
if(!isset($rawMR->id) or (isset($rawMR->message) and $rawMR->message == '404 Not found') or empty($rawMR)) return $this->display();
$branchList = $this->loadModel('gitlab')->getBranches($MR->hostID, $MR->targetProject);
$host = $this->loadModel('pipeline')->getByID($MR->hostID);
$scm = $host->type;
$branchList = $this->loadModel($scm)->getBranches($MR->hostID, $MR->targetProject);
$targetBranchList = array();
foreach($branchList as $branch) $targetBranchList[$branch] = $branch;
/* Fetch user list both in Zentao and current GitLab project. */
$bindedUsers = $this->gitlab->getUserIdRealnamePairs($MR->hostID);
$rawProjectUsers = $this->gitlab->apiGetProjectUsers($MR->hostID, $MR->targetProject);
$users = array();
foreach($rawProjectUsers as $rawProjectUser)
{
if(!empty($bindedUsers[$rawProjectUser->id])) $users[$rawProjectUser->id] = $bindedUsers[$rawProjectUser->id];
}
$gitlabUsers = $this->gitlab->getUserAccountIdPairs($MR->hostID);
$bindedUsers = $this->$scm->getUserIdRealnamePairs($MR->hostID);
$gitUsers = $this->$scm->getUserAccountIdPairs($MR->hostID);
/* Check permissions. */
if(!$this->app->user->admin)
if(!$this->app->user->admin and $scm == 'gitlab')
{
$groupIDList = array(0 => 0);
$groups = $this->gitlab->apiGetGroups($MR->hostID, 'name_asc', 'developer');
$groups = $this->scm->apiGetGroups($MR->hostID, 'name_asc', 'developer');
foreach($groups as $group) $groupIDList[] = $group->id;
$sourceProject = $this->gitlab->apiGetSingleProject($MR->hostID, $MR->sourceProject);
$isDeveloper = $this->gitlab->checkUserAccess($MR->hostID, 0, $sourceProject, $groupIDList, 'developer');
$sourceProject = $this->scm->apiGetSingleProject($MR->hostID, $MR->sourceProject);
$isDeveloper = $this->scm->checkUserAccess($MR->hostID, 0, $sourceProject, $groupIDList, 'developer');
if(!isset($gitlabUsers[$this->app->user->account]) or !$isDeveloper) return print(js::alert($this->lang->mr->errorLang[3]) . js::locate($this->createLink('mr', 'browse')));
if(!isset($gitUsers[$this->app->user->account]) or !$isDeveloper) return print(js::alert($this->lang->mr->errorLang[3]) . js::locate($this->createLink('mr', 'browse')));
}
/* Import lang for required modules. */
@@ -192,10 +196,11 @@ class mr extends control
$this->view->title = $this->lang->mr->edit;
$this->view->MR = $MR;
$this->view->host = $host;
$this->view->targetBranchList = $targetBranchList;
$this->view->users = $this->loadModel('user')->getPairs('noletter|noclosed');
$this->view->assignee = $MR->assignee;
$this->view->reviewer = zget($gitlabUsers, $MR->reviewer, '');
$this->view->reviewer = zget($gitUsers, $MR->reviewer, '');
$this->display();
}
+56 -19
View File
@@ -123,7 +123,7 @@ class mrModel extends model
public function getGiteaProjects($hostID = 0)
{
$projects = $this->loadModel('gitea')->apiGetProjects($hostID);
return array($hostID => array_column($projects, 'full_name', 'full_name'));
return array($hostID => array_column($projects, null, 'full_name'));
}
/**
@@ -365,6 +365,7 @@ class mrModel extends model
->get();
$oldMR = $this->getByID($MRID);
if($oldMR->sourceProject == $oldMR->targetProject and $oldMR->sourceBranch == $MR->targetBranch) dao::$errors['targetBranch'] = $this->lang->mr->errorLang[1];
$this->dao->update(TABLE_MR)->data($MR)->checkIF($MR->needCI, 'jobID', 'notempty');
if(dao::isError()) return array('result' => 'fail', 'message' => dao::getError());
@@ -381,21 +382,8 @@ class mrModel extends model
}
}
/* Update MR in GitLab. */
$newMR = new stdclass;
$newMR->title = $MR->title;
$newMR->description = $MR->description;
$newMR->target_branch = $MR->targetBranch;
$newMR->remove_source_branch = $MR->removeSourceBranch == '1' ? true : false;
$newMR->squash = $MR->squash == '1' ? 1 : 0;
if($MR->assignee)
{
$gitlabAssignee = $this->gitlab->getUserIDByZentaoAccount($oldMR->hostID, $MR->assignee);
if($gitlabAssignee) $newMR->assignee_ids = $gitlabAssignee;
}
/* Known issue: `reviewer_ids` takes no effect. */
$rawMR = $this->apiUpdateMR($oldMR->hostID, $oldMR->targetProject, $oldMR->mriid, $newMR);
$rawMR = $this->apiUpdateMR($oldMR->hostID, $oldMR->targetProject, $oldMR->mriid, $MR);
if(!isset($rawMR->id) and isset($rawMR->message))
{
$errorMessage = $this->convertApiError($rawMR->message);
@@ -766,8 +754,19 @@ class mrModel extends model
*/
public function apiGetSingleMR($hostID, $projectID, $MRID)
{
$url = sprintf($this->gitlab->getApiRoot($hostID), "/projects/$projectID/merge_requests/$MRID");
return json_decode(commonModel::http($url));
$host = $this->loadModel('pipeline')->getByID($hostID);
if($host->type == 'gitlab')
{
$url = sprintf($this->gitlab->getApiRoot($hostID), "/projects/$projectID/merge_requests/$MRID");
return json_decode(commonModel::http($url));
}
else
{
$url = sprintf($this->loadModel('gitea')->getApiRoot($hostID), "/repos/$projectID/pulls/$MRID");
$MR = json_decode(commonModel::http($url));
return $MR;
}
}
/**
@@ -799,8 +798,45 @@ class mrModel extends model
*/
public function apiUpdateMR($hostID, $projectID, $MRID, $MR)
{
$url = sprintf($this->gitlab->getApiRoot($hostID), "/projects/$projectID/merge_requests/$MRID");
return json_decode(commonModel::http($url, $MR, $options = array(CURLOPT_CUSTOMREQUEST => 'PUT')));
$host = $this->loadModel('pipeline')->getByID($hostID);
$newMR = new stdclass;
$newMR->title = $MR->title;
if($host->type == 'gitlab')
{
$newMR->description = $MR->description;
$newMR->target_branch = $MR->targetBranch;
$newMR->remove_source_branch = $MR->removeSourceBranch == '1' ? true : false;
$newMR->squash = $MR->squash == '1' ? 1 : 0;
if($MR->assignee)
{
$gitlabAssignee = $this->gitlab->getUserIDByZentaoAccount($oldMR->hostID, $MR->assignee);
if($gitlabAssignee) $newMR->assignee_ids = $gitlabAssignee;
}
$url = sprintf($this->gitlab->getApiRoot($hostID), "/projects/$projectID/merge_requests/$MRID");
return json_decode(commonModel::http($url, $MR, $options = array(CURLOPT_CUSTOMREQUEST => 'PUT')));
}
else
{
$url = sprintf($this->loadModel('gitea')->getApiRoot($hostID), "/repos/$projectID/pulls/$MRID");
$newMR->base = $MR->targetBranch;
$newMR->body = $MR->description;
if($MR->assignee)
{
$assignee = $this->gitea->getUserIDByZentaoAccount($this->post->hostID, $MR->assignee);
if($assignee) $newMR->assignee = $assignee;
}
$mergeResult = json_decode(commonModel::http($url, $newMR, array(), array(), 'json', 'PATCH'));
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) and $mergeResult->state == 'open') $mergeResult->state = 'opened';
return $mergeResult;
}
}
/**
@@ -1750,6 +1786,7 @@ class mrModel extends model
{
if(empty($sourceProject) or empty($sourceBranch) or empty($targetProject) or empty($targetBranch)) return array('result' => 'success');
if($sourceProject == $targetProject and $sourceBranch == $targetBranch) return array('result' => 'fail', 'message' => $this->lang->mr->errorLang[1]);
$dbOpenedID = $this->dao->select('id')->from(TABLE_MR)
->where('hostID')->eq($hostID)
->andWhere('sourceProject')->eq($sourceProject)
+10 -3
View File
@@ -66,8 +66,8 @@
}
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;
$sourceProject = isset($projects[$MR->hostID][$MR->sourceProject]) ? $projects[$MR->hostID][$MR->sourceProject]->full_name . ':' . $MR->sourceBranch : $MR->sourceProject . ':' . $MR->sourceBranch;
$targetProject = isset($projects[$MR->hostID][$MR->targetProject]) ? $projects[$MR->hostID][$MR->targetProject]->full_name . ':' . $MR->targetBranch : $MR->targetProject . ':' . $MR->targetBranch;
}
?>
<tr>
@@ -90,7 +90,14 @@
<td class='c-actions'>
<?php
$canDelete = ($app->user->admin or (isset($projects[$MR->hostID][$MR->sourceProject]->owner->id) and $projects[$MR->hostID][$MR->sourceProject]->owner->id == $openIDList[$MR->hostID])) ? '' : 'disabled';
$canEdit = (isset($projects[$MR->hostID][$MR->sourceProject]->isDeveloper) and $projects[$MR->hostID][$MR->sourceProject]->isDeveloper == true) ? '' : 'disabled';
if($repo->SCM == 'Gitlab')
{
$canEdit = (isset($projects[$MR->hostID][$MR->sourceProject]->isDeveloper) and $projects[$MR->hostID][$MR->sourceProject]->isDeveloper == true) ? '' : 'disabled';
}
else
{
$canEdit = (isset($projects[$MR->hostID][$MR->sourceProject]->allow_merge_commits) and $projects[$MR->hostID][$MR->sourceProject]->allow_merge_commits == true) ? '' : 'disabled';
}
common::printLink('mr', 'view', "mr={$MR->id}", '<i class="icon icon-eye"></i>', '', "title='{$lang->mr->view}' class='btn btn-info'");
common::printIcon('mr', 'edit', "mr={$MR->id}", $MR, 'list', '', '', '', false, "{$canEdit}");
common::printLink('mr', 'diff', "mr={$MR->id}", '<i class="icon icon-diff"></i>', '', "title='{$lang->mr->viewDiff}' class='btn btn-info'");
+5 -5
View File
@@ -32,15 +32,15 @@
<form id='mrForm' method='post' class='form-ajax'>
<table class='table table-form'>
<tr>
<th><?php echo $lang->gitlab->common;?></th>
<td><?php echo $this->loadModel('gitlab')->getByID($MR->hostID)->name;?></td>
<th><?php echo $lang->mr->server;?></th>
<td><?php echo $this->loadModel('pipeline')->getByID($MR->hostID)->name;?></td>
</tr>
<tr>
<th style="white-space: nowrap;"><?php echo $lang->mr->sourceProject;?></th>
<td>
<div>
<span class='fix-border text-left'>
<?php echo $this->loadModel('gitlab')->apiGetSingleProject($MR->hostID, $MR->sourceProject)->name_with_namespace;?>:
<?php echo $host->type == 'gitlab' ? $this->loadModel('gitlab')->apiGetSingleProject($MR->hostID, $MR->sourceProject)->name_with_namespace : $MR->sourceProject;?>:
<?php echo $MR->sourceBranch;?>
</span>
</div>
@@ -52,12 +52,12 @@
<div class='input-group'>
<?php if($MR->status == 'merged' or $MR->status == 'closed'):?>
<span class='fix-border text-left'>
<?php echo $this->loadModel('gitlab')->apiGetSingleProject($MR->hostID, $MR->targetProject)->name_with_namespace;?>:
<?php echo $host->type == 'gitlab' ? $this->loadModel('gitlab')->apiGetSingleProject($MR->hostID, $MR->targetProject)->name_with_namespace : $MR->targetProject;?>:
<?php echo $MR->targetBranch;?>
</span>
<?php else:?>
<span class='input-group-addon fix-border'>
<?php echo $this->loadModel('gitlab')->apiGetSingleProject($MR->hostID, $MR->targetProject)->name_with_namespace;?>
<?php echo $host->type == 'gitlab' ? $this->loadModel('gitlab')->apiGetSingleProject($MR->hostID, $MR->targetProject)->name_with_namespace : $MR->targetProject;?>:
</span>
<?php echo html::select('targetBranch', $targetBranchList, $MR->targetBranch, "class='form-control chosen'");?>
<?php endif;?>