版本管理之gitlab实践教程:基础篇(11)

这篇文章介绍一下gitlab与项目相关的操作,我们会从api入手,学习项目的增删改查,如果通过gitlab的页面操作当然这些会很简单,但是如果在自己的项目中与gitlab的集成比较紧密的情况下,很多时候则可能需要通过调用api来自动完成一些操作,从这篇文章开始也会使用新的gitlab的版本,同时会将easypack上与gitlab-ce的关联版本进行更新。

gitlab的v3与项目相关的api

项番 说明
No.1 列出所有项目
No.2 对某一项目进行增删查
No.3 获取项目事件
No.4 获取某一项目的所有成员列表
No.5 对某一成员进行增删改查
No.6 对项目的webhooks进行增删改查
No.7 获取所有分支信息
No.8 获取某一个分支信息
No.9 对分支进行保护操作或者取消保护操作

gitlab的v4与项目相关的api

gitlab目前的版本在gitlab9,而相关的api也升到了v4,相比之下,项目相关的功能有了如下的一些强化:

项番 说明 命令
No.1 获取所有项目 GET /projects
No.2 获取制定用户的项目信息,会根据用户的权限显示其能所看到的 GET /users/:user_id/projects
No.3 显示某一特定项目 GET /projects/:id
No.4 获取某一项目的用户列表 GET /projects/:id/users
No.5 创建项目 POST /projects
No.6 为某个特定用户创建项目,此操作仅admin可用 POST /projects/user/:user_id
No.7 更新项目 PUT /projects/:id
No.8 Fork一个项目 POST /projects/:id/fork
No.9 列出某一项目的fork信息,此功能为10.1版本引入 GET /projects/:id/forks
No.10 对某一项目进行加星 POST /projects/:id/star
No.11 取消对某一项目的加星 POST /projects/:id/unstar
No.12 对项目进行归档操作 POST /projects/:id/archive
No.13 撤销对项目的归档操作 POST /projects/:id/unarchive
No.14 删除某一指定项目包括所有关联资源比如issue和merge request等 DELETE /projects/:id
No.15 为merge request或者issue或者comment等操作提供上传文件操作 POST /projects/:id/uploads
No.16 为某个group设定项目共享 POST /projects/:id/share
No.17 删除为某个group提供的项目共享 DELETE /projects/:id/share/:group_id
No.18 获取项目hook列表 GET /projects/:id/hooks
No.19 获取某个特定项目的某个hook信息 GET /projects/:id/hooks/:hook_id
No.20 添加项目hook POST /projects/:id/hooks
No.21 更新项目hook信息 PUT /projects/:id/hooks/:hook_id
No.22 删除项目hook信息 DELETE /projects/:id/hooks/:hook_id
No.23 在既存的项目中创建fork关联 POST /projects/:id/fork/:forked_from_id
No.24 删除fork关联 DELETE /projects/:id/fork
No.25 按条件查询项目 GET /projects通过search属性指定
No.26 对某个项目进行housekeeping操作,此功能为9.0开始引入 POST /projects/:id/housekeeping
No.26 列举某一项目仓库下的所有分支 GET /projects/:id/repository/branches
No.27 获取某一特定分支信息 GET /projects/:id/repository/branches/:branch
No.28 设定保护分支 PUT /projects/:id/repository/branches/:branch/protect 此api在后续版本中将会被POST /projects/:id/protected_branches所代替
No.29 取消保护分支 PUT /projects/:id/repository/branches/:branch/unprotect 此api在后续版本中将会被DELETE /projects/:id/protected_branches/:name所替代
No.30 创建项目仓库分支 POST /projects/:id/repository/branches
No.31 删除项目仓库分支 DELETE /projects/:id/repository/branches/:branch
No.32 获取项目成员列表信息 GET /projects/:id/members 类似可用GET /groups/:id/members用于获得group成员列表信息
No.33 获取某一项目成员信息 GET /projects/:id/members/:user_id 类似可用GET /groups/:id/members/:user_id获得某一group成员信息
No.34 添加某一项目成员信息 POST /projects/:id/members 类似可用POST /groups/:id/members添加某一group成员信息
No.35 更新某一项目成员信息 PUT /projects/:id/members/:user_id 类似可用PUT /groups/:id/members/:user_id更新某一group成员信息
No.36 删除某一项目成员信息 DELETE /projects/:id/members/:user_id 类似可用DELETE /groups/:id/members/:user_id获得某一group成员信息

