]> git.lizzy.rs Git - rust.git/blob - src/tools/publish_toolstate.py
Auto merge of #54624 - arielb1:evaluate-outlives, r=nikomatsakis
[rust.git] / src / tools / publish_toolstate.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # Copyright 2017 The Rust Project Developers. See the COPYRIGHT
5 # file at the top-level directory of this distribution and at
6 # http://rust-lang.org/COPYRIGHT.
7 #
8 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
9 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
10 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
11 # option. This file may not be copied, modified, or distributed
12 # except according to those terms.
13
14 import sys
15 import re
16 import json
17 import copy
18 import datetime
19 import collections
20 import textwrap
21 try:
22     import urllib2
23 except ImportError:
24     import urllib.request as urllib2
25
26 # List of people to ping when the status of a tool changed.
27 MAINTAINERS = {
28     'miri': '@oli-obk @RalfJung @eddyb',
29     'clippy-driver': '@Manishearth @llogiq @mcarton @oli-obk',
30     'rls': '@nrc',
31     'rustfmt': '@nrc',
32     'book': '@carols10cents @steveklabnik',
33     'nomicon': '@frewsxcv @Gankro',
34     'reference': '@steveklabnik @Havvy @matthewjasper @alercah',
35     'rust-by-example': '@steveklabnik @marioidival @projektir',
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
50 def update_latest(
51     current_commit,
52     relevant_pr_number,
53     relevant_pr_url,
54     current_datetime
55 ):
56     '''Updates `_data/latest.json` to match build result of the given commit.
57     '''
58     with open('_data/latest.json', 'rb+') as f:
59         latest = json.load(f, object_pairs_hook=collections.OrderedDict)
60
61         current_status = {
62             os: read_current_status(current_commit, 'history/' + os + '.tsv')
63             for os in ['windows', 'linux']
64         }
65
66         slug = 'rust-lang/rust'
67         message = textwrap.dedent('''\
68             ðŸ“£ Toolstate changed by {}!
69
70             Tested on commit {}@{}.
71             Direct link to PR: <{}>
72
73         ''').format(relevant_pr_number, slug, current_commit, relevant_pr_url)
74         anything_changed = False
75         for status in latest:
76             tool = status['tool']
77             changed = False
78
79             for os, s in current_status.items():
80                 old = status[os]
81                 new = s.get(tool, old)
82                 status[os] = new
83                 if new > old:
84                     changed = True
85                     message += '🎉 {} on {}: {} â†’ {}.\n' \
86                         .format(tool, os, old, new)
87                 elif new < old:
88                     changed = True
89                     message += '💔 {} on {}: {} â†’ {} (cc {}, @rust-lang/infra).\n' \
90                         .format(tool, os, old, new, MAINTAINERS.get(tool))
91
92             if changed:
93                 status['commit'] = current_commit
94                 status['datetime'] = current_datetime
95                 anything_changed = True
96
97         if not anything_changed:
98             return ''
99
100         f.seek(0)
101         f.truncate(0)
102         json.dump(latest, f, indent=4, separators=(',', ': '))
103         return message
104
105
106 if __name__ == '__main__':
107     cur_commit = sys.argv[1]
108     cur_datetime = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
109     cur_commit_msg = sys.argv[2]
110     save_message_to_path = sys.argv[3]
111     github_token = sys.argv[4]
112
113     relevant_pr_match = re.search('#([0-9]+)', cur_commit_msg)
114     if relevant_pr_match:
115         number = relevant_pr_match.group(1)
116         relevant_pr_number = 'rust-lang/rust#' + number
117         relevant_pr_url = 'https://github.com/rust-lang/rust/pull/' + number
118     else:
119         number = '-1'
120         relevant_pr_number = '<unknown PR>'
121         relevant_pr_url = '<unknown>'
122
123     message = update_latest(
124         cur_commit,
125         relevant_pr_number,
126         relevant_pr_url,
127         cur_datetime
128     )
129     if not message:
130         print('<Nothing changed>')
131         sys.exit(0)
132
133     print(message)
134     with open(save_message_to_path, 'w') as f:
135         f.write(message)
136
137     # Write the toolstate comment on the PR as well.
138     gh_url = 'https://api.github.com/repos/rust-lang/rust/issues/{}/comments' \
139         .format(number)
140     response = urllib2.urlopen(urllib2.Request(
141         gh_url,
142         json.dumps({'body': message}),
143         {
144             'Authorization': 'token ' + github_token,
145             'Content-Type': 'application/json',
146         }
147     ))
148     response.read()