Skip to content

Commit b3d7359

Browse files
authored
Merge pull request #137 from sommersoft/swap_stats
Libraries: List Merged PRs
2 parents 76bec5f + ba06a8c commit b3d7359

File tree

3 files changed

+60
-13
lines changed

3 files changed

+60
-13
lines changed

adabot/circuitpython_libraries.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@
103103
if vals[0].startswith("validate")
104104
]
105105

106-
pr_sort_re = re.compile("(?<=\(Open\s)(.+)(?=\sdays)")
106+
pr_sort_re = re.compile(r"(?<=\(Open\s)(.+)(?=\sdays)")
107+
close_pr_sort_re = re.compile(r"(?<=\(Days\sopen:\s)(.+)(?=\))")
107108

108109
def run_library_checks(validators, bundle_submodules, latest_pylint, kw_args):
109110
"""runs the various library checking functions"""
@@ -165,7 +166,8 @@ def run_library_checks(validators, bundle_submodules, latest_pylint, kw_args):
165166
insights = blinka_insights
166167
elif repo["name"] == "circuitpython":
167168
insights = core_insights
168-
errors = validator.gather_insights(repo, insights, since)
169+
closed_metric = bool(insights == lib_insights)
170+
errors = validator.gather_insights(repo, insights, since, show_closed_metric=closed_metric)
169171
if errors:
170172
print("insights error")
171173
for error in errors:
@@ -213,15 +215,26 @@ def run_library_checks(validators, bundle_submodules, latest_pylint, kw_args):
213215
output_handler()
214216
output_handler("Libraries")
215217
print_pr_overview(lib_insights)
216-
output_handler("* {} open pull requests".format(len(lib_insights["open_prs"])))
217-
sorted_prs = sorted(lib_insights["open_prs"],
218-
key=lambda days: int(pr_sort_re.search(days).group(1)),
218+
output_handler(" * Merged pull requests:")
219+
sorted_prs = sorted(lib_insights["merged_prs"],
220+
key=lambda days: int(close_pr_sort_re.search(days).group(1)),
219221
reverse=True)
220222
for pr in sorted_prs:
221-
output_handler(" * {}".format(pr))
223+
output_handler(" * {}".format(pr))
222224
print_issue_overview(lib_insights)
223-
output_handler("* {} open issues".format(len(lib_insights["open_issues"])))
224-
output_handler(" * https://circuitpython.org/contributing")
225+
output_handler("* https://circuitpython.org/contributing")
226+
output_handler(" * {} open issues".format(len(lib_insights["open_issues"])))
227+
open_pr_days = [
228+
int(pr_sort_re.search(pr).group(1)) for pr in lib_insights["open_prs"]
229+
if pr_sort_re.search(pr) is not None
230+
]
231+
output_handler(
232+
" * {0} open pull requests (Oldest: {1}, Newest: {2})".format(
233+
len(lib_insights["open_prs"]),
234+
max(open_pr_days),
235+
max((min(open_pr_days), 1)) # ensure the minumum is '1'
236+
)
237+
)
225238
output_handler("Library updates in the last seven days:")
226239
if len(new_libs) != 0:
227240
output_handler("**New Libraries**")
@@ -280,7 +293,14 @@ def output_handler(message="", quiet=False):
280293

281294
def print_circuitpython_download_stats():
282295
"""Gather and report analytics on the main CircuitPython repository."""
283-
response = github.get("/repos/adafruit/circuitpython/releases")
296+
try:
297+
response = github.get("/repos/adafruit/circuitpython/releases")
298+
except (ValueError, RuntimeError):
299+
output_handler(
300+
"Core CircuitPython GitHub download statistics request failed."
301+
)
302+
return
303+
284304
if not response.ok:
285305
output_handler(
286306
"Core CircuitPython GitHub download statistics request failed."
@@ -419,7 +439,7 @@ def print_circuitpython_download_stats():
419439
output_handler()
420440

421441
def print_pr_overview(*insights):
422-
merged_prs = sum([x["merged_prs"] for x in insights])
442+
merged_prs = sum([len(x["merged_prs"]) for x in insights])
423443
authors = set().union(*[x["pr_merged_authors"] for x in insights])
424444
reviewers = set().union(*[x["pr_reviewers"] for x in insights])
425445

adabot/lib/circuitpython_library_validators.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ def validate_core_driver_page(self, repo):
783783

784784

785785

786-
def gather_insights(self, repo, insights, since):
786+
def gather_insights(self, repo, insights, since, show_closed_metric=False):
787787
"""Gather analytics about a repository like open and merged pull requests.
788788
This expects a dictionary with GitHub API repository state (like from the
789789
list_repos function) and will fill in the provided insights dictionary
@@ -814,7 +814,34 @@ def gather_insights(self, repo, insights, since):
814814
insights["active_prs"] += 1
815815
else:
816816
if pr_info["merged"]:
817-
insights["merged_prs"] += 1
817+
merged_info = ""
818+
if show_closed_metric:
819+
created = datetime.datetime.strptime(
820+
issue["created_at"],
821+
"%Y-%m-%dT%H:%M:%SZ"
822+
)
823+
merged = datetime.datetime.strptime(
824+
issue["closed_at"],
825+
"%Y-%m-%dT%H:%M:%SZ"
826+
)
827+
828+
days_open = merged - created
829+
if days_open.days < 0: # opened earlier today
830+
days_open += datetime.timedelta(
831+
days=(days_open.days * -1)
832+
)
833+
elif days_open.days == 0:
834+
days_open += datetime.timedelta(
835+
days=(1)
836+
)
837+
merged_info = " (Days open: {})".format(days_open.days)
838+
839+
pr_link = "{0}{1}".format(
840+
issue["pull_request"]["html_url"],
841+
merged_info
842+
)
843+
insights["merged_prs"].append(pr_link)
844+
818845
insights["pr_merged_authors"].add(pr_info["user"]["login"])
819846
insights["pr_reviewers"].add(pr_info["merged_by"]["login"])
820847
pr_reviews = github.get(str(pr_info["url"]) + "/reviews")

adabot/lib/common_funcs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ class InsightData(collections.UserDict):
285285

286286
def __init__(self):
287287
self.data = {
288-
"merged_prs": 0,
288+
"merged_prs": [],
289289
"closed_prs": 0,
290290
"new_prs": 0,
291291
"active_prs": 0,

0 commit comments

Comments
 (0)