diff --git a/README.md b/README.md index 9894627..fbbe3f4 100644 --- a/README.md +++ b/README.md @@ -411,6 +411,13 @@ policies: - "*.zip" ``` +- `FilterByRegexpPath` - delete artifacts whose path matches the specified regexp + +```yaml +- rule: DeleteByRegexpPath + path: "\d" +``` + ## Create your own rule If you want to create your own rule, you can do it! diff --git a/artifactory_cleanup/rules/filters.py b/artifactory_cleanup/rules/filters.py index acb44bb..374e467 100644 --- a/artifactory_cleanup/rules/filters.py +++ b/artifactory_cleanup/rules/filters.py @@ -1,5 +1,5 @@ from artifactory_cleanup.rules.utils import to_masks -from artifactory_cleanup.rules.base import Rule +from artifactory_cleanup.rules.base import ArtifactsList, Rule class FilterRule(Rule): @@ -68,3 +68,18 @@ class ExcludeFilename(FilterRule): attribute_name = "name" operator = "$nmatch" boolean_operator = "$and" + + +class FilterByRegexpPath(Rule): + """ + Filter artifacts paths by regex pattern. + """ + + def init(self, path): + self.path = rf"{path}" + + def filter(self, artifacts: ArtifactsList) -> ArtifactsList: + for artifact in artifacts[:]: + if re.match(self.path, artifact["path"]) is None: + artifacts.keep(artifact) + return artifacts