Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(output): save list of deleted artifacts as json #145

Merged
merged 5 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,11 @@ docker run -v "$(pwd)":/app devopshq/artifactory-cleanup artifactory-cleanup --l
# Save the table summary in a file
artifactory-cleanup --output=myfile.txt

# Save the summary in a Json file
# Save the summary in a json file
artifactory-cleanup --output=myfile.txt --output-format=json

# Save the summary in a json file and append the list of all removed artifacts
artifactory-cleanup --output=myfile.json --output-format json --output-artifacts
```

# Rules
Expand Down
12 changes: 7 additions & 5 deletions artifactory_cleanup/artifactorycleanup.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
from concurrent.futures import ThreadPoolExecutor
from datetime import date
from typing import List, Iterator
from typing import List, Iterator, Optional

from attr import dataclass
from requests import Session

from artifactory_cleanup.errors import ArtifactoryCleanupException
from artifactory_cleanup.rules.base import CleanupPolicy, ArtifactDict
from artifactory_cleanup.rules.base import ArtifactsList, CleanupPolicy, ArtifactDict


@dataclass
class CleanupSummary:
policy_name: str
artifacts_removed: int
artifacts_size: int
removed_artifacts: ArtifactsList


class ArtifactoryCleanup:
Expand All @@ -38,7 +39,7 @@ def _init_policies(self, today):
for policy in self.policies:
policy.init(self.session, today)

def cleanup(self, block_ctx_mgr, test_ctx_mgr) -> Iterator[CleanupSummary]:
def cleanup(self, block_ctx_mgr, test_ctx_mgr) -> Iterator[Optional[CleanupSummary]]:
for policy in self.policies:
with block_ctx_mgr(policy.name):
# Prepare
Expand Down Expand Up @@ -72,12 +73,13 @@ def _delete(artifact):
try:
artifacts_size = sum([x["size"] for x in artifacts_to_remove])
print("Summary size: {}".format(artifacts_size))
yield CleanupSummary(
summary = CleanupSummary(
policy_name=policy.name,
artifacts_size=artifacts_size,
artifacts_removed=len(artifacts_to_remove),
artifacts=artifacts_to_remove
allburov marked this conversation as resolved.
Show resolved Hide resolved
)

yield summary
except KeyError:
print("Summary size not defined")
yield None
Expand Down
33 changes: 20 additions & 13 deletions artifactory_cleanup/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
ArtifactoryCleanup,
)
from artifactory_cleanup.base_url_session import BaseUrlSession
from artifactory_cleanup.context_managers import get_context_managers
from artifactory_cleanup.errors import InvalidConfigError
from artifactory_cleanup.loaders import (
PythonLoader,
YamlConfigLoader,
)
from artifactory_cleanup.context_managers import get_context_managers

requests.packages.urllib3.disable_warnings()

Expand Down Expand Up @@ -95,6 +95,13 @@ class ArtifactoryCleanupCLI(cli.Application):
mandatory=False,
)

_output_artifacts = cli.Flag(
"--output-artifacts",
help="When --output-format is json, append the list of all removed artifacts to output",
mandatory=False,
default=False,
)

@property
def VERSION(self):
# To prevent circular imports
Expand Down Expand Up @@ -137,13 +144,13 @@ def _print_table(self, result: dict):
print(self._format_table(result))

def _create_output_file(self, result, filename, format):
text = None
text = ""
if format == "table":
text = self._format_table(result).get_string()
else:
text = json.dumps(result)
elif format == "json":
text = json.dumps(result, indent=4)

with open(filename, "w") as file:
with open(filename, "w", encoding="utf-8") as file:
file.write(text)

def main(self):
Expand Down Expand Up @@ -189,14 +196,14 @@ def main(self):
continue
total_size += summary.artifacts_size

result["policies"].append(
{
"name": summary.policy_name,
"file_count": summary.artifacts_removed,
"size": summary.artifacts_size,
}
)

policy = {
"name": summary.policy_name,
"file_count": summary.artifacts_removed,
"size": summary.artifacts_size
}
if self._output_artifacts:
policy["removed_artifacts"] = summary.removed_artifacts
result["policies"].append(policy)
result["total_size"] = total_size

self._print_table(result)
Expand Down
Loading