21
21
# THE SOFTWARE.
22
22
23
23
import argparse
24
+ import datetime
24
25
import requests
25
26
26
27
from adabot import github_requests as github
32
33
dest = "remove_label" )
33
34
34
35
36
+ # Hacktoberfest Season
37
+ # - lists are in [start, stop] format.
38
+ # - tuples are in (month, day) format.
39
+ _ADD_SEASON = [(9 , 29 ), (10 , 30 )]
40
+ _REMOVE_SEASON = [(11 , 1 ), (11 , 10 )]
41
+
42
+ def is_hacktober_season ():
43
+ """ Checks if the current day falls within either the add range (_ADD_SEASON)
44
+ or the remove range (_REMOVE_SEASON). Returns boolean if within
45
+ Hacktoberfest season, and which action to take.
46
+ """
47
+ today = datetime .date .today ()
48
+ add_range = [
49
+ datetime .date (today .year , * month_day ) for month_day in _ADD_SEASON
50
+ ]
51
+ remove_range = [
52
+ datetime .date (today .year , * month_day ) for month_day in _REMOVE_SEASON
53
+ ]
54
+ if add_range [0 ] <= today <= add_range [1 ]:
55
+ return True , "add"
56
+ elif remove_range [0 ] <= today <= remove_range [1 ]:
57
+ return True , "remove"
58
+
59
+ return False , None
60
+
61
+
35
62
def get_open_issues (repo ):
36
63
""" Retrieve all open issues for given repo.
37
64
"""
@@ -97,12 +124,13 @@ def assign_hacktoberfest(repo, issues=None, remove_labels=False):
97
124
""" Gathers open issues on a repo, and assigns the 'Hacktoberfest' label
98
125
to each issue if its not already assigned.
99
126
"""
100
- labels_assigned = 0
127
+ labels_changed = 0
101
128
102
129
if not issues :
103
130
issues = get_open_issues (repo )
104
131
105
132
for issue in issues :
133
+ update_issue = False
106
134
label_names = [label ["name" ] for label in issue ["labels" ]]
107
135
has_good_first = "good first issue" in label_names
108
136
has_hacktober = {"Hacktoberfest" , "hacktoberfest" } & set (label_names )
@@ -113,33 +141,36 @@ def assign_hacktoberfest(repo, issues=None, remove_labels=False):
113
141
label for label in lable_names
114
142
if label not in has_hacktober
115
143
]
144
+ update_issue = True
116
145
else :
117
146
if has_good_first and not has_hacktober :
118
147
label_exists = ensure_hacktober_label_exists (repo )
119
148
if not label_exists :
120
149
continue
121
-
122
- params = {
123
- "labels" : label_names
124
- }
125
- result = github .patch ("/repos/"
126
- + repo ["full_name" ]
127
- + "/issues/"
128
- + str (issue ["number" ]),
129
- json = params )
130
-
131
- if result .ok :
132
- labels_changed += 1
133
- else :
134
- # sadly, GitHub will only silently ignore labels that are
135
- # not added and return a 200. so this will most likely only
136
- # trigger on endpoint/connection failures.
137
- print ("Failed to add Hacktoberfest label to: {}" .format (issue ["url" ]))
150
+ update_issue = True
151
+
152
+ if update_issue :
153
+ params = {
154
+ "labels" : label_names
155
+ }
156
+ result = github .patch ("/repos/"
157
+ + repo ["full_name" ]
158
+ + "/issues/"
159
+ + str (issue ["number" ]),
160
+ json = params )
161
+
162
+ if result .ok :
163
+ labels_changed += 1
164
+ else :
165
+ # sadly, GitHub will only silently ignore labels that are
166
+ # not added and return a 200. so this will most likely only
167
+ # trigger on endpoint/connection failures.
168
+ print ("Failed to add Hacktoberfest label to: {}" .format (issue ["url" ]))
138
169
139
170
return labels_changed
140
171
141
- def process_hacktoberfest (repo , remove_labels = False ):
142
- result = assign_hacktoberfest (repo , remove_labels )
172
+ def process_hacktoberfest (repo , issues = None , remove_labels = False ):
173
+ result = assign_hacktoberfest (repo , issues , remove_labels )
143
174
return result
144
175
145
176
0 commit comments