Skip to content

Commit

Permalink
Add listing of artifacts in the repository (PyGithub#2313)
Browse files Browse the repository at this point in the history
Implement calls for listing Github Actions artifacts linked to a
repository.

The test runs on an external repository because PyGithub doesn't have
any artifacts in its CI.
  • Loading branch information
Aleksei Fedotov committed Oct 13, 2022
1 parent 6d96972 commit 8d3f6e6
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 32 deletions.
28 changes: 28 additions & 0 deletions github/Repository.py
Expand Up @@ -65,6 +65,7 @@
# Copyright 2018 Yves Zumbach <yzumbach@andrew.cmu.edu> #
# Copyright 2018 Leying Chen <leyingc@andrew.cmu.edu> #
# Copyright 2020 Pascal Hofmann <mail@pascalhofmann.de> #
# Copyright 2022 Aleksei Fedotov <aleksei@fedotov.email> #
# #
# This file is part of PyGithub. #
# http://pygithub.readthedocs.io/ #
Expand All @@ -91,6 +92,7 @@

from deprecated import deprecated

import github.Artifact
import github.Branch
import github.CheckRun
import github.CheckSuite
Expand Down Expand Up @@ -3703,6 +3705,32 @@ def get_check_run(self, check_run_id):
)
return github.CheckRun.CheckRun(self._requester, headers, data, completed=True)

def get_artifacts(self):
"""
:calls: `GET /repos/{owner}/{repo}/actions/artifacts <https://docs.github.com/en/rest/actions/artifacts#list-artifacts-for-a-repository>`_
:rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Artifact.Artifact`
"""

return github.PaginatedList.PaginatedList(
github.Artifact.Artifact,
self._requester,
f"{self.url}/actions/artifacts",
None,
list_item="artifacts",
)

def get_artifact(self, artifact_id: int):
"""
:calls: `GET /repos/{owner}/{repo}/actions/artifacts/{artifact_id} <https://docs.github.com/en/rest/actions/artifacts#get-an-artifact>`_
:rtype: :class:`github.Artifact.Artifact`
"""
assert isinstance(artifact_id, int), artifact_id
headers, data = self._requester.requestJsonAndCheck(
"GET", f"{self.url}/actions/artifacts/{artifact_id}"
)

return github.Artifact.Artifact(self._requester, headers, data, completed=True)

def _initAttributes(self):
self._allow_merge_commit = github.GithubObject.NotSet
self._allow_rebase_merge = github.GithubObject.NotSet
Expand Down
3 changes: 3 additions & 0 deletions github/Repository.pyi
@@ -1,6 +1,7 @@
from datetime import date, datetime
from typing import Any, Dict, List, Optional, Union, overload

