Advertisement
Guest User

Untitled

a guest
Sep 21st, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. import gitlab
  2. import datetime
  3. from datetime import timedelta
  4. from dateutil import parser
  5.  
  6.  
  7. def get_next_month(date):
  8. nextMonth = date + timedelta(days=5)
  9.  
  10. if nextMonth.month == date.month:
  11. while nextMonth.month == date.month:
  12. nextMonth = nextMonth + timedelta(days=5)
  13.  
  14. return nextMonth
  15.  
  16.  
  17. def get_last_month(date):
  18. return date + timedelta(days=-date.day)
  19.  
  20.  
  21. def has_current_milestone(now, milestone):
  22. if milestone.start_date is None or milestone.due_date is None:
  23. print("not a current milestone")
  24. return False
  25.  
  26. hasStartDate = False
  27. hasEndDate = False
  28.  
  29. start_date = parser.parse(milestone.start_date)
  30. end_date = parser.parse(milestone.due_date)
  31.  
  32. if now.day > 15:
  33. if start_date.month == now.month and start_date.day == 15:
  34. hasStartDate = True
  35.  
  36. nextMonth = now + timedelta(days=16)
  37.  
  38. if end_date.month == nextMonth.month and end_date.day == 14:
  39. hasEndDate = True
  40. else:
  41. now_delta = now + timedelta(days=-now.day)
  42.  
  43. if start_date.month == now_delta.month and start_date.day == 15:
  44. hasStartDate = True
  45.  
  46. if end_date.month == now.month and end_date.day == 14:
  47. hasEndDate = True
  48.  
  49. if hasStartDate and hasEndDate:
  50. return True
  51.  
  52. return False
  53.  
  54.  
  55. def has_upcoming_milestone(now, milestone):
  56. if milestone.start_date is None or milestone.due_date is None:
  57. print("not a current milestone")
  58. return False
  59.  
  60. hasStartDate = False
  61. hasEndDate = False
  62.  
  63. start_date = parser.parse(milestone.start_date)
  64. end_date = parser.parse(milestone.due_date)
  65.  
  66. if now.day > 15:
  67. nextMonth = get_next_month(now)
  68. nextNextMonth = get_next_month(nextMonth)
  69.  
  70. if start_date.month == nextMonth.month and start_date.day == 15:
  71. hasStartDate = True
  72.  
  73. if end_date.month == nextNextMonth.month and end_date.day == 14:
  74. hasEndDate = True
  75. else:
  76. if start_date.month == now.month and start_date.day == 15:
  77. hasStartDate = True
  78.  
  79. nextMonth = get_next_month(now)
  80.  
  81. if end_date.month == nextMonth.month and end_date.day == 14:
  82. hasEndDate = True
  83.  
  84. if hasStartDate and hasEndDate:
  85. return True
  86.  
  87. return False
  88.  
  89.  
  90. def formatDigit(digit):
  91. return "{:02d}".format(digit)
  92.  
  93.  
  94. def create_milestone(milestones, start_date, end_date):
  95. print("creating milestone")
  96. milestone_name = f"{start_date.year}.{formatDigit(start_date.month)}.{formatDigit(start_date.day)}"
  97. milestones.create({'title': milestone_name, 'start_date': start_date.isoformat(), 'due_date': end_date.isoformat()})
  98.  
  99.  
  100. gl = gitlab.Gitlab('https://gitlab.com', private_token='7cg_8B-SzSB6ogL2odSp')
  101.  
  102. projects = gl.projects.list(owned=True)
  103.  
  104. now = datetime.datetime.now()
  105.  
  106. for project in projects:
  107. if project.name != 'sandbox':
  108. continue
  109.  
  110. milestones = project.milestones.list()
  111.  
  112. hasCurrentMilestone = False
  113. hasUpcomingMilestone = False
  114.  
  115. for milestone in milestones:
  116. if not hasCurrentMilestone:
  117. hasCurrentMilestone = has_current_milestone(now, milestone)
  118.  
  119. if not hasUpcomingMilestone:
  120. hasUpcomingMilestone = has_upcoming_milestone(now, milestone)
  121.  
  122. print(f"project {project.name} has current milestone? {hasCurrentMilestone}")
  123. print(f"project {project.name} has upcoming milestone? {hasUpcomingMilestone}")
  124.  
  125. start_date = None
  126. end_date = None
  127.  
  128. if not hasCurrentMilestone:
  129. if now.day > 15:
  130. start_date = datetime.date(year=now.year, month=now.month, day=15)
  131.  
  132. nextMonth = get_next_month(now)
  133.  
  134. end_date = datetime.date(year=nextMonth.year, month=nextMonth.month, day=14)
  135. else:
  136. last_month = get_last_month(now)
  137.  
  138. start_date = datetime.date(year=last_month.year, month=last_month.month, day=15)
  139. end_date = datetime.date(year=now.year, month=now.month, day=14)
  140.  
  141. create_milestone(project.milestones, start_date, end_date)
  142.  
  143. if not hasUpcomingMilestone:
  144. if now.day > 15:
  145. nextMonth = get_next_month(now)
  146. start_date = datetime.date(year=nextMonth.year, month=nextMonth.month, day=15)
  147.  
  148. nextNextMonth = get_next_month(nextMonth)
  149. end_date = datetime.date(year=nextNextMonth.year, month=nextNextMonth.month, day=14)
  150. else:
  151. start_date = datetime.date(year=now.year, month=now.month, day=15)
  152.  
  153. nextMonth = get_next_month(now)
  154. end_date = datetime.date(year=nextMonth.year, month=nextMonth.month, day=14)
  155.  
  156. create_milestone(project.milestones, start_date, end_date)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement