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