277 lines
7.8 KiB
PHP
277 lines
7.8 KiB
PHP
<?php
|
|
/**
|
|
* The model file of mr module of ZenTaoPMS.
|
|
*
|
|
* @copyright Copyright 2009-2021 青岛易软天创网络科技有限公司(QingDao Nature Easy Soft Network Technology Co,LTD, www.cnezsoft.com)
|
|
* @license ZPL (http://zpl.pub/page/zplv12.html)
|
|
* @author dingguodong <dingguodong@easycorp.ltd>
|
|
* @package mr
|
|
* @version $Id$
|
|
* @link http://www.zentao.net
|
|
*/
|
|
class mrModel extends model
|
|
{
|
|
/**
|
|
* The construct method, to do some auto things.
|
|
*
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->loadModel('gitlab');
|
|
}
|
|
|
|
/**
|
|
* Get a MR by id.
|
|
*
|
|
* @param int $id
|
|
* @access public
|
|
* @return object
|
|
*/
|
|
public function getByID($id)
|
|
{
|
|
return $this->dao->select('*')->from(TABLE_MR)->where('id')->eq($id)->fetch();
|
|
}
|
|
|
|
/**
|
|
* Get MR list of gitlab project.
|
|
*
|
|
* @param string $orderBy
|
|
* @param object $pager
|
|
* @access public
|
|
* @return array
|
|
*/
|
|
public function getList($orderBy = 'id_desc', $pager = null)
|
|
{
|
|
$MRList = $this->dao->select('*')
|
|
->from(TABLE_MR)
|
|
->where('deleted')->eq('0')
|
|
->orderBy($orderBy)
|
|
->page($pager)
|
|
->fetchAll('id');
|
|
|
|
foreach($MRList as $MR) $MR = $this->processMR($MR);
|
|
|
|
return $MRList;
|
|
}
|
|
|
|
/**
|
|
* ProcessMR info by api.
|
|
*
|
|
* @param object $MR
|
|
* @access public
|
|
* @return object
|
|
*/
|
|
public function processMR($MR)
|
|
{
|
|
$rawMR = $this->apiGetSingleMR($MR->gitlabID, $MR->projectID, $MR->id);
|
|
|
|
$MR->name = $rawMR->title;
|
|
$MR->targetBranch = $rawMR->target_branch;
|
|
$MR->sourceBranch = $rawMR->source_branch;
|
|
$MR->targetProject = $rawMR->target_project_id;
|
|
$MR->canMerge = $rawMR->merge_status;
|
|
$MR->status = $rawMR->state;
|
|
return $MR;
|
|
}
|
|
|
|
/**
|
|
* Delete one MR.
|
|
*
|
|
* condition: when user deleting a repo.
|
|
*
|
|
* @param int $MRID
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function deleteMR($MRID)
|
|
{
|
|
$this->dao->delete()->from(TABLE_MR)
|
|
->andWhere('mrID')->eq($MRID)
|
|
->exec();
|
|
}
|
|
|
|
/**
|
|
* Get gitlab pairs.
|
|
*
|
|
* @access public
|
|
* @return array
|
|
*/
|
|
public function getPairs($repoID)
|
|
{
|
|
$MR = $this->dao->select('id,title')
|
|
->from(TABLE_MR)
|
|
->where('deleted')->eq('0')
|
|
->AndWhere('repoID')->eq($repoID)
|
|
->orderBy('id')->fetchPairs('id', 'title');
|
|
return array('' => '') + $MR;
|
|
}
|
|
|
|
/**
|
|
* Create MR function.
|
|
*
|
|
* @access public
|
|
* @return bool
|
|
*/
|
|
public function create()
|
|
{
|
|
$gitlabID = $this->post->gitlabID;
|
|
$projectID = $this->post->projectID;
|
|
|
|
$sourceProject = $this->post->sourceProject;
|
|
$sourceBranch = $this->post->sourceBranch;
|
|
$targetProject = $this->post->targetProject;
|
|
$targetBranch = $this->post->targetBranch;
|
|
|
|
if($projectID != $sourceProject) return false;
|
|
}
|
|
|
|
/**
|
|
* Get Forks of a project.
|
|
*
|
|
* @docs https://docs.gitlab.com/ee/api/projects.html#list-forks-of-a-project
|
|
* @param int $gitlabID
|
|
* @param int $projectID
|
|
* @access public
|
|
* @return object
|
|
*/
|
|
public function apiGetForks($gitlabID, $projectID)
|
|
{
|
|
$url = sprintf($this->getApiRoot($gitlabID), "/projects/$projectID/forks");
|
|
return json_decode(commonModel::http($url));
|
|
}
|
|
|
|
/**
|
|
* Create MR by API.
|
|
*
|
|
* @docs https://docs.gitlab.com/ee/api/merge_requests.html#create-mr
|
|
* @param int $gitlabID
|
|
* @param int $projectID
|
|
* @param object $params
|
|
* @access public
|
|
* @return object
|
|
*/
|
|
public function apiCreateMR($gitlabID, $projectID, $params)
|
|
{
|
|
$url = sprintf($this->getApiRoot($gitlabID), "/projects/$projectID/merge_requests");
|
|
$MR = new stdclass;
|
|
return json_decode(commonModel::http($url, $MR));
|
|
}
|
|
|
|
/**
|
|
* Get MR list by API.
|
|
*
|
|
* @docs https://docs.gitlab.com/ee/api/merge_requests.html#list-project-merge-requests
|
|
* @param int $gitlabID
|
|
* @param int $projectID
|
|
* @access public
|
|
* @return object
|
|
*/
|
|
public function apiGetMRList($gitlabID, $projectID)
|
|
{
|
|
$url = sprintf($this->getApiRoot($gitlabID), "/projects/$projectID/merge_requests");
|
|
return json_decode(commonModel::http($url));
|
|
}
|
|
|
|
/**
|
|
* Get single MR by API.
|
|
*
|
|
* @docs https://docs.gitlab.com/ee/api/merge_requests.html#get-single-mr
|
|
* @param int $gitlabID
|
|
* @param int $projectID
|
|
* @param int $MRID
|
|
* @access public
|
|
* @return object
|
|
*/
|
|
public function apiGetSingleMR($gitlabID, $projectID, $MRID)
|
|
{
|
|
$url = sprintf($this->getApiRoot($gitlabID), "/projects/$projectID/merge_requests/$MRID");
|
|
return json_decode(commonModel::http($url));
|
|
}
|
|
|
|
/**
|
|
* Update MR by API.
|
|
*
|
|
* @docs https://docs.gitlab.com/ee/api/merge_requests.html#update-mr
|
|
* @param int $gitlabID
|
|
* @param int $projectID
|
|
* @param int $MRID
|
|
* @param object $params
|
|
* @access public
|
|
* @return object
|
|
*/
|
|
public function apiUpdateMR($gitlabID, $projectID, $MRID, $params)
|
|
{
|
|
$url = sprintf($this->getApiRoot($gitlabID), "/projects/$projectID/merge_requests/$MRID");
|
|
return json_decode(commonModel::http($url, $MR, $options = array(CURLOPT_CUSTOMREQUEST => 'PUT')));
|
|
}
|
|
|
|
/**
|
|
* Delete MR by API.
|
|
*
|
|
* @docs https://docs.gitlab.com/ee/api/merge_requests.html#delete-a-merge-request
|
|
* @param int $gitlabID
|
|
* @param int $projectID
|
|
* @param int $MRID
|
|
* @access public
|
|
* @return object
|
|
*/
|
|
public function apiDeleteMR($gitlabID, $projectID, $MRID)
|
|
{
|
|
$url = sprintf($this->getApiRoot($gitlabID), "/projects/$projectID/merge_requests/$MRID");
|
|
return json_decode(commonModel::http($url, null, array(CURLOPT_CUSTOMREQUEST => 'DELETE')));
|
|
}
|
|
|
|
/**
|
|
* Accept MR by API.
|
|
*
|
|
* @docs https://docs.gitlab.com/ee/api/merge_requests.html#accept-mr
|
|
* @param int $gitlabID
|
|
* @param int $projectID
|
|
* @param int $MRID
|
|
* @access public
|
|
* @return object
|
|
*/
|
|
public function apiAcceptMR($gitlabID, $projectID, $MRID)
|
|
{
|
|
$url = sprintf($this->getApiRoot($gitlabID), "/projects/$projectID/merge_requests/$MRID");
|
|
return json_decode(commonModel::http($url, $data, $options = array(CURLOPT_CUSTOMREQUEST => 'PUT')));
|
|
}
|
|
|
|
/**
|
|
* Get MR diff versions by API.
|
|
*
|
|
* @docs https://docs.gitlab.com/ee/api/merge_requests.html#get-mr-diff-versions
|
|
* @param int $gitlabID
|
|
* @param int $projectID
|
|
* @param int $MRID
|
|
* @access public
|
|
* @return object
|
|
*/
|
|
public function apiGetDiffVersions($gitlabID, $projectID, $MRID)
|
|
{
|
|
$url = sprintf($this->getApiRoot($gitlabID), "/projects/$projectID/merge_requests/$MRID");
|
|
return json_decode(commonModel::http($url));
|
|
}
|
|
|
|
/**
|
|
* Get single diff version by API.
|
|
*
|
|
* @docs https://docs.gitlab.com/ee/api/merge_requests.html#get-a-single-mr-diff-version
|
|
* @param int $gitlabID
|
|
* @param int $projectID
|
|
* @param int $MRID
|
|
* @param int $versionID
|
|
* @access public
|
|
* @return object
|
|
*/
|
|
public function apiGetSingleDiffVersion($gitlabID, $projectID, $MRID, $versionID)
|
|
{
|
|
$url = sprintf($this->getApiRoot($gitlabID), "/projects/$projectID/merge_requests/$MRID/versions/$versionID");
|
|
return json_decode(commonModel::http($url));
|
|
}
|
|
}
|
|
|