Skip to content

Commit

Permalink
Added a rule for removing artifacts by regex (#118)
Browse files Browse the repository at this point in the history
* Added a rule for removing artifacts by redex

* Fix conversations

* Fix artifact naming

---------

Co-authored-by: root <root@Hardar.localdomain>
  • Loading branch information
Hardar3 and root authored Oct 10, 2023
1 parent 8f3a721 commit ce0d854
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion artifactory_cleanup/rules/delete.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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

0 comments on commit ce0d854

Please sign in to comment.