]> git.lizzy.rs Git - rust.git/blob - src/tools/publish_toolstate.py
Add a profiles section to the manifest
[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
28 def read_current_status(current_commit, path):
29     '''Reads build status of `current_commit` from content of `history/*.tsv`
30     '''
31     with open(path, 'rU') as f:
32         for line in f:
33             (commit, status) = line.split('\t', 1)
34             if commit == current_commit:
35                 return json.loads(status)
36     return {}
37
38
39 def update_latest(
40     current_commit,
41     relevant_pr_number,
42     relevant_pr_url,
43     current_datetime
44 ):
45     '''Updates `_data/latest.json` to match build result of the given commit.
46     '''
47     with open('_data/latest.json', 'rb+') as f:
48         latest = json.load(f, object_pairs_hook=collections.OrderedDict)
49
50         current_status = {
51             os: read_current_status(current_commit, 'history/' + os + '.tsv')
52             for os in ['windows', 'linux']
53         }
54
55         slug = 'rust-lang/rust'
56         message = textwrap.dedent('''\
57             ðŸ“£ Toolstate changed by {}!
58
59             Tested on commit {}@{}.
60             Direct link to PR: <{}>
61
62         ''').format(relevant_pr_number, slug, current_commit, relevant_pr_url)
63         anything_changed = False
64         for status in latest:
65             tool = status['tool']
66             changed = False
67
68             for os, s in current_status.items():
69                 old = status[os]
70                 new = s.get(tool, old)
71                 status[os] = new
72                 if new > old:
73                     changed = True
74                     message += '🎉 {} on {}: {} â†’ {} (cc {}, @rust-lang/infra).\n' \
75                         .format(tool, os, old, new, MAINTAINERS.get(tool))
76                 elif new < old:
77                     changed = True
78                     message += '💔 {} on {}: {} â†’ {} (cc {}, @rust-lang/infra).\n' \
79                         .format(tool, os, old, new, MAINTAINERS.get(tool))
80
81             if changed:
82                 status['commit'] = current_commit
83                 status['datetime'] = current_datetime
84                 anything_changed = True
85
86         if not anything_changed:
87             return ''
88
89         f.seek(0)
90         f.truncate(0)
91         json.dump(latest, f, indent=4, separators=(',', ': '))
92         return message
93
94
95 if __name__ == '__main__':
96     cur_commit = sys.argv[1]
97     cur_datetime = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
98     cur_commit_msg = sys.argv[2]
99     save_message_to_path = sys.argv[3]
100     github_token = sys.argv[4]
101
102     relevant_pr_match = re.search('#([0-9]+)', cur_commit_msg)
103     if relevant_pr_match:
104         number = relevant_pr_match.group(1)
105         relevant_pr_number = 'rust-lang/rust#' + number
106         relevant_pr_url = 'https://github.com/rust-lang/rust/pull/' + number
107     else:
108         number = '-1'
109         relevant_pr_number = '<unknown PR>'
110         relevant_pr_url = '<unknown>'
111
112     message = update_latest(
113         cur_commit,
114         relevant_pr_number,
115         relevant_pr_url,
116         cur_datetime
117     )
118     if not message:
119         print('<Nothing changed>')
120         sys.exit(0)
121
122     print(message)
123
124     if not github_token:
125         print('Dry run only, not committing anything')
126         sys.exit(0)
127
128     with open(save_message_to_path, 'w') as f:
129         f.write(message)
130
131     # Write the toolstate comment on the PR as well.
132     gh_url = 'https://api.github.com/repos/rust-lang/rust/issues/{}/comments' \
133         .format(number)
134     response = urllib2.urlopen(urllib2.Request(
135         gh_url,
136         json.dumps({'body': message}),
137         {
138             'Authorization': 'token ' + github_token,
139             'Content-Type': 'application/json',
140         }
141     ))
142     response.read()