gpt4 book ai didi

api - GitLab API - 通过 repo URL 查找 GitLab 项目 ID

转载 作者:行者123 更新时间:2023-12-05 03:20:32 26 4
gpt4 key购买 nike

我有一个 GitLab 存储库 url,试图使用 GitLab API 查找项目 ID。看着这个https://docs.gitlab.com/ee/api/projects.html#list-all-projects页面我没有找到按 repo url 搜索的选项。

例如。

给出这个 URL https://gitlab.com/gitlab-learn-labs/gitops/classgroup-unilogik-2/shanekba/world-greetings-env-1

找到项目 ID:38149446

有没有找到这个项目 ID 的选项?

谢谢

最佳答案

您可以使用存储库的路径从 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"
}
}
}

关于api - GitLab API - 通过 repo URL 查找 GitLab 项目 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73142806/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com