Python Gitlab API - 列出组/子组的共享项目

Posted

技术标签:

【中文标题】Python Gitlab API - 列出组/子组的共享项目【英文标题】:Python Gitlab API - list shared projects of a group/subgroup 【发布时间】:2021-12-17 20:49:02 【问题描述】:

我需要在带有子组的 Gitlab 组中查找所有项目和共享项目。我设法列出了所有项目的名称,如下所示:

group = gl.groups.get(11111, lazy=True)

# find all projects, also in subgroups
projects=group.projects.list(include_subgroups=True, all=True)
for prj in projects:
    print(prj.attributes['name'])
print("")

我缺少的是还列出组内的共享项目。或者换一种说法:找出我的小组是成员的所有项目。 Python API 可以做到这一点吗?

【问题讨论】:

仅将 HTTP API 与 requests 库一起使用,我就取得了更大的成功。在我看来,这比使用 python 库效果更好.. 不幸的是,我对 HTTP API 的经验为零,对我来说继续使用 python 会更容易。 ;) docs.gitlab.com/ee/api/api_resources.html 【参考方案1】:

您可以通过.shared_projects属性获取共享项目:

group = gl.groups.get(11111)
for proj in group.shared_projects:
    print(proj['path_with_namespace'])

但是,您不能将lazy=True 参数用于gl.groups.get

>>> group = gl.groups.get(11111, lazy=True)
>>> group.shared_projects
AttributeError: shared_projects

【讨论】:

不幸的是,这对我不起作用。我只得到和以前一样的结果,这意味着所有项目,而不是共享项目。【参考方案2】:

因此,受到 sytech 回答的启发,我发现它一开始就不起作用,因为共享项目仍然隐藏在子组中。所以我想出了以下代码,它可以挖掘所有不同级别的子组以查找所有共享项目。我认为这可以写得更优雅,但它对我有用:

# group definition
main_group_id = 11111

# create empty list that will contain final result
list_subgroups_id_all = []

# create empty list that act as temporal storage of the results outside the function
list_subgroups_id_stored = []



# function to create a list of subgroups of a group (id)
def find_subgroups(group_id):

    # retrieve group object
    group = gl.groups.get(group_id)

    # create empty lists to store id of subgroups
    list_subgroups_id = []

    #iterate through group to find id of all subgroups
    for sub in group.subgroups.list():
        list_subgroups_id.append(sub.id)

    return(list_subgroups_id)


# function to iterate over the various groups for subgroup detection
def iterate_subgroups(group_id, list_subgroups_id_all):

    # for a given id, find existing subgroups (id) and store them in a list
    list_subgroups_id = find_subgroups(group_id)

    # add the found items to the list storage variable, so that the results are not overwritten
    list_subgroups_id_stored.append(list_subgroups_id)

    # for each found subgroup_id, test if it is already part of the total id list
    # if not, keep store it and test for more subgroups
    for test_id in list_subgroups_id:
        if test_id not in list_subgroups_id_all:

            # add it to total subgroup id list (final results list)
            list_subgroups_id_all.append(test_id)

            # check whether test_id contains more subgroups
            list_subgroups_id_tmp = iterate_subgroups(test_id, list_subgroups_id_all)

            #if so, append to stored subgroup list that is currently checked
            list_subgroups_id_stored.append(list_subgroups_id_tmp)

    return(list_subgroups_id_all)

# find all subgroup and subsubgroups, etc... store ids in list
list_subgroups_id_all = iterate_subgroups(main_group_id , list_subgroups_id_all)

print("***ids of all subgroups***")
print(list_subgroups_id_all)
print("")

print("***names of all subgroups***")
list_names = []
for ids in list_subgroups_id_all:
    group = gl.groups.get(ids)
    group_name = group.attributes['name']
    list_names.append(group_name)
print(list_names)
#print(list_subgroups_name_all)
print("")

# print all directly integrated projects of the main group, also those in subgroups
print("***integrated projects***")
group = gl.groups.get(main_group_id)
projects=group.projects.list(include_subgroups=True, all=True)
for prj in projects:
    print(prj.attributes['name'])
print("")

# print all shared projects
print("***shared projects***")
for sub in list_subgroups_id_all:
    group = gl.groups.get(sub)
    for shared_prj in group.shared_projects:
        print(shared_prj['path_with_namespace'])
print("")

还有一个问题 - 一开始我通过它的 id 检索主组(这里:11111),但我实际上也可以通过查找组的名称来获得这个 id 吗?类似:group_id = gl.group.get(attribute='name','foo')(不工作)?

【讨论】:

以上是关于Python Gitlab API - 列出组/子组的共享项目的主要内容,如果未能解决你的问题,请参考以下文章

gitlab通过api创建组项目成员

无法在 Kubernetes 集群中执行 GitLab Runner:无法在命名空间“gitlab”中的 API 组“”中创建资源“秘密”

python-gitlab库操作gitlab的API

在 Gitlab 中创建新的子组时出现 500 错误

gitlab中组的分类及权限介绍

python gitlab api 合并请求失败,出现 gitlab.execeptions.GitlabMRClosedError 405