Skip to content

Commit 135df82

Browse files
authored
Merge pull request #6 from NorbertGebicki-TomTom/patch-2
Update artifactCleanup.groovy
2 parents c303f3b + 7adb7f0 commit 135df82

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

cleanup/artifactCleanup/artifactCleanup.groovy

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Global {
4545

4646
def pluginGroup = 'cleaners'
4747
def config = new ConfigSlurper().parse(new File(ctx.artifactoryHome.haAwareEtcDir, PROPERTIES_FILE_PATH).toURL())
48-
def regex = ~/-\d+\.\d+\.\d+/
48+
def regex = ~/.*-[\.\d+]*[-+][\.\d+]*\..*/
4949

5050

5151
executions {
@@ -123,8 +123,12 @@ private def artifactCleanup(int months, String[] repos, log, paceTimeMS, dryRun
123123
Global.stopCleaning = false
124124
int cntFoundArtifacts = 0
125125
int cntNoDeletePermissions = 0
126+
int cntNoDeleteRegexPermissions = 0
127+
int cntRemovedArtifacts = 0
126128
long bytesFound = 0
127129
long bytesFoundWithNoDeletePermission = 0
130+
long bytesFoundWithRegexProtection = 0
131+
long bytesRemoved = 0
128132
def artifactsCleanedUp = searches.artifactsNotDownloadedSince(monthsUntil, monthsUntil, repos)
129133
artifactsCleanedUp.find {
130134
try {
@@ -152,24 +156,31 @@ private def artifactCleanup(int months, String[] repos, log, paceTimeMS, dryRun
152156
cntNoDeletePermissions++
153157
}
154158
if (dryRun) {
155-
log.info "Found $it, $cntFoundArtifacts/$artifactsCleanedUp.size total $bytesFound bytes"
156-
log.info "\t==> currentUser: ${security.currentUser().getUsername()}"
157-
log.info "\t==> canDelete: ${security.canDelete(it)}"
159+
log.debug "Found $it, $cntFoundArtifacts/$artifactsCleanedUp.size total $bytesFound bytes"
160+
log.debug "\t==> currentUser: ${security.currentUser().getUsername()}"
161+
log.debug "\t==> canDelete: ${security.canDelete(it)}"
158162
if (!checkName(keepArtifacts, keepArtifactsRegex, it)) {
159163
log.info "\t==> protected by regex: ${keepArtifactsRegex}"
160164
}
161165
} else {
162166
if (security.canDelete(it) && (checkName(keepArtifacts, keepArtifactsRegex, it))) {
163-
log.info "Deleting $it, $cntFoundArtifacts/$artifactsCleanedUp.size total $bytesFound bytes"
167+
bytesRemoved += repositories.getItemInfo(it)?.getSize()
168+
log.debug "Deleting $it, [$cntFoundArtifacts/$artifactsCleanedUp.size]. Removed $bytesRemoved of total $bytesFound bytes"
164169
repositories.delete it
165170
} else {
166-
log.info "Can't delete $it (user ${security.currentUser().getUsername()} has no delete permissions), " +
171+
log.debug "Can't delete $it (user ${security.currentUser().getUsername()} has no delete permissions), " +
167172
"$cntFoundArtifacts/$artifactsCleanedUp.size total $bytesFound bytes"
168-
cntNoDeletePermissions++
173+
if (security.canDelete(it)) {
174+
// Because previous block was not processed, security.canDelete(it) AND/OR checkName is FALSE.
175+
// I am checking if permissions to delete files are proper. If that's true, that means function checkName returned FALSE
176+
// and artifact is protected by Regex.
177+
bytesFoundWithRegexProtection += repositories.getItemInfo(it)?.getSize()
178+
cntNoDeleteRegexPermissions++
179+
}
169180
}
170181
}
171182
} catch (ItemNotFoundRuntimeException ex) {
172-
log.info "Failed to find $it, skipping"
183+
log.debug "Failed to find $it, skipping"
173184
}
174185

175186
def sleepTime = (Global.paceTimeMS > 0) ? Global.paceTimeMS : paceTimeMS
@@ -184,8 +195,11 @@ private def artifactCleanup(int months, String[] repos, log, paceTimeMS, dryRun
184195
log.info "Dry run - nothing deleted. Found $cntFoundArtifacts artifacts consuming $bytesFound bytes"
185196
log.info "From that $cntNoDeletePermissions artifacts no delete permission by user ($bytesFoundWithNoDeletePermission bytes)"
186197
} else {
187-
log.info "Finished cleanup, try to delete $cntFoundArtifacts artifacts that took up $bytesFound bytes"
198+
cntRemovedArtifacts = cntFoundArtifacts - cntNoDeletePermissions - cntNoDeleteRegexPermissions
199+
log.info "Finished cleanup $repos repositories, try to delete $cntFoundArtifacts artifacts that took up $bytesFound bytes"
188200
log.info "From that $cntNoDeletePermissions artifacts no delete permission by user ($bytesFoundWithNoDeletePermission bytes)"
201+
log.info "and $cntNoDeleteRegexPermissions artifacts is protected by regex $keepArtifactsRegex ($bytesFoundWithRegexProtection bytes)"
202+
log.info "After this cleanup, $cntRemovedArtifacts artifacts ($bytesRemoved bytes) was removed from $repos repositories"
189203
}
190204
}
191205

@@ -235,4 +249,4 @@ private def getSkippedPaths(String[] repos) {
235249

236250
private def checkName(keepArtifacts, keepArtifactsRegex, artifactName) {
237251
return !keepArtifacts || !((artifactName =~ keepArtifactsRegex).count > 0)
238-
}
252+
}

0 commit comments

Comments
 (0)