Debian Codegraph @

tools/changelog.py

raw
1 #!/usr/bin/env python3
2 """
3 Script to generate contributor and pull request lists
4
5 This script generates contributor and pull request lists for release
6 changelogs using Github v3 protocol. Use requires an authentication token in
7 order to have sufficient bandwidth, you can get one following the directions at
8 `<https://help.github.com/articles/creating-an-access-token-for-command-line-use/>_
9 Don't add any scope, as the default is read access to public information. The
10 token may be stored in an environment variable as you only get one chance to
11 see it.
12
13 Usage::
14
15 $ ./tools/announce.py <token> <revision range>
16
17 The output is utf8 rst.
18
19 Dependencies
20 ------------
21
22 - gitpython
23 - pygithub
24 - git >= 2.29.0
25
26 Some code was copied from scipy `tools/gh_list.py` and `tools/authors.py`.
27
28 Examples
29 --------
30
31 From the bash command line with $GITHUB token::
32
33 $ ./tools/announce $GITHUB v1.13.0..v1.14.0 > 1.14.0-changelog.rst
34
35 """
36 import os
37 import re
38
39 from git import Repo
40 from github import Github
41
42 this_repo = Repo(os.path.join(os.path.dirname(__file__), ".."))
43
44 author_msg =\
45 """
46 A total of %d people contributed to this release. People with a "+" by their
47 names contributed a patch for the first time.
48 """
49
50 pull_request_msg =\
51 """
52 A total of %d pull requests were merged for this release.
53 """
54
55
56 def get_authors(revision_range):
57 lst_release, cur_release = [r.strip() for r in revision_range.split('..')]
58 authors_pat = r'^.*\t(.*)$'
59
60 # authors and co-authors in current and previous releases.
61 grp1 = '--group=author'
62 grp2 = '--group=trailer:co-authored-by'
63 cur = this_repo.git.shortlog('-s', grp1, grp2, revision_range)
64 pre = this_repo.git.shortlog('-s', grp1, grp2, lst_release)
65 authors_cur = set(re.findall(authors_pat, cur, re.M))
66 authors_pre = set(re.findall(authors_pat, pre, re.M))
67
68 # Ignore the bot Homu.
69 authors_cur.discard('Homu')
70 authors_pre.discard('Homu')
71
72 # Ignore the bot dependabot-preview
73 authors_cur.discard('dependabot-preview')
74 authors_pre.discard('dependabot-preview')
75
76 # Append '+' to new authors.
77 authors_new = [s + ' +' for s in authors_cur - authors_pre]
78 authors_old = list(authors_cur & authors_pre)
79 authors = authors_new + authors_old
80 authors.sort()
81 return authors
82
83
84 def get_pull_requests(repo, revision_range):
85 prnums = []
86
87 # From regular merges
88 merges = this_repo.git.log(
89 '--oneline', '--merges', revision_range)
90 issues = re.findall(r"Merge pull request \#(\d*)", merges)
91 prnums.extend(int(s) for s in issues)
92
93 # From Homu merges (Auto merges)
94 issues = re. findall(r"Auto merge of \#(\d*)", merges)
95 prnums.extend(int(s) for s in issues)
96
97 # From fast forward squash-merges
98 commits = this_repo.git.log(
99 '--oneline', '--no-merges', '--first-parent', revision_range)
100 issues = re.findall(r'^.*\((\#|gh-|gh-\#)(\d+)\)$', commits, re.M)
101 prnums.extend(int(s[1]) for s in issues)
102
103 # get PR data from github repo
104 prnums.sort()
105 prs = [repo.get_pull(n) for n in prnums]
106 return prs
107
108
109 def main(token, revision_range):
110 lst_release, cur_release = [r.strip() for r in revision_range.split('..')]
111
112 github = Github(token)
113 github_repo = github.get_repo('numpy/numpy')
114
115 # document authors
116 authors = get_authors(revision_range)
117 heading = "Contributors"
118 print()
119 print(heading)
120 print("=" * len(heading))
121 print(author_msg % len(authors))
122
123 for s in authors:
124 print('* ' + s)
125
126 # document pull requests
127 pull_requests = get_pull_requests(github_repo, revision_range)
128 heading = "Pull requests merged"
129 pull_msg = "* `#{0} <{1}>`__: {2}"
130
131 print()
132 print(heading)
133 print("=" * len(heading))
134 print(pull_request_msg % len(pull_requests))
135
136 def backtick_repl(matchobj):
137 """repl to add an escaped space following a code block if needed"""
138 if matchobj.group(2) != ' ':
139 post = r'\ ' + matchobj.group(2)
140 else:
141 post = matchobj.group(2)
142 return '``' + matchobj.group(1) + '``' + post
143
144 for pull in pull_requests:
145 # sanitize whitespace
146 title = re.sub(r"\s+", " ", pull.title.strip())
147
148 # substitute any single backtick not adjacent to a backtick
149 # for a double backtick
150 title = re.sub(
151 r"(?P<pre>(?:^|(?<=[^`])))`(?P<post>(?=[^`]|$))",
152 r"\g<pre>``\g<post>",
153 title
154 )
155 # add an escaped space if code block is not followed by a space
156 title = re.sub(r"``(.*?)``(.)", backtick_repl, title)
157
158 # sanitize asterisks
159 title = title.replace('*', '\\*')
160
161 if len(title) > 60:
162 remainder = re.sub(r"\s.*$", "...", title[60:])
163 if len(remainder) > 20:
164 # just use the first 80 characters, with ellipses.
165 # note: this was previously bugged,
166 # assigning to `remainder` rather than `title`
167 title = title[:80] + "..."
168 else:
169 # use the first 60 characters and the next word
170 title = title[:60] + remainder
171
172 if title.count('`') % 4 != 0:
173 # ellipses have cut in the middle of a code block,
174 # so finish the code block before the ellipses
175 title = title[:-3] + '``...'
176
177 print(pull_msg.format(pull.number, pull.html_url, title))
178
179
180 if __name__ == "__main__":
181 from argparse import ArgumentParser
182
183 parser = ArgumentParser(description="Generate author/pr lists for release")
184 parser.add_argument('token', help='github access token')
185 parser.add_argument('revision_range', help='<revision>..<revision>')
186 args = parser.parse_args()
187 main(args.token, args.revision_range)