Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import gitlab
- import datetime
- from datetime import timedelta
- from dateutil import parser
- def get_next_month(date):
- nextMonth = date + timedelta(days=5)
- if nextMonth.month == date.month:
- while nextMonth.month == date.month:
- nextMonth = nextMonth + timedelta(days=5)
- return nextMonth
- def get_last_month(date):
- return date + timedelta(days=-date.day)
- def has_current_milestone(now, milestone):
- if milestone.start_date is None or milestone.due_date is None:
- print("not a current milestone")
- return False
- hasStartDate = False
- hasEndDate = False
- start_date = parser.parse(milestone.start_date)
- end_date = parser.parse(milestone.due_date)
- if now.day > 15:
- if start_date.month == now.month and start_date.day == 15:
- hasStartDate = True
- nextMonth = now + timedelta(days=16)
- if end_date.month == nextMonth.month and end_date.day == 14:
- hasEndDate = True
- else:
- now_delta = now + timedelta(days=-now.day)
- if start_date.month == now_delta.month and start_date.day == 15:
- hasStartDate = True
- if end_date.month == now.month and end_date.day == 14:
- hasEndDate = True
- if hasStartDate and hasEndDate:
- return True
- return False
- def has_upcoming_milestone(now, milestone):
- if milestone.start_date is None or milestone.due_date is None:
- print("not a current milestone")
- return False
- hasStartDate = False
- hasEndDate = False
- start_date = parser.parse(milestone.start_date)
- end_date = parser.parse(milestone.due_date)
- if now.day > 15:
- nextMonth = get_next_month(now)
- nextNextMonth = get_next_month(nextMonth)
- if start_date.month == nextMonth.month and start_date.day == 15:
- hasStartDate = True
- if end_date.month == nextNextMonth.month and end_date.day == 14:
- hasEndDate = True
- else:
- if start_date.month == now.month and start_date.day == 15:
- hasStartDate = True
- nextMonth = get_next_month(now)
- if end_date.month == nextMonth.month and end_date.day == 14:
- hasEndDate = True
- if hasStartDate and hasEndDate:
- return True
- return False
- def formatDigit(digit):
- return "{:02d}".format(digit)
- def create_milestone(milestones, start_date, end_date):
- print("creating milestone")
- milestone_name = f"{start_date.year}.{formatDigit(start_date.month)}.{formatDigit(start_date.day)}"
- milestones.create({'title': milestone_name, 'start_date': start_date.isoformat(), 'due_date': end_date.isoformat()})
- gl = gitlab.Gitlab('https://gitlab.com', private_token='7cg_8B-SzSB6ogL2odSp')
- projects = gl.projects.list(owned=True)
- now = datetime.datetime.now()
- for project in projects:
- if project.name != 'sandbox':
- continue
- milestones = project.milestones.list()
- hasCurrentMilestone = False
- hasUpcomingMilestone = False
- for milestone in milestones:
- if not hasCurrentMilestone:
- hasCurrentMilestone = has_current_milestone(now, milestone)
- if not hasUpcomingMilestone:
- hasUpcomingMilestone = has_upcoming_milestone(now, milestone)
- print(f"project {project.name} has current milestone? {hasCurrentMilestone}")
- print(f"project {project.name} has upcoming milestone? {hasUpcomingMilestone}")
- start_date = None
- end_date = None
- if not hasCurrentMilestone:
- if now.day > 15:
- start_date = datetime.date(year=now.year, month=now.month, day=15)
- nextMonth = get_next_month(now)
- end_date = datetime.date(year=nextMonth.year, month=nextMonth.month, day=14)
- else:
- last_month = get_last_month(now)
- start_date = datetime.date(year=last_month.year, month=last_month.month, day=15)
- end_date = datetime.date(year=now.year, month=now.month, day=14)
- create_milestone(project.milestones, start_date, end_date)
- if not hasUpcomingMilestone:
- if now.day > 15:
- nextMonth = get_next_month(now)
- start_date = datetime.date(year=nextMonth.year, month=nextMonth.month, day=15)
- nextNextMonth = get_next_month(nextMonth)
- end_date = datetime.date(year=nextNextMonth.year, month=nextNextMonth.month, day=14)
- else:
- start_date = datetime.date(year=now.year, month=now.month, day=15)
- nextMonth = get_next_month(now)
- end_date = datetime.date(year=nextMonth.year, month=nextMonth.month, day=14)
- create_milestone(project.milestones, start_date, end_date)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement