Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fetching latest run in ArtifactDownloader #531

Merged
merged 2 commits into from
Aug 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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