git 组下所有项目的 Gitlab API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31498473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Gitlab API for all projects under group
提问by Raghuveer
I want to get a list of all the projects which are under a particular group in Gitlab. Here is the example scenario:
我想获得 Gitlab 中特定组下所有项目的列表。这是示例场景:
Group A (id: 1) has 3 Project
A组(ID:1)有3个项目
Group A / Project 1
Group A / Project 2
Group A / Project 3
A组/项目1
A组/项目2
A组/项目3
Group B (id: 2) has 5 Projects
B 组 (id: 2) 有 5 个项目
Group B / Project 1
Group B / Project 2
Group B / Project 3
Group B / Project 4
Group B / Project 5
B组/项目1
B组/项目2
B组/项目3
B组/项目4
B组/项目5
Now if I hit the rest api GET /groups
it will give me only the list of groups. If i hit the rest api GET /projects/all
, it will give me a list of all the projects.
现在,如果我点击其余的 api,GET /groups
它只会给我组列表。如果我点击了其余的 api GET /projects/all
,它会给我一个所有项目的列表。
What I am looking for, is an operation something like GET /groups/:groupid/projects/all
我正在寻找的是类似的操作 GET /groups/:groupid/projects/all
That is: all the projects for that particular group. Like if I say GET /groups/1/projects/all
it will give me Project 1, Project 2 and Project 3
.
即:该特定组的所有项目。就像如果我说它GET /groups/1/projects/all
会给我Project 1, Project 2 and Project 3
。
The only way I can think of is to get a list of all the projects and loop over them to see if it matches my group name, but this will be lot of unnecessary parsing.
我能想到的唯一方法是获取所有项目的列表并遍历它们以查看它是否与我的组名匹配,但这将是很多不必要的解析。
How can I achieve this in a better way?
我怎样才能以更好的方式实现这一目标?
I am working on Gitlab CE 7.2.1. I am referring the Gitlab API documententation
我正在研究 Gitlab CE 7.2.1。我指的是Gitlab API 文档
采纳答案by Dave Kirk
I was looking to do something similar, getting all of the projects from a number of groups.
我希望做一些类似的事情,从多个小组中获取所有项目。
There are 2 ways of doing this that I can see, depending on how much info you know about the group and how dynamic you need it to be.
我可以看到有两种方法可以做到这一点,这取决于您对小组的了解程度以及您需要它的动态程度。
Option 1
选项1
If you know the IDs of the groups that you need, then you can get the group by ID and that will provide you with the projects
如果你知道你需要的组的 ID,那么你可以通过 ID 获取组,这将为你提供项目
projects = Gitlab.group(group_id).projects
projects = Gitlab.group(group_id).projects
Option 2
选项 2
If you don't know the group IDs or need to be able to pass in group names more dynamically, you will need to make an extra call to get all of the groups, loop through them and get the individual groups. This may not be any better than your original idea of looping over all of the projects, depends on how many groups / projects you have
如果您不知道组 ID 或需要能够更动态地传递组名称,则需要进行额外调用以获取所有组,循环遍历它们并获取各个组。这可能不会比你循环所有项目的最初想法更好,这取决于你有多少组/项目
groups = []
Gitlab.groups.each do |group|
if ['your_group_name', 'another_group_name'].include? group.name
groups << Gitlab.group(group.id)
end
end
projects = []
groups.each do |group|
projects << group.projects
end
I am by no means an expert Ruby programmer, so there are no doubt far better ways of achieving this or improving the code, but this worked for my needs as it only needed to be run occasionally so speed was not an issue for me
我绝不是专业的 Ruby 程序员,所以毫无疑问有更好的方法来实现这一点或改进代码,但这对我的需求有效,因为它只需要偶尔运行,所以速度对我来说不是问题
回答by Hyman Yu
I tested on Gitlab 8.0. Its group API can provide the project list under a specific group. Just send the GET request to http://gitlab.example.com/api/v3/groups/[group_id]?private_token=xxxxxxxxxxxx
with your private token.
我在 Gitlab 8.0 上测试过。它的组 API 可以提供特定组下的项目列表。只需http://gitlab.example.com/api/v3/groups/[group_id]?private_token=xxxxxxxxxxxx
使用您的私人令牌发送 GET 请求即可。
For example: http://gitlab.example.com/api/v3/groups/3?private_token=xxxxxxxxxxxxx
.
例如:http://gitlab.example.com/api/v3/groups/3?private_token=xxxxxxxxxxxxx
。
In the JSON response, the list is an array under projects
key.
在 JSON 响应中,列表是projects
键下的数组。
回答by Dante
This is fairly handy if you use curl.
如果您使用 curl,这将非常方便。
Just use this code
只需使用此代码
curl --header "PRIVATE-TOKEN: xxxxxxxxxxxxxxx" http://gitlab.your_namespace.com/api/v4/groups/your_group/projects
curl --header "PRIVATE-TOKEN: xxxxxxxxxxxxxxx" http://gitlab.your_namespace.com/api/v4/groups/your_group/projects
回答by Bertrand Martel
You can also use the recently released Gitlab GraphQL APIto query groups by name :
您还可以使用最近发布的Gitlab GraphQL API按名称查询组:
{
group(fullPath: "your_group_here") {
projects {
nodes {
name
description
httpUrlToRepo
nameWithNamespace
starCount
}
}
}
}
You can go to the following URL : https://[your_gitlab_host]/-/graphql-explorerand past the above query
您可以访问以下 URL:https://[your_gitlab_host]/-/graphql-explorer并通过上述查询
The Graphql endpoint is a POST on "https://$gitlab_url/api/graphql" An example using curl& jq:
Graphql 端点是“https://$gitlab_url/api/graphql”上的 POST 使用curl& jq的示例:
gitlab_url=<your gitlab host>
access_token=<your access token>
group_name=<your group>
curl -s -H "Authorization: Bearer $access_token" \
-H "Content-Type:application/json" \
-d '{
"query": "{ group(fullPath: \"'$group_name'\") { projects {nodes { name description httpUrlToRepo nameWithNamespace starCount}}}}"
}' "https://$gitlab_url/api/graphql" | jq '.'
回答by nandeesh
Adding to @Dante's answer,
添加到@Dante 的回答中,
This gives first 20 projects in the group.
这给出了组中的前 20 个项目。
curl --header "PRIVATE-TOKEN: xxxxxxxxxxxxxxx" https://gitlab.your_namespace.com/api/v4/groups/your_group_id/projects
To get more projects we should add 'page' and 'per_page' parameter.
要获得更多项目,我们应该添加 'page' 和 'per_page' 参数。
The below request will fetch you up to 100 projects under requested group.
以下请求将在请求的组下为您获取最多 100 个项目。
curl --header "PRIVATE-TOKEN: xxxxxxxxxxxxxxx" https://gitlab.your_namespace.com/api/v4/groups/your_group_id/projects?&per_page=100" .
If you now want all projects, you have to loop through the pages. Change the page parameter.
如果您现在想要所有项目,则必须循环浏览页面。更改页面参数。
Add json_pp to your request to get a nicely formatted output.
将 json_pp 添加到您的请求中以获得格式良好的输出。
curl --header "PRIVATE-TOKEN: xxxxxxxxxxxxxxx" https://gitlab.your_namespace.com/api/v4/groups/your_group_id/projects | json_pp
回答by kevin mcdonnell
adding to Betrands answer on GraphQL. You can see all subgroups by using query below.
在 GraphQL 上添加 Betrands 答案。您可以使用下面的查询查看所有子组。
{
group(fullPath: "**your_group_here**") {
projects (includeSubgroups: true){
nodes {
name
description
archived
}
}
}
}