Forked from emexelem/purge_nexus_v3_releases.groovy
Created
October 20, 2017 14:39
-
-
Save ksandrmatveyev/7efb20056d0e88c2999af2686e9480a4 to your computer and use it in GitHub Desktop.
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'); | |
// Get a repository | |
def repo = repository.repositoryManager.get('nuget-releases'); | |
// Get a database transaction | |
def tx = repo.facet(StorageFacet).txSupplier().get(); | |
// Begin the transaction | |
tx.begin(); | |
// Search assets that havn't been downloaded for more than three months | |
tx.findAssets(Query.builder().where('last_downloaded <').param(DateTime.now().minusMonths(3).toString(fmt)).build(), [repo]).each { asset -> | |
def component = tx.findComponent(asset.componentId()); | |
// Check if there is newer components of the same name | |
def count = tx.countComponents(Query.builder().where('name').eq(component.name()).and('version >').param(component.version()).build(), [repo]); | |
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); | |
} | |
} | |
// End the transaction | |
tx.commit(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment