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