##// END OF EJS Templates
updates to github stats...
MinRK -
Show More
@@ -1,180 +1,200 b''
1 1 #!/usr/bin/env python
2 2 """Simple tools to query github.com and gather stats about issues.
3 3 """
4 4 #-----------------------------------------------------------------------------
5 5 # Imports
6 6 #-----------------------------------------------------------------------------
7 7
8 8 from __future__ import print_function
9 9
10 10 import json
11 11 import re
12 12 import sys
13 13
14 14 from datetime import datetime, timedelta
15 15 from subprocess import check_output
16 16 from gh_api import get_paged_request, make_auth_header, get_pull_request
17 17
18 18 #-----------------------------------------------------------------------------
19 19 # Globals
20 20 #-----------------------------------------------------------------------------
21 21
22 22 ISO8601 = "%Y-%m-%dT%H:%M:%SZ"
23 23 PER_PAGE = 100
24 24
25 25 #-----------------------------------------------------------------------------
26 26 # Functions
27 27 #-----------------------------------------------------------------------------
28 28
29 29 def get_issues(project="ipython/ipython", state="closed", pulls=False):
30 30 """Get a list of the issues from the Github API."""
31 31 which = 'pulls' if pulls else 'issues'
32 32 url = "https://api.github.com/repos/%s/%s?state=%s&per_page=%i" % (project, which, state, PER_PAGE)
33 33 return get_paged_request(url, headers=make_auth_header())
34 34
35 def round_hour(dt):
36 return dt.replace(minute=0,second=0,microsecond=0)
35 37
36 38 def _parse_datetime(s):
37 39 """Parse dates in the format returned by the Github API."""
38 40 if s:
39 41 return datetime.strptime(s, ISO8601)
40 42 else:
41 43 return datetime.fromtimestamp(0)
42 44
43 45
44 46 def issues2dict(issues):
45 47 """Convert a list of issues to a dict, keyed by issue number."""
46 48 idict = {}
47 49 for i in issues:
48 50 idict[i['number']] = i
49 51 return idict
50 52
51 53
52 54 def is_pull_request(issue):
53 55 """Return True if the given issue is a pull request."""
54 56 return bool(issue.get('pull_request', {}).get('html_url', None))
55 57
56 58
57 59 def split_pulls(all_issues, project="ipython/ipython"):
58 60 """split a list of closed issues into non-PR Issues and Pull Requests"""
59 61 pulls = []
60 62 issues = []
61 63 for i in all_issues:
62 64 if is_pull_request(i):
63 65 pull = get_pull_request(project, i['number'], auth=True)
64 66 pulls.append(pull)
65 67 else:
66 68 issues.append(i)
67 69 return issues, pulls
68 70
69 71
70 72
71 def issues_closed_since(period=timedelta(days=365), project="ipython/ipython"):
73 def issues_closed_since(period=timedelta(days=365), project="ipython/ipython", pulls=False):
72 74 """Get all issues closed since a particular point in time. period
73 75 can either be a datetime object, or a timedelta object. In the
74 76 latter case, it is used as a time before the present.
75 77 """
76 78
77 which = 'issues'
79 which = 'pulls' if pulls else 'issues'
78 80
79 81 if isinstance(period, timedelta):
80 since = datetime.now() - period
82 since = round_hour(datetime.utcnow() - period)
81 83 else:
82 84 since = period
83 85 url = "https://api.github.com/repos/%s/%s?state=closed&sort=updated&since=%s&per_page=%i" % (project, which, since.strftime(ISO8601), PER_PAGE)
84 86 allclosed = get_paged_request(url, headers=make_auth_header())
85 87
86 issues, pulls = split_pulls(allclosed, project=project)
87 issues = [i for i in issues if _parse_datetime(i['closed_at']) > since]
88 pulls = [p for p in pulls if p['merged_at'] and _parse_datetime(p['merged_at']) > since]
88 filtered = [ i for i in allclosed if _parse_datetime(i['closed_at']) > since ]
89 if pulls:
90 filtered = [ i for i in filtered if _parse_datetime(i['merged_at']) > since ]
91 # filter out PRs not against master (backports)
92 filtered = [ i for i in filtered if i['base']['ref'] == 'master' ]
93 else:
94 filtered = [ i for i in filtered if not is_pull_request(i) ]
89 95
90 return issues, pulls
96 return filtered
91 97
92 98
93 99 def sorted_by_field(issues, field='closed_at', reverse=False):
94 100 """Return a list of issues sorted by closing date date."""
95 101 return sorted(issues, key = lambda i:i[field], reverse=reverse)
96 102
97 103
98 104 def report(issues, show_urls=False):
99 105 """Summary report about a list of issues, printing number and title.
100 106 """
101 107 # titles may have unicode in them, so we must encode everything below
102 108 if show_urls:
103 109 for i in issues:
104 110 role = 'ghpull' if 'merged_at' in i else 'ghissue'
105 111 print('* :%s:`%d`: %s' % (role, i['number'],
106 112 i['title'].encode('utf-8')))
107 113 else:
108 114 for i in issues:
109 115 print('* %d: %s' % (i['number'], i['title'].encode('utf-8')))
110 116
111 117 #-----------------------------------------------------------------------------
112 118 # Main script
113 119 #-----------------------------------------------------------------------------
114 120
115 121 if __name__ == "__main__":
122 # deal with unicode
123 import codecs
124 sys.stdout = codecs.getwriter('utf8')(sys.stdout)
125
116 126 # Whether to add reST urls for all issues in printout.
117 127 show_urls = True
118 128
119 129 # By default, search one month back
120 130 tag = None
121 131 if len(sys.argv) > 1:
122 132 try:
123 133 days = int(sys.argv[1])
124 134 except:
125 135 tag = sys.argv[1]
126 136 else:
127 137 tag = check_output(['git', 'describe', '--abbrev=0']).strip()
128 138
129 139 if tag:
130 140 cmd = ['git', 'log', '-1', '--format=%ai', tag]
131 141 tagday, tz = check_output(cmd).strip().rsplit(' ', 1)
132 since = datetime.strptime(tagday, "%Y-%m-%d %H:%M:%S")# - timedelta(days=30 * 6)
142 since = datetime.strptime(tagday, "%Y-%m-%d %H:%M:%S")
143 h = int(tz[1:3])
144 m = int(tz[3:])
145 td = timedelta(hours=h, minutes=m)
146 if tz[0] == '-':
147 since += td
148 else:
149 since -= td
133 150 else:
134 since = datetime.now() - timedelta(days=days)
151 since = datetime.utcnow() - timedelta(days=days)
152
153 since = round_hour(since)
135 154
136 155 print("fetching GitHub stats since %s (tag: %s)" % (since, tag), file=sys.stderr)
137 156 # turn off to play interactively without redownloading, use %run -i
138 157 if 1:
139 issues, pulls = issues_closed_since(since)
140
158 issues = issues_closed_since(since, pulls=False)
159 pulls = issues_closed_since(since, pulls=True)
160
141 161 # For regular reports, it's nice to show them in reverse chronological order
142 162 issues = sorted_by_field(issues, reverse=True)
143 163 pulls = sorted_by_field(pulls, reverse=True)
144 164
145 165 n_issues, n_pulls = map(len, (issues, pulls))
146 166 n_total = n_issues + n_pulls
147 167
148 168 # Print summary report we can directly include into release notes.
169
149 170 print()
150 171 since_day = since.strftime("%Y/%m/%d")
151 172 today = datetime.today().strftime("%Y/%m/%d")
152 173 print("GitHub stats for %s - %s (tag: %s)" % (since_day, today, tag))
153 174 print()
154 175 print("These lists are automatically generated, and may be incomplete or contain duplicates.")
155 176 print()
156 177 if tag:
157 178 # print git info, in addition to GitHub info:
158 179 since_tag = tag+'..'
159 180 cmd = ['git', 'log', '--oneline', since_tag]
160 181 ncommits = len(check_output(cmd).splitlines())
161 182
162 author_cmd = ['git', 'log', '--format=* %aN', since_tag]
163 all_authors = check_output(author_cmd).splitlines()
164 unique_authors = sorted(set(all_authors))
165
183 author_cmd = ['git', 'log', '--use-mailmap', "--format='* %aN'", since_tag]
184 all_authors = check_output(author_cmd).decode('utf-8', 'replace').splitlines()
185 unique_authors = sorted(set(all_authors), key=lambda s: s.lower())
166 186 print("The following %i authors contributed %i commits." % (len(unique_authors), ncommits))
167 187 print()
168 188 print('\n'.join(unique_authors))
169 189 print()
170 190
171 191 print()
172 192 print("We closed a total of %d issues, %d pull requests and %d regular issues;\n"
173 193 "this is the full list (generated with the script \n"
174 194 ":file:`tools/github_stats.py`):" % (n_total, n_pulls, n_issues))
175 195 print()
176 196 print('Pull Requests (%d):\n' % n_pulls)
177 197 report(pulls, show_urls)
178 198 print()
179 199 print('Issues (%d):\n' % n_issues)
180 200 report(issues, show_urls)
General Comments 0
You need to be logged in to leave comments. Login now