java.lang.IllegalStateException: Unit of work has not been set

Hi,

I was suggested to run this kind of groovy code as a Nexus task in order to create a new asset in a raw hosted repo. It is just a snippet from a bigger script. The content of the file asset is more complex than in this example. So far, I cannot do this using REST API because the content is built inside the server.

I got this error: “java.lang.IllegalStateException: Unit of work has not been set”.
It is from org.sonatype.nexus.orient.raw.internal.RawContentFacetImpl:

@TransactionalStoreBlob
protected Content doPutContent(final String path, final TempBlob tempBlob, final Payload payload)
throws IOException

What should I have to do to make this code working?!

Regards.

import org.sonatype.nexus.content.raw.RawContentFacet
import org.sonatype.nexus.repository.Repository
import org.sonatype.nexus.repository.view.payloads.StringPayload

class AClass {
private final log
private final repo

AClass(log, repo) {
    this.log = log
    this.repo = repo
}

void doStuff() {
    String name = "myFile"
    String repoName = "myRepo" // of type raw hosted

    Repository myRepo = this.repo.repositoryManager.get(repoName)
    StringPayload content = new StringPayload("dummy text for now", null)

    RawContentFacet facet = myRepo.facet(RawContentFacet.class)
    facet.put(name, content)
}
}

new AClass(log, repository).doStuff()

Your script is interacting with internal API, so there’s zero guarantee your script will be compatible with any future (or past) releases. If possible, consider using our REST API - you should be able to work with REST in your script just fine. To answer your original question: the error message says you are trying to interact with storage outside of transaction, and remember to to close the transaction afterwards.

Hi Dawid.

I have tried to do my work using the REST API but I’m not able to obtain the asset file size using REST API. I was able to do that using internal api in a groovy script task. I need to generate a PULP_MANIFEST file in the repo and its content should contain: filename, sha256 and the file size. REST API provides me the filename and sha256 but not the size. I’m somehow forced to use the internal API.

Please, can you provide me an working example that can create an asset file in a raw hosted repo? I have such an example (using transactions) in a previous topic of mine but somehow it’s not working. Maybe you can take a look and see what’s wrong. It is not throwing any exception nor error.

Regards.