diff --git a/module/gitlab/model.php b/module/gitlab/model.php index 08c18d4ae3..785c79bad8 100644 --- a/module/gitlab/model.php +++ b/module/gitlab/model.php @@ -902,9 +902,9 @@ class gitlabModel extends model * @param int $gitlabID * @param int $projectID * @access public - * @return object|array|null + * @return object|array|null|false */ - public function apiDeleteProject(int $gitlabID, int $projectID): object|array|null + public function apiDeleteProject(int $gitlabID, int $projectID): object|array|null|false { if(empty($projectID)) return false; @@ -969,29 +969,6 @@ class gitlabModel extends model return $this->projects[$gitlabID][$projectID]; } - /** - * 根据多个版本库多线程获取项目列表。 - * Multi get projects by repos. - * - * @param array $repos - * @access public - * @return void - */ - public function apiMultiGetProjects(array $repos): void - { - $requests = array(); - foreach($repos as $id => $repo) - { - $requests[$id]['url'] = sprintf($this->getApiRoot($repo->serviceHost, false), "/projects/{$repo->serviceProject}"); - } - $this->app->loadClass('requests', true); - $results = requests::request_multiple($requests, array('timeout' => 2)); - foreach($results as $id => $result) - { - $this->projects[$repos[$id]->serviceHost][$repos[$id]->serviceProject] = (!empty($result->body) and substr($result->body, 0, 1) == '{') ? json_decode($result->body) : ''; - } - } - /** * 通过api获取一个用户。 * Get single user by API. @@ -1022,49 +999,20 @@ class gitlabModel extends model return json_decode(commonModel::http($url)); } - /** - * 获取gitlab项目最新用户。 - * Get project users. - * - * @param int $gitlabID - * @param int $projectID - * @access public - * @return object|array|null - */ - public function apiGetProjectUsers(int $gitlabID, int $projectID): object|array|null - { - $url = sprintf($this->getApiRoot($gitlabID), "/projects/$projectID/users"); - return json_decode(commonModel::http($url)); - } - /** * 获取gitlab项目最新成员。 * Get project all members(users and users in groups). * - * @param int $gitlabID - * @param int $projectID + * @param int $gitlabID + * @param int $projectID + * @param int $userID * @access public * @return object|array|null */ - public function apiGetProjectMembers(int $gitlabID, int $projectID): object|array|null + public function apiGetProjectMembers(int $gitlabID, int $projectID, int $userID = 0): object|array|null { - $url = sprintf($this->getApiRoot($gitlabID), "/projects/$projectID/members/all"); - return json_decode(commonModel::http($url)); - } - - /** - * 获取gitlab项目某个成员信息。 - * Get the member detail in project. - * - * @param int $gitlabID - * @param int $projectID - * @param int $userID - * @access public - * @return object|array|null - */ - public function apiGetProjectMember(int $gitlabID, int $projectID, int $userID): object|array|null - { - $url = sprintf($this->getApiRoot($gitlabID), "/projects/$projectID/members/all/$userID"); + $path = "/projects/{$projectID}/members/all" . ($userID ? "/{$userID}" : ''); + $url = sprintf($this->getApiRoot($gitlabID), $path); return json_decode(commonModel::http($url)); } @@ -1084,38 +1032,6 @@ class gitlabModel extends model return json_decode(commonModel::http($url)); } - /** - * 通过API获取项目的分叉。 - * Get Forks of a project by API. - * - * @link https://docs.gitlab.com/ee/api/projects.html#list-forks-of-a-project - * @param int $gitlabID - * @param int $projectID - * @access public - * @return object|array|null - */ - public function apiGetForks(int $gitlabID, int $projectID): object|array|null - { - $url = sprintf($this->getApiRoot($gitlabID), "/projects/$projectID/forks"); - return json_decode(commonModel::http($url)); - } - - /** - * 获取项目是否fork别的项目而来。 - * Get upstream project by API. - * - * @param int $gitlabID - * @param int $projectID - * @access public - * @return string|array - */ - public function apiGetUpstream(int $gitlabID, int $projectID): string|array - { - $currentProject = $this->apiGetSingleProject($gitlabID, $projectID); - if(isset($currentProject->forked_from_project)) return $currentProject->forked_from_project; - return array(); - } - /** * 通过api获取项目 hooks。 * Get hooks. @@ -1126,30 +1042,10 @@ class gitlabModel extends model * @link https://docs.gitlab.com/ee/api/projects.html#list-project-hooks * @return object|array|null */ - public function apiGetHooks(int $gitlabID, int $projectID): object|array|null + public function apiGetHooks(int $gitlabID, int $projectID, int $hookID = 0): object|array|null { $apiRoot = $this->getApiRoot($gitlabID, false); - $apiPath = "/projects/{$projectID}/hooks"; - $url = sprintf($apiRoot, $apiPath); - - return json_decode(commonModel::http($url)); - } - - /** - * 通过api获取指定hook。 - * Get specific hook by api. - * - * @param int $gitlabID - * @param int $projectID - * @param int $hookID - * @access public - * @link https://docs.gitlab.com/ee/api/projects.html#get-project-hook - * @return object|array|null - */ - public function apiGetHook(int $gitlabID, int $projectID, int $hookID): object|array|null - { - $apiRoot = $this->getApiRoot($gitlabID, false); - $apiPath = "/projects/$projectID/hooks/$hookID)"; + $apiPath = "/projects/{$projectID}/hooks" . ($hookID ? "/{$hookID}" : ''); $url = sprintf($apiRoot, $apiPath); return json_decode(commonModel::http($url)); diff --git a/module/gitlab/test/gitlab.class.php b/module/gitlab/test/gitlab.class.php index 1215c95427..9bba263185 100644 --- a/module/gitlab/test/gitlab.class.php +++ b/module/gitlab/test/gitlab.class.php @@ -197,6 +197,46 @@ class gitlabTest return $this->gitlab->apiGetSingleTag($gitlabID, $projectID, $tag); } + /** + * Api get signle branch. + * + * @param int $gitlabID + * @param int $projectID + * @param string $branch + * @access public + * @return object + */ + public function apiGetSingleBranchTest($gitlabID, $projectID, $branch) + { + return $this->gitlab->apiGetSingleBranch($gitlabID, $projectID, $branch); + } + + /** + * Api get signle user. + * + * @param int $gitlabID + * @param int $userID + * @access public + * @return object|array|null + */ + public function apiGetSingleUserTest(int $gitlabID, int $userID): object|array|null + { + return $this->gitlab->apiGetSingleUser($gitlabID, $userID); + } + + /** + * Api get signle group. + * + * @param int $gitlabID + * @param int $groupID + * @access public + * @return object|array|null + */ + public function apiGetSingleGroupTest(int $gitlabID, int $groupID): object|array|null + { + return $this->gitlab->apiGetSingleGroup($gitlabID, $groupID); + } + /** * Api get signle project. * @@ -210,4 +250,20 @@ class gitlabTest { return $this->gitlab->apiGetSingleProject($gitlabID, $projectID, $useUser); } + + public function addPushWebhookTest(int $repoID, string $token, int $projectID = 0) + { + $repo = $this->tester->loadModel('repo')->getByID($repoID); + if($projectID) $repo->project = $projectID; + + $result = $this->gitlab->addPushWebhook($repo, $token); + if(is_array($result)) $result = true; + return $result; + } + + public function isWebhookExistsTest(int $repoID, string $url = '') + { + $repo = $this->tester->loadModel('repo')->getByID($repoID); + return $this->gitlab->isWebhookExistsTest($repo, $url); + } } diff --git a/module/gitlab/test/model/addpushwebhook.php b/module/gitlab/test/model/addpushwebhook.php new file mode 100644 index 0000000000..999a7c9312 --- /dev/null +++ b/module/gitlab/test/model/addpushwebhook.php @@ -0,0 +1,27 @@ +#!/usr/bin/env php +gen(5); +zdTable('repo')->gen(1); + +$gitlab = new gitlabTest(); + +$repoID = 1; +$url = 'http:/api.php/v1/gitlab/webhook?repoID=1'; + +r($gitlab->isWebhookExistsTest($repoID, '')) && p() && e('0'); //检查url为空的webhook是否存在 +r($gitlab->isWebhookExistsTest($repoID, $url)) && p() && e('1'); //用正常的url检查webhook是否存在 diff --git a/module/gitlab/test/model/apicreatehook.php b/module/gitlab/test/model/apicreatehook.php new file mode 100644 index 0000000000..0e452c0e7c --- /dev/null +++ b/module/gitlab/test/model/apicreatehook.php @@ -0,0 +1,34 @@ +#!/usr/bin/env php +gen(5); + +$gitlab = $tester->loadModel('gitlab'); + +$gitlabID = 1; +$projectID = 2; +$emptyHook = new stdclass(); + +$hook = new stdclass(); +$hook->url = 'http://unittest.com/api.php/v1/gitlab/webhook?repoID=1'; +$hook->push_events = true; +$hook->merge_requests_events = true; + +r($gitlab->apiCreateHook(0, 0, $emptyHook)) && p() && e('0'); //使用空的gitlabID,projectID,hook对象创建GitLabhook +r($gitlab->apiCreateHook(0, 0, $hook)) && p() && e('0'); //使用空的gitlabID、projectID,正确的hook对象创建GitLabhook +r($gitlab->apiCreateHook($gitlabID, 0, $hook)) && p('message') && e('404 Project Not Found'); //使用正确的gitlabID、hook信息,错误的projectID创建hook +r($gitlab->apiCreateHook($gitlabID, $projectID, $hook)) && p('url') && e('http://unittest.com/api.php/v1/gitlab/webhook?repoID=1'); //通过gitlabID,projectID,hook对象正确创建GitLabhook diff --git a/module/gitlab/test/model/apideletegroup.php b/module/gitlab/test/model/apideletegroup.php new file mode 100644 index 0000000000..24cc1d2e58 --- /dev/null +++ b/module/gitlab/test/model/apideletegroup.php @@ -0,0 +1,52 @@ +#!/usr/bin/env php +gen(5); + +$gitlab = $tester->loadModel('gitlab'); + +$gitlabID = 1; + +/* Create group. */ +$group = new stdclass(); +$group->name = 'unitTestGroup17'; +$group->path = 'unit_test_group17'; +$group->description = 'unit_test_group desc'; +$group->visibility = 'public'; +$group->request_access_enabled = '1'; +$group->lfs_enabled = '1'; +$group->project_creation_level = 'developer'; +$group->subgroup_creation_level = 'maintainer'; +$gitlab->apiCreateGroup($gitlabID, $group); + +/* Get groupID. */ +$gitlabGroups = $gitlab->apiGetGroups($gitlabID); +foreach($gitlabGroups as $gitlabGroup) +{ + if($gitlabGroup->name == 'unitTestGroup17') + { + $groupID = $gitlabGroup->id; + break; + } +} + +$group = new stdclass(); +$group->description = 'apiUpdatedGroup'; + +r($gitlab->apiDeleteGroup($gitlabID, 0)) && p() && e('0'); //使用空的groupID删除gitlab群组 +r($gitlab->apiDeleteGroup(0, $groupID)) && p() && e('0'); //使用错误gitlabID删除群组 + +r($gitlab->apiDeleteGroup($gitlabID, $groupID)) && p('message') && e('202 Accepted'); //通过gitlabID,projectID,分支对象正确删除GitLab群组 \ No newline at end of file diff --git a/module/gitlab/test/model/apideleteproject.php b/module/gitlab/test/model/apideleteproject.php new file mode 100644 index 0000000000..c6a1c97c91 --- /dev/null +++ b/module/gitlab/test/model/apideleteproject.php @@ -0,0 +1,49 @@ +#!/usr/bin/env php +gen(5); + +$gitlab = $tester->loadModel('gitlab'); + +$gitlabID = 1; + +/* Create project. */ +$project = new stdclass(); +$project->name = 'unitTestProject17'; +$project->path = 'unit_test_project17'; +$project->description = 'unit_test_project desc'; +$project->visibility = 'public'; +$project->namespace_id = '1'; +$gitlab->apiCreateProject($gitlabID, $project); + +/* Get projectID. */ +$gitlabProjects = $gitlab->apiGetProjects($gitlabID); +foreach($gitlabProjects as $gitlabProject) +{ + if($gitlabProject->path == 'unit_test_project17') + { + $projectID = $gitlabProject->id; + break; + } +} + +$project = new stdclass(); +$project->description = 'apiUpdatedProject'; + +r($gitlab->apiDeleteProject($gitlabID, 0)) && p() && e('0'); //使用空的projectID删除项目 +r($gitlab->apiDeleteProject(0, $projectID)) && p() && e('0'); //使用错误gitlabID删除项目 + +r($gitlab->apiDeleteProject($gitlabID, $projectID)) && p('message') && e('202 Accepted'); //通过gitlabID,项目id正确删除项目 diff --git a/module/gitlab/test/model/apigethooks.php b/module/gitlab/test/model/apigethooks.php new file mode 100644 index 0000000000..ce0e7514c0 --- /dev/null +++ b/module/gitlab/test/model/apigethooks.php @@ -0,0 +1,43 @@ +#!/usr/bin/env php +gen(5); + +$gitlab = $tester->loadModel('gitlab'); + +$gitlabID = 1; +$projectID = 2; +$result = $gitlab->apiGetHooks($gitlabID, $projectID); +$hook = end($result); +$hookResult = $gitlab->apiGetHooks($gitlabID, $projectID, $hook->id); +r(isset($hook->url)) && p() && e('1'); //通过gitlabID,projectID,获取GitLab hook列表 +r(count($result) > 0) && p() && e('1'); //通过gitlabID,projectID,获取GitLab hook数量 +r(isset($hookResult->id)) && p() && e('1'); //通过指定hookID获取hook信息 + +$gitlabID = 1; +$projectID = 1; +r(count($gitlab->apiGetHooks($gitlabID, $projectID))) && p() && e('0'); //当前项目没有hook时,获取GitLab hook列表 + +$gitlabID = 1; +$result = $gitlab->apiGetHooks($gitlabID, 0); +r($gitlab->apiGetHooks($gitlabID, 0)) && p('message') && e('404 Project Not Found'); //当gitlabID存在,projectID不存在时,获取GitLab hook列表 + +$result = $gitlab->apiGetHooks(0, 0); +if(empty($result)) $result = 'return empty'; +r($result) && p() && e('return empty'); //当gitlabID,projectID都为0时,获取GitLabi hook列表 diff --git a/module/gitlab/test/model/apigetprojectmembers.php b/module/gitlab/test/model/apigetprojectmembers.php new file mode 100644 index 0000000000..b0fbb3e3a4 --- /dev/null +++ b/module/gitlab/test/model/apigetprojectmembers.php @@ -0,0 +1,38 @@ +#!/usr/bin/env php +gen(5); + +$gitlab = $tester->loadModel('gitlab'); + +$gitlabID = 0; +$projectID = 0; + +$result = $gitlab->apiGetProjectMembers($gitlabID, $projectID); +if(!$result) $result = 'return null'; +r($result) && p() && e('return null'); //使用空的数据查询群组用户 + +$gitlabID = 1; +r($gitlab->apiGetProjectMembers($gitlabID, $projectID)) && p('message') && e('404 Project Not Found'); //使用空的projectID查询群组用户 + +$projectID = 2; +$result = $gitlab->apiGetProjectMembers($gitlabID, $projectID); +r($result[0]->id > 0) && p() && e('1'); //通过gitlabID,projectID查询群组用户 + +$userID = 4; +r($gitlab->apiGetProjectMembers($gitlabID, $projectID, $userID)) && p('name') && e('测试用户1'); //通过gitlabID,projectID,userID查询群组用户 diff --git a/module/gitlab/test/model/apigetsinglebranch.php b/module/gitlab/test/model/apigetsinglebranch.php new file mode 100644 index 0000000000..90494486c1 --- /dev/null +++ b/module/gitlab/test/model/apigetsinglebranch.php @@ -0,0 +1,32 @@ +#!/usr/bin/env php +apiGetSingleBranch(); +timeout=0 +cid=1 + +- 查询正确的branch信息属性name @branch1 +- 使用不存在的gitlabID查询branch信息 @0 +- 使用不存在的projectID查询branch信息属性message @404 Project Not Found +- 使用不存在的branch名称查询branch信息属性message @404 Branch Not Found + +*/ + +zdTable('pipeline')->gen(5); + +$gitlab = new gitlabTest(); + +$branch1 = $gitlab->apiGetSingleBranchTest(1, 2, 'branch1'); +$branch2 = $gitlab->apiGetSingleBranchTest(0, 2, 'branch1'); +$branch3 = $gitlab->apiGetSingleBranchTest(1, 0, 'branch1'); +$branch4 = $gitlab->apiGetSingleBranchTest(1, 2, 'branch12345'); + +r($branch1) && p('name') && e('branch1'); // 查询正确的branch信息 +r($branch2) && p() && e('0'); // 使用不存在的gitlabID查询branch信息 +r($branch3) && p('message') && e('404 Project Not Found'); // 使用不存在的projectID查询branch信息 +r($branch4) && p('message') && e('404 Branch Not Found'); // 使用不存在的branch名称查询branch信息 diff --git a/module/gitlab/test/model/apigetsinglegroup.php b/module/gitlab/test/model/apigetsinglegroup.php new file mode 100644 index 0000000000..432c62790a --- /dev/null +++ b/module/gitlab/test/model/apigetsinglegroup.php @@ -0,0 +1,29 @@ +#!/usr/bin/env php +apiGetSingleGroup(); +timeout=0 +cid=1 + +- 查询正确的group信息属性name @testGroup +- 使用不存在的gitlabID查询group信息 @0 +- 使用不存在的group名称查询group信息属性message @404 Group Not Found + +*/ + +zdTable('pipeline')->gen(5); + +$gitlab = new gitlabTest(); + +$group1 = $gitlab->apiGetSingleGroupTest(1, 14); +$group2 = $gitlab->apiGetSingleGroupTest(0, 2); +$group3 = $gitlab->apiGetSingleGroupTest(1, 100001); + +r($group1) && p('name') && e('testGroup'); // 查询正确的group信息 +r($group2) && p() && e('0'); // 使用不存在的gitlabID查询group信息 +r($group3) && p('message') && e('404 Group Not Found'); // 使用不存在的group名称查询group信息 \ No newline at end of file diff --git a/module/gitlab/test/model/apigetsingleuser.php b/module/gitlab/test/model/apigetsingleuser.php new file mode 100644 index 0000000000..cd391bf7cb --- /dev/null +++ b/module/gitlab/test/model/apigetsingleuser.php @@ -0,0 +1,29 @@ +#!/usr/bin/env php +apiGetSingleUser(); +timeout=0 +cid=1 + +- 查询正确的user信息属性name @测试用户1 +- 使用不存在的gitlabID查询user信息 @0 +- 使用不存在的user名称查询user信息属性message @404 User Not Found + +*/ + +zdTable('pipeline')->gen(5); + +$gitlab = new gitlabTest(); + +$user1 = $gitlab->apiGetSingleUserTest(1, 4); +$user2 = $gitlab->apiGetSingleUserTest(0, 2); +$user3 = $gitlab->apiGetSingleUserTest(1, 100001); + +r($user1) && p('name') && e('测试用户1'); // 查询正确的user信息 +r($user2) && p() && e('0'); // 使用不存在的gitlabID查询user信息 +r($user3) && p('message') && e('404 User Not Found'); // 使用不存在的user名称查询user信息 diff --git a/module/gitlab/test/model/apiupdategroup.php b/module/gitlab/test/model/apiupdategroup.php new file mode 100644 index 0000000000..e40829d604 --- /dev/null +++ b/module/gitlab/test/model/apiupdategroup.php @@ -0,0 +1,53 @@ +#!/usr/bin/env php +gen(5); + +$gitlab = $tester->loadModel('gitlab'); + +$gitlabID = 1; + +/* Create group. */ +$group = new stdclass(); +$group->name = 'unitTestGroup17'; +$group->path = 'unit_test_group17'; +$group->description = 'unit_test_group desc'; +$group->visibility = 'public'; +$group->request_access_enabled = '1'; +$group->lfs_enabled = '1'; +$group->project_creation_level = 'developer'; +$group->subgroup_creation_level = 'maintainer'; +$gitlab->apiCreateGroup($gitlabID, $group); + +/* Get groupID. */ +$gitlabGroups = $gitlab->apiGetGroups($gitlabID); +foreach($gitlabGroups as $gitlabGroup) +{ + if($gitlabGroup->name == 'unitTestGroup17') + { + $groupID = $gitlabGroup->id; + break; + } +} + +$group = new stdclass(); +$group->description = 'apiUpdatedGroup'; + +r($gitlab->apiUpdateGroup($gitlabID, $group)) && p() && e('0'); //使用空的groupID创建gitlab群组 +r($gitlab->apiUpdateGroup(0, $group)) && p() && e('0'); //使用错误gitlabID创建群组 + +$group->id = $groupID; +r($gitlab->apiUpdateGroup($gitlabID, $group)) && p('description') && e('apiUpdatedGroup'); //通过gitlabID,用户对象正确更新用户名字 \ No newline at end of file diff --git a/module/gitlab/test/model/apiupdateproject.php b/module/gitlab/test/model/apiupdateproject.php new file mode 100644 index 0000000000..08a615ae07 --- /dev/null +++ b/module/gitlab/test/model/apiupdateproject.php @@ -0,0 +1,50 @@ +#!/usr/bin/env php +gen(5); + +$gitlab = $tester->loadModel('gitlab'); + +$gitlabID = 1; + +/* Create project. */ +$project = new stdclass(); +$project->name = 'unitTestProject17'; +$project->path = 'unit_test_project17'; +$project->description = 'unit_test_project desc'; +$project->visibility = 'public'; +$project->namespace_id = '1'; +$gitlab->apiCreateProject($gitlabID, $project); + +/* Get projectID. */ +$gitlabProjects = $gitlab->apiGetProjects($gitlabID); +foreach($gitlabProjects as $gitlabProject) +{ + if($gitlabProject->path == 'unit_test_project17') + { + $projectID = $gitlabProject->id; + break; + } +} + +$project = new stdclass(); +$project->description = 'apiUpdatedProject'; + +r($gitlab->apiUpdateProject($gitlabID, $project)) && p() && e('0'); //使用空的projectID创建gitlab群组 +r($gitlab->apiUpdateProject(0, $project)) && p() && e('0'); //使用错误gitlabID创建群组 + +$project->id = $projectID; +r($gitlab->apiUpdateProject($gitlabID, $project)) && p('description') && e('apiUpdatedProject'); //通过gitlabID,用户对象正确更新项目描述 diff --git a/module/gitlab/test/model/iswebhookexists.php b/module/gitlab/test/model/iswebhookexists.php new file mode 100644 index 0000000000..7ad9d549a7 --- /dev/null +++ b/module/gitlab/test/model/iswebhookexists.php @@ -0,0 +1,28 @@ +#!/usr/bin/env php +gen(5); +zdTable('repo')->gen(1); + +$gitlab = new gitlabTest(); + +$repoID = 1; +$token = ''; +$_SERVER['REQUEST_URI'] = 'http://unittest/'; + +r($gitlab->addPushWebhookTest($repoID, $token)) && p() && e('0'); //使用repoID为1,不存在的项目id推送webhook +r($gitlab->addPushWebhookTest($repoID, $token, 2)) && p() && e('1'); //使用repoID为1,存在的项目id推送webhook \ No newline at end of file