-
-
Save ksandrmatveyev/7ec1ebd4de0f6fab96368a686ba4a77a to your computer and use it in GitHub Desktop.
Script that can be used to purge nexus v3 releases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.sonatype.nexus.repository.storage.StorageFacet; | |
import org.sonatype.nexus.repository.storage.Query; | |
import org.joda.time.DateTime; | |
import org.joda.time.format.DateTimeFormat; | |
def fmt = DateTimeFormat.forPattern('yyyy-MM-dd HH:mm:ss'); | |
[ | |
'maven-releases', | |
'other-releases' | |
].each { reponame -> | |
// Get a repository | |
def repo = repository.repositoryManager.get(reponame); | |
// Get a database transaction | |
def tx = repo.facet(StorageFacet).txSupplier().get(); | |
// Search assets that haven't been downloaded for more than three months | |
try { | |
// Begin the transaction | |
tx.begin(); | |
tx.findAssets(Query.builder() | |
.where('last_downloaded <') | |
.param(DateTime.now().minusMonths(3).toString(fmt)) | |
.build(), [repo]).each { asset -> | |
if (asset.componentId() != null) { | |
def component = tx.findComponent(asset.componentId()); | |
if (component != null) { | |
def count = tx.countComponents(Query.builder().where('name').eq(component.name()).and('version >').param(component.version()).build(), [repo]); | |
// Check if there is newer components of the same name | |
if (count > 0) { | |
log.info("Delete asset ${asset.name()} as it has not been downloaded since 3 months and has a newer version") | |
//tx.deleteAsset(asset); | |
tx.deleteComponent(component); | |
} | |
} else { | |
log.info("Asset ${asset.name()} wasn't downloaded for more than 3 months, I hope we dont' need it anymore even if this is the only version.") | |
tx.deleteAsset(asset); | |
} | |
} | |
} | |
// End the transaction | |
tx.commit(); | |
} catch (all) { | |
log.info("Exception: ${all}") | |
all.printStackTrace() | |
tx.rollback() | |
} finally { | |
tx.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment