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

Feat/workflow run logs #42

Merged
merged 4 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
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
36 changes: 36 additions & 0 deletions github/WorkflowRun.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ def logs_url(self) -> str:
self._completeIfNotSet(self._logs_url)
return self._logs_url.value

@property
def run_attempt_logs_url(self) -> str:
return self.logs_url[:-4] + f"attempts/{self.run_attempt}/logs"

@property
def check_suite_url(self) -> str:
self._completeIfNotSet(self._check_suite_url)
Expand Down Expand Up @@ -284,6 +288,38 @@ def jobs(self, _filter: Opt[str] = NotSet) -> PaginatedList[WorkflowJob]:
list_item="jobs",
)

def get_logs_download_link_for_this_attempt(self) -> str:
"""
:calls "`GET /repos/{owner}/{repo}/actions/runs/{run_id}/attempts/{attempt}/logs <https://docs.github.com/en/rest/actions/workflow-runs?apiVersion=2022-11-28#download-workflow-run-attempt-logs>`_
"""
return self._get_logs_download_link(self.run_attempt_logs_url)

def get_logs_download_link_for_latest_attempt(self) -> str:
"""
:calls "`GET /repos/{owner}/{repo}/actions/runs/{run_id}/logs <https://docs.github.com/en/rest/actions/workflow-runs?apiVersion=2022-11-28#download-workflow-run-logs>`_
"""
return self._get_logs_download_link(self.logs_url)

def _get_logs_download_link(self, url: str) -> str:
"""
A download link will be returned. Here's one way of extracting its content in-memory:

from io import BytesIO
from zipfile import ZipFile

download_link = workflow_run.get_logs_for_this_run_attempt_download_link()
response = requests.get(download_link, allow_redirects=True)
assert response.headers['content-type'] == 'application/zip'
zip_file = ZipFile(BytesIO(response.content))
return {
PurePosixPath(zip_info.filename): zip_file.read(zip_info.filename).decode()
for zip_info in zip_file.filelist
}
"""
headers, _ = self._requester.requestJsonAndCheck("GET", url)
assert "location" in headers
return headers["location"]

def _useAttributes(self, attributes: dict[str, Any]) -> None:
if "id" in attributes: # pragma no branch
self._id = self._makeIntAttribute(attributes["id"])
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ integrations = []

[tool.setuptools_scm]

[tool.setuptools]
packages = ["github"]

[tool.setuptools.package-data]
github = ["py.typed", '*.pyi']

Expand Down
4 changes: 4 additions & 0 deletions tests/WorkflowRun.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ def testAttributes(self):
self.workflow_run.logs_url,
"https://api.github.com/repos/PyGithub/PyGithub/actions/runs/3881497935/logs",
)
self.assertEqual(
self.workflow_run.run_attempt_logs_url,
"https://api.github.com/repos/PyGithub/PyGithub/actions/runs/3881497935/attempts/1/logs",
)
self.assertEqual(
self.workflow_run.check_suite_url,
"https://api.github.com/repos/PyGithub/PyGithub/check-suites/10279069747",
Expand Down