GitLab API - 通过仓库URL查找GitLab项目ID

4
1个回答

3

您可以使用代码库的路径从API中访问项目信息。您只需对路径进行URL编码即可。

使用项目API

project_path="gitlab-learn-labs%2fgitops%2fclassgroup-unilogik-2%2fshanekba%2fworld-greetings-env-1"
url="https://gitlab.com/api/v4/projects/${project_path}"
curl -s "${url}" | jq .id

输出:

38149446

作为从原始 URL 开始的完整示例:

project_url="https://gitlab.com/gitlab-learn-labs/gitops/classgroup-unilogik-2/shanekba/world-greetings-env-1"

project_path="$(cut -d/ -f4- <<< ${project_url})"
urlencoded_path="${project_path//'\/'/%2f}"  # use `urlencode` if installed, but this works in a pinch
curl -s "https://gitlab.com/api/v4/projects/${urlencoded_path}" | jq .id

使用GraphQL

你还可以使用 GraphQL API。像这样的 GraphQL 查询:

{
  project(fullPath: "gitlab-org/gitlab") {
    fullPath
    id
  }
}

响应中将会包含ID:

{
  "data": {
    "project": {
      "fullPath": "gitlab-org/gitlab",
      "id": "gid://gitlab/Project/278964"
    }
  }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接