我是编写python脚本的极端初学者,因为我目前正在学习它。
我正在编写一个代码,我将在python-gitlab 模块的帮助下提取我拥有的名为 等的分支tobedeleted_branch1
。tobedeleted_branch2
经过这么多的研究和一切,我能够使用下面给出的脚本提取分支的名称,但现在我想要的是,我需要删除正在打印的分支。
所以我有一个计划,我将继续将打印输出存储在一个变量中,并立即删除它们,但我仍然无法将它们存储在一个变量中。
一旦我将'n'个分支存储在该变量中,我想删除它们。
我浏览了文档,但我不知道如何在 python 脚本中使用它。
模块:https ://python-gitlab.readthedocs.io/en/stable/index.html
借助模块 REF 删除分支: https ://python-gitlab.readthedocs.io/en/stable/gl_objects/branches.html#branches
对此的任何帮助都将受到高度赞赏。
import gitlab, os
TOKEN = "MYTOKEN"
GITLAB_HOST = 'MYINSTANCE'
gl = gitlab.Gitlab(GITLAB_HOST, private_token=TOKEN)
# set gitlab group id
group_id = 6
group = gl.groups.get(group_id, lazy=True)
#get all projects
projects = group.projects.list(include_subgroups=True, all=True)
#get all project ids
project_ids = []
for project in projects:
project_ids.append((project.id))
print(project_ids)
for project in project_ids:
project = gl.projects.get(project)
branches = project.branches.list()
for branch in branches:
if "tobedeleted" in branch.attributes['name']:
print(branch.attributes['name'])
另外,我很确定这不是编写脚本的干净方式。您能否放弃有关如何使其变得更好的建议?
谢谢