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