gitlab版本

可使用官方gitlab-ce镜像,也可以使用docker pull liumiaocn/gitlab的镜像,目前版本在10.4.2

[root@devops ~]# docker pull liumiaocn/gitlab
Using default tag: latest
latest: Pulling from liumiaocn/gitlab
1be7f2b886e8: Pull complete 
...
Digest: sha256:fcca9cddd0506e91961a043561b40fb166dbdcd6f4a254d80853d4bb92cabed3
Status: Downloaded newer image for liumiaocn/gitlab:latest
[root@devops ~]#

版本管理之gitlab实践教程:基础篇(11)_第1张图片

事前准备

生成token
版本管理之gitlab实践教程:基础篇(11)_第2张图片

确认生成的token信息,因为只会显示一次,需要记住,token相当于用户名和密码的合集,千万注意要和银行卡密码一样进行慎重管理。
版本管理之gitlab实践教程:基础篇(11)_第3张图片

api使用

接下来将会使用api进行常见的项目操作

确认当前gitlab上的项目信息

[root@devops ~]# curl --request GET --header "PRIVATE-TOKEN: sqiSUhn3tHYXe8nSGRDi" "http://127.0.0.1:32001/api/v4/projects"
[][root@devops ~]# 

可以看到返回的为[],一个空的列表集合

创建一个项目

创建项目的时候用POST的方法/projects即可,但需要指定至少name或者path中的一个,其余则会根据具体情况进行缺省设定,比如group或者namespace如果不指定或者获取不到信息则会缺省设定成当前用户的namespace,没有做特殊设定则是和用户名一致。

[root@devops ~]# curl --request GET --header "PRIVATE-TOKEN: sqiSUhn3tHYXe8nSGRDi" "http://127.0.0.1:32001/api/v4/projects"
[{"id":1,"description":null,"name":"demodefaultprj","name_with_namespace":"Administrator / demodefaultprj","path":"demodefaultprj","path_with_namespace":"root/demodefaultprj","created_at":"2018-01-30T16:19:01.367Z","default_branch":null,"tag_list":[],"ssh_url_to_repo":"git@3ff5a6afdc80:root/demodefaultprj.git","http_url_to_repo":"http://3ff5a6afdc80/root/demodefaultprj.git","web_url":"http://3ff5a6afdc80/root/demodefaultprj","avatar_url":null,"star_count":0,"forks_count":0,"last_activity_at":"2018-01-30T16:19:01.367Z","_links":{"self":"http://3ff5a6afdc80/api/v4/projects/1","issues":"http://3ff5a6afdc80/api/v4/projects/1/issues","merge_requests":"http://3ff5a6afdc80/api/v4/projects/1/merge_requests","repo_branches":"http://3ff5a6afdc80/api/v4/projects/1/repository/branches","labels":"http://3ff5a6afdc80/api/v4/projects/1/labels","events":"http://3ff5a6afdc80/api/v4/projects/1/events","members":"http://3ff5a6afdc80/api/v4/projects/1/members"},"archived":false,"visibility":"private","owner":{"id":1,"name":"Administrator","username":"root","state":"active","avatar_url":"http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\u0026d=identicon","web_url":"http://3ff5a6afdc80/root"},"resolve_outdated_diff_discussions":false,"container_registry_enabled":true,"issues_enabled":true,"merge_requests_enabled":true,"wiki_enabled":true,"jobs_enabled":true,"snippets_enabled":true,"shared_runners_enabled":true,"lfs_enabled":true,"creator_id":1,"namespace":{"id":1,"name":"root","path":"root","kind":"user","full_path":"root","parent_id":null},"import_status":"none","open_issues_count":0,"public_jobs":true,"ci_config_path":null,"shared_with_groups":[],"only_allow_merge_if_pipeline_succeeds":false,"request_access_enabled":false,"only_allow_merge_if_all_discussions_are_resolved":false,"printing_merge_request_link_enabled":true,"permissions":{"project_access":{"access_level":40,"notification_level":3},"group_access":null}}][root@devops ~]# 
[root@devops ~]#

看的不是很清晰,可以使用jq命令,没有的话可以在centos上使用yum install安装一下, 因为在扩展包中,所以之前还需要yum install epel-release一下,或者更简单的方式则为下载一个可执行文件翻到PATH指定的目录中即可。

