]> git.lizzy.rs Git - rust.git/blob - src/tools/publish_toolstate.py
Simplify SaveHandler trait
[rust.git] / src / tools / publish_toolstate.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import sys
5 import re
6 import os
7 import json
8 import datetime
9 import collections
10 import textwrap
11 try:
12     import urllib2
13 except ImportError:
14     import urllib.request as urllib2
15
16 # List of people to ping when the status of a tool or a book changed.
17 MAINTAINERS = {
18     'miri': '@oli-obk @RalfJung @eddyb',
19     'clippy-driver': '@Manishearth @llogiq @mcarton @oli-obk @phansch',
20     'rls': '@Xanewok',
21     'rustfmt': '@topecongiro',
22     'book': '@carols10cents @steveklabnik',
23     'nomicon': '@frewsxcv @Gankro',
24     'reference': '@steveklabnik @Havvy @matthewjasper @ehuss',
25     'rust-by-example': '@steveklabnik @marioidival @projektir',
26     'embedded-book': (
27         '@adamgreig @andre-richter @jamesmunns @korken89 '
28         '@ryankurte @thejpster @therealprof'
29     ),
30     'edition-guide': '@ehuss @Centril @steveklabnik',
31     'rustc-guide': '@mark-i-m @spastorino'
32 }
33
34 REPOS = {
35     'miri': 'https://github.com/rust-lang/miri',
36     'clippy-driver': 'https://github.com/rust-lang/rust-clippy',
37     'rls': 'https://github.com/rust-lang/rls',
38     'rustfmt': 'https://github.com/rust-lang/rustfmt',
39     'book': 'https://github.com/rust-lang/book',
40     'nomicon': 'https://github.com/rust-lang-nursery/nomicon',
41     'reference': 'https://github.com/rust-lang-nursery/reference',
42     'rust-by-example': 'https://github.com/rust-lang/rust-by-example',
43     'embedded-book': 'https://github.com/rust-embedded/book',
44     'edition-guide': 'https://github.com/rust-lang-nursery/edition-guide',
45     'rustc-guide': 'https://github.com/rust-lang/rustc-guide',
46 }
47
48
49 def read_current_status(current_commit, path):
50     '''Reads build status of `current_commit` from content of `history/*.tsv`
51     '''
52     with open(path, 'rU') as f:
53         for line in f:
54             (commit, status) = line.split('\t', 1)
55             if commit == current_commit:
56                 return json.loads(status)
57     return {}
58
59 def gh_url():
60     return os.environ['TOOLSTATE_ISSUES_API_URL']
61
62 def maybe_delink(message):
63     if os.environ.get('TOOLSTATE_SKIP_MENTIONS') is not None:
64         return message.replace("@", "")
65     return message
66
67 def issue(
68     tool,
69     status,
70     maintainers,
71     relevant_pr_number,
72     relevant_pr_user,
73     pr_reviewer,
74 ):
75     # Open an issue about the toolstate failure.
76     assignees = [x.strip() for x in maintainers.split('@') if x != '']
77     if status == 'test-fail':
78         status_description = 'has failing tests'
79     else:
80         status_description = 'no longer builds'
81     request = json.dumps({
82         'body': maybe_delink(textwrap.dedent('''\
83         Hello, this is your friendly neighborhood mergebot.
84         After merging PR {}, I observed that the tool {} {}.
85         A follow-up PR to the repository {} is needed to fix the fallout.
86
87         cc @{}, do you think you would have time to do the follow-up work?
88         If so, that would be great!
89
90         cc @{}, the PR reviewer, and @rust-lang/compiler -- nominating for prioritization.
91
92         ''').format(
93             relevant_pr_number, tool, status_description,
94             REPOS.get(tool), relevant_pr_user, pr_reviewer
95         )),
96         'title': '`{}` no longer builds after {}'.format(tool, relevant_pr_number),
97         'assignees': assignees,
98         'labels': ['T-compiler', 'I-nominated'],
99     })
100     print("Creating issue:\n{}".format(request))
101     response = urllib2.urlopen(urllib2.Request(
102         gh_url(),
103         request,
104         {
105             'Authorization': 'token ' + github_token,
106             'Content-Type': 'application/json',
107         }
108     ))
109     response.read()
110
111 def update_latest(
112     current_commit,
113     relevant_pr_number,
114     relevant_pr_url,
115     relevant_pr_user,
116     pr_reviewer,
117     current_datetime
118 ):
119     '''Updates `_data/latest.json` to match build result of the given commit.
120     '''
121     with open('_data/latest.json', 'rb+') as f:
122         latest = json.load(f, object_pairs_hook=collections.OrderedDict)
123
124         current_status = {
125             os: read_current_status(current_commit, 'history/' + os + '.tsv')
126             for os in ['windows', 'linux']
127         }
128
129         slug = 'rust-lang/rust'
130         message = textwrap.dedent('''\
131             ðŸ“£ Toolstate changed by {}!
132
133             Tested on commit {}@{}.
134             Direct link to PR: <{}>
135
136         ''').format(relevant_pr_number, slug, current_commit, relevant_pr_url)
137         anything_changed = False
138         for status in latest:
139             tool = status['tool']
140             changed = False
141             create_issue_for_status = None # set to the status that caused the issue
142
143             for os, s in current_status.items():
144                 old = status[os]
145                 new = s.get(tool, old)
146                 status[os] = new
147                 if new > old: # comparing the strings, but they are ordered appropriately!
148                     # things got fixed or at least the status quo improved
149                     changed = True
150                     message += '🎉 {} on {}: {} â†’ {} (cc {}, @rust-lang/infra).\n' \
151                         .format(tool, os, old, new, MAINTAINERS.get(tool))
152                 elif new < old:
153                     # tests or builds are failing and were not failing before
154                     changed = True
155                     title = '💔 {} on {}: {} â†’ {}' \
156                         .format(tool, os, old, new)
157                     message += '{} (cc {}, @rust-lang/infra).\n' \
158                         .format(title, MAINTAINERS.get(tool))
159                     # Most tools only create issues for build failures.
160                     # Other failures can be spurious.
161                     if new == 'build-fail' or (tool == 'miri' and new == 'test-fail'):
162                         create_issue_for_status = new
163
164             if create_issue_for_status is not None:
165                 try:
166                     issue(
167                         tool, create_issue_for_status, MAINTAINERS.get(tool, ''),
168                         relevant_pr_number, relevant_pr_user, pr_reviewer,
169                     )
170                 except urllib2.HTTPError as e:
171                     # network errors will simply end up not creating an issue, but that's better
172                     # than failing the entire build job
173                     print("HTTPError when creating issue for status regression: {0}\n{1}"
174                           .format(e, e.read()))
175                 except IOError as e:
176                     print("I/O error when creating issue for status regression: {0}".format(e))
177                 except:
178                     print("Unexpected error when creating issue for status regression: {0}"
179                           .format(sys.exc_info()[0]))
180                     raise
181
182             if changed:
183                 status['commit'] = current_commit
184                 status['datetime'] = current_datetime
185                 anything_changed = True
186
187         if not anything_changed:
188             return ''
189
190         f.seek(0)
191         f.truncate(0)
192         json.dump(latest, f, indent=4, separators=(',', ': '))
193         return message
194
195
196 if __name__ == '__main__':
197     cur_commit = sys.argv[1]
198     cur_datetime = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
199     cur_commit_msg = sys.argv[2]
200     save_message_to_path = sys.argv[3]
201     github_token = sys.argv[4]
202
203     # assume that PR authors are also owners of the repo where the branch lives
204     relevant_pr_match = re.search(
205         r'Auto merge of #([0-9]+) - ([^:]+):[^,]+, r=(\S+)',
206         cur_commit_msg,
207     )
208     if relevant_pr_match:
209         number = relevant_pr_match.group(1)
210         relevant_pr_user = relevant_pr_match.group(2)
211         relevant_pr_number = 'rust-lang/rust#' + number
212         relevant_pr_url = 'https://github.com/rust-lang/rust/pull/' + number
213         pr_reviewer = relevant_pr_match.group(3)
214     else:
215         number = '-1'
216         relevant_pr_user = 'ghost'
217         relevant_pr_number = '<unknown PR>'
218         relevant_pr_url = '<unknown>'
219         pr_reviewer = 'ghost'
220
221     message = update_latest(
222         cur_commit,
223         relevant_pr_number,
224         relevant_pr_url,
225         relevant_pr_user,
226         pr_reviewer,
227         cur_datetime
228     )
229     if not message:
230         print('<Nothing changed>')
231         sys.exit(0)
232
233     print(message)
234
235     if not github_token:
236         print('Dry run only, not committing anything')
237         sys.exit(0)
238
239     with open(save_message_to_path, 'w') as f:
240         f.write(message)
241
242     # Write the toolstate comment on the PR as well.
243     issue_url = gh_url() + '/{}/comments'.format(number)
244     response = urllib2.urlopen(urllib2.Request(
245         issue_url,
246         json.dumps({'body': maybe_delink(message)}),
247         {
248             'Authorization': 'token ' + github_token,
249             'Content-Type': 'application/json',
250         }
251     ))
252     response.read()