545

GitLab API使用

 4 years ago
source link: https://www.chenwenguan.com/how-to-use-gitlab-api/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

项目上需要读取GitLab工程的分支数据,做Jenkins编包参数输入自动匹配的处理,本文记录下通过GitLab API获取GitLab工程分支branches数据的详细操作步骤,其他操作可参考 官方GitLab API文档

一、GitLab API获取Projects数据

http://gitlab.xxxxxxx.com/api/v3/projects/id/repository/branches?private_token=abcdefghijk

通过官方文档的说明,如果要获取一个工程的分支数据,除了private_token参数必填之外,还需要知道这个工程的 id ,但从GitLab操作界面上并没有工程id查看的入口。

http://gitlab.xxxxxxx.com/profile/account

private token参数可以通过界面入口查看,一般在profile->account路径下。

逆向思考,这个id是在projects下面的,如果获取到所有projects的数据之后,里面应该会有独立project的所有参数数据。

假设Token参数为 abcdefghijk ,URL域名参数替换成自己的,这边用 xxxxxxx 字符串代替, 接着用Linux命令在终端测试获取下数据:

curl --header "PRIVATE-TOKEN:abcdefghijk" "http://gitlab.xxxxxxx.com/api/v3/projects"

执行之后获取到的数据是默认前20条(默认一个页面20条数据),JSON数据结构如下,可以看到里面的id字段就是请求分支数据需要的id参数。

[{
        "id": 1480889,
        "path": "sample-test",
        "path_with_namespace": "sample-project/sample-test",
        "description": "***********",
        "default_branch": "master",
        "tag_list": [

        ],
        "public": false,
        "archived": false,
        "visibility_level": 0,
        "ssh_url_to_repo": "[email protected]:sample-project/sample-test.git",
        "http_url_to_repo": "http://gitlab.xxxxxxx.com/sample-project/sample-test.git",
        "web_url": "http://gitlab.xxxxxxx.com/sample-project/sample-test",
        "name": "sample-test",
        "name_with_namespace": "sample-project/sample-test",
        "issues_enabled": true,
        "merge_requests_enabled": true,
        "wiki_enabled": true,
        "builds_enabled": true,
        "snippets_enabled": false,
        "created_at": "2019-09-16T15:24:37.000+08:00",
        "last_activity_at": "2019-11-14T19:37:31.000+08:00",
        "shared_runners_enabled": true,
        "creator_id": 72835,
        "namespace": {
            "id": 88067,
            "name": "sample-project",
            "path": "sample-project",
            "owner_id": null,
            "created_at": "2018-01-03T10:52:13.000+08:00",
            "updated_at": "2018-01-03T10:52:13.000+08:00",
            "description": "*************",
            "state": "",
            "avatar": {
              "url": null
            },
        "public": false,
            "parent_id": null,
            "visibility_level": 10,
            "organization_id": null
        },
        "avatar_url": null,
        "checkemail": true
},...]

二、GitLab API获取指定Project数据

如果GitLab上有几百个工程,总不能把所有的都获取下来再去过滤吧,通过查看API文档可以用search参数去过滤想要获取的project数据,比如这边要查找的是awsome_android_2.0项目的数据。

curl --header "PRIVATE-TOKEN:abcdefghijk" "http://gitlab.xxxxxxx.com/api/v3/projects?search=awsome_android_2.0"

通过上面这条命令获取到的还是以awsome_android_2.0开头的前20条数据,可GitLab仓库实际有上百条数据,这就需要获取后面页数的数据,或者直接把翻页的数据个数设置到100(官网文档描述默认20,最大100)来一次性获取所有数据。

如果要查找awsome_android_2.0项目下sample-test工程的数据,URL地址是

http://gitlab.xxxxxxx.com/awsome_android_2.0/sample-test

不能通过search参数赋值awsome_android_2.0/sample-test直接查询,测试返回的结果是空的。

三、GitLab API获取翻页数据

这边翻页的参数用per_page,如果要获取指定页数的数据用参数page。

curl --header "PRIVATE-TOKEN:abcdefghijk" "http://gitlab.xxxxxxx.com/api/v3/projects?search=awsome_android_2.0&per_page=100"

如果要查看到底有几页数据,可以加个head参数来查看返回结果

curl --head --header "PRIVATE-TOKEN:abcdefghijk" "http://gitlab.xxxxxxx.com/api/v3/projects?search=awsome_android_2.0&per_page=100"

返回结果示例:

HTTP/1.1 200 OK
Server: Tengine
Date: Sun, 01 Dec 2019 08:58:06 GMT
Content-Type: application/json
Content-Length: 79552
Connection: keep-alive
Vary: Accept-Encoding
Cache-Control: no-cache
Link: <http://gitlab.xxxxxxx.com/api/v3/projects?page=1&per_page=100>; rel="first", 
<http://gitlab.xxxxxxx.com/api/v3/projects?page=1&per_page=100>; rel="last"
Status: 200 OK
Vary: Origin
X-Request-Id: **********************
X-Runtime: 0.501186

我这边没有显示官网介绍的 X-Total-Pages 参数,这个参数就是显示有几页数据,估计是公司接口有修改。

四、GitLab API获取branches数据

通过前面的步骤获取到awsome_android_2.0仓库下所有工程的数据之后,知道这个工程的id值,就可以接着通过id参数来获取这个工程的所有分支数据。

比如在获取到的所有工程数据中,过滤查找到awsome_android_2.0/sample-test工程的JSON数据, 145168 是这个工程的id参数,通过下面的命令可以获取到所有分支数据。

curl --header "PRIVATE-TOKEN:abcdefghijk" "http://gitlab.xxxxxxx.com/api/v3/projects/145168/repository/branches"

返回结果示例:

[{
     "name": "dev_custom_test_001",
     "commit": {
         "id": "*********************",
         "message": "Merge remote-tracking branch 'origin/dev_custom_test' into dev_custom_test\n",
         "parent_ids": [
         "****************",
         "****************"
         ],
         "authored_date": "2019-01-04T16:14:29.000+08:00",
         "author_name": "abc",
         "author_email": "[email protected]",
         "committed_date": "2019-01-04T16:14:29.000+08:00",
         "committer_name": "abc",
         "committer_email": "[email protected]"
     },
     "protected": false
},...]

五、AngularJS Http请求GitLab API

上面通过Linux命令已经把数据请求测试通过了,接着是在AngularJS Http中配置请求实现,下面给出简化示例代码:

var requestPath = '/api/v3/projects/145168/repository/branches?private_token=abcdefghijk';
var option = {
  hostname: 'xxxxxxx.com',
  port: 80,
  method: 'GET',
  path: requestPath
}
var req = http.request(option, function (response) {

}

六、其他参考资料

Node GitLab封装接口调用


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK