關(guān)于GitLabAPI的詳細(xì)使用教程
1 簡介
GitLab 作為一個(gè)開源、強(qiáng)大的分布式版本控制系統(tǒng),已經(jīng)成為互聯(lián)網(wǎng)公司、軟件開發(fā)公司的主流版本管理工具。使用過 GitLab 的都知道,想要提交一段代碼,可以通過 git push 提交到遠(yuǎn)程倉庫,也可以直接在 GitLab 平臺上修改提交。然而上述兩種提交方式都是人工提交代碼,需要手動登錄 GitLab 或者在第一次 commit 的時(shí)候提供 GitLab 帳號和密碼。
那么,假設(shè)有這么一個(gè)需求場景:我們開發(fā)了一個(gè)效率平臺,可以自動拉分支、自動提交代碼到遠(yuǎn)程倉庫。這個(gè)需求該如何實(shí)現(xiàn)?其實(shí)很簡單,GitLab 提供了一套完整的 API,讓第三方平臺可以通過 API 自動創(chuàng)建帳號、自動提交代碼、自動拉分支,等等。API 涉及到的功能非常全面,覆蓋了分支、tag、代碼提交、用戶、群組、項(xiàng)目等,基本上人工可以做的所有操作,都可以通過 API 自動實(shí)現(xiàn)。
2 API 介紹
GitLab API 的使用可以參考你所使用的 GitLab 服務(wù)上的幫助文檔。也可以參考 GitLab API 官網(wǎng)文檔。
所有 API 請求都需要身份驗(yàn)證。您需要 private_token 通過 URL 或標(biāo)頭傳遞參數(shù)。如果作為標(biāo)頭傳遞,標(biāo)頭名稱必須是“PRIVATE-TOKEN”(大寫并用破折號代替下劃線)。您可以在個(gè)人資料中找到或重置您的私人令牌。
登錄您的 GitLab 賬號,在左側(cè)欄中選定【Profile Settings】,再在左側(cè)欄中選定【Account】,如下圖所示:

