2 # -*- coding: utf-8 -*-
4 ## This script publishes the new "current" toolstate in the toolstate repo (not to be
5 ## confused with publishing the test results, which happens in
6 ## `src/ci/docker/x86_64-gnu-tools/checktools.sh`).
7 ## It is set as callback for `src/ci/docker/x86_64-gnu-tools/repo.sh` by the CI scripts
8 ## when a new commit lands on `master` (i.e., after it passed all checks on `auto`).
20 import urllib.request as urllib2
22 # List of people to ping when the status of a tool or a book changed.
24 'miri': '@oli-obk @RalfJung @eddyb',
25 'clippy-driver': '@Manishearth @llogiq @mcarton @oli-obk @phansch',
27 'rustfmt': '@topecongiro',
28 'book': '@carols10cents @steveklabnik',
29 'nomicon': '@frewsxcv @Gankro',
30 'reference': '@steveklabnik @Havvy @matthewjasper @ehuss',
31 'rust-by-example': '@steveklabnik @marioidival @projektir',
33 '@adamgreig @andre-richter @jamesmunns @korken89 '
34 '@ryankurte @thejpster @therealprof'
36 'edition-guide': '@ehuss @Centril @steveklabnik',
37 'rustc-guide': '@mark-i-m @spastorino'
41 'miri': 'https://github.com/rust-lang/miri',
42 'clippy-driver': 'https://github.com/rust-lang/rust-clippy',
43 'rls': 'https://github.com/rust-lang/rls',
44 'rustfmt': 'https://github.com/rust-lang/rustfmt',
45 'book': 'https://github.com/rust-lang/book',
46 'nomicon': 'https://github.com/rust-lang-nursery/nomicon',
47 'reference': 'https://github.com/rust-lang-nursery/reference',
48 'rust-by-example': 'https://github.com/rust-lang/rust-by-example',
49 'embedded-book': 'https://github.com/rust-embedded/book',
50 'edition-guide': 'https://github.com/rust-lang-nursery/edition-guide',
51 'rustc-guide': 'https://github.com/rust-lang/rustc-guide',
55 def read_current_status(current_commit, path):
56 '''Reads build status of `current_commit` from content of `history/*.tsv`
58 with open(path, 'rU') as f:
60 (commit, status) = line.split('\t', 1)
61 if commit == current_commit:
62 return json.loads(status)
66 return os.environ['TOOLSTATE_ISSUES_API_URL']
68 def maybe_delink(message):
69 if os.environ.get('TOOLSTATE_SKIP_MENTIONS') is not None:
70 return message.replace("@", "")
81 # Open an issue about the toolstate failure.
82 assignees = [x.strip() for x in maintainers.split('@') if x != '']
83 if status == 'test-fail':
84 status_description = 'has failing tests'
86 status_description = 'no longer builds'
87 request = json.dumps({
88 'body': maybe_delink(textwrap.dedent('''\
89 Hello, this is your friendly neighborhood mergebot.
90 After merging PR {}, I observed that the tool {} {}.
91 A follow-up PR to the repository {} is needed to fix the fallout.
93 cc @{}, do you think you would have time to do the follow-up work?
94 If so, that would be great!
96 cc @{}, the PR reviewer, and @rust-lang/compiler -- nominating for prioritization.
99 relevant_pr_number, tool, status_description,
100 REPOS.get(tool), relevant_pr_user, pr_reviewer
102 'title': '`{}` no longer builds after {}'.format(tool, relevant_pr_number),
103 'assignees': assignees,
104 'labels': ['T-compiler', 'I-nominated'],
106 print("Creating issue:\n{}".format(request))
107 response = urllib2.urlopen(urllib2.Request(
111 'Authorization': 'token ' + github_token,
112 'Content-Type': 'application/json',
125 '''Updates `_data/latest.json` to match build result of the given commit.
127 with open('_data/latest.json', 'rb+') as f:
128 latest = json.load(f, object_pairs_hook=collections.OrderedDict)
131 os: read_current_status(current_commit, 'history/' + os + '.tsv')
132 for os in ['windows', 'linux']
135 slug = 'rust-lang/rust'
136 message = textwrap.dedent('''\
137 📣 Toolstate changed by {}!
139 Tested on commit {}@{}.
140 Direct link to PR: <{}>
142 ''').format(relevant_pr_number, slug, current_commit, relevant_pr_url)
143 anything_changed = False
144 for status in latest:
145 tool = status['tool']
147 create_issue_for_status = None # set to the status that caused the issue
149 for os, s in current_status.items():
151 new = s.get(tool, old)
153 if new > old: # comparing the strings, but they are ordered appropriately!
154 # things got fixed or at least the status quo improved
156 message += '🎉 {} on {}: {} → {} (cc {}, @rust-lang/infra).\n' \
157 .format(tool, os, old, new, MAINTAINERS.get(tool))
159 # tests or builds are failing and were not failing before
161 title = '💔 {} on {}: {} → {}' \
162 .format(tool, os, old, new)
163 message += '{} (cc {}, @rust-lang/infra).\n' \
164 .format(title, MAINTAINERS.get(tool))
165 # Most tools only create issues for build failures.
166 # Other failures can be spurious.
167 if new == 'build-fail' or (tool == 'miri' and new == 'test-fail'):
168 create_issue_for_status = new
170 if create_issue_for_status is not None:
173 tool, create_issue_for_status, MAINTAINERS.get(tool, ''),
174 relevant_pr_number, relevant_pr_user, pr_reviewer,
176 except urllib2.HTTPError as e:
177 # network errors will simply end up not creating an issue, but that's better
178 # than failing the entire build job
179 print("HTTPError when creating issue for status regression: {0}\n{1}"
180 .format(e, e.read()))
182 print("I/O error when creating issue for status regression: {0}".format(e))
184 print("Unexpected error when creating issue for status regression: {0}"
185 .format(sys.exc_info()[0]))
189 status['commit'] = current_commit
190 status['datetime'] = current_datetime
191 anything_changed = True
193 if not anything_changed:
198 json.dump(latest, f, indent=4, separators=(',', ': '))
202 if __name__ == '__main__':
203 cur_commit = sys.argv[1]
204 cur_datetime = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
205 cur_commit_msg = sys.argv[2]
206 save_message_to_path = sys.argv[3]
207 github_token = sys.argv[4]
209 # assume that PR authors are also owners of the repo where the branch lives
210 relevant_pr_match = re.search(
211 r'Auto merge of #([0-9]+) - ([^:]+):[^,]+, r=(\S+)',
214 if relevant_pr_match:
215 number = relevant_pr_match.group(1)
216 relevant_pr_user = relevant_pr_match.group(2)
217 relevant_pr_number = 'rust-lang/rust#' + number
218 relevant_pr_url = 'https://github.com/rust-lang/rust/pull/' + number
219 pr_reviewer = relevant_pr_match.group(3)
222 relevant_pr_user = 'ghost'
223 relevant_pr_number = '<unknown PR>'
224 relevant_pr_url = '<unknown>'
225 pr_reviewer = 'ghost'
227 message = update_latest(
236 print('<Nothing changed>')
242 print('Dry run only, not committing anything')
245 with open(save_message_to_path, 'w') as f:
248 # Write the toolstate comment on the PR as well.
249 issue_url = gh_url() + '/{}/comments'.format(number)
250 response = urllib2.urlopen(urllib2.Request(
252 json.dumps({'body': maybe_delink(message)}),
254 'Authorization': 'token ' + github_token,
255 'Content-Type': 'application/json',