Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
download_file_from_latest_job.py 2.57 KiB
#!/usr/bin/env python3
"""

Downloads given file of the artifacts of a pipeline job.

"""

import argparse
import logging
import os
import sys

import gitlab as gl

import common

__author__ = "Jonas Höppner"
__email__ = "jonas.hoeppner@garz-fricke.com"

from download_job_artifacts import download_job_artifact
from get_pipeline_jobs import get_pipeline_jobs

verbose = 0


def main(args):
    parser = argparse.ArgumentParser(description=__doc__, usage="%(prog)s [OPTIONS]")

    parser.add_argument(
        "--gitlab-url",
        help="""URL to the GitLab instance""",
        dest="gitlab_url",
        action="store",
        default=common.GITLAB_URL,
    )
    parser.add_argument(
        "--token",
        help="""GitLab REST API private access token""",
        dest="token",
        required=True,
    )
    parser.add_argument(
        "--project",
        action="store",
        dest="project",
        help="Specify the project by either by id or by path.",
        required=True,
    )
    parser.add_argument(
        "--pipeline",
        action="store",
        dest="pipeline",
        help="Specify the pipeline by id.",
    )
    parser.add_argument(
        "-s",
        "--stage",
        action="store",
        default=None,
        help="Filter the jobs by the given stage, if omnitted all jobs are returned.",
    )
    parser.add_argument(
        "-n",
        "--name",
        action="store",
        default=None,
        help="Filter the jobs by given name, if omnitted all jobs are returned.",
    )
    parser.add_argument(
        "--path",
        action="store",
        default=None,
        help="Path inside the artifacts, if set only one single file is downloaded instead of the complete artifacts.",
    )
    parser.add_argument(
        "-v",
        "--verbose",
        action="count",
        dest="verbose",
        default=0,
        help="Increase verbosity.",
    )

    options = parser.parse_args(args)
    if options.verbose:
        logging.basicConfig(level=logging.DEBUG)

    logging.debug(options)
    gitlab = gl.Gitlab(options.gitlab_url, private_token=options.token)
    jobs = get_pipeline_jobs(
        gitlab, options.project, options.pipeline, options.name, options.stage
    )

    def sort_by_finish_ts(j):
        return j.finished_at

    jobs.sort(key=sort_by_finish_ts)
    job = jobs[0]

    filename = download_job_artifact(
        gitlab, dest=os.path.basename(options.path), path=options.path, job=job
    )
    print("Downloaded {} for job {} to {}".format(options.path, job.name, filename))


if __name__ == "__main__":
    main(sys.argv[1:])