Skip to content

Commit 777bff6

Browse files
authored
report: Use taskcluster client library, fixes #386 (#390)
1 parent 768dc93 commit 777bff6

File tree

4 files changed

+69
-73
lines changed

4 files changed

+69
-73
lines changed

.taskcluster.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ tasks:
149149
- sh
150150
- -lxce
151151
- "git clone --quiet ${repository} /src && cd /src && git checkout ${head_rev} -b checks &&
152-
cd /src/report && ./ci-test.sh"
152+
cd /src/report && pip install -r requirements.txt && ./ci-test.sh"
153153
metadata:
154154
name: "Code Coverage Report checks: unit tests"
155155
description: Check python code with unittest
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
11
# -*- coding: utf-8 -*-
2+
3+
from taskcluster.helper import TaskclusterConfig
4+
5+
taskcluster = TaskclusterConfig("https://firefox-ci-tc.services.mozilla.com")
6+
7+
# Force root url to avoid proxy as grcov is not available on Community instance
8+
taskcluster.options = {"rootUrl": taskcluster.default_url}

report/firefox_code_coverage/codecoverage.py

Lines changed: 60 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,9 @@
1212
import time
1313
import warnings
1414

15-
try:
16-
from urllib.parse import urlencode
17-
from urllib.request import Request, urlopen, urlretrieve
18-
except ImportError:
19-
from urllib import urlencode, urlretrieve
20-
from urllib2 import Request, urlopen
15+
import requests
2116

17+
from firefox_code_coverage import taskcluster
2218

2319
FINISHED_STATUSES = ["completed", "failed", "exception"]
2420
ALL_STATUSES = FINISHED_STATUSES + ["unscheduled", "pending", "running"]
@@ -28,91 +24,87 @@
2824
GRCOV_ARTIFACT = "public/build/grcov.tar.xz"
2925

3026

31-
def get_json(url, params=None, headers={}):
32-
if params is not None:
33-
url += "?" + urlencode(params)
34-
35-
request = Request(url, headers=headers)
36-
r = urlopen(request).read().decode("utf-8")
37-
38-
return json.loads(r)
39-
40-
4127
def is_taskcluster_loaner():
4228
return "TASKCLUSTER_INTERACTIVE" in os.environ
4329

4430

4531
def get_task(branch, revision):
46-
task = get_json(
47-
f"https://firefox-ci-tc.services.mozilla.com/api/index/v1/task/gecko.v2.{branch}.revision.{revision}.firefox.decision"
48-
)
32+
index = taskcluster.get_service("index")
33+
task = index.findTask(f"gecko.v2.{branch}.revision.{revision}.firefox.decision")
4934
return task["taskId"]
5035

5136

5237
def get_last_task():
53-
revision = get_json(
38+
resp = requests.get(
5439
"https://api.coverage.moz.tools/v2/latest?repository=mozilla-central"
55-
)[0]["revision"]
40+
)
41+
resp.raise_for_status()
42+
data = resp.json()
43+
revision = data[0]["revision"]
5644
return get_task("mozilla-central", revision)
5745

5846

5947
def get_task_details(task_id):
60-
task_details = get_json(
61-
"https://firefox-ci-tc.services.mozilla.com/api/queue/v1/task/" + task_id
62-
)
63-
return task_details
48+
queue = taskcluster.get_service("queue")
49+
return queue.task(task_id)
6450

6551

6652
def get_task_artifacts(task_id):
67-
artifacts = get_json(
68-
"https://firefox-ci-tc.services.mozilla.com/api/queue/v1/task/"
69-
+ task_id
70-
+ "/artifacts"
71-
)
72-
return artifacts["artifacts"]
53+
queue = taskcluster.get_service("queue")
54+
response = queue.listLatestArtifacts(task_id)
55+
return response["artifacts"]
7356

7457

