]> git.lizzy.rs Git - rust.git/blob - src/tools/publish_toolstate.py
improve tests as suggested by review comments
[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 datetime
18 import collections
19 import textwrap
20 try:
21     import urllib2
22 except ImportError:
23     import urllib.request as urllib2
24
25 # List of people to ping when the status of a tool changed.
26 MAINTAINERS = {
27     'miri': '@oli-obk @RalfJung @eddyb',
28     'clippy-driver': '@Manishearth @llogiq @mcarton @oli-obk',
29     'rls': '@nrc @Xanewok',
30     'rustfmt': '@nrc',
31     'book': '@carols10cents @steveklabnik',
32     'nomicon': '@frewsxcv @Gankro',
33     'reference': '@steveklabnik @Havvy @matthewjasper @alercah',
34     'rust-by-example': '@steveklabnik @marioidival @projektir',
35 }
36
37 EMOJI = {
38     'miri': '🛰️',
39     'clippy-driver': '📎',
40     'rls': '💻',
41     'rustfmt': '📝',
42     'book': '📖',
43     'nomicon': '👿',
44     'reference': '📚',
45     'rust-by-example': '👩‍🏫',
46 }
47
48 def read_current_status(current_commit, path):
49     '''Reads build status of `current_commit` from content of `history/*.tsv`
50     '''
51     with open(path, 'rU') as f:
52         for line in f:
53             (commit, status) = line.split('\t', 1)
54             if commit == current_commit:
55                 return json.loads(status)
56     return {}
57
58
59 def update_latest(
60     current_commit,
61     relevant_pr_number,
62     relevant_pr_url,
63     current_datetime
64 ):
65     '''Updates `_data/latest.json` to match build result of the given commit.
66     '''
67     with open('_data/latest.json', 'rb+') as f:
68         latest = json.load(f, object_pairs_hook=collections.OrderedDict)
69
70         current_status = {
71             os: read_current_status(current_commit, 'history/' + os + '.tsv')
72             for os in ['windows', 'linux']
73         }
74
75         slug = 'rust-lang/rust'
76         long_message = textwrap.dedent('''\
77             Tested on commit {}@{}.
78             Direct link to PR: <{}>
79
80         ''').format(slug, current_commit, relevant_pr_url)
81         emoji_status = []
82         anything_changed = False
83         for status in latest:
84             tool = status['tool']
85             changed = False
86
87             for os, s in current_status.items():
88                 old = status[os]
89                 new = s.get(tool, old)
90                 status[os] = new
91                 if new > old:
92                     changed = True
93                     long_message += '🎉 {} on {}: {} → {}.\n' \
94                         .format(tool, os, old, new)
95                     emoji = "{}🎉".format(EMOJI.get(tool))
96                     if msg not in emoji_status:
97                         emoji_status += [msg]
98                 elif new < old:
99                     changed = True
100                     long_message += '💔 {} on {}: {} → {} (cc {}, @rust-lang/infra).\n' \
101                         .format(tool, os, old, new, MAINTAINERS.get(tool))
102                     emoji = "{}💔".format(EMOJI.get(tool))
103                     if msg not in emoji_status:
104                         emoji_status += [msg]
105
106             if changed:
107                 status['commit'] = current_commit
108                 status['datetime'] = current_datetime
109                 anything_changed = True
110
111         if not anything_changed:
112             return ''
113
114         short_message = "📣 Toolstate changed by {}! ({})"
115             .format(relevant_pr_number, '/'.join(emoji_status))
116         message = short_message + "\n\n" + long_message
117         f.seek(0)
118         f.truncate(0)
119         json.dump(latest, f, indent=4, separators=(',', ': '))
120         return message
121
122
123 if __name__ == '__main__':
124     cur_commit = sys.argv[1]
125     cur_datetime = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
126     cur_commit_msg = sys.argv[2]
127     save_message_to_path = sys.argv[3]
128     github_token = sys.argv[4]
129
130     relevant_pr_match = re.search('#([0-9]+)', cur_commit_msg)
131     if relevant_pr_match:
132         number = relevant_pr_match.group(1)
133         relevant_pr_number = 'rust-lang/rust#' + number
134         relevant_pr_url = 'https://github.com/rust-lang/rust/pull/' + number
135     else:
136         number = '-1'
137         relevant_pr_number = '<unknown PR>'
138         relevant_pr_url = '<unknown>'
139
140     message = update_latest(
141         cur_commit,
142         relevant_pr_number,
143         relevant_pr_url,
144         cur_datetime
145     )
146     if not message:
147         print('<Nothing changed>')
148         sys.exit(0)
149
150     print(message)
151     with open(save_message_to_path, 'w') as f:
152         f.write(message)
153
154     # Write the toolstate comment on the PR as well.
155     gh_url = 'https://api.github.com/repos/rust-lang/rust/issues/{}/comments' \
156         .format(number)
157     response = urllib2.urlopen(urllib2.Request(
158         gh_url,
159         json.dumps({'body': message}),
160         {
161             'Authorization': 'token ' + github_token,
162             'Content-Type': 'application/json',
163         }
164     ))
165     response.read()