Hi,
I need to create a groovy task that scans the content of a raw repo and create a new asset in that repo that is a new file containing some info about the assets already existing in that repo (ex a PULP_MANIFEST file).
I was able to scan the repo and create the new file content and now I need to create a new asset with that content.
Below is my code that it is supposed to create a new dummy asset in a specified raw repo. It is not doing what I expect and it throws no exception.
Please, can someone take a look and explain what’s wrong? Regards.
import org.slf4j.LoggerFactory
import org.sonatype.nexus.common.collect.AttributesMap
import org.sonatype.nexus.common.hash.HashAlgorithm
import org.sonatype.nexus.repository.Repository
import org.sonatype.nexus.repository.storage.Asset
import org.sonatype.nexus.repository.storage.Bucket
import org.sonatype.nexus.repository.storage.StorageFacet
import org.sonatype.nexus.repository.storage.StorageTx
import org.sonatype.nexus.repository.storage.TempBlob
import org.sonatype.nexus.repository.view.Payload
import org.sonatype.nexus.repository.view.payloads.StringPayload
import org.sonatype.nexus.repository.view.Content
import org.sonatype.nexus.script.plugin.internal.provisioning.RepositoryApiImpl
def log = LoggerFactory.getLogger(NewAsset.class)
def repoName = “my_files”
new NewAsset(log, repository, repoName).createDummy()
class NewAsset {
private final log
private final RepositoryApiImpl repo
private final String repoName
NewAsset(log, RepositoryApiImpl repo, String repoName) {
this.log = log
this.repo = repo
this.repoName = repoName
}
Repository getRepository() {
return this.repo.repositoryManager.get(this.repoName)
}
private void put(StorageTx tx, String path, Payload content) throws IOException {
final List<HashAlgorithm> hashAlgorithms = Arrays.asList(HashAlgorithm.MD5, HashAlgorithm.SHA1, HashAlgorithm.SHA256)
StorageFacet storageFacet = getRepository().facet(StorageFacet.class)
TempBlob tempBlob = storageFacet.createTempBlob(content, hashAlgorithms)
doPutContent(tx, path, tempBlob, content)
}
private void doPutContent(StorageTx tx, String path, TempBlob tempBlob, Payload payload) throws IOException {
Asset asset = createAsset(tx, path)
AttributesMap contentAttributes = null
if (payload instanceof Content) {
contentAttributes = ((Content) payload).getAttributes()
}
Content.applyToAsset(asset, Content.maintainLastModified(asset, contentAttributes))
tx.setBlob(asset, path, tempBlob, null, payload.getContentType(), false)
tx.saveAsset(asset)
}
private Asset createAsset(StorageTx tx, String assetName) {
final Bucket bucket = tx.findBucket(getRepository())
Asset asset = tx.createAsset(bucket, getRepository().getFormat())
asset.name(assetName)
asset.markAsDownloaded()
return asset
}
void createDummy() throws IOException {
StringPayload payload = new StringPayload("dummy text for now", "text/plain")
final StorageFacet storageFacet = getRepository().facet(StorageFacet)
final StorageTx tx = storageFacet.txSupplier().get()
try {
tx.begin()
put(tx, "/pulp_manifest2", payload)
} catch (Exception e) {
log.info("Exception:", e.toString())
tx.rollback()
}
finally {
tx.close()
}
}
}