|
| 1 | +import org.apache.commons.lang3.StringUtils |
| 2 | +import org.artifactory.api.repo.exception.ItemNotFoundRuntimeException |
| 3 | + |
| 4 | +import groovy.time.TimeCategory |
| 5 | +import groovy.time.TimeDuration |
| 6 | +import groovy.transform.Field |
| 7 | +import org.artifactory.security.UserInfo |
| 8 | + |
| 9 | +// ToDO: |
| 10 | +// 1. property file with scheduler config |
| 11 | +// 2. fix numberArtifactsToKeep to keep provided number of artifacts, not deleting as much artifacts instead |
| 12 | + |
| 13 | +def pluginGroup = 'deprecates' |
| 14 | + |
| 15 | +executions { |
| 16 | + deprecate(groups: [pluginGroup]) { params -> |
| 17 | + def months = params['months'] ? params['months'][0] as int : 6 |
| 18 | + def repos = params['repos'] as String[] |
| 19 | + def dryRun = params['dryRun'] ? params['dryRun'][0].toBoolean() : false |
| 20 | + def numberArtifactsToKeep = params['numberArtifactsToKeep'] ? params['numberArtifactsToKeep'][0] as int : 3 |
| 21 | + artifactDeprecate(months, repos, log, dryRun, numberArtifactsToKeep) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | +private def artifactDeprecate(int months, String[] repos, log, dryRun = false, numberArtifactsToKeep = 3) { |
| 27 | + |
| 28 | + log.info "-----------[ Starting Deprecating Artifacts... ]-----------" |
| 29 | + log.info "---> Variables: \n months: $months \n repos: $repos \n log: $log \n dryRun: $dryRun \n numberArtifactsToKeep: $numberArtifactsToKeep \n" |
| 30 | + |
| 31 | + def monthsUntil = Calendar.getInstance() |
| 32 | + monthsUntil.add(Calendar.MONTH, -months) |
| 33 | + |
| 34 | + int cntFoundArtifacts = 0 |
| 35 | + int cntNoDeletePermissions = 0 |
| 36 | + def artifactsCleanedUp = searches.artifactsCreatedOrModifiedInRange(null, monthsUntil, repos) |
| 37 | + def sortedArtifactsCleanedUp = artifactsCleanedUp.sort { repositories.getItemInfo(it)?.lastUpdated } |
| 38 | + // Here we can use sortedArtifactsCleanedUp.length - numberArtifactsToKeep |
| 39 | + def artifactsToDelete = sortedArtifactsCleanedUp.take(numberArtifactsToKeep) |
| 40 | + |
| 41 | + |
| 42 | + log.info "\n ===> sortedArtifactsCleanedUp: $sortedArtifactsCleanedUp\n\n ===> artifactsToDelete: $artifactsToDelete" |
| 43 | + |
| 44 | + artifactsToDelete.find { |
| 45 | + try { |
| 46 | + if (!security.canDelete(it)) { |
| 47 | + cntNoDeletePermissions++ |
| 48 | + } |
| 49 | + if (dryRun) { |
| 50 | + log.info "Found $it!" |
| 51 | + log.info "\t==> currentUser: ${security.currentUser().getUsername()}" |
| 52 | + log.info "\t==> canDelete: ${security.canDelete(it)}" |
| 53 | + } else { |
| 54 | + if (security.canDelete(it)) { |
| 55 | + log.info "Deleting $it!" |
| 56 | + repositories.delete it |
| 57 | + } else { |
| 58 | + log.info "Can't delete $it (user ${security.currentUser().getUsername()} has no delete permissions), " + |
| 59 | + "$cntFoundArtifacts/$artifactsCleanedUp.size" |
| 60 | + cntNoDeletePermissions++ |
| 61 | + } |
| 62 | + } |
| 63 | + } catch (ItemNotFoundRuntimeException ex) { |
| 64 | + log.info "Failed to find $it, skipping" |
| 65 | + } |
| 66 | + |
| 67 | + return false |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | + |
0 commit comments