diff --git a/scripts/delete_job_artifacts.py b/scripts/delete_job_artifacts.py new file mode 100755 index 0000000000000000000000000000000000000000..67804bbc975663c864def89bc9d26a547b2985b8 --- /dev/null +++ b/scripts/delete_job_artifacts.py @@ -0,0 +1,103 @@ +#!/usr/bin/env python3 +""" + +Query job by project and tag. +Delete job artifacts if it is not a git tag (release) + +""" + + +import argparse +import logging +import sys + +import gitlab as gl + +__author__ = "Jonas Höppner" +__email__ = "jonas.hoeppner@garz-fricke.com" + +GITLAB_SERVER = "https://git.seco.com" +# ID of the guf_yocto group +GITLAB_GROUP_ID = "556" + +DISTRO_PROJECT_ID = "1748" +MACHINE_PROJECT_ID = "2074" +MANIFEST_PROJECT_ID = "1725" + +DEFAULTBRANCH = "dunfell" + +GITLAB_TIMEFORMAT = "%Y-%m-%dT%H:%M:%S.%f%z" +TIMEFORMAT = "%Y-%m-%d %H:%M" + +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=GITLAB_SERVER, + ) + parser.add_argument( + "--token", + help="""GitLab REST API private access token""", + dest="token", + required=True, + ) + parser.add_argument( + "--project", + help="""GitLab project where the jobs are queried from""", + dest="project", + default="seco-ne/yocto/manifest", + ) + + parser.add_argument( + "--filter-tag", help="""Only jobs with this gitlab job tag set""", default=None + ) + parser.add_argument( + "--filter-status", help="""Only jobs with this this status""", default=None + ) + + 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) + + project = gitlab.projects.get(options.project) + + job_it = project.jobs.list(iterator=True) + for job in job_it: + # Skip release tags + if job.tag: + continue + if options.filter_status is not None and job.status != options.filter_status: + continue + if options.filter_tag is not None: + if not options.filter_tag in job.tag_list: + continue + job.delete_artifacts() + logging.debug( + "Deleted artifacts for %s: %s %s.", + job.name, + job.status, + str(job.tag_list), + ) + + +if __name__ == "__main__": + main(sys.argv[1:])