为了所有Gitlab 服务器上所有projectName、path、nameSpace、branchs等信息,需要用到Gitlab API.
首先需要配置PRIVATE-TOKEN,在Gitlab Server,Account-setting中配置TOKEN
开始API操作
浏览具有访问权限的项目:
curl --header "PRIVATE-TOKEN: EeVPPydkxJuxhaabbbgb1" https://git.kavenran.com/api/v4/projects
返回的JSON数据如(官方示例):1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62[
{
"id": 4,
"description": null,
"default_branch": "master",
"visibility": "private",
"ssh_url_to_repo": "git@example.com:diaspora/diaspora-client.git",
"http_url_to_repo": "http://example.com/diaspora/diaspora-client.git",
"web_url": "http://example.com/diaspora/diaspora-client",
"tag_list": [
"example",
"disapora client"
],
"owner": {
"id": 3,
"name": "Diaspora",
"created_at": "2013-09-30T13:46:02Z"
},
"name": "Diaspora Client",
"name_with_namespace": "Diaspora / Diaspora Client",
"path": "diaspora-client",
"path_with_namespace": "diaspora/diaspora-client",
"issues_enabled": true,
"open_issues_count": 1,
"merge_requests_enabled": true,
"jobs_enabled": true,
"wiki_enabled": true,
"snippets_enabled": false,
"container_registry_enabled": false,
"created_at": "2013-09-30T13:46:02Z",
"last_activity_at": "2013-09-30T13:46:02Z",
"creator_id": 3,
"namespace": {
"id": 3,
"name": "Diaspora",
"path": "diaspora",
"kind": "group",
"full_path": "diaspora"
},
"import_status": "none",
"archived": false,
"avatar_url": "http://example.com/uploads/project/avatar/4/uploads/avatar.png",
"shared_runners_enabled": true,
"forks_count": 0,
"star_count": 0,
"runners_token": "b8547b1dc37721d05889db52fa2f02",
"public_jobs": true,
"shared_with_groups": [],
"only_allow_merge_if_pipeline_succeeds": false,
"only_allow_merge_if_all_discussions_are_resolved": false,
"request_access_enabled": false,
"approvals_before_merge": 0,
"statistics": {
"commit_count": 37,
"storage_size": 1038090,
"repository_size": 1038090,
"lfs_objects_size": 0,
"job_artifacts_size": 0
}
},
...
]
如果觉得返回数据太多,可以使用简化版本:curl --header "PRIVATE-TOKEN: EeVPPydkxJuxhqnaaagb1" https://git.kavenran.com/api/v4/projects?simple=true
返回数据格式如:
1 | [ |
特别注意,返回的数据是分页的,每页默认20条,如果项目较多需要使用分页参数。curl --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" https://gitlab.example.com/api/v4/projects/8/issues/8/notes?per_page=50
如何知道有多少页呢?
curl --head --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" https://gitlab.example.com/api/v4/projects/8/issues/8/notes?per_page=50
1 | HTTP/1.1 200 OK |
X-Total-Pages: 3 就是总的页数,访问下一页:
curl --head --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" https://gitlab.example.com/api/v4/projects/8/issues/8/notes?per_page=50&pages=2
其它API接口及参数请查阅:
https://docs.gitlab.com/ee/api/README.html
如果本文到这里就结束了,那只能是一篇坑文,下面shell库实现了获取服务器所有项目信息,并组装为以下格式的数据:
|| groupname || projectname || https_url || default_branch ||
1 |
|
获取的数据在.gitlab_result中:1
2
3SkyOne DESK https://kaven@git.kavenran.com/SkyOne/DESK.git master
SkyOne XBING https://kaven@git.kavenran.com/SkyOne/XBING.git master
SkyTwo WANL https://kaven@git.kavenran.com/SkyTwo/WANL.git master