如何在python中使用gitlab库通过项目id获取项目url

问题描述 投票:0回答:1

我正在为 gitlab 编写一些脚本,我想仅使用 项目 ID 来获取 gitlab 中项目的 url 所以我阅读了有关 python-gitlab 库的所有文档,但找不到它的一些属性。

这是我的代码

import gitlab
import time

from gitlab.exceptions import GitlabCreateError


gitlab_url = 'gitlab.com'
new_gitlab_url = 'gitlab.com'
private_token = 'something in token'
new_private_token = 'something in token'
group_id = 1804
new_group_id = 4178 



gl = gitlab.Gitlab(new_gitlab_url, private_token=new_private_token)
gl.auth()
print("Authentication to NEW is successful!")

new_group = gl.groups.get(new_group_id)
new_projects = new_group.projects.list(get_all=True, iterator=True)
new_projects_ids = [(project.id, project.name) for project in new_projects]
print(new_projects_ids)


def get_group_full_url_by_id(new_group_id):
    group = gl.groups.get(new_group_id)
    projects = group.projects.list(get_all=True, iterator=True)
    projects_urls = [project.full_path for project in projects]
    # print(projects_ids)
    return projects_urls

project_urls = get_group_full_url_by_id(new_group_id)
print(project_urls)

这是我收到的错误(是的,我知道项目没有完整路径)

Exception has occurred: AttributeError
'GroupProject' object has no attribute 'full_path'

<class 'gitlab.v4.objects.projects.GroupProject'> was created via a
list() call and only a subset of the data may be present. To ensure
all data is present get the object using a get(object.id) call. For
more details, see:

https://python-gitlab.readthedocs.io/en/v4.2.0/faq.html#attribute-error-list
  File "C:\Users\KAMBAROK\Desktop\bot\testing verions\test.py", line 30, in get_group_full_url_by_id
    projects_urls = [project.full_path for project in projects]
                     ^^^^^^^^^^^^^^^^^
  File "C:\Users\KAMBAROK\Desktop\bot\testing verions\test.py", line 34, in <module>
    project_urls = get_group_full_url_by_id(new_group_id)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'GroupProject' object has no attribute 'full_path'

<class 'gitlab.v4.objects.projects.GroupProject'> was created via a
list() call and only a subset of the data may be present. To ensure
all data is present get the object using a get(object.id) call. For
more details, see:

https://python-gitlab.readthedocs.io/en/v4.2.0/faq.html#attribute-error-list

希望有人能帮忙解决这个问题

python url gitlab python-gitlab
1个回答
0
投票

group.projects.list
返回
GroupProject
对象的列表,您可以使用它从 gitlab 查询项目。对于这些小组项目对象之一
group_project
使用:

project_id = group_project.get_id()
project = gl.projects.get(project_id)

project
对象现在包含感兴趣的数据。 您可以使用
project.http_url_to_repo
获取 http url(或使用
project.ssh_url_to_repo
获取 ssh)。我通过打印项目并寻找 url 属性找到了它。

您的代码可能如下所示:

def get_group_full_url_by_id(new_group_id):
    group = gl.groups.get(new_group_id)
    group_projects = group.projects.list(get_all=True, iterator=True)
    projects_urls = [gl.projects.get(gp.get_id()).http_url_to_repo for gp in group_projects]
    return projects_urls
© www.soinside.com 2019 - 2024. All rights reserved.