Skip to content

Commit a5882cc

Browse files
authored
fix: token for CircleCI artifacts (#1001)
Although the endpoints to list workflow jobs and artifacts used to be accessible to anyone, now there is a 401 response code. Previously we worked around this by changing the API version back to v1.1, but now it's happening again for both versions. This apparently happens often. https://discuss.circleci.com/t/api-token-needed-to-fetch-artifacts/50881/3 So, it makes sense to just use a token. I've configured this in CircleCI already. (Another PR will be coming soon for the comment responder to fetch the artifacts.)
1 parent 4ca1505 commit a5882cc

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

bioconda_utils/artifacts.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,16 @@ def parse_azure_build_id(url: str) -> str:
196196

197197
def get_circleci_artifacts(check_run, platform):
198198
circleci_workflow_id = json.loads(check_run.external_id)["workflow-id"]
199+
# Must use a Personal token for API v2
200+
token = os.environ.get("CIRCLECI_TOKEN")
201+
if not token:
202+
logger.critical("CIRCLECI_TOKEN required to download CircleCI artifacts list.")
203+
exit(1)
204+
headers = {"Circle-Token": token}
205+
199206
# Use API v2 because v1.1 does not have a workflow endpoint
200207
url_wf = f"https://circleci.com/api/v2/workflow/{circleci_workflow_id}/job"
201-
res_wf = requests.get(url_wf)
208+
res_wf = requests.get(url_wf, headers=headers)
202209
json_wf = json.loads(res_wf.text)
203210

204211
if len(json_wf["items"]) == 0:
@@ -207,15 +214,14 @@ def get_circleci_artifacts(check_run, platform):
207214
for job in json_wf["items"]:
208215
if job["name"].startswith(f"build_and_test-{platform}"):
209216
circleci_job_num = job["job_number"]
210-
# Use API v1.1 because v2 requires authentication
211-
url = f"https://circleci.com/api/v1.1/project/gh/bioconda/bioconda-recipes/{circleci_job_num}/artifacts"
212-
res = requests.get(url)
217+
url = f"https://circleci.com/api/v2/project/gh/bioconda/bioconda-recipes/{circleci_job_num}/artifacts"
218+
res = requests.get(url, headers=headers)
213219
res.raise_for_status()
214220
json_job = json.loads(res.text)
215-
if len(json_job) == 0:
221+
if len(json_job["items"]) == 0:
216222
raise ValueError("No artifacts found!")
217223
else:
218-
for artifact in json_job:
224+
for artifact in json_job["items"]:
219225
artifact_url = artifact["url"]
220226
if artifact_url.endswith((".html", ".json", ".json.bz2", ".json.zst")):
221227
continue

0 commit comments

Comments
 (0)