From a08553255a3dfd8c6ef78dd43666e395231e2d11 Mon Sep 17 00:00:00 2001
From: Ralf Jung <jung@mpi-sws.org>
Date: Thu, 3 Mar 2016 19:43:32 +0100
Subject: [PATCH] fix gitlab extractor to be more robust

---
 benchmark/gitlab-extract.py | 23 ++++++++++++++++-------
 benchmark/parse_log.py      |  2 +-
 2 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/benchmark/gitlab-extract.py b/benchmark/gitlab-extract.py
index 63a3a176a..93cb1c1c1 100755
--- a/benchmark/gitlab-extract.py
+++ b/benchmark/gitlab-extract.py
@@ -4,7 +4,7 @@ import requests
 import parse_log
 
 def last(it):
-    r = first(it) # errors out if it is empty
+    r = None
     for i in it:
         r = i
     return r
@@ -12,7 +12,7 @@ def last(it):
 def first(it):
     for i in it:
         return i
-    raise Exception("The iterator is empty")
+    return None
 
 def req(path):
     url = '%s/api/v3/%s' % (args.server, path)
@@ -47,20 +47,29 @@ if args.commits is None:
 
 projects = req("projects")
 project = first(filter(lambda p: p['path_with_namespace'] == args.project, projects.json()))
+if project is None:
+    sys.stderr.write("Project not found.\n")
+    sys.exit(1)
 
 for commit in parse_log.parse_git_commits(args.commits):
     print("Fetching {}...".format(commit))
+    commit_data = req("/projects/{}/repository/commits/{}".format(project['id'], commit))
+    if commit_data.status_code != 200:
+        raise Exception("Commit not found?")
     builds = req("/projects/{}/repository/commits/{}/builds".format(project['id'], commit))
     if builds.status_code != 200:
-        continue
-    try:
-        build = first(sorted(builds.json(), key = lambda b: -int(b['id'])))
-    except Exception:
         # no build
         continue
+    build = first(sorted(builds.json(), key = lambda b: -int(b['id'])))
+    assert build is not None
+    if build['status'] == 'failed':
+        # build failed
+        continue
+    # now fetch the build times
     build_times = requests.get("{}/builds/{}/artifacts/file/build-time.txt".format(project['web_url'], build['id']))
     if build_times.status_code != 200:
-        continue
+        raise Exception("No artifact at build?")
     # Output in the log file format
     log_file.write("# {}\n".format(commit))
     log_file.write(build_times.text)
+    log_file.flush()
diff --git a/benchmark/parse_log.py b/benchmark/parse_log.py
index 82da2030a..46a9ec82c 100644
--- a/benchmark/parse_log.py
+++ b/benchmark/parse_log.py
@@ -35,7 +35,7 @@ def parse(file, parse_times = True):
         # nothing else we know about
         raise Exception("Unexpected line: {}".format(line))
     # end of file. previous commit, if any, is done now.
-    if times is not None:
+    if commit is not None:
         yield Result(commit, times)
 
 def parse_git_commits(commits):
-- 
GitLab