Skip to content

Commit e6f0485

Browse files
committed
add scheduled Hacktober labeling to circuitpython_liraries.py
1 parent ed7f398 commit e6f0485

File tree

3 files changed

+87
-20
lines changed

3 files changed

+87
-20
lines changed

adabot/circuitpython_libraries.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from adabot import pypi_requests as pypi
3636
from adabot.lib import circuitpython_library_validators as cirpy_lib_vals
3737
from adabot.lib import common_funcs
38+
from adabot.lib import assign_hacktober_label as hacktober
3839

3940
# Setup ArgumentParser
4041
cmd_line_parser = argparse.ArgumentParser(
@@ -132,6 +133,8 @@ def run_library_checks(validators, bundle_submodules, latest_pylint, kw_args):
132133
"open_issues": [],
133134
"issue_authors": set(),
134135
"issue_closers": set(),
136+
"hacktober_assigned": 0,
137+
"hacktober_removed": 0,
135138
}
136139
core_insights = copy.deepcopy(lib_insights)
137140
for k in core_insights:
@@ -426,6 +429,20 @@ def print_issue_overview(*insights):
426429
.format(closed_issues, len(issue_closers),
427430
new_issues, len(issue_authors)))
428431

432+
# print Hacktoberfest labels changes if its Hacktober
433+
in_season, season_action = hacktober.is_hacktober_season()
434+
if in_season:
435+
hacktober_changes = ""
436+
if season_action == "add":
437+
hacktober_changes = "* Assigned Hacktoberfest label to {} issues.".format(
438+
sum([x["hacktober_assigned"] for x in insights])
439+
)
440+
elif season_action == "remove":
441+
hacktober_changes += "* Removed Hacktoberfest label from {} issues.".format(
442+
sum([x["hacktober_removed"] for x in insights])
443+
)
444+
output_handler(hacktober_changes)
445+
429446
if __name__ == "__main__":
430447
validator_kwarg_list = {}
431448
startup_message = [

adabot/lib/assign_hacktober_label.py

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# THE SOFTWARE.
2222

2323
import argparse
24+
import datetime
2425
import requests
2526

2627
from adabot import github_requests as github
@@ -32,6 +33,32 @@
3233
dest="remove_label")
3334

3435

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+
3562
def get_open_issues(repo):
3663
""" Retrieve all open issues for given repo.
3764
"""
@@ -97,12 +124,13 @@ def assign_hacktoberfest(repo, issues=None, remove_labels=False):
97124
""" Gathers open issues on a repo, and assigns the 'Hacktoberfest' label
98125
to each issue if its not already assigned.
99126
"""
100-
labels_assigned = 0
127+
labels_changed = 0
101128

102129
if not issues:
103130
issues = get_open_issues(repo)
104131

105132
for issue in issues:
133+
update_issue = False
106134
label_names = [label["name"] for label in issue["labels"]]
107135
has_good_first = "good first issue" in label_names
108136
has_hacktober = {"Hacktoberfest", "hacktoberfest"} & set(label_names)
@@ -113,33 +141,36 @@ def assign_hacktoberfest(repo, issues=None, remove_labels=False):
113141
label for label in lable_names
114142
if label not in has_hacktober
115143
]
144+
update_issue = True
116145
else:
117146
if has_good_first and not has_hacktober:
118147
label_exists = ensure_hacktober_label_exists(repo)
119148
if not label_exists:
120149
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"]))
138169

139170
return labels_changed
140171

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)
143174
return result
144175

145176

adabot/lib/circuitpython_library_validators.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from adabot import travis_requests as travis
2929
from adabot import pypi_requests as pypi
3030
from adabot.lib import common_funcs
31+
from adabot.lib import assign_hacktober_label as hacktober
3132

3233

3334
# Define constants for error strings to make checking against them more robust:
@@ -859,6 +860,24 @@ def gather_insights(self, repo, insights, since):
859860
days_open.days)
860861
insights["open_issues"].append(issue_link)
861862

863+
# process Hacktoberfest labels if it is Hacktoberfest season
864+
in_season, season_action = hacktober.is_hacktober_season()
865+
if in_season:
866+
hacktober_issues = [
867+
issue for issue in issues if "pull_request" not in issue
868+
]
869+
if season_action == "add":
870+
insights["hacktober_assigned"] += (
871+
hacktober.assign_hacktoberfest(repo,
872+
issues=hacktober_issues)
873+
)
874+
elif season_action == "remove":
875+
insights["hacktober_removed"] += (
876+
hacktober.assign_hacktoberfest(repo,
877+
issues=hacktober_issues,
878+
remove_labels=True)
879+
)
880+
862881
# get milestones for core repo
863882
if repo["name"] == "circuitpython":
864883
params = {"state": "open"}

0 commit comments

Comments
 (0)