[root@devops ~]# curl --request GET --header "PRIVATE-TOKEN: sqiSUhn3tHYXe8nSGRDi" "http://127.0.0.1:32001/api/v4/projects" 2>/dev/null |jq . 
[
  {
    "id": 1,
    "description": null,
    "name": "demodefaultprj",
    "name_with_namespace": "Administrator / demodefaultprj",
    "path": "demodefaultprj",
    "path_with_namespace": "root/demodefaultprj",
    "created_at": "2018-01-30T16:19:01.367Z",
    "default_branch": null,
    "tag_list": [],
    "ssh_url_to_repo": "git@3ff5a6afdc80:root/demodefaultprj.git",
    "http_url_to_repo": "http://3ff5a6afdc80/root/demodefaultprj.git",
    "web_url": "http://3ff5a6afdc80/root/demodefaultprj",
    "avatar_url": null,
    "star_count": 0,
    "forks_count": 0,
    "last_activity_at": "2018-01-30T16:19:01.367Z",
    "_links": {
      "self": "http://3ff5a6afdc80/api/v4/projects/1",
      "issues": "http://3ff5a6afdc80/api/v4/projects/1/issues",
      "merge_requests": "http://3ff5a6afdc80/api/v4/projects/1/merge_requests",
      "repo_branches": "http://3ff5a6afdc80/api/v4/projects/1/repository/branches",
      "labels": "http://3ff5a6afdc80/api/v4/projects/1/labels",
      "events": "http://3ff5a6afdc80/api/v4/projects/1/events",
      "members": "http://3ff5a6afdc80/api/v4/projects/1/members"
    },
    "archived": false,
    "visibility": "private",
    "owner": {
      "id": 1,
      "name": "Administrator",
      "username": "root",
      "state": "active",
      "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
      "web_url": "http://3ff5a6afdc80/root"
    },
    "resolve_outdated_diff_discussions": false,
    "container_registry_enabled": true,
    "issues_enabled": true,
    "merge_requests_enabled": true,
    "wiki_enabled": true,
    "jobs_enabled": true,
    "snippets_enabled": true,
    "shared_runners_enabled": true,
    "lfs_enabled": true,
    "creator_id": 1,
    "namespace": {
      "id": 1,
      "name": "root",
      "path": "root",
      "kind": "user",
      "full_path": "root",
      "parent_id": null
    },
    "import_status": "none",
    "open_issues_count": 0,
    "public_jobs": true,
    "ci_config_path": null,
    "shared_with_groups": [],
    "only_allow_merge_if_pipeline_succeeds": false,
    "request_access_enabled": false,
    "only_allow_merge_if_all_discussions_are_resolved": false,
    "printing_merge_request_link_enabled": true,
    "permissions": {
      "project_access": {
        "access_level": 40,
        "notification_level": 3
      },
      "group_access": null
    }
  }
]
[root@devops ~]#

从中可以清晰的看到,虽然我们只是传入了一个name值,剩余的则根据default进行了设定,比如namespace为root,比如可见性为private。另外登陆gitlab,也可以清楚的确认其状态。
版本管理之gitlab实践教程:基础篇(11)_第4张图片

查看项目用户

从上面返回的jason结果可以看到此项目的id为1,我们可以查询一下项目id为1的所有项目成员情况

[root@devops ~]# curl --request GET --header "PRIVATE-TOKEN: sqiSUhn3tHYXe8nSGRDi" "http://127.0.0.1:32001/api/v4/projects/1/users" 2>/dev/null |jq . 
[
  {
    "id": 1,
    "name": "Administrator",
    "username": "root",
    "state": "active",
    "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
    "web_url": "http://3ff5a6afdc80/root"
  }
]
[root@devops ~]#

给项目加星

