Skip to content

Commit

Permalink
Fix fetching latest run in ArtifactDownloader (#531)
Browse files Browse the repository at this point in the history
* Fix fetching latest run in ArtifactDownloader

* Skip flake8 on assigned lambdas
  • Loading branch information
kmazurek authored Aug 11, 2021
1 parent 4868dd2 commit 2d92316
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions goth/runner/download/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,30 +98,35 @@ def _get_workflow(self, workflow_name: str) -> dict:

def _get_latest_run(
self, workflow: dict, branch: str, commit: Optional[str] = None
) -> dict:
"""Filter out the latest workflow run."""
) -> Optional[dict]:
"""Filter out the latest successful workflow run."""
workflow_id = workflow["id"]
logger.debug("Fetching workflow runs. workflow_id=%s", workflow_id)

if commit:
paged_workflow_runs = paged(
self.gh_api.actions.list_workflow_runs, workflow_id, status="completed"
branch_query = lambda run: run["head_branch"] == branch # noqa: E731
commit_query = lambda run: run["head_sha"].startswith(commit) # noqa: E731

paged_workflow_runs = paged(
self.gh_api.actions.list_workflow_runs,
workflow_id,
conclusion="success",
)

for page in paged_workflow_runs:
latest_run = next(
filter(commit_query if commit else branch_query, page.workflow_runs),
None,
)

for page in paged_workflow_runs:
workflow_runs = next(
filter(
lambda r: r["head_sha"].startswith(commit), page.workflow_runs
),
None,
if latest_run:
logger.debug("latest_run=%s", json.dumps(obj2dict(latest_run)))
return latest_run
else:
logger.debug(
"page.workflow_runs=%s", json.dumps(obj2dict(page.workflow_runs))
)

if workflow_runs:
return workflow_runs

return self.gh_api.actions.list_workflow_runs(
workflow_id, branch=branch, status="completed"
).workflow_runs[0]
return None

def _get_artifact(self, artifact_name: str, workflow_run: dict) -> Optional[dict]:
artifacts_url = workflow_run["artifacts_url"]
Expand Down Expand Up @@ -186,6 +191,10 @@ def download(
)
workflow = self._get_workflow(workflow_name)
latest_run = self._get_latest_run(workflow, branch, commit)
if not latest_run:
raise RuntimeError(
f"Failed to find latest workflow run. workflow_name={workflow_name}"
)
artifact = self._get_artifact(artifact_name, latest_run)
if not artifact:
raise AssetNotFound(f"Artifact not found. name={artifact_name}")
Expand Down

0 comments on commit 2d92316

Please sign in to comment.