forked from greenelab/scrumlord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upkeep.py
147 lines (114 loc) · 4.16 KB
/
upkeep.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import argparse
import datetime
import logging
import re
import holidays
from github import Github
from github.Issue import Issue
from typing import Iterator
PA_HOLIDAYS = holidays.country_holidays("US", subdiv="PA")
log = logging.getLogger(__name__)
log.setLevel(logging.ERROR)
def close_old_issues(issues: list[Issue], lifespan: int):
"""
Closes scrum issues older than the number of days specified by lifespan.
"""
lifespan = datetime.timedelta(days=lifespan)
today = get_today()
for issue in issues:
date = issue_title_to_date(issue.title)
if not date:
continue
if today - date > lifespan:
log.info(f"Closing {issue.title}")
try:
issue.edit(state="closed")
except Exception:
log.error("Closing issue failed:", exc_info=True)
def create_scrum_issue(repository, date):
"""
Creates a scrum issue for the given date.
"""
body = "\n".join(
(
"Please be sure to include:",
" - What you worked on yesterday.",
" - What you plan on working on today.",
" - Any blockers.",
)
)
title = f"{date}: e-scrum for {date:%A, %B %-d, %Y}"
log.info(f"Creating {title}")
try:
repository.create_issue(title=title, body=body)
except Exception:
log.error("Creating issue failed:", exc_info=True)
def get_future_dates_without_issues(
issues: list[Issue], workdays_ahead=2
) -> list[datetime.date]:
"""
Looks through issues and yields the dates of future workdays
(includes today) that don't have open issues.
"""
future_dates = set(get_upcoming_workdays(workdays_ahead))
future_dates -= {issue_title_to_date(issue.title) for issue in issues}
return sorted(future_dates)
def get_holidays() -> set[str]:
with open("holidays.txt", "r") as holidays_file:
lines = [line.strip() for line in holidays_file.readlines()]
return set([line for line in lines if line and not line.startswith("#")])
def get_today() -> datetime.date:
"""
Returns the datetime.date for today. Needed since tests cannot mock a
builtin type: http://stackoverflow.com/a/24005764/4651668
"""
return datetime.date.today()
def get_upcoming_workdays(workdays_ahead=2) -> Iterator[datetime.date]:
"""
Returns a generator of the next number of workdays specified by
workdays_ahead. The current day is yielded first, if a workday,
and does not count as one of workdays_ahead.
"""
date = get_today()
if is_workday(date):
yield date
i = 0
while i < workdays_ahead:
date += datetime.timedelta(days=1)
if is_workday(date):
yield date
i += 1
def is_holiday(date: datetime.date) -> bool:
"""
Returns `True` if a date is a holiday. Returns `False` otherwise.
"""
holiday = PA_HOLIDAYS.get(date, "").replace(" (Observed)", "")
return holiday in get_holidays()
def is_workday(date: datetime.date) -> bool:
"""
Returns `True` if a date is a workday. Returns `False` otherwise.
"""
return date.weekday() not in holidays.WEEKEND and not is_holiday(date)
def issue_title_to_date(title: str) -> datetime.date:
"""
Returns a datetime.date object from a scrum issue title.
"""
pattern = re.compile(r"([0-9]{4})-([0-9]{2})-([0-9]{2}):")
match = pattern.match(title)
if match:
return datetime.date(*map(int, match.groups()))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--lifespan", type=int, default=7)
parser.add_argument("--repository", default="AlexsLemonade/scrumlord-test")
parser.add_argument("--token", help="GitHub access token")
parser.add_argument("--workdays-ahead", type=int, default=2)
args = parser.parse_args()
github = Github(args.token)
repository = github.get_repo(args.repository)
issues = repository.get_issues(state="open")
# Close old issues.
close_old_issues(issues, args.lifespan)
# Create upcoming issues.
for date in get_future_dates_without_issues(issues, args.workdays_ahead):
create_scrum_issue(repository, date)