[root@devops ~]# curl --request POST --header "PRIVATE-TOKEN: sqiSUhn3tHYXe8nSGRDi" --data "name=demodefaultprj" "http://127.0.0.1:32001/api/v4/projects/1/star" |jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1864  100  1845  100    19  26963    277 --:--:-- --:--:-- --:--:-- 27132
{
  "id": 1,
  "description": null,
  "name": "demodefaultprj",
  "name_with_namespace": "Administrator / demodefaultprj",
  "path": "demodefaultprj",
  "path_with_namespace": "root/demodefaultprj",
  "created_at": "2018-01-30T16:19:01.367Z",
  "default_branch": null,
  "tag_list": [],
  "ssh_url_to_repo": "git@3ff5a6afdc80:root/demodefaultprj.git",
  "http_url_to_repo": "http://3ff5a6afdc80/root/demodefaultprj.git",
  "web_url": "http://3ff5a6afdc80/root/demodefaultprj",
  "avatar_url": null,
  "star_count": 1,
  "forks_count": 0,
  "last_activity_at": "2018-01-30T16:19:01.367Z",
  "_links": {
    "self": "http://3ff5a6afdc80/api/v4/projects/1",
    "issues": "http://3ff5a6afdc80/api/v4/projects/1/issues",
    "merge_requests": "http://3ff5a6afdc80/api/v4/projects/1/merge_requests",
    "repo_branches": "http://3ff5a6afdc80/api/v4/projects/1/repository/branches",
    "labels": "http://3ff5a6afdc80/api/v4/projects/1/labels",
    "events": "http://3ff5a6afdc80/api/v4/projects/1/events",
    "members": "http://3ff5a6afdc80/api/v4/projects/1/members"
  },
  "archived": false,
  "visibility": "private",
  "owner": {
    "id": 1,
    "name": "Administrator",
    "username": "root",
    "state": "active",
    "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
    "web_url": "http://3ff5a6afdc80/root"
  },
  "resolve_outdated_diff_discussions": false,
  "container_registry_enabled": true,
  "issues_enabled": true,
  "merge_requests_enabled": true,
  "wiki_enabled": true,
  "jobs_enabled": true,
  "snippets_enabled": true,
  "shared_runners_enabled": true,
  "lfs_enabled": true,
  "creator_id": 1,
  "namespace": {
    "id": 1,
    "name": "root",
    "path": "root",
    "kind": "user",
    "full_path": "root",
    "parent_id": null
  },
  "import_status": "none",
  "open_issues_count": 0,
  "public_jobs": true,
  "ci_config_path": null,
  "shared_with_groups": [],
  "only_allow_merge_if_pipeline_succeeds": false,
  "request_access_enabled": false,
  "only_allow_merge_if_all_discussions_are_resolved": false,
  "printing_merge_request_link_enabled": true
}
[root@devops ~]#

评星的操作对每个用户只能做一次,再次调用api,不会报错,但是也不会添加了

[root@devops ~]# curl --request POST --header "PRIVATE-TOKEN: sqiSUhn3tHYXe8nSGRDi" --data "name=demodefaultprj" "http://127.0.0.1:32001/api/v4/projects/1/star" |jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    19    0     0  100    19      0   1099 --:--:-- --:--:-- --:--:--  1117
[root@devops ~]# echo $?
0
[root@devops ~]# 

版本管理之gitlab实践教程:基础篇(11)_第5张图片

设定可见行

使用git clone的时候发现,需要输入用户名和密码,因为gitlab的private项目仅对项目成员可见,为了演示方便,将其设定为public,但在实际的项目中,请尽可能地将权限设紧。

[root@devops ~]# git clone http://127.0.0.1:32001/root/demodefaultprj.git
Cloning into 'demodefaultprj'...
Username for 'http://127.0.0.1:32001':

项目可见行有public/internal/private三种,我们这里将其设定为public

