0

我正在尝试authored_date从我在 python 的帮助下获得的属性中提取特定的细节。

我的最终目标是什么:

我想提取名为 的特定分支,tobedeleted_branch1如果超过 7 天tobedeleted_branch2,则在我的脚本的帮助下删除它们。authored_date

我是这方面的初学者,目前正在学习。

所以,我想做的是,

从输出中提取创作日期并检查它是否早于 7 天。如果它超过 7 天,我将继续执行我想在 if 条件下执行的任何操作。

import gitlab, os
#from gitlab.v4.objects import *
# authenticate
TOKEN = "MYTOKEN"
GITLAB_HOST = 'MYINSTANCE' # or your instance
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.path_with_namespace, project.id, project.name ))
    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'])
           #print(branch)
           print(branch.attributes['commit'])
           #branch.delete()

我从打印得到的输出print(branch.attributes['commit'])是这样的:

{'id': '1245829930', 'short_id': '124582', 'created_at': '2021-11-15T09:10:26.000+00:00', 'parent_ids': None, 'title': 'branch name commit' into \'master\'"', 'message': 'branch name commit', 'author_name': 'Administrator', 'author_email': 'someemail@gmail.com', 'authored_date': '2021-11-15T09:10:26.000+00:00', 'committer_name': 'Administrator', 'committer_email': 'someemail@gmail.com', 'committed_date': '2021-11-15T09:10:26.000+00:00', 'trailers': None, 'web_url': 'someweburl'}

从上面的输出中,我想提取“authored_date”并检查它是否超过 7 天,我将继续删除合并的分支。

对此的任何帮助都将受到高度赞赏。

4

3 回答 3

0

在循环中使用branch.attributes关于您的提交,您可以执行以下操作(确保导入日期时间)。您需要将要删除的分支名称附加到您将在之后迭代的列表中,因为您不想修改当前正在迭代的任何对象(即从branches您仍在迭代的对象中删除项目超过)。

from datetime import datetime

...
branches_to_delete = []
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'])
           #print(branch)
           print(branch.attributes['commit'])
           branch_date_object = datetime.strptime((branch.attributes['commit']['authored_date'].split('.')[0]), "%Y-%m-%dT%H:%M:%S")
           days_diff = datetime.now() - branch_date_object
           if days_diff.days > 7:
               branches_to_delete.append(branch.attributes['name'])

for branch in branches_to_delete:
    # perform your delete functionality
于 2021-11-15T12:28:15.850 回答
0
from datetime import datetime
def get_day_diff(old_date):
    old_datetime = datetime.fromisoformat(old_date)
    # Check timezone of date
    tz_info = old_datetime.tzinfo
    current_datetime = datetime.now(tz_info)
    datetime_delta = current_datetime - old_datetime
    return datetime_delta.days

# test with authored_date
authored_date = '2021-11-15T09:10:26.000+00:00'
if get_day_diff(authored_date) > 7:
  # then delete branch
  branch.delete()
于 2021-11-15T12:34:43.913 回答
0
import datetime
created_at = '2021-11-15T09:10:26.000+00:00'
t = datetime.datetime.fromisoformat(created_at)
n = datetime.datetime.now(tz=t.tzinfo)

if n - t <= datetime.timedelta(days=7):
    ... # do someting
于 2021-11-15T12:17:01.263 回答