github_fetch_artifacts.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (c) 2020 Project CHIP Authors
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. import logging
  18. import github
  19. import requests
  20. # Artifacts can be fetched using:
  21. # curl -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/project-chip/connectedhomeip/actions/artifacts
  22. class ArtifactInfo(github.GithubObject.NonCompletableGithubObject):
  23. def _initAttributes(self):
  24. pass
  25. def _useAttributes(self, attr):
  26. if 'id' in attr:
  27. self.id = self._makeIntAttribute(attr['id']).value
  28. if 'node_id' in attr:
  29. self.node_id = self._makeStringAttribute(attr['node_id']).value
  30. if 'name' in attr:
  31. self.name = self._makeStringAttribute(attr['name']).value
  32. if 'size_in_bytes' in attr:
  33. self.size_in_bytes = self._makeIntAttribute(
  34. attr['size_in_bytes']).value
  35. if 'url' in attr:
  36. self.url = self._makeStringAttribute(attr['url']).value
  37. if 'archive_download_url' in attr:
  38. self.archive_download_url = self._makeStringAttribute(
  39. attr['archive_download_url']).value
  40. if 'expired' in attr:
  41. self.expired = self._makeBoolAttribute(attr['expired']).value
  42. if 'created_at' in attr:
  43. self.created_at = self._makeDatetimeAttribute(
  44. attr['created_at']).value
  45. if 'updated_at' in attr:
  46. self.expires_at = self._makeDatetimeAttribute(
  47. attr['updated_at']).value
  48. def downloadBlob(self):
  49. url = self.archive_download_url
  50. logging.info('Fetching: %r' % url)
  51. status, headers, _ = self._requester.requestBlob('GET', url)
  52. if status != 302:
  53. raise Exception(
  54. 'Expected a redirect during blob download but got status %d, headers %r.' % (status, headers))
  55. response = requests.get(headers['location'])
  56. response.raise_for_status()
  57. return response.content
  58. def delete(self):
  59. """Delete this artifact."""
  60. logging.warning('DELETING artifact ' + self.url)
  61. self._requester.requestJsonAndCheck('DELETE', self.url)
  62. class ArtifactFetcher(github.GithubObject.NonCompletableGithubObject):
  63. def __init__(self, repo):
  64. self.url = repo.url + '/actions/artifacts'
  65. self._requester = repo._requester
  66. def get_artifacts(self):
  67. return github.PaginatedList.PaginatedList(
  68. ArtifactInfo,
  69. self._requester,
  70. self.url,
  71. None,
  72. headers={'Accept': 'application/vnd.github.v3+json'},
  73. list_item='artifacts',
  74. )
  75. def getAllArtifacts(githubToken, githubRepo):
  76. """Get all artifacts visible in the given repo."""
  77. api = github.Github(githubToken)
  78. repo = api.get_repo(githubRepo)
  79. return ArtifactFetcher(repo).get_artifacts()