I am trying to extract the specific details like authored_date
from the attribute which I am getting with help of python.
What is my end goal:
I want to extract the specific branches which are named as tobedeleted_branch1
, tobedeleted_branch2
and delete them with help of my script if the authored_date
is more than 7 days.
I am beginner in this and learning this currently.
So, what I want to do is,
Extract the authored date from the output and check if it is older than 7 days. If it is older than 7 days I will go ahead and perform whatever I want to perform in if condition.
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()
The output which I am getting from printing the print(branch.attributes['commit'])
is like :
{'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'}
From the above output, I want to extract the 'authored_date' and check if it is greated than 7 days I will go ahead and delete the merged branch.
Any help regarding this is highly appreciated.