gitlab_api.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import os
  2. import re
  3. import argparse
  4. import tempfile
  5. import tarfile
  6. import zipfile
  7. from functools import wraps
  8. import gitlab
  9. class Gitlab(object):
  10. JOB_NAME_PATTERN = re.compile(r"(\w+)(\s+(\d+)/(\d+))?")
  11. DOWNLOAD_ERROR_MAX_RETRIES = 3
  12. def __init__(self, project_id=None):
  13. config_data_from_env = os.getenv("PYTHON_GITLAB_CONFIG")
  14. if config_data_from_env:
  15. # prefer to load config from env variable
  16. with tempfile.NamedTemporaryFile("w", delete=False) as temp_file:
  17. temp_file.write(config_data_from_env)
  18. config_files = [temp_file.name]
  19. else:
  20. # otherwise try to use config file at local filesystem
  21. config_files = None
  22. self.gitlab_inst = gitlab.Gitlab.from_config(config_files=config_files)
  23. self.gitlab_inst.auth()
  24. if project_id:
  25. self.project = self.gitlab_inst.projects.get(project_id)
  26. else:
  27. self.project = None
  28. def get_project_id(self, name, namespace=None):
  29. """
  30. search project ID by name
  31. :param name: project name
  32. :param namespace: namespace to match when we have multiple project with same name
  33. :return: project ID
  34. """
  35. projects = self.gitlab_inst.projects.list(search=name)
  36. res = []
  37. for project in projects:
  38. if namespace is None:
  39. if len(projects) == 1:
  40. res.append(project.id)
  41. break
  42. if project.namespace["path"] == namespace:
  43. if project.name == name:
  44. res.insert(0, project.id)
  45. else:
  46. res.append(project.id)
  47. if not res:
  48. raise ValueError("Can't find project")
  49. return res[0]
  50. def download_artifacts(self, job_id, destination):
  51. """
  52. download full job artifacts and extract to destination.
  53. :param job_id: Gitlab CI job ID
  54. :param destination: extract artifacts to path.
  55. """
  56. job = self.project.jobs.get(job_id)
  57. with tempfile.NamedTemporaryFile(delete=False) as temp_file:
  58. job.artifacts(streamed=True, action=temp_file.write)
  59. with zipfile.ZipFile(temp_file.name, "r") as archive_file:
  60. archive_file.extractall(destination)
  61. def retry_download(func):
  62. """
  63. This wrapper will only catch IOError and retry the whole function.
  64. So only use it with download functions, read() inside and atomic
  65. functions
  66. """
  67. @wraps(func)
  68. def wrapper(self, *args, **kwargs):
  69. retried = 0
  70. while True:
  71. try:
  72. res = func(self, *args, **kwargs)
  73. except (IOError, EOFError) as e:
  74. retried += 1
  75. if retried > self.DOWNLOAD_ERROR_MAX_RETRIES:
  76. raise e # get out of the loop
  77. else:
  78. print('Retried for the {} time'.format(retried))
  79. continue
  80. else:
  81. break
  82. return res
  83. return wrapper
  84. def download_artifact(self, job_id, artifact_path, destination=None):
  85. """
  86. download specific path of job artifacts and extract to destination.
  87. :param job_id: Gitlab CI job ID
  88. :param artifact_path: list of path in artifacts (relative path to artifact root path)
  89. :param destination: destination of artifact. Do not save to file if destination is None
  90. :return: A list of artifact file raw data.
  91. """
  92. job = self.project.jobs.get(job_id)
  93. raw_data_list = []
  94. for a_path in artifact_path:
  95. try:
  96. data = job.artifact(a_path)
  97. except gitlab.GitlabGetError as e:
  98. print("Failed to download '{}' form job {}".format(a_path, job_id))
  99. raise e
  100. raw_data_list.append(data)
  101. if destination:
  102. file_path = os.path.join(destination, a_path)
  103. try:
  104. os.makedirs(os.path.dirname(file_path))
  105. except OSError:
  106. # already exists
  107. pass
  108. with open(file_path, "wb") as f:
  109. f.write(data)
  110. return raw_data_list
  111. def find_job_id(self, job_name, pipeline_id=None, job_status="success"):
  112. """
  113. Get Job ID from job name of specific pipeline
  114. :param job_name: job name
  115. :param pipeline_id: If None, will get pipeline id from CI pre-defined variable.
  116. :param job_status: status of job. One pipeline could have multiple jobs with same name after retry.
  117. job_status is used to filter these jobs.
  118. :return: a list of job IDs (parallel job will generate multiple jobs)
  119. """
  120. job_id_list = []
  121. if pipeline_id is None:
  122. pipeline_id = os.getenv("CI_PIPELINE_ID")
  123. pipeline = self.project.pipelines.get(pipeline_id)
  124. jobs = pipeline.jobs.list(all=True)
  125. for job in jobs:
  126. match = self.JOB_NAME_PATTERN.match(job.name)
  127. if match:
  128. if match.group(1) == job_name and job.status == job_status:
  129. job_id_list.append({"id": job.id, "parallel_num": match.group(3)})
  130. return job_id_list
  131. @retry_download
  132. def download_archive(self, ref, destination, project_id=None):
  133. """
  134. Download archive of certain commit of a repository and extract to destination path
  135. :param ref: commit or branch name
  136. :param destination: destination path of extracted archive file
  137. :param project_id: download project of current instance if project_id is None
  138. :return: root path name of archive file
  139. """
  140. if project_id is None:
  141. project = self.project
  142. else:
  143. project = self.gitlab_inst.projects.get(project_id)
  144. with tempfile.NamedTemporaryFile(delete=False) as temp_file:
  145. try:
  146. project.repository_archive(sha=ref, streamed=True, action=temp_file.write)
  147. except gitlab.GitlabGetError as e:
  148. print("Failed to archive from project {}".format(project_id))
  149. raise e
  150. print("archive size: {:.03f}MB".format(float(os.path.getsize(temp_file.name)) / (1024 * 1024)))
  151. with tarfile.open(temp_file.name, "r") as archive_file:
  152. root_name = archive_file.getnames()[0]
  153. archive_file.extractall(destination)
  154. return os.path.join(os.path.realpath(destination), root_name)
  155. if __name__ == '__main__':
  156. parser = argparse.ArgumentParser()
  157. parser.add_argument("action")
  158. parser.add_argument("project_id", type=int)
  159. parser.add_argument("--pipeline_id", "-i", type=int, default=None)
  160. parser.add_argument("--ref", "-r", default="master")
  161. parser.add_argument("--job_id", "-j", type=int, default=None)
  162. parser.add_argument("--job_name", "-n", default=None)
  163. parser.add_argument("--project_name", "-m", default=None)
  164. parser.add_argument("--destination", "-d", default=None)
  165. parser.add_argument("--artifact_path", "-a", nargs="*", default=None)
  166. args = parser.parse_args()
  167. gitlab_inst = Gitlab(args.project_id)
  168. if args.action == "download_artifacts":
  169. gitlab_inst.download_artifacts(args.job_id, args.destination)
  170. if args.action == "download_artifact":
  171. gitlab_inst.download_artifact(args.job_id, args.artifact_path, args.destination)
  172. elif args.action == "find_job_id":
  173. job_ids = gitlab_inst.find_job_id(args.job_name, args.pipeline_id)
  174. print(";".join([",".join([str(j["id"]), j["parallel_num"]]) for j in job_ids]))
  175. elif args.action == "download_archive":
  176. gitlab_inst.download_archive(args.ref, args.destination)
  177. elif args.action == "get_project_id":
  178. ret = gitlab_inst.get_project_id(args.project_name)
  179. print("project id: {}".format(ret))