From ce0d854aa5ce320b7b95dfa24ade5d9d2a8ba681 Mon Sep 17 00:00:00 2001 From: Artur <89927081+Hardar3@users.noreply.github.com> Date: Tue, 10 Oct 2023 15:14:02 +0300 Subject: [PATCH] Added a rule for removing artifacts by regex (#118) * Added a rule for removing artifacts by redex * Fix conversations * Fix artifact naming --------- Co-authored-by: root --- README.md | 7 +++++++ artifactory_cleanup/rules/delete.py | 22 +++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ffdcf02..4a6c54d 100644 --- a/README.md +++ b/README.md @@ -237,6 +237,13 @@ policies: - rule: DeleteEmptyFolders ``` +- `DeleteByRegexpName` - delete artifacts whose name matches the specified regexp + +```yaml +- rule: DeleteByRegexpName + regex_pattern: "\d" +``` + ## Keep - `KeepLatestNFiles` - Leaves the last (by creation time) files in the amount of N pieces. WITHOUT accounting diff --git a/artifactory_cleanup/rules/delete.py b/artifactory_cleanup/rules/delete.py index 4c48989..fa76994 100644 --- a/artifactory_cleanup/rules/delete.py +++ b/artifactory_cleanup/rules/delete.py @@ -1,7 +1,8 @@ from datetime import timedelta +import re from artifactory_cleanup.rules import utils -from artifactory_cleanup.rules.base import Rule +from artifactory_cleanup.rules.base import ArtifactsList, Rule class DeleteOlderThan(Rule): @@ -102,3 +103,22 @@ def filter(self, artifacts): repositories = utils.build_repositories(artifacts) folders = utils.get_empty_folders(repositories) return folders + + +class DeleteByRegexpName(Rule): + """ + Remove artifacts by regex pattern. + """ + + def __init__(self, regex_pattern): + self.regex_pattern = rf"{regex_pattern}" + + def aql_add_filter(self, filters): + print("Here's filters that we get\n", filters) + return filters + + def filter(self, artifacts: ArtifactsList) -> ArtifactsList: + for artifact in artifacts[:]: + if re.match(self.regex_pattern, artifact["name"]) is None: + artifacts.remove(artifact) + return artifacts \ No newline at end of file