如果未提供或提供無效,private_token 則將返回錯(cuò)誤消息,狀態(tài)碼為 401:
{
"message": "401 Unauthorized"
}
API 請求應(yīng)以 API 的參數(shù)和 API 的版本為前綴。API 版本在 lib/api.rb 定義,例如,v4 API 的前綴就是/api/v4。
有效 API 請求示例:
GET http://gitlab.example.com/api/v4/projects?private_token=<your_access_token>
使用 curl 和通過標(biāo)頭身份驗(yàn)證的有效 API 請求示例:
curl --header "PRIVATE-TOKEN: <your_access_token>" "http://gitlab.example.com/api/v4/projects"
這兩個(gè)例子分別列舉了 token 作為參數(shù),和作為 Header 的使用方法。在我們的程序中,我們只需要選擇一種自己方便的方式就可以了。
API 使用 JSON 來序列化數(shù)據(jù)。您無需在 API URL 的末尾指定.json。
3 API 使用
3.1 獲取 Projects 數(shù)據(jù)
http://gitlab.example.com/api/v4/projects/<project_id>/repository/branches?private_token=<your_access_token>
通過官方文檔的說明,如果要獲取一個(gè)工程的分支數(shù)據(jù),除了 private_token 參數(shù)必填之外,還需要知道這個(gè)工程的 project_id,但從 GitLab 操作界面上并沒有工程 id 查看的入口。
所以需要獲取到所有 projects 的數(shù)據(jù),然后得到某個(gè) project 的所有參數(shù)數(shù)據(jù)。
把 URL 域名參數(shù)和 Token 參數(shù)替換成自己的,用 Linux 命令在終端測試獲取下數(shù)據(jù):
curl --header "PRIVATE-TOKEN:<your_access_token>" "http://gitlab.example.com/api/v4/projects"
執(zhí)行之后獲取到的數(shù)據(jù)是默認(rèn)前20條(默認(rèn)一個(gè)頁面20條數(shù)據(jù)),JSON 數(shù)據(jù)結(jié)構(gòu)如下,可以看到里面的 id 字段就是請求分支數(shù)據(jù)需要的 id 參數(shù)。
[
{
"id": 1234,
"description": null,
"name": "Diaspora Client",
"name_with_namespace": "Diaspora / Diaspora Client",
"path": "diaspora-client",
"path_with_namespace": "diaspora/diaspora-client",
"created_at": "2022-09-30T13:46:02Z",
"default_branch": "main",
"tag_list": [
"example",
"disapora client"
],
"topics": [
"example",
"disapora client"
],
"ssh_url_to_repo": "git@gitlab.example.com:diaspora/diaspora-client.git",
"http_url_to_repo": "https://gitlab.example.com/diaspora/diaspora-client.git",
"web_url": "https://gitlab.example.com/diaspora/diaspora-client",
"readme_url": "https://gitlab.example.com/diaspora/diaspora-client/blob/master/README.md",
"avatar_url": "https://gitlab.example.com/uploads/project/avatar/4/uploads/avatar.png",
"forks_count": 0,
"star_count": 0,
"last_activity_at": "2022-09-30T13:46:02Z",
"namespace": {
"id": 2,
"name": "Diaspora",
"path": "diaspora",
"kind": "group",
"full_path": "diaspora",
"parent_id": null,
"avatar_url": null,
"web_url": "https://gitlab.example.com/diaspora"
}
},
{
...
}
]
3.2 獲取指定 Project 數(shù)據(jù)
如果 GitLab 上有幾百個(gè)工程,總不能把所有的都獲取下來再去過濾吧,通過查看 API 文檔可以用 search 參數(shù)根據(jù) project 名稱去搜索想要獲取的 project 數(shù)據(jù),比如這邊要查找 test 項(xiàng)目的數(shù)據(jù)。示例請求:
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects?search=test"
通過上面這條命令就可以獲取到項(xiàng)目名包含 test 的前20條數(shù)據(jù)(官網(wǎng)文檔描述默認(rèn)20,最大100,可通過 per_page 參數(shù)設(shè)置)。
3.3 獲取 branches 數(shù)據(jù)
通過前面的步驟獲取到 test 項(xiàng)目的數(shù)據(jù)之后,知道這個(gè)project的 id 值,就可以接著通過 id 參數(shù)來獲取這個(gè) project 的所有分支數(shù)據(jù)。示例請求:
curl --header "PRIVATE-TOKEN:<your_access_token>" "http://gitlab.xxxxxxx.com/api/v4/projects/<project_id>/repository/branches"
示例響應(yīng):
[
{
"name": "main",
"merged": false,
"protected": true,
"default": true,
"developers_can_push": false,
"developers_can_merge": false,
"can_push": true,
"web_url": "https://gitlab.example.com/my-group/my-project/-/tree/main",
"commit": {
"author_email": "john@example.com",
"author_name": "John Smith",
"authored_date": "2022-06-27T05:51:39-07:00",
"committed_date": "2022-06-28T03:44:20-07:00",
"committer_email": "john@example.com",
"committer_name": "John Smith",
"id": "7b5c3cc8be40ee161ae89a06bba6229da1032a0c",
"short_id": "7b5c3cc",
"title": "add projects API",
"message": "add projects API",
"parent_ids": [
"4ad91d3c1144c406e50c7b33bae684bd6837faf8"
]
}
},
...
]
3.4 獲取指定 branche 數(shù)據(jù)
上面是獲取這個(gè) project 的所有分支數(shù)據(jù),如果要獲取指定分支的數(shù)據(jù):
GET /projects/:id/repository/branches/:branch
比如獲取 master 分支的數(shù)據(jù),示例請求:
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/<project_id>/repository/branches/master"
示例響應(yīng):
{
"name": "master",
"merged": false,
"protected": true,
"default": true,
"developers_can_push": false,
"developers_can_merge": false,
"can_push": true,
"web_url": "https://gitlab.example.com/my-group/my-project/-/tree/main",
"commit": {
"author_email": "john@example.com",
"author_name": "John Smith",
"authored_date": "2022-06-27T05:51:39-07:00",
"committed_date": "2022-06-28T03:44:20-07:00",
"committer_email": "john@example.com",
"committer_name": "John Smith",
"id": "7b5c3cc8be40ee161ae89a06bba6229da1032a0c",
"short_id": "7b5c3cc",
"title": "add projects API",
"message": "add projects API",
"parent_ids": [
"4ad91d3c1144c406e50c7b33bae684bd6837faf8"
]
}
}
3.5 獲取倉庫提交列表
獲取項(xiàng)目中的倉庫提交列表:
GET /projects/:id/repository/commits
示例請求:
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/<project_id>/repository/commits"
示例響應(yīng):
[
{
"id": "ed899a2f4b50b4370feeea94676502b42383c746",
"short_id": "ed899a2f4b5",
"title": "Replace sanitize with escape once",
"author_name": "Example User",
"author_email": "user@example.com",
"authored_date": "2022-09-20T11:50:22.001+00:00",
"committer_name": "Administrator",
"committer_email": "admin@example.com",
"committed_date": "2022-09-20T11:50:22.001+00:00",
"created_at": "2022-09-20T11:50:22.001+00:00",
"message": "Replace sanitize with escape once",
"parent_ids": [
"6104942438c14ec7bd21c6cd5bd995272b3faff6"
],
"web_url": "https://gitlab.example.com/thedude/gitlab-foss/-/commit/ed899a2f4b50b4370feeea94676502b42383c746"
},
{
"id": "6104942438c14ec7bd21c6cd5bd995272b3faff6",
"short_id": "6104942438c",
"title": "Sanitize for network graph",
"author_name": "randx",
"author_email": "user@example.com",
"committer_name": "ExampleName",
"committer_email": "user@example.com",
"created_at": "2022-09-20T09:06:12.201+00:00",
"message": "Sanitize for network graph",
"parent_ids": [
"ae1d9fb46aa2b07ee9836d49862ec4e2c46fbbba"
],
"web_url": "https://gitlab.example.com/thedude/gitlab-foss/-/commit/ed899a2f4b50b4370feeea94676502b42383c746"
}
]
到此這篇關(guān)于關(guān)于GitLabAPI的詳細(xì)使用教程的文章就介紹到這了,更多相關(guān)GitLabAPI使用教程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ISAPI-REWRITE偽靜態(tài)規(guī)則寫法以及說明
ISAPI-REWRITE偽靜態(tài)規(guī)則寫法以及說明,很多朋友對rewrite的規(guī)則不太熟悉,這里介紹下,方便需要的朋友2012-06-06
ssh服務(wù)器拒絕了密碼 請?jiān)僭囈淮我呀鉀Q(親測有效)
這篇文章主要介紹了解決ssh服務(wù)器拒絕了密碼 請?jiān)僭囈淮蔚膯栴},本文通過兩種方法給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
github的使用_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了github使用的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
xampp中apache在windows7下無法啟動的解決方法
下載zip版本的xampp,無法啟動apache ,現(xiàn)象是短暫顯示了running標(biāo)志之后就停止了,日志顯示W(wǎng)ARNING:terminating worker thread 0。2010-03-03
IIS與APACHE實(shí)現(xiàn)HTTP重定向到HTTPS
本文介紹IIS7和Apache上實(shí)現(xiàn)訪問HTTP跳轉(zhuǎn)到HTTPS訪問的方法,網(wǎng)站設(shè)計(jì)出于安全的考慮需要使用https協(xié)議,但不少用戶因?yàn)檩斎刖W(wǎng)址的習(xí)慣不喜歡帶上https協(xié)議,導(dǎo)致訪問異常,因此需要一種重定向功能,實(shí)現(xiàn)HTTP網(wǎng)站重定向到HTTPS網(wǎng)站的方法,具體操作如下2019-02-02
兩臺服務(wù)器之間無密碼傳輸數(shù)據(jù)和操作的方法
這篇文章主要介紹了兩臺服務(wù)器之間無密碼傳輸數(shù)據(jù)和操作的方法,需要的朋友可以參考下2017-04-04
詳解阿里云服務(wù)器添加安全組規(guī)則(圖文教程)
這篇文章主要介紹了詳解阿里云服務(wù)器添加安全組規(guī)則(圖文教程),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2020-07-07

