api获得全部项目 调用gitlab_Gitlab API适用于组下的所有项目

本文讲述了如何通过Gitlab API获取特定组下的所有项目,讨论了两种方法:一种是直接通过组ID获取项目,另一种是对所有组进行迭代以找到目标组并获取其项目。提供了相应的Ruby代码示例。
摘要由CSDN通过智能技术生成

I want to get a list of all the projects which are under a particular group in Gitlab. Here is the example scenario:

Group A (id: 1) has 3 Project

Group A / Project 1

Group A / Project 2

Group A / Project 3

Group B (id: 2) has 5 Projects

Group B / Project 1

Group B / Project 2

Group B / Project 3

Group B / Project 4

Group B / Project 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.

What I am looking for, is an operation something like 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.

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

解决方案

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

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

projects = Gitlab.group(group_id).projects

Option 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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值