溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

GitLab API如何使用教程

發(fā)布時間:2023-03-31 11:20:48 來源:億速云 閱讀:114 作者:iii 欄目:開發(fā)技術

這篇文章主要介紹了GitLab API如何使用教程的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇GitLab API如何使用教程文章都會有所收獲,下面我們一起來看看吧。

1 簡介

GitLab 作為一個開源、強大的分布式版本控制系統(tǒng),已經成為互聯(lián)網(wǎng)公司、軟件開發(fā)公司的主流版本管理工具。使用過 GitLab 的都知道,想要提交一段代碼,可以通過 git push 提交到遠程倉庫,也可以直接在 GitLab 平臺上修改提交。然而上述兩種提交方式都是人工提交代碼,需要手動登錄 GitLab 或者在第一次 commit 的時候提供 GitLab 帳號和密碼。
那么,假設有這么一個需求場景:我們開發(fā)了一個效率平臺,可以自動拉分支、自動提交代碼到遠程倉庫。這個需求該如何實現(xiàn)?其實很簡單,GitLab 提供了一套完整的 API,讓第三方平臺可以通過 API 自動創(chuàng)建帳號、自動提交代碼、自動拉分支,等等。API 涉及到的功能非常全面,覆蓋了分支、tag、代碼提交、用戶、群組、項目等,基本上人工可以做的所有操作,都可以通過 API 自動實現(xiàn)。

2 API 介紹

GitLab API 的使用可以參考你所使用的 GitLab 服務上的幫助文檔。也可以參考 GitLab API 官網(wǎng)文檔。
所有 API 請求都需要身份驗證。您需要 private_token 通過 URL 或標頭傳遞參數(shù)。如果作為標頭傳遞,標頭名稱必須是“PRIVATE-TOKEN”(大寫并用破折號代替下劃線)。您可以在個人資料中找到或重置您的私人令牌。
登錄您的 GitLab 賬號,在左側欄中選定【Profile Settings】,再在左側欄中選定【Account】,如下圖所示:

GitLab API如何使用教程

如果未提供或提供無效,private_token 則將返回錯誤消息,狀態(tài)碼為 401:

{
  "message": "401 Unauthorized"
}

API 請求應以 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 和通過標頭身份驗證的有效 API 請求示例:

curl --header "PRIVATE-TOKEN: <your_access_token>" "http://gitlab.example.com/api/v4/projects"

這兩個例子分別列舉了 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>

通過官方文檔的說明,如果要獲取一個工程的分支數(shù)據(jù),除了 private_token 參數(shù)必填之外,還需要知道這個工程的 project_id,但從 GitLab 操作界面上并沒有工程 id 查看的入口。
所以需要獲取到所有 projects 的數(shù)據(jù),然后得到某個 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ù)是默認前20條(默認一個頁面20條數(shù)據(jù)),JSON 數(shù)據(jù)結構如下,可以看到里面的 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 上有幾百個工程,總不能把所有的都獲取下來再去過濾吧,通過查看 API 文檔可以用 search 參數(shù)根據(jù) project 名稱去搜索想要獲取的 project 數(shù)據(jù),比如這邊要查找 test 項目的數(shù)據(jù)。示例請求:

curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects?search=test"

通過上面這條命令就可以獲取到項目名包含 test 的前20條數(shù)據(jù)(官網(wǎng)文檔描述默認20,最大100,可通過 per_page 參數(shù)設置)。

3.3 獲取 branches 數(shù)據(jù)

通過前面的步驟獲取到 test 項目的數(shù)據(jù)之后,知道這個project的 id 值,就可以接著通過 id 參數(shù)來獲取這個 project 的所有分支數(shù)據(jù)。示例請求:

curl --header "PRIVATE-TOKEN:<your_access_token>" "http://gitlab.xxxxxxx.com/api/v4/projects/<project_id>/repository/branches"

示例響應:

[
  {
    "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ù)

上面是獲取這個 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"

示例響應:

{
  "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 獲取倉庫提交列表

獲取項目中的倉庫提交列表:

GET /projects/:id/repository/commits

示例請求:

curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/<project_id>/repository/commits"

示例響應:

[
  {
    "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"
  }
]

關于“GitLab API如何使用教程”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“GitLab API如何使用教程”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI