forked from alisw/ali-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
report-pr-errors
executable file
·335 lines (279 loc) · 12.1 KB
/
report-pr-errors
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env python
from __future__ import print_function
from argparse import ArgumentParser, Namespace
import atexit
try:
from commands import getstatusoutput
except ImportError:
from subprocess import getstatusoutput
from glob import glob
from os.path import dirname, join
import re
import os
import sys
import datetime
from alibot_helpers.github_utilities import calculateMessageHash, github_token
from alibot_helpers.github_utilities import setGithubStatus, parseGithubRef
from alibot_helpers.github_utilities import GithubCachedClient, PickledCache
from alibot_helpers.utilities import to_unicode
# Allow uploading logs to S3
try:
from boto3.s3.transfer import S3Transfer
import boto3
except:
pass
def parse_args():
parser = ArgumentParser()
parser.add_argument("--work-dir", "-w", default="sw", dest="workDir")
parser.add_argument("--default", default="release")
parser.add_argument("--devel-prefix", "-z",
dest="develPrefix",
default="")
parser.add_argument("--pr",
required=True,
help=("Pull request which was checked in "
"<org>/<project>#<nr>@ref format"))
parser.add_argument("--no-comments",
action="store_true",
dest="noComments",
default=False,
help="Use Details button, do not post a comment")
parser.add_argument("--success",
action="store_true",
dest="success",
default=False,
help="Signal a green status, not error")
parser.add_argument("--status", "-s",
required=True,
help="Check which had the error")
parser.add_argument("--dry-run", "-n",
action="store_true",
default=False,
help="Do not actually comment")
parser.add_argument("--limit", "-l",
default=50,
help="Max number of lines from the report")
parser.add_argument("--message", "-m",
dest="message",
help="Message to be posted")
parser.add_argument("--logs-dest",
dest="logsDest",
default="rsync://repo.marathon.mesos/store/logs",
help="Destination store for logs. Either rsync://<rsync server enpoint> or s3://<bucket>.<server>")
parser.add_argument("--log-url",
dest="logsUrl",
default="https://ali-ci.cern.ch/repo/logs",
help="Destination path for logs")
parser.add_argument("--debug", "-d",
action="store_true",
default=False,
help="Turn on debug output")
args = parser.parse_args()
if "#" not in args.pr:
parser.error("You need to specify a pull request")
if "@" not in args.pr:
parser.error("You need to specify a commit this error refers to")
return args
class Logs(object):
def __init__(self, args, is_branch):
self.work_dir = args.workDir
self.develPrefix = args.develPrefix
self.limit = args.limit
self.norm_status = re.sub('[^a-zA-Z0-9_-]', '_', args.status)
self.full_log = self.constructFullLogName(args.pr)
self.is_branch = is_branch
self.full_log_latest = self.constructFullLogName(args.pr, latest=True)
self.dest = args.logsDest
self.url = join(args.logsUrl, self.full_log)
def parse(self):
self.find()
self.grep()
self.cat(self.full_log)
if self.is_branch: self.cat(self.full_log_latest, no_delete=True)
if self.dest.startswith("rsync:"):
self.rsync(self.dest)
elif self.dest.startswith("s3"):
self.s3Upload(self.dest)
else:
print("Unknown destination url %s" % self.dest)
def constructFullLogName(self, pr, latest=False):
# file to which we cat all the individual logs
pr = parse_pr(pr)
return join(pr.repo_name, pr.id, "latest" if latest else pr.commit, self.norm_status, "fullLog.txt")
def find(self):
# Python's glob * matches at least one char
search_paths = [join(self.work_dir, "BUILD/*latest*/log"),
join(self.work_dir, "BUILD/*latest/log")]
print("Searching all logs matching: %s" % ", ".join(search_paths), file=sys.stderr)
globbed = []
for sp in search_paths:
globbed.extend(glob(sp))
suffix = ("latest" + "-" + self.develPrefix).strip("-")
logs = {x for x in globbed if dirname(x).endswith(suffix)}
print("Found:\n%s" % "\n".join(logs), file=sys.stderr)
self.logs = logs
def grep(self):
"""Grep for errors in the build logs, or, if none are found,
return the last N lines where N is the limit argument.
"""
error_log = ""
for log in self.logs:
cmd = "cat %s | grep -e ': error:' -A 3 -B 3 " % log
cmd += "|| tail -n %s %s" % (self.limit, log)
err, out = getstatusoutput(cmd)
if err:
print("Error while parsing logs", file=sys.stderr)
print(out, file=sys.stderr)
continue
error_log += log + "\n"
error_log += out
error_log = "\n".join(error_log.split("\n")[0:self.limit])
error_log.strip(" \n\t")
self.error_log = error_log
def cat(self, tgtFile, no_delete=False):
cmd = "%s && mkdir -p `dirname copy-logs/%s`" % ("true" if no_delete else "rm -rf copy-logs", tgtFile)
err, out = getstatusoutput(cmd)
if err:
print(out, file=sys.stderr)
cmd = "echo 'Builing on: '`hostname` >> copy-logs/%s" % tgtFile
print(cmd, file=sys.stderr)
err, out = getstatusoutput(cmd)
print(out, file=sys.stderr)
if len(self.logs):
cmd = "echo 'The following files are present in the log:' >> copy-logs/%s" % tgtFile
print(cmd, file=sys.stderr)
err, out = getstatusoutput(cmd)
print(out, file=sys.stderr)
else:
cmd = "echo 'No logs found. Please check actual builders log in aurora.\nSee http://alisw.github.io/infrastructure-pr-testing for more instructions.' >> copy-logs/%s" % tgtFile
print(cmd, file=sys.stderr)
err, out = getstatusoutput(cmd)
print(out, file=sys.stderr)
for log in self.logs:
cmd = "echo '- %s' >> copy-logs/%s" % (log, tgtFile)
print(cmd, file=sys.stderr)
err, out = getstatusoutput(cmd)
print(out, file=sys.stderr)
for log in self.logs:
cmd = "echo '## Begin %s' >> copy-logs/%s" % (log, tgtFile)
print(cmd, file=sys.stderr)
err, out = getstatusoutput(cmd)
print(out, file=sys.stderr)
cmd = "cat %s >> copy-logs/%s" % (log, tgtFile)
print(cmd, file=sys.stderr)
err, out = getstatusoutput(cmd)
print(out, file=sys.stderr)
cmd = "echo '## End %s' >> copy-logs/%s" % (log, tgtFile)
print(cmd, file=sys.stderr)
err, out = getstatusoutput(cmd)
print(out, file=sys.stderr)
def rsync(self, dest):
err, out = getstatusoutput("cd copy-logs && rsync -av ./ %s" % dest)
if err:
print("Error while copying logs to store.", file=sys.stderr)
print(out, file=sys.stderr)
def s3Upload(self, dest):
m = re.compile("^s3://([^.]+)[.](.*)").match(dest)
bucket_name = m.group(1)
server = m.group(2)
s3_client = boto3.client('s3',
aws_access_key_id=os.environ["AWS_ACCESS_KEY_ID"],
aws_secret_access_key=os.environ["AWS_SECRET_ACCESS_KEY"],
endpoint_url="https://%s" % (server))
s3_client.list_buckets()
transfer = S3Transfer(s3_client)
matches = []
for root, dirnames, filenames in os.walk('copy-logs'):
for filename in filenames:
matches.append(os.path.join(root, filename))
for src in matches:
try:
dst = re.compile('^copy-logs/').sub('', src)
transfer.upload_file(src, bucket_name, dst, extra_args={'ContentType': 'text/plain',
'ContentDisposition': 'inline'})
except Exception as e:
print("Failed to upload %s to %s in bucket %s.%s" % (src, dst, bucket_name, server))
def handle_branch(cgh, pr, logs, args):
# pr_id in this case is in fact a branch name
branch = cgh.get("/repos/{repo_name}/branches/{branch}",
repo_name=pr.repo_name,
branch=pr.id)
sha = branch["commit"]["sha"]
message = "Error while checking %s for %s:\n" % (args.status, sha)
if args.message:
message += to_unicode(args.message)
else:
message += "```\n%s\n```\nFull log [here](%s).\n" % (to_unicode(logs.error_log), to_unicode(logs.url))
messageSha = calculateMessageHash(message)
ns = Namespace(commit=args.pr,
status=args.status + "/error",
message="",
url="")
setGithubStatus(cgh, ns)
sys.exit(0)
def handle_pr_id(cgh, pr, logs, args):
commit = cgh.get("/repos/{repo_name}/commits/{ref}",
repo_name=pr.repo_name,
ref=pr.commit)
sha = commit["sha"]
message = "Error while checking %s for %s at %s:\n" % (args.status, sha, datetime.datetime.now().strftime("%Y-%m-%d %H:%M"))
if args.message:
message += args.message
else:
message += "```\n%s\n```\nFull log [here](%s).\n" % (to_unicode(logs.error_log), to_unicode(logs.url))
if args.dry_run:
# commit does not exist...
print("Will annotate %s" % commit["sha"])
print(message)
sys.exit(0)
# Set status
ghStatus = "/success" if args.success else "/error"
ns = Namespace(commit=args.pr, status=args.status + ghStatus, message="", url=logs.url)
setGithubStatus(cgh, ns)
# Comment if appropriate
if args.noComments or args.success:
return
prIssueComments = cgh.get("/repos/{repo_name}/issues/{pr_id}/comments",
repo_name=pr.repo_name,
pr_id=pr.id)
messageHash = calculateMessageHash(message)
for comment in prIssueComments:
if comment["body"].startswith("Error while checking %s for %s" % (args.status, sha)):
if calculateMessageHash(comment["body"]) != messageHash:
print("Comment was different. Updating", file=sys.stderr)
cgh.patch(
"/repos/{repo_name}/issues/comments/{commentID}",
{"body": message},
repo_name=pr.repo_name,
commentID=comment["id"]
)
sys.exit(0)
print("Found same comment for the same commit", file=sys.stderr)
sys.exit(0)
cgh.post(
"repos/{repo_name}/issues/{pr_id}/comments",
{"body": message},
repo_name=pr.repo_name,
pr_id=pr.id
)
def parse_pr(pr):
repo_name, pr_id, pr_commit = parseGithubRef(pr)
return Namespace(repo_name=repo_name,
id=pr_id,
commit=pr_commit)
def main():
args = parse_args()
pr = parse_pr(args.pr)
logs = Logs(args, is_branch=not pr.id.isdigit())
if not args.message:
logs.parse()
cache = PickledCache('.cached-commits')
with GithubCachedClient(token=github_token(), cache=cache) as cgh:
# If the branch is not a PR, we should look for open issues
# for the branch. This should really folded as a special case
# of the PR case.
func = handle_branch if not pr.id.isdigit() else handle_pr_id
func(cgh, pr, logs, args)
cgh.printStats()
if __name__ == "__main__":
main()