7558
def get_tasks_in_group(group_id):
76-
reply = get_json(
77-
"https://firefox-ci-tc.services.mozilla.com/api/queue/v1/task-group/"
78-
+ group_id
79-
+ "/list",
80-
{"limit": "200"},
81-
)
82-
tasks = reply["tasks"]
83-
while "continuationToken" in reply:
84-
reply = get_json(
85-
"https://firefox-ci-tc.services.mozilla.com/api/queue/v1/task-group/"
86-
+ group_id
87-
+ "/list",
88-
{"limit": "200", "continuationToken": reply["continuationToken"]},
89-
)
90-
tasks += reply["tasks"]
59+
tasks = []
60+
61+
def _save_tasks(response):
62+
tasks.extend(response["tasks"])
63+
64+
queue = taskcluster.get_service("queue")
65+
queue.listTaskGroup(group_id, paginationHandler=_save_tasks)
66+
9167
return tasks
9268

9369

70+
def download_binary(url, path, retries=5):
71+
"""Download a binary file from an url"""
72+
for i in range(1, retries + 1):
73+
try:
74+
artifact = requests.get(url, stream=True)
75+
artifact.raise_for_status()
76+
77+
with open(path, "wb") as f:
78+
for chunk in artifact.iter_content(chunk_size=8192):
79+
f.write(chunk)
80+
break
81+
except: # noqa: E722
82+
try:
83+
os.remove(path)
84+
except OSError:
85+
pass
86+
87+
if i == retries:
88+
raise Exception(
89+
"Download failed after {} retries - {}".format(retries, url)
90+
)
91+
92+
time.sleep(7 * i)
93+
94+
9495
def download_artifact(task_id, artifact, artifacts_path):
9596
fname = os.path.join(
9697
artifacts_path, task_id + "_" + os.path.basename(artifact["name"])
9798
)
99+
100+
# As recommended by Taskcluster doc, use requests to download
101+
# from the artifact public url instead of relying on the client method
102+
queue = taskcluster.get_service("queue")
103+
url = queue.buildUrl("getLatestArtifact", task_id, artifact["name"])
104+
98105
if not os.path.exists(fname):
99-
while True:
100-
try:
101-
urlretrieve(
102-
"https://firefox-ci-tc.services.mozilla.com/api/queue/v1/task/"
103-
+ task_id
104-
+ "/artifacts/"
105-
+ artifact["name"],
106-
fname,
107-
)
108-
break
109-
except: # noqa: E722
110-
try:
111-
os.remove(fname)
112-
except OSError:
113-
pass
114-
115-
time.sleep(7)
106+
download_binary(url, fname)
107+
116108
return fname
117109

118110

@@ -145,11 +137,8 @@ def get_platform(task_name):
145137

146138

147139
def get_task_status(task_id):
148-
status = get_json(
149-
"https://firefox-ci-tc.services.mozilla.com/api/queue/v1/task/{}/status".format(
150-
task_id
151-
)
152-
)
140+
queue = taskcluster.get_service("queue")
141+
status = queue.status(task_id)
153142
return status["status"]["state"]
154143

155144

@@ -316,10 +305,9 @@ def download_grcov():
316305

317306
dest = tempfile.mkdtemp(suffix="grcov")
318307
archive = os.path.join(dest, "grcov.tar.xz")
319-
url = "https://firefox-ci-tc.services.mozilla.com/api/index/v1/task/{index}/artifacts/{artifact}".format(
320-
index=GRCOV_INDEX, artifact=GRCOV_ARTIFACT
321-
)
322-
urlretrieve(url, archive)
308+
index = taskcluster.get_service("index")
309+
url = index.buildUrl("findArtifactFromTask", GRCOV_INDEX, GRCOV_ARTIFACT)
310+
download_binary(url, archive)
323311

324312
# Extract archive in temp
325313
tar = tarfile.open(archive, "r:xz")

report/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
taskcluster==24.2.0

0 commit comments

Comments
 (0)