Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions gitlab_artifact_cleanup/artifact_cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ def __init__(self, gitlab_url: str, access_token: str, dry_run: bool = False):
"""
self._gitlab = _Gitlab(gitlab_url, private_token=access_token)
self._dry_run = dry_run
self.artifacts_size_total = 0
self.cleaned_job_count_total = 0
self.repository_count = 0

def delete_old_artifacts(
self,
Expand All @@ -55,9 +58,6 @@ def delete_old_artifacts(
now = datetime.now(timezone.utc)
if isinstance(repository_paths_with_namespace, str):
repository_paths_with_namespace = [repository_paths_with_namespace]
artifacts_size_total = 0
cleaned_job_count_total = 0
repository_count = 0

for repository_path in repository_paths_with_namespace:
try:
Expand All @@ -68,6 +68,7 @@ def delete_old_artifacts(
branches_to_hash = {branch.name: branch.commit["id"] for branch in project.branches.list(iterator=True)}
tags_to_hash = {tag.name: tag.commit["id"] for tag in project.tags.list(iterator=True)}

self.repository_count += 1
artifacts_size_project = 0
cleaned_job_count_project = 0
for job in project.jobs.list(iterator=True):
Expand All @@ -92,6 +93,10 @@ def delete_old_artifacts(
for artifact in job._attrs["artifacts"]
if delete_logs or artifact["file_type"] == "archive"
)
self.cleaned_job_count_total += 1
cleaned_job_count_project += 1
self.artifacts_size_total += artifacts_size
artifacts_size_project += artifacts_size
job_created_at_localtime = (
datetime.fromisoformat(job.created_at).astimezone().strftime("%Y-%m-%d %H:%M:%S")
)
Expand Down Expand Up @@ -140,29 +145,27 @@ def job_description(
"Would delete artifacts of "
+ job_description(job, job_created_at_localtime, artifacts_size, job_branch, job_tag)
)
cleaned_job_count_project += 1
artifacts_size_project += artifacts_size
cleaned_job_count_total += cleaned_job_count_project
artifacts_size_total += artifacts_size_project
repository_count += 1
if cleaned_job_count_project > 0:
logger.info(
'Found "%d" old dangling jobs, with "%s" of attached artifacts in total in project "%s".',
'Found "%d" old jobs, with "%s" of attached artifacts in total in project "%s".',
cleaned_job_count_project,
human_size(artifacts_size_project),
project.path_with_namespace,
)
else:
logger.info(
'Found no old dangling jobs with attached artifacts in project "%s".', project.path_with_namespace
'Found no old jobs with attached artifacts in project "%s".', project.path_with_namespace
)

if repository_count > 1:
if cleaned_job_count_total > 0:
self.print_summary()

def print_summary(self) -> None:
if self.repository_count > 1:
if self.cleaned_job_count_total > 0:
logger.info(
'Found "%d" old dangling jobs, with "%s" of attached artifacts in total.',
cleaned_job_count_total,
human_size(artifacts_size_total),
'Found "%d" old jobs, with "%s" of attached artifacts in total.',
self.cleaned_job_count_total,
human_size(self.artifacts_size_total),
)
else:
logger.info("Found no old dangling jobs with attached artifacts in any project.")
logger.info("Found no old jobs with attached artifacts in any project.")
22 changes: 18 additions & 4 deletions gitlab_artifact_cleanup/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,25 @@ def setup_stderr_logging(verbosity_level: Verbosity) -> None:
setup_colored_stderr_logging(format_string="[%(levelname)s] %(message)s")


def handle_clean_artifacts(args: argparse.Namespace) -> None:
def init(args: argparse.Namespace) -> Gitlab:
"""
Initialize resources.

:param args: The arguments parsed from the command line and the configuration file
"""
return Gitlab(args.gitlab_url, args.gitlab_access_token, args.dry_run)


def handle_clean_artifacts(
gitlab: Gitlab,
args: argparse.Namespace,
) -> None:
"""
Connect to a GitLab server and delete old artifacts according to the passed command line arguments and the config.

:param gitlab: The Gitlab resource
:param args: The arguments parsed from the command line and the configuration file
"""
gitlab = Gitlab(args.gitlab_url, args.gitlab_access_token, args.dry_run)
gitlab.delete_old_artifacts(
args.repository_paths,
days_to_keep=args.days_to_keep,
Expand Down Expand Up @@ -282,7 +294,8 @@ def main() -> None:
Config.write_default_config()
logger.info('Wrote a default config file to "%s"', CONFIG_FILEPATH)
sys.exit(0)
handle_clean_artifacts(args)
gitlab = init(args)
handle_clean_artifacts(gitlab, args)
except expected_exceptions as e:
logger.error(str(e))
if "args" in locals() and args.verbosity_level is Verbosity.DEBUG:
Expand All @@ -292,5 +305,6 @@ def main() -> None:
sys.exit(i)
sys.exit(1)
except KeyboardInterrupt:
pass
logger.info('Interrupted by user')
gitlab.print_summary()
sys.exit(0)