diff --git a/README.md b/README.md index 9e724dd..62bcb3d 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