首页 > 解决方案 > 按分支和消息过滤 gitlab 提交 - gitlab API

问题描述

我正在编写使用 gitlab api 的应用程序。

为此,我想通过分支和提交消息搜索/过滤提交。

但是 Gitlab 支持两个端点,它们是

搜索 :

/api/v4/projects/24/search?scope=commits&search=fixed

这将仅按消息过滤,而不是按分支过滤。

从提交 api

/projects/:id/repository/commits

存储库提交

即使这种支持也只支持少数参数。

但我想过滤为特定分支完成的提交。

标签: gitgitlabgitlab-api

解决方案


不要认为您可以在单个 api 调用中通过消息和分支获得过滤响应。

  1. 您可以使用 commits api 来获取按分支过滤的所有提交,然后您可以在应用程序中按消息过滤它们。

GET /projects/:id/repository/commits?ref_name=<your branch name>

示例响应:

[
  {
    "id": "ed899a2f4b50b4370feeea94676502b42383c746",
    "short_id": "ed899a2f4b5",
    "title": "Replace sanitize with escape once",
    "author_name": "Example User",
    "author_email": "user@example.com",
    "authored_date": "2012-09-20T11:50:22+03:00",
    "committer_name": "Administrator",
    "committer_email": "admin@example.com",
    "committed_date": "2012-09-20T11:50:22+03:00",
    "created_at": "2012-09-20T11:50:22+03:00",
    "message": "Replace sanitize with escape once",   ----> Message
    "parent_ids": [
      "6104942438c14ec7bd21c6cd5bd995272b3faff6"
    ],
    "web_url": "https://gitlab.example.com/thedude/gitlab-foss/-/commit/ed899a2f4b50b4370feeea94676502b42383c746"
  }
]
  1. 或者您可以先按消息搜索,然后获取提交所属的引用列表,GET /projects/:id/repository/commits/:sha/refs 但这将需要两个 API 调用,一个是获取按消息过滤的提交,然后是获取提交所属的引用列表。
[
  {"type": "branch", "name": "'test'"},
  {"type": "branch", "name": "add-balsamiq-file"},
  {"type": "branch", "name": "wip"},
  {"type": "tag", "name": "v1.1.0"}
 ]

推荐阅读