[root@devops ~]# curl --request PUT --header "PRIVATE-TOKEN: sqiSUhn3tHYXe8nSGRDi" --data "id=1&name=demodefaultprj&visibility=public" "http://127.0.0.1:32001/api/v4/projects/1" |jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1945  100  1903  100    42  28483    628 --:--:-- --:--:-- --:--:-- 28833
{
  "id": 1,
  "description": null,
  "name": "demodefaultprj",
  "name_with_namespace": "Administrator / demodefaultprj",
  "path": "demodefaultprj",
  "path_with_namespace": "root/demodefaultprj",
  "created_at": "2018-01-30T16:19:01.367Z",
  "default_branch": null,
  "tag_list": [],
  "ssh_url_to_repo": "git@3ff5a6afdc80:root/demodefaultprj.git",
  "http_url_to_repo": "http://3ff5a6afdc80/root/demodefaultprj.git",
  "web_url": "http://3ff5a6afdc80/root/demodefaultprj",
  "avatar_url": null,
  "star_count": 1,
  "forks_count": 0,
  "last_activity_at": "2018-01-30T16:19:01.367Z",
  "_links": {
    "self": "http://3ff5a6afdc80/api/v4/projects/1",
    "issues": "http://3ff5a6afdc80/api/v4/projects/1/issues",
    "merge_requests": "http://3ff5a6afdc80/api/v4/projects/1/merge_requests",
    "repo_branches": "http://3ff5a6afdc80/api/v4/projects/1/repository/branches",
    "labels": "http://3ff5a6afdc80/api/v4/projects/1/labels",
    "events": "http://3ff5a6afdc80/api/v4/projects/1/events",
    "members": "http://3ff5a6afdc80/api/v4/projects/1/members"
  },
  "archived": false,
  "visibility": "public",
  "owner": {
    "id": 1,
    "name": "Administrator",
    "username": "root",
    "state": "active",
    "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
    "web_url": "http://3ff5a6afdc80/root"
  },
  "resolve_outdated_diff_discussions": false,
  "container_registry_enabled": true,
  "issues_enabled": true,
  "merge_requests_enabled": true,
  "wiki_enabled": true,
  "jobs_enabled": true,
  "snippets_enabled": true,
  "shared_runners_enabled": true,
  "lfs_enabled": true,
  "creator_id": 1,
  "namespace": {
    "id": 1,
    "name": "root",
    "path": "root",
    "kind": "user",
    "full_path": "root",
    "parent_id": null
  },
  "import_status": "none",
  "import_error": null,
  "open_issues_count": 0,
  "runners_token": "biEyzj65oLvhVLmVzDbx",
  "public_jobs": true,
  "ci_config_path": null,
  "shared_with_groups": [],
  "only_allow_merge_if_pipeline_succeeds": false,
  "request_access_enabled": false,
  "only_allow_merge_if_all_discussions_are_resolved": false,
  "printing_merge_request_link_enabled": true
}
[root@devops ~]# 

从结果可以看到visibility已经被设定为public了, 另外确实任何人也都可以进行git clone的操作了

[root@devops ~]# git clone http://127.0.0.1:32001/root/demodefaultprj.git
Cloning into 'demodefaultprj'...
warning: You appear to have cloned an empty repository.
[root@devops ~]# cd demodefaultprj/
[root@devops demodefaultprj]# git branch
[root@devops demodefaultprj]#

创建一个group

在gitlab中的group就像是用来管理多个项目的文件夹一样,同时可通过对这个group的进行成员的添加删除以及访问权限的设定以达到对不同成员对项目的访问需要。我们可以通过api进行group的创建,需要设定的是name和path,比如我们创建如下的group:

项目 设定值
name demogroup
path demogrouppath
[root@devops ~]# curl --request POST --header "PRIVATE-TOKEN: sqiSUhn3tHYXe8nSGRDi" --data "name=demogroup&path=demogrouppath" "http://127.0.0.1:32001/api/v4/groups" |jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   348  100   315  100    33   1833    192 --:--:-- --:--:-- --:--:--  1842
{
  "id": 2,
  "name": "demogroup",
  "path": "demogrouppath",
  "description": "",
  "visibility": "private",
  "lfs_enabled": true,
  "avatar_url": null,
  "web_url": "http://3ff5a6afdc80/groups/demogrouppath",
  "request_access_enabled": false,
  "full_name": "demogroup",
  "full_path": "demogrouppath",
  "parent_id": null,
  "projects": [],
  "shared_projects": []
}
[root@devops ~]#

这样可以得到如下的group信息
版本管理之gitlab实践教程:基础篇(11)_第6张图片

确认用户信息

通过GET users的接口可以确认用户信息

[root@devops ~]# curl --request GET --header "PRIVATE-TOKEN: sqiSUhn3tHYXe8nSGRDi" "http://127.0.0.1:32001/api/v4/users" |jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   718  100   718    0     0  28055      0 --:--:-- --:--:-- --:--:-- 28720
[
  {
    "id": 1,
    "name": "Administrator",
    "username": "root",
    "state": "active",
    "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
    "web_url": "http://3ff5a6afdc80/root",
    "created_at": "2018-01-30T15:45:58.954Z",
    "bio": null,
    "location": null,
    "skype": "",
    "linkedin": "",
    "twitter": "",
    "website_url": "",
    "organization": null,
    "last_sign_in_at": "2018-01-30T15:48:09.249Z",
    "confirmed_at": "2018-01-30T15:45:58.780Z",
    "last_activity_on": null,
    "email": "[email protected]",
    "theme_id": 1,
    "color_scheme_id": 1,
    "projects_limit": 100000,
    "current_sign_in_at": "2018-01-30T15:48:09.249Z",
    "identities": [],
    "can_create_group": true,
    "can_create_project": true,
    "two_factor_enabled": false,
    "external": false,
    "is_admin": true
  }
]
[root@devops ~]#

