GitLab基础:使用API获取某个项目下所有成员及权限信息

这篇文章记录一下对于一个GitLab的老版本(8.11.2)使用API获取某个项目下的所有成员权限的列表的方法。


背景

因一个特殊的对应需求,无法直接使用GitLab直接提供功能进行备份,其实GitLab提供了不少有用的组合形态的API可以用于此方面处理。

API

查看了一下目前官方的最新API(v4)版本,对于groups和projects下面用户的权限信息,主要如下所示:

groups下成员查询:curl --header “PRIVATE-TOKEN: <your_access_token>” “https://gitlab.example.com/api/v4/groups/:id/members”

projects下成员查询:curl --header “PRIVATE-TOKEN: <your_access_token>” “https://gitlab.example.com/api/v4/projects/:id/members”

已知问题

在有继承关系时groups下的成员权限有时显示可能会不太正确,详细可参看如下issue:

事前准备

  • yaml文件
liumiaocn:gitlab liumiao$ cat docker-compose.yml 
version: '2'    
services:
  # Version Control service: Gitlab
  gitlab:
    image: gitlab/gitlab-ce:8.11.2-ce.1
    ports:
      - "32001:80"
      - "30022:22"
      - "443:443"
    volumes:
      - /Users/liumiao/gitlab/log/:/var/log/gitlab
      - /Users/liumiao/gitlab/data/:/var/opt/gitlab
      - /Users/liumiao/gitlab/conf/:/etc/gitlab
    restart: "no"
liumiaocn:gitlab liumiao$ 
  • 启动状态
liumiaocn:gitlab liumiao$ docker-compose ps
     Name             Command       State                                 Ports                               
--------------------------------------------------------------------------------------------------------------
gitlab_gitlab_1   /assets/wrapper   Up      0.0.0.0:30022->22/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:32001->80/tcp
liumiaocn:gitlab liumiao$ 
  • project/user准备
    在这里插入图片描述
    project2的详细信息如下
    在这里插入图片描述
  • token准备
    在此版本中仍然有private token,不过本文中使用的仍然是Access Token的方式,使用此Token也没有问题,这个版本中仍然有session api。
    在这里插入图片描述

查询

  • 查询 project下的成员信息
liumiaocn:gitlab liumiao$ curl --header "PRIVATE-TOKEN: LW_PqexsjxVgVY4yJQs1" "http://localhost:32001/api/v3/projects/2/members" 2>/dev/null |jq .
[
  {
    "name": "user4",
    "username": "user4",
    "id": 5,
    "state": "active",
    "avatar_url": "http://www.gravatar.com/avatar/7cc73669801d4e98648067643fe4b055?s=80&d=identicon",
    "web_url": "http://6438783cc6ca/u/user4",
    "access_level": 40,
    "expires_at": null
  },
  {
    "name": "user3",
    "username": "user3",
    "id": 4,
    "state": "active",
    "avatar_url": "http://www.gravatar.com/avatar/8d967bc57202b6d44618dcb0753afe5d?s=80&d=identicon",
    "web_url": "http://6438783cc6ca/u/user3",
    "access_level": 30,
    "expires_at": null
  },
  {
    "name": "user2",
    "username": "user2",
    "id": 3,
    "state": "active",
    "avatar_url": "http://www.gravatar.com/avatar/cc00a8c917457e6e7259900fc25ac879?s=80&d=identicon",
    "web_url": "http://6438783cc6ca/u/user2",
    "access_level": 20,
    "expires_at": null
  },
  {
    "name": "user1",
    "username": "usr1",
    "id": 2,
    "state": "active",
    "avatar_url": "http://www.gravatar.com/avatar/eb7c8c7791f4f4c7cdd712635277a1f2?s=80&d=identicon",
    "web_url": "http://6438783cc6ca/u/usr1",
    "access_level": 10,
    "expires_at": null
  }
]
liumiaocn:gitlab liumiao$ 
  • 查询groups下的成员信息
liumiaocn:gitlab liumiao$ curl --header "PRIVATE-TOKEN: LW_PqexsjxVgVY4yJQs1" "http://localhost:32001/api/v3/groups/1/members" 2>/dev/null |jq .
{
  "message": "404 Group Not Found"
}
liumiaocn:gitlab liumiao$ curl --header "PRIVATE-TOKEN: LW_PqexsjxVgVY4yJQs1" "http://localhost:32001/api/v3/groups/2/members" 2>/dev/null |jq .
[
  {
    "name": "Administrator",
    "username": "root",
    "id": 1,
    "state": "active",
    "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
    "web_url": "http://6438783cc6ca/u/root",
    "access_level": 50,
    "expires_at": null
  }
]
liumiaocn:gitlab liumiao$
  • 使用Private Token也没有问题
liumiaocn:gitlab liumiao$ curl --header "PRIVATE-TOKEN: F-cDq99zfnZveHk_VajZ" "http://localhost:32001/api/v3/groups/2/members" 2>/dev/null |jq .
[
  {
    "name": "Administrator",
    "username": "root",
    "id": 1,
    "state": "active",
    "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
    "web_url": "http://6438783cc6ca/u/root",
    "access_level": 50,
    "expires_at": null
  }
]
liumiaocn:gitlab liumiao$ 
liumiaocn:gitlab liumiao$ curl --header "PRIVATE-TOKEN: F-cDq99zfnZveHk_VajZ" "http://localhost:32001/api/v3/projects/2/members" 2>/dev/null |jq .
[
  {
    "name": "user4",
    "username": "user4",
    "id": 5,
    "state": "active",
    "avatar_url": "http://www.gravatar.com/avatar/7cc73669801d4e98648067643fe4b055?s=80&d=identicon",
    "web_url": "http://6438783cc6ca/u/user4",
    "access_level": 40,
    "expires_at": null
  },
  {
    "name": "user3",
    "username": "user3",
    "id": 4,
    "state": "active",
    "avatar_url": "http://www.gravatar.com/avatar/8d967bc57202b6d44618dcb0753afe5d?s=80&d=identicon",
    "web_url": "http://6438783cc6ca/u/user3",
    "access_level": 30,
    "expires_at": null
  },
  {
    "name": "user2",
    "username": "user2",
    "id": 3,
    "state": "active",
    "avatar_url": "http://www.gravatar.com/avatar/cc00a8c917457e6e7259900fc25ac879?s=80&d=identicon",
    "web_url": "http://6438783cc6ca/u/user2",
    "access_level": 20,
    "expires_at": null
  },
  {
    "name": "user1",
    "username": "usr1",
    "id": 2,
    "state": "active",
    "avatar_url": "http://www.gravatar.com/avatar/eb7c8c7791f4f4c7cdd712635277a1f2?s=80&d=identicon",
    "web_url": "http://6438783cc6ca/u/usr1",
    "access_level": 10,
    "expires_at": null
  }
]
liumiaocn:gitlab liumiao$ 

总结

淼叔 CSDN认证博客专家 神经网络 TensorFlow NLP
资深架构师,PMP、OCP、CSM、HPE University讲师,EXIN DevOps Professional与DevOps Master认证讲师,曾担任HPE GD China DevOps & Agile Leader,帮助企业级客户提供DevOps咨询培训以及实施指导。熟悉通信和金融领域,有超过十年金融外汇行业的架构设计、开发、维护经验,在十几年的IT从业生涯中拥有了软件开发设计领域接近全生命周期的经验和知识积累,著有企业级DevOps技术与工具实战。
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页