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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
#!/usr/bin/env python
"""
Simple tools to query github.com and gather stats about issues.
To generate a report for Matplotlib 3.0.0, run:
python github_stats.py --milestone 3.0.0 --since-tag v2.0.0
"""
# -----------------------------------------------------------------------------
# Imports
# -----------------------------------------------------------------------------
import sys
from argparse import ArgumentParser
from datetime import datetime, timedelta
from subprocess import check_output
from gh_api import (
get_paged_request, make_auth_header, get_pull_request, is_pull_request,
get_milestone_id, get_issues_list, get_authors,
)
# -----------------------------------------------------------------------------
# Globals
# -----------------------------------------------------------------------------
ISO8601 = "%Y-%m-%dT%H:%M:%SZ"
PER_PAGE = 100
REPORT_TEMPLATE = """\
.. _github-stats:
{title}
{title_underline}
GitHub statistics for {since_day} (tag: {tag}) - {today}
These lists are automatically generated, and may be incomplete or contain duplicates.
We closed {n_issues} issues and merged {n_pulls} pull requests.
{milestone}
The following {nauthors} authors contributed {ncommits} commits.
{unique_authors}
{links}
Previous GitHub statistics
--------------------------
.. toctree::
:maxdepth: 1
:glob:
:reversed:
prev_whats_new/github_stats_*"""
MILESTONE_TEMPLATE = (
'The full list can be seen `on GitHub '
'<https://github.com/{project}/milestone/{milestone_id}?closed=1>`__\n')
LINKS_TEMPLATE = """
GitHub issues and pull requests:
Pull Requests ({n_pulls}):
{pull_request_report}
Issues ({n_issues}):
{issue_report}
"""
# -----------------------------------------------------------------------------
# Functions
# -----------------------------------------------------------------------------
def round_hour(dt):
return dt.replace(minute=0, second=0, microsecond=0)
def _parse_datetime(s):
"""Parse dates in the format returned by the GitHub API."""
return datetime.strptime(s, ISO8601) if s else datetime.fromtimestamp(0)
def issues2dict(issues):
"""Convert a list of issues to a dict, keyed by issue number."""
return {i['number']: i for i in issues}
def split_pulls(all_issues, project="matplotlib/matplotlib"):
"""Split a list of closed issues into non-PR Issues and Pull Requests."""
pulls = []
issues = []
for i in all_issues:
if is_pull_request(i):
pull = get_pull_request(project, i['number'], auth=True)
pulls.append(pull)
else:
issues.append(i)
return issues, pulls
def issues_closed_since(period=timedelta(days=365),
project='matplotlib/matplotlib', pulls=False):
"""
Get all issues closed since a particular point in time.
*period* can either be a datetime object, or a timedelta object. In the
latter case, it is used as a time before the present.
"""
which = 'pulls' if pulls else 'issues'
if isinstance(period, timedelta):
since = round_hour(datetime.utcnow() - period)
else:
since = period
url = (
f'https://api.github.com/repos/{project}/{which}'
f'?state=closed'
f'&sort=updated'
f'&since={since.strftime(ISO8601)}'
f'&per_page={PER_PAGE}')
allclosed = get_paged_request(url, headers=make_auth_header())
filtered = (i for i in allclosed
if _parse_datetime(i['closed_at']) > since)
if pulls:
filtered = (i for i in filtered
if _parse_datetime(i['merged_at']) > since)
# filter out PRs not against main (backports)
filtered = (i for i in filtered if i['base']['ref'] == 'main')
else:
filtered = (i for i in filtered if not is_pull_request(i))
return list(filtered)
def sorted_by_field(issues, field='closed_at', reverse=False):
"""Return a list of issues sorted by closing date."""
return sorted(issues, key=lambda i: i[field], reverse=reverse)
def report(issues, show_urls=False):
"""Summary report about a list of issues, printing number and title."""
lines = []
if show_urls:
for i in issues:
role = 'ghpull' if 'merged_at' in i else 'ghissue'
number = i['number']
title = i['title'].replace('`', '``').strip()
lines.append(f'* :{role}:`{number}`: {title}')
else:
for i in issues:
number = i['number']
title = i['title'].replace('`', '``').strip()
lines.append('* {number}: {title}')
return '\n'.join(lines)
# -----------------------------------------------------------------------------
# Main script
# -----------------------------------------------------------------------------
if __name__ == "__main__":
# Whether to add reST urls for all issues in printout.
show_urls = True
parser = ArgumentParser()
parser.add_argument(
'--since-tag', type=str,
help='The git tag to use for the starting point '
'(typically the last macro release).')
parser.add_argument(
'--milestone', type=str,
help='The GitHub milestone to use for filtering issues [optional].')
parser.add_argument(
'--days', type=int,
help='The number of days of data to summarize (use this or --since-tag).')
parser.add_argument(
'--project', type=str, default='matplotlib/matplotlib',
help='The project to summarize.')
parser.add_argument(
'--links', action='store_true', default=False,
help='Include links to all closed Issues and PRs in the output.')
opts = parser.parse_args()
tag = opts.since_tag
# set `since` from days or git tag
if opts.days:
since = datetime.utcnow() - timedelta(days=opts.days)
else:
if not tag:
tag = check_output(['git', 'describe', '--abbrev=0'],
encoding='utf8').strip()
cmd = ['git', 'log', '-1', '--format=%ai', tag]
tagday, tz = check_output(cmd, encoding='utf8').strip().rsplit(' ', 1)
since = datetime.strptime(tagday, "%Y-%m-%d %H:%M:%S")
h = int(tz[1:3])
m = int(tz[3:])
td = timedelta(hours=h, minutes=m)
if tz[0] == '-':
since += td
else:
since -= td
since = round_hour(since)
milestone = opts.milestone
project = opts.project
print(f'fetching GitHub stats since {since} (tag: {tag}, milestone: {milestone})',
file=sys.stderr)
if milestone:
milestone_id = get_milestone_id(project=project, milestone=milestone,
auth=True)
issues_and_pulls = get_issues_list(project=project, milestone=milestone_id,
state='closed', auth=True)
issues, pulls = split_pulls(issues_and_pulls, project=project)
else:
issues = issues_closed_since(since, project=project, pulls=False)
pulls = issues_closed_since(since, project=project, pulls=True)
# For regular reports, it's nice to show them in reverse chronological order.
issues = sorted_by_field(issues, reverse=True)
pulls = sorted_by_field(pulls, reverse=True)
n_issues, n_pulls = map(len, (issues, pulls))
n_total = n_issues + n_pulls
since_day = since.strftime("%Y/%m/%d")
today = datetime.today()
title = (f'GitHub statistics for {milestone.lstrip("v")} '
f'{today.strftime("(%b %d, %Y)")}')
ncommits = 0
all_authors = []
if tag:
# print git info, in addition to GitHub info:
since_tag = f'{tag}..'
cmd = ['git', 'log', '--oneline', since_tag]
ncommits += len(check_output(cmd).splitlines())
author_cmd = ['git', 'log', '--use-mailmap', '--format=* %aN', since_tag]
all_authors.extend(
check_output(author_cmd, encoding='utf-8', errors='replace').splitlines())
pr_authors = []
for pr in pulls:
pr_authors.extend(get_authors(pr))
ncommits = len(pr_authors) + ncommits - len(pulls)
author_cmd = ['git', 'check-mailmap'] + pr_authors
with_email = check_output(author_cmd,
encoding='utf-8', errors='replace').splitlines()
all_authors.extend(['* ' + a.split(' <')[0] for a in with_email])
unique_authors = sorted(set(all_authors), key=lambda s: s.lower())
if milestone:
milestone_str = MILESTONE_TEMPLATE.format(project=project,
milestone_id=milestone_id)
else:
milestone_str = ''
if opts.links:
links = LINKS_TEMPLATE.format(n_pulls=n_pulls,
pull_request_report=report(pulls, show_urls),
n_issues=n_issues,
issue_report=report(issues, show_urls))
else:
links = ''
# Print summary report we can directly include into release notes.
print(REPORT_TEMPLATE.format(title=title, title_underline='=' * len(title),
since_day=since_day, tag=tag,
today=today.strftime('%Y/%m/%d'),
n_issues=n_issues, n_pulls=n_pulls,
milestone=milestone_str,
nauthors=len(unique_authors), ncommits=ncommits,
unique_authors='\n'.join(unique_authors), links=links))
|