Skip to content

Commit 82d2e97

Browse files
committed
fix: regression to artifact handling in util/taskcluster.py
1 parent d75dacf commit 82d2e97

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/taskgraph/util/taskcluster.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
import copy
77
import datetime
88
import functools
9+
import io
910
import logging
1011
import os
11-
from typing import Dict, List, Union
12+
from typing import Any, Dict, List, Union
1213

1314
import requests
1415
import taskcluster_urls as liburls
@@ -69,13 +70,32 @@ def get_taskcluster_client(service: str):
6970
return getattr(taskcluster, service[0].upper() + service[1:])(options)
7071

7172

72-
def _handle_artifact(path, response):
73+
def _handle_artifact(
74+
path: str, response: Union[requests.Response, Dict[str, Any]]
75+
) -> Any:
76+
if isinstance(response, dict):
77+
# When taskcluster client returns non-JSON responses, it wraps them in {"response": <Response>}
78+
if "response" in response and isinstance(
79+
response["response"], requests.Response
80+
):
81+
response = response["response"]
82+
else:
83+
# If we already a dict (parsed JSON), return it directly.
84+
return response
85+
86+
# We have a response object, load the content based on the path extension.
7387
if path.endswith(".json"):
7488
return response.json()
7589

7690
if path.endswith(".yml"):
7791
return yaml.load_stream(response.content)
7892

93+
# Otherwise return raw response content
94+
if hasattr(response.raw, "tell") and response.raw.tell() > 0:
95+
# Stream was already read, create a new one from content. This can
96+
# happen when mocking with responses.
97+
return io.BytesIO(response.content)
98+
7999
response.raw.read = functools.partial(response.raw.read, decode_content=True)
80100
return response.raw
81101

test/test_util_taskcluster.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ def test_get_artifact_url(monkeypatch):
8484
assert tc.get_artifact_url(task_id, path) == expected_proxy
8585

8686

87-
@pytest.mark.xfail
8887
def test_get_artifact(responses, root_url):
8988
tid = "abc123"
9089
tc.get_taskcluster_client.cache_clear()
@@ -93,7 +92,6 @@ def test_get_artifact(responses, root_url):
9392
responses.get(
9493
f"{root_url}/api/queue/v1/task/{tid}/artifacts/artifact.txt",
9594
body=b"foobar",
96-
stream=True,
9795
)
9896
raw = tc.get_artifact(tid, "artifact.txt")
9997
assert raw.read() == b"foobar"
@@ -194,7 +192,6 @@ def test_find_task_id_batched(responses, root_url):
194192
assert result == {"index.abc": "abc", "index.def": "def"}
195193

196194

197-
@pytest.mark.xfail
198195
def test_get_artifact_from_index(responses, root_url):
199196
index = "foo"
200197
path = "file.txt"
@@ -203,7 +200,6 @@ def test_get_artifact_from_index(responses, root_url):
203200
responses.get(
204201
f"{root_url}/api/index/v1/task/{index}/artifacts/{path}",
205202
body=b"foobar",
206-
stream=True,
207203
)
208204

209205
result = tc.get_artifact_from_index(index, path)

0 commit comments

Comments
 (0)