这里可以看到目前唯一的一个用户root,我们接下来会创建一个新的用户demouser,然后将这个demouser添加到group中,同时对其赋予权限。

创建demouser

创建用户时需要有三个必填的选项,name/username和email,还有很多可选的选项,这里我们加上一个初始密码选项

项目 设定值
name demouser
username demousername
email [email protected]
[root@devops ~]# curl --request POST --header "PRIVATE-TOKEN: sqiSUhn3tHYXe8nSGRDi" --data "name=demouser&username=demousername&email=liumiaocn@outlook.com&password=liumiao123" "http://127.0.0.1:32001/api/v4/users" |jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   727  100   644  100    83   3282    423 --:--:-- --:--:-- --:--:--  3302
{
  "id": 2,
  "name": "demouser",
  "username": "demousername",
  "state": "active",
  "avatar_url": "http://www.gravatar.com/avatar/95c1f7ff72d71b448592a335ba80fb64?s=80&d=identicon",
  "web_url": "http://3ff5a6afdc80/demousername",
  "created_at": "2018-02-03T12:37:48.245Z",
  "bio": null,
  "location": null,
  "skype": "",
  "linkedin": "",
  "twitter": "",
  "website_url": "",
  "organization": null,
  "last_sign_in_at": null,
  "confirmed_at": null,
  "last_activity_on": null,
  "email": "[email protected]",
  "theme_id": 1,
  "color_scheme_id": 1,
  "projects_limit": 100000,
  "current_sign_in_at": null,
  "identities": [],
  "can_create_group": true,
  "can_create_project": true,
  "two_factor_enabled": false,
  "external": false
}
[root@devops ~]# 

向group中添加成员

向group中添加成员需要设定三个必填选项,gorupid/userid以及用户的访问权限,用户的访问权限在gitlab中有如下几种:

权限编码 权限说明
10 Guest权限
20 Reporter权限
30 Developer权限
40 Master权限
50 Owner权限
[root@devops ~]# curl --request POST --header "PRIVATE-TOKEN: sqiSUhn3tHYXe8nSGRDi" --data "id=2&user_id=2&access_level=30" "http://127.0.0.1:32001/api/v4/groups/2/members" |jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   276  100   246  100    30    626     76 --:--:-- --:--:-- --:--:--   627
{
  "id": 2,
  "name": "demouser",
  "username": "demousername",
  "state": "active",
  "avatar_url": "http://www.gravatar.com/avatar/95c1f7ff72d71b448592a335ba80fb64?s=80&d=identicon",
  "web_url": "http://3ff5a6afdc80/demousername",
  "access_level": 30,
  "expires_at": null
}
[root@devops ~]#

可以看到,demouser已经以developer的权限被加到demogroup中了
版本管理之gitlab实践教程:基础篇(11)_第7张图片

设定共享

目前我们已经有了一个demogroup,在这个group中有一个demouser的成员,而使用设定共享的api可以使得这个group和已经创建的项目建立关联。必须设定项目有如下几项

项目 说明 设定值
id 项目编号 1
group_id group编号
group_access group权限编码 30

group权限编码具体设定范围如下:

权限编码 权限说明
10 Guest权限
20 Reporter权限
30 Developer权限
40 Master权限
[root@devops ~]# curl --request POST --header "PRIVATE-TOKEN: sqiSUhn3tHYXe8nSGRDi" --data "id=1&group_id=2&group_access=30" "http://127.0.0.1:32001/api/v4/projects/1/share" |jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   103  100    72  100    31    947    408 --:--:-- --:--:-- --:--:--   960
{
  "id": 1,
  "project_id": 1,
  "group_id": 2,
  "group_access": 30,
  "expires_at": null
}
[root@devops ~]#

而从项目的的成员中也可以看出该group对此项目是可见的,当然前面即使不是项目成员,由于这个project是public属性,所有人都是可见的,但是并不是所有人都是项目成员,从这张图中可以清楚地看到demogroup与该项目的关联关系已经建立。
版本管理之gitlab实践教程:基础篇(11)_第8张图片

总结

这篇文章介绍了一下gitlab10的project相关的api操作,如何使用gitlab提供的api动态地创建项目/用户/group,以及如何将user加入到group中,以及如何在group和project之间建立关联。

你可能感兴趣的:(工具,DevOps,DevOps自动化工具,DevOps系列之:版本管理)