from github.Artifact import Artifact
from github.AuthenticatedUser import AuthenticatedUser
from github.Branch import Branch
from github.CheckRun import CheckRun
Expand Down Expand Up @@ -305,6 +306,8 @@ class Repository(CompletableGithubObject):
def get_archive_link(
self, archive_format: str, ref: Union[str, _NotSetType] = ...
) -> str: ...
def get_artifact(self) -> Artifact: ...
def get_artifacts(self) -> PaginatedList[Artifact]: ...
def get_assignees(self) -> PaginatedList[NamedUser]: ...
def get_branch(self, branch: str) -> Branch: ...
def rename_branch(self, branch: Union[str, Branch], new_name: str) -> bool: ...
Expand Down
37 changes: 29 additions & 8 deletions tests/Artifact.py
Expand Up @@ -6,16 +6,37 @@
class Artifact(Framework.TestCase):
def setUp(self):
super().setUp()
self.artifact = (
self.g.get_repo("github/vscode-codeql")
.get_workflow_run(160995070)
.get_artifacts()[0]
self.repo = self.g.get_repo("github/vscode-codeql")

def testGetArtifactsFromWorkflow(self):
artifact = self.repo.get_workflow_run(160995070).get_artifacts()[0]

self.assertEqual(artifact.name, "vscode-codeql-extension")
self.assertTrue(artifact.expired)
self.assertEqual(
repr(artifact), 'Artifact(name="vscode-codeql-extension", id=10495898)'
)

def testGetSingleArtifactFromRepo(self):
artifact = self.repo.get_artifact(378970214)

self.assertEqual(artifact.name, "vscode-codeql-extension")
self.assertFalse(artifact.expired)
self.assertEqual(
repr(artifact), 'Artifact(name="vscode-codeql-extension", id=378970214)'
)

def testAttributes(self):
self.assertEqual(self.artifact.name, "vscode-codeql-extension")
def testGetArtifactsFromRepo(self):
artifact_id = 378970214
artifacts = self.repo.get_artifacts()
for item in artifacts:
if item.id == artifact_id:
artifact = item
break
else:
assert False, f"No artifact {artifact_id} is found"

self.assertTrue(self.artifact.expired)
self.assertEqual(
repr(self.artifact), 'Artifact(name="vscode-codeql-extension", id=10495898)'
repr(artifact),
f'Artifact(name="vscode-codeql-extension", id={artifact_id})',
)
26 changes: 2 additions & 24 deletions tests/ReplayData/Artifact.setUp.txt

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions tests/ReplayData/Artifact.testGetArtifactsFromRepo.txt

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions tests/ReplayData/Artifact.testGetArtifactsFromWorkflow.txt
@@ -0,0 +1,22 @@
https
GET
api.github.com
None
/repos/github/vscode-codeql/actions/runs/160995070
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
None
200
[('Server', 'GitHub.com'), ('Date', 'Wed, 28 Sep 2022 11:31:33 GMT'), ('Content-Type', 'application/json; charset=utf-8'), ('Transfer-Encoding', 'chunked'), ('Cache-Control', 'public, max-age=60, s-maxage=60'), ('Vary', 'Accept, Accept-Encoding, Accept, X-Requested-With'), ('ETag', 'W/"4e62f3e45aa64c41f5d8dda4bf9c8e155c592b4f9d70cedf888671f278775a36"'), ('X-GitHub-Media-Type', 'github.v3; format=json'), ('X-RateLimit-Limit', '60'), ('X-RateLimit-Remaining', '47'), ('X-RateLimit-Reset', '1664368108'), ('X-RateLimit-Used', '13'), ('X-RateLimit-Resource', 'core'), ('Access-Control-Expose-Headers', 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset'), ('Access-Control-Allow-Origin', '*'), ('Strict-Transport-Security', 'max-age=31536000; includeSubdomains; preload'), ('X-Frame-Options', 'deny'), ('X-Content-Type-Options', 'nosniff'), ('X-XSS-Protection', '0'), ('Referrer-Policy', 'origin-when-cross-origin, strict-origin-when-cross-origin'), ('Content-Security-Policy', "default-src 'none'"), ('Content-Encoding', 'gzip'), ('X-GitHub-Request-Id', 'AB22:78D3:20371B2:20B2648:63343095')]
{"id":160995070,"name":"Release","node_id":"MDExOldvcmtmbG93UnVuMTYwOTk1MDcw","head_branch":"v1.3.1","head_sha":"c4353981fa5a3565d075d187276eebd92b2a20fc","path":".github/workflows/release.yml","display_title":"Release","run_number":47,"event":"push","status":"completed","conclusion":"success","workflow_id":247289,"check_suite_id":887193234,"check_suite_node_id":"MDEwOkNoZWNrU3VpdGU4ODcxOTMyMzQ=","url":"https://api.github.com/repos/github/vscode-codeql/actions/runs/160995070","html_url":"https://github.com/github/vscode-codeql/actions/runs/160995070","pull_requests":[],"created_at":"2020-07-07T18:34:48Z","updated_at":"2020-07-07T18:36:58Z","actor":{"login":"jcreedcmu","id":1500822,"node_id":"MDQ6VXNlcjE1MDA4MjI=","avatar_url":"https://avatars.githubusercontent.com/u/1500822?v=4","gravatar_id":"","url":"https://api.github.com/users/jcreedcmu","html_url":"https://github.com/jcreedcmu","followers_url":"https://api.github.com/users/jcreedcmu/followers","following_url":"https://api.github.com/users/jcreedcmu/following{/other_user}","gists_url":"https://api.github.com/users/jcreedcmu/gists{/gist_id}","starred_url":"https://api.github.com/users/jcreedcmu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jcreedcmu/subscriptions","organizations_url":"https://api.github.com/users/jcreedcmu/orgs","repos_url":"https://api.github.com/users/jcreedcmu/repos","events_url":"https://api.github.com/users/jcreedcmu/events{/privacy}","received_events_url":"https://api.github.com/users/jcreedcmu/received_events","type":"User","site_admin":false},"run_attempt":1,"referenced_workflows":[],"run_started_at":"2020-07-07T18:34:48Z","jobs_url":"https://api.github.com/repos/github/vscode-codeql/actions/runs/160995070/jobs","logs_url":"https://api.github.com/repos/github/vscode-codeql/actions/runs/160995070/logs","check_suite_url":"https://api.github.com/repos/github/vscode-codeql/check-suites/887193234","artifacts_url":"https://api.github.com/repos/github/vscode-codeql/actions/runs/160995070/artifacts","cancel_url":"https://api.github.com/repos/github/vscode-codeql/actions/runs/160995070/cancel","rerun_url":"https://api.github.com/repos/github/vscode-codeql/actions/runs/160995070/rerun","previous_attempt_url":null,"workflow_url":"https://api.github.com/repos/github/vscode-codeql/actions/workflows/247289","head_commit":{"id":"c4353981fa5a3565d075d187276eebd92b2a20fc","tree_id":"b5561db1b83aa7f5bd0e5126b375caaf79cc16fb","message":"update CHANGELOG for release","timestamp":"2020-07-07T18:28:53Z","author":{"name":"Jason Reed","email":"jcreedcmu@github.com"},"committer":{"name":"Jason Reed","email":"jcreedcmu@github.com"}},"repository":{"id":211169016,"node_id":"MDEwOlJlcG9zaXRvcnkyMTExNjkwMTY=","name":"vscode-codeql","full_name":"github/vscode-codeql","private":false,"owner":{"login":"github","id":9919,"node_id":"MDEyOk9yZ2FuaXphdGlvbjk5MTk=","avatar_url":"https://avatars.githubusercontent.com/u/9919?v=4","gravatar_id":"","url":"https://api.github.com/users/github","html_url":"https://github.com/github","followers_url":"https://api.github.com/users/github/followers","following_url":"https://api.github.com/users/github/following{/other_user}","gists_url":"https://api.github.com/users/github/gists{/gist_id}","starred_url":"https://api.github.com/users/github/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/github/subscriptions","organizations_url":"https://api.github.com/users/github/orgs","repos_url":"https://api.github.com/users/github/repos","events_url":"https://api.github.com/users/github/events{/privacy}","received_events_url":"https://api.github.com/users/github/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/github/vscode-codeql","description":"An extension for Visual Studio Code that adds rich language support for CodeQL","fork":false,"url":"https://api.github.com/repos/github/vscode-codeql","forks_url":"https://api.github.com/repos/github/vscode-codeql/forks","keys_url":"https://api.github.com/repos/github/vscode-codeql/keys{/key_id}","collaborators_url":"https://api.github.com/repos/github/vscode-codeql/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/github/vscode-codeql/teams","hooks_url":"https://api.github.com/repos/github/vscode-codeql/hooks","issue_events_url":"https://api.github.com/repos/github/vscode-codeql/issues/events{/number}","events_url":"https://api.github.com/repos/github/vscode-codeql/events","assignees_url":"https://api.github.com/repos/github/vscode-codeql/assignees{/user}","branches_url":"https://api.github.com/repos/github/vscode-codeql/branches{/branch}","tags_url":"https://api.github.com/repos/github/vscode-codeql/tags","blobs_url":"https://api.github.com/repos/github/vscode-codeql/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/github/vscode-codeql/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/github/vscode-codeql/git/refs{/sha}","trees_url":"https://api.github.com/repos/github/vscode-codeql/git/trees{/sha}","statuses_url":"https://api.github.com/repos/github/vscode-codeql/statuses/{sha}","languages_url":"https://api.github.com/repos/github/vscode-codeql/languages","stargazers_url":"https://api.github.com/repos/github/vscode-codeql/stargazers","contributors_url":"https://api.github.com/repos/github/vscode-codeql/contributors","subscribers_url":"https://api.github.com/repos/github/vscode-codeql/subscribers","subscription_url":"https://api.github.com/repos/github/vscode-codeql/subscription","commits_url":"https://api.github.com/repos/github/vscode-codeql/commits{/sha}","git_commits_url":"https://api.github.com/repos/github/vscode-codeql/git/commits{/sha}","comments_url":"https://api.github.com/repos/github/vscode-codeql/comments{/number}","issue_comment_url":"https://api.github.com/repos/github/vscode-codeql/issues/comments{/number}","contents_url":"https://api.github.com/repos/github/vscode-codeql/contents/{+path}","compare_url":"https://api.github.com/repos/github/vscode-codeql/compare/{base}...{head}","merges_url":"https://api.github.com/repos/github/vscode-codeql/merges","archive_url":"https://api.github.com/repos/github/vscode-codeql/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/github/vscode-codeql/downloads","issues_url":"https://api.github.com/repos/github/vscode-codeql/issues{/number}","pulls_url":"https://api.github.com/repos/github/vscode-codeql/pulls{/number}","milestones_url":"https://api.github.com/repos/github/vscode-codeql/milestones{/number}","notifications_url":"https://api.github.com/repos/github/vscode-codeql/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/github/vscode-codeql/labels{/name}","releases_url":"https://api.github.com/repos/github/vscode-codeql/releases{/id}","deployments_url":"https://api.github.com/repos/github/vscode-codeql/deployments"},"head_repository":{"id":211169016,"node_id":"MDEwOlJlcG9zaXRvcnkyMTExNjkwMTY=","name":"vscode-codeql","full_name":"github/vscode-codeql","private":false,"owner":{"login":"github","id":9919,"node_id":"MDEyOk9yZ2FuaXphdGlvbjk5MTk=","avatar_url":"https://avatars.githubusercontent.com/u/9919?v=4","gravatar_id":"","url":"https://api.github.com/users/github","html_url":"https://github.com/github","followers_url":"https://api.github.com/users/github/followers","following_url":"https://api.github.com/users/github/following{/other_user}","gists_url":"https://api.github.com/users/github/gists{/gist_id}","starred_url":"https://api.github.com/users/github/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/github/subscriptions","organizations_url":"https://api.github.com/users/github/orgs","repos_url":"https://api.github.com/users/github/repos","events_url":"https://api.github.com/users/github/events{/privacy}","received_events_url":"https://api.github.com/users/github/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/github/vscode-codeql","description":"An extension for Visual Studio Code that adds rich language support for CodeQL","fork":false,"url":"https://api.github.com/repos/github/vscode-codeql","forks_url":"https://api.github.com/repos/github/vscode-codeql/forks","keys_url":"https://api.github.com/repos/github/vscode-codeql/keys{/key_id}","collaborators_url":"https://api.github.com/repos/github/vscode-codeql/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/github/vscode-codeql/teams","hooks_url":"https://api.github.com/repos/github/vscode-codeql/hooks","issue_events_url":"https://api.github.com/repos/github/vscode-codeql/issues/events{/number}","events_url":"https://api.github.com/repos/github/vscode-codeql/events","assignees_url":"https://api.github.com/repos/github/vscode-codeql/assignees{/user}","branches_url":"https://api.github.com/repos/github/vscode-codeql/branches{/branch}","tags_url":"https://api.github.com/repos/github/vscode-codeql/tags","blobs_url":"https://api.github.com/repos/github/vscode-codeql/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/github/vscode-codeql/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/github/vscode-codeql/git/refs{/sha}","trees_url":"https://api.github.com/repos/github/vscode-codeql/git/trees{/sha}","statuses_url":"https://api.github.com/repos/github/vscode-codeql/statuses/{sha}","languages_url":"https://api.github.com/repos/github/vscode-codeql/languages","stargazers_url":"https://api.github.com/repos/github/vscode-codeql/stargazers","contributors_url":"https://api.github.com/repos/github/vscode-codeql/contributors","subscribers_url":"https://api.github.com/repos/github/vscode-codeql/subscribers","subscription_url":"https://api.github.com/repos/github/vscode-codeql/subscription","commits_url":"https://api.github.com/repos/github/vscode-codeql/commits{/sha}","git_commits_url":"https://api.github.com/repos/github/vscode-codeql/git/commits{/sha}","comments_url":"https://api.github.com/repos/github/vscode-codeql/comments{/number}","issue_comment_url":"https://api.github.com/repos/github/vscode-codeql/issues/comments{/number}","contents_url":"https://api.github.com/repos/github/vscode-codeql/contents/{+path}","compare_url":"https://api.github.com/repos/github/vscode-codeql/compare/{base}...{head}","merges_url":"https://api.github.com/repos/github/vscode-codeql/merges","archive_url":"https://api.github.com/repos/github/vscode-codeql/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/github/vscode-codeql/downloads","issues_url":"https://api.github.com/repos/github/vscode-codeql/issues{/number}","pulls_url":"https://api.github.com/repos/github/vscode-codeql/pulls{/number}","milestones_url":"https://api.github.com/repos/github/vscode-codeql/milestones{/number}","notifications_url":"https://api.github.com/repos/github/vscode-codeql/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/github/vscode-codeql/labels{/name}","releases_url":"https://api.github.com/repos/github/vscode-codeql/releases{/id}","deployments_url":"https://api.github.com/repos/github/vscode-codeql/deployments"}}

https
GET
api.github.com
None
/repos/github/vscode-codeql/actions/runs/160995070/artifacts
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
None
200
[('Server', 'GitHub.com'), ('Date', 'Wed, 28 Sep 2022 11:31:33 GMT'), ('Content-Type', 'application/json; charset=utf-8'), ('Transfer-Encoding', 'chunked'), ('Cache-Control', 'public, max-age=60, s-maxage=60'), ('Vary', 'Accept, Accept-Encoding, Accept, X-Requested-With'), ('ETag', 'W/"384dd95df518dd09f3403cdb0efea6f07b615808530fa59263a9f3f6a39e0436"'), ('X-GitHub-Media-Type', 'github.v3; format=json'), ('X-RateLimit-Limit', '60'), ('X-RateLimit-Remaining', '46'), ('X-RateLimit-Reset', '1664368108'), ('X-RateLimit-Used', '14'), ('X-RateLimit-Resource', 'core'), ('Access-Control-Expose-Headers', 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset'), ('Access-Control-Allow-Origin', '*'), ('Strict-Transport-Security', 'max-age=31536000; includeSubdomains; preload'), ('X-Frame-Options', 'deny'), ('X-Content-Type-Options', 'nosniff'), ('X-XSS-Protection', '0'), ('Referrer-Policy', 'origin-when-cross-origin, strict-origin-when-cross-origin'), ('Content-Security-Policy', "default-src 'none'"), ('Content-Encoding', 'gzip'), ('X-GitHub-Request-Id', 'AB2A:11933:1E407FD:1EBB285:63343095')]
{"total_count":1,"artifacts":[{"id":10495898,"node_id":"MDg6QXJ0aWZhY3QxMDQ5NTg5OA==","name":"vscode-codeql-extension","size_in_bytes":5555905,"url":"https://api.github.com/repos/github/vscode-codeql/actions/artifacts/10495898","archive_download_url":"https://api.github.com/repos/github/vscode-codeql/actions/artifacts/10495898/zip","expired":true,"created_at":"2020-07-07T18:36:58Z","updated_at":"2020-07-07T18:36:58Z","expires_at":"2020-10-05T18:36:58Z","workflow_run":{"id":160995070,"repository_id":211169016,"head_repository_id":211169016,"head_branch":"v1.3.1","head_sha":"c4353981fa5a3565d075d187276eebd92b2a20fc"}}]}

0 comments on commit 8d3f6e6

Please sign in to comment.