Skip to content

Instantly share code, notes, and snippets.

@kortov
Last active March 12, 2020 14:24
Show Gist options
  • Save kortov/5207f968be75ee9035a7e8471760de6e to your computer and use it in GitHub Desktop.
Save kortov/5207f968be75ee9035a7e8471760de6e to your computer and use it in GitHub Desktop.
import org.sonatype.nexus.repository.storage.Asset
import org.sonatype.nexus.repository.storage.Query
import org.sonatype.nexus.repository.storage.StorageFacet
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import org.joda.time.*
import org.joda.time.format.*
def request = new JsonSlurper().parseText(args)
def repo = repository.repositoryManager.get(request.repoName)
assert request.repoName: 'repoName parameter is required'
assert request.lastDownloaded: 'lastDownloaded parameter is required, format: yyyy-mm-dd'
assert request.updatedLastDownloaded: 'updatedLastDownloaded parameter is required, format: yyyy-mm-dd'
assert request.lastUpdated: 'lastUpdated parameter is required, format: yyyy-mm-dd'
class UpdateDate {
def repoName;
def lastDownloaded;
def updatedLastDownloaded;
def lastUpdated;
def repo;
StorageFacet storageFacet;
def tx;
// Преобразуем строку в формат даты времени
DateTimeFormatter jodaFormat = DateTimeFormat.forPattern("yyyy-MM-dd")
def updatedLastDownloadedDate = jodaFormat.parseDateTime(updatedLastDownloaded)
UpdateDate(repoName, lastDownloaded, updatedLastDownloaded, lastUpdated, repository) {
this.repoName = repoName
this.lastDownloaded = lastDownloaded
this.updatedLastDownloaded = updatedLastDownloaded
this.lastUpdated = lastUpdated
this.repo = repository.repositoryManager.get(repoName)
this.storageFacet = this.repo.facet(StorageFacet)
this.tx = this.storageFacet.txSupplier().get()
}
def go()
{
try {
tx.begin()
log.info("Finding Assets where last updated date > ${lastUpdated} and last downloaded date > ${lastDownloaded}...")
//Iterable<Asset> assets = tx.findAssets(Query.builder().where('last_downloaded > ').param(request.startDate).build(), [repo])
Iterable<Asset> assets = tx.findAssets(Query.builder().where('last_downloaded >').param(lastDownloaded).build(), [repo]).take(10000).each { asset ->
if (asset.componentId() != null) {
def component = tx.findComponent(asset.componentId());
if (component != null) {
log.info("Found COMPONENTS in ASSET (component !=null): ${component}")
//log.info("COMPONENT Get lastDownloadedDate: ${component.lastDownloaded}, updating...")
//component.lastDownloaded(updatedLastDownloadedDate)
//log.info("COMPONENT Get new lastDownloadedDate: ${component.lastDownloaded}.")
//log.info("ASSET Get lastDownloadedDate: ${asset.lastDownloaded}, updating...")
//asset.lastDownloaded(updatedLastDownloadedDate)
//log.info("ASSET Get new lastDownloadedDate: ${asset.lastDownloaded}.")
}
} else {
log.info("Not found COMPONENTS in ASSET (component = null): ${asset.name()}")
//log.info("ASSET Get lastDownloadedDate: ${asset.lastDownloaded}, updating...")
//asset.lastDownloaded(updatedLastDownloadedDate)
//log.info("ASSET Get new lastDownloadedDate: ${asset.lastDownloaded}.")
}
}
if (assets.size <= 0) {
log.info("Not found Assets for your query...")
}
/*assets.each { Asset asset ->
log.info("ASSET Get lastDownloadedDate: ${asset.lastDownloaded}, updating...")
asset.lastDownloaded(updatedLastDownloadedDate)
log.info("ASSET Get new lastDownloadedDate: ${asset.lastDownloaded}.")
}*/
// End the transaction
log.info("Committing...")
tx.commit();
log.info("Committing done.")
}
catch (all) {
log.info("Exception: ${all}")
all.printStackTrace()
log.info("Rolling back changes...")
tx.rollback()
log.info("Rollback done.")
} finally {
tx.close();
log.info("Transaction closed.")
}
}
}
new UpdateDate(request.repoName, request.lastDownloaded, request.updatedLastDownloaded, request.lastUpdated, repository).go()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment