How to list all assets (groupId, name, size) in a Nexus3 repository? - sql

I know it is very simple to do in OrientDB :
select * from asset where bucket.repository_name = 'my-repo-release';
but I need to get this list remotely, not in local orientdb console, so I need a groovy script and I can't find it anywhere.

Here is the example script :
https://github.com/sonatype-nexus-community/nexus-scripting-examples
it can be found in the "nexus-script-example" project :
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
def assetListFile = new File('/tmp/assetListFile.txt')
def request = new JsonSlurper().parseText("{\"repoName\":\"maven-releases\",\"startDate\":\"2016-01-01\"}")
assert request.repoName: 'repoName parameter is required'
assert request.startDate: 'startDate parameter is required, format: yyyy-mm-dd'
log.info("Gathering Asset list for repository: ${request.repoName} as of startDate: ${request.startDate}")
def repo = repository.repositoryManager.get(request.repoName)
StorageFacet storageFacet = repo.facet(StorageFacet)
def tx = storageFacet.txSupplier().get()
try {
tx.begin()
Iterable<Asset> assets = tx.findAssets(Query.builder().where('last_updated > ').param(request.startDate).build(), [repo])
assets.each {Asset asset ->
assetListFile << asset.name() + '\n'
}
}
finally {
tx.close()
}
All properties of an asset (object org.sonatype.nexus.repository.storage.Asset) are accessible, including asset.size().

Related

MapStruct Property has no write accessor in MetaData for target name

I have a FlatDTO that needs to be mapped to a nested Response containing InfoData and MetaData
The code for the response is generated by OpenAPI. So the below definitions can't be changed.
package org.mapstruct.example.kotlin.openapi
import com.fasterxml.jackson.annotation.JsonProperty
import javax.validation.Valid
data class Response(
#field:Valid #field:JsonProperty("infoData", required = true) val infoData: InfoData,
#field:Valid #field:JsonProperty("metaData", required = true) val metaData: MetaData
)
data class InfoData(
#field:JsonProperty("id", required = true) val id: kotlin.String,
)
data class MetaData(
#field:JsonProperty("firstProperty") val firstProperty: String? = null,
)
I have defined FlatDTO as follows.
package org.mapstruct.example.kotlin.models
data class FlatDTO(
var id: String? = null,
var firstProperty: String,
)
Here is my mapper class which maps FlatDTO to Response
package org.mapstruct.example.kotlin.mapper
import org.mapstruct.Mapper
import org.mapstruct.Mapping
import org.mapstruct.Mappings
import org.mapstruct.example.kotlin.models.FlatDTO
import org.mapstruct.example.kotlin.openapi.Response
#Mapper
interface DataMapper {
#Mappings(
Mapping(target = "infoData.id", source = "id"),
Mapping(target = "metaData.firstProperty", source = "firstProperty")
)
fun flatToResponse(flatDTO: FlatDTO): Response
}
When I try to build the code using mvn clean install
I get the following error.
error: Property "firstProperty" has no write accessor in MetaData for target name "metaData.firstProperty".
[ERROR] #org.mapstruct.Mappings(value = {#org.mapstruct.Mapping(target = "infoData.id", source = "id"), #org.mapstruct.Mapping(target = "metaData.firstProperty", source = "firstProperty")})
I understand that this message is trying to say that there is no setter function for firstProperty because it's defined as val but that code cannot be edited. I can write my own custom mapper that works just fine.
I wanted to understand if there is a way to use MapStruct in this scenario.

Sonatype Nexus search api does not return all the artifacts

We use sonatype nexus OSS 3.14.0-04 to store all our artifacts and we use Jenkins as our CI/CD took. I introduced snapshot support recently. From Jenkins I use the below groovy script to fetch all artifact versions and populate an Active choice drop-down:
#!/usr/bin/env groovy
import groovy.json.JsonSlurper
def source = "release"
def url = "http://<<nexus-host>>/service/rest/v1/search?repository=maven-releases&maven.groupId=com.xyz&maven.artifactId=woof&maven.extension=war"
Set<String> versions = new HashSet<>();
def continuationToken = null
def count = 0
def url1 = url
while (true) {
def xml = url1.toURL().text
JsonSlurper parser = new groovy.json.JsonSlurper()
Map parsedJson = parser.parseText(xml)
def rawVer = parsedJson.items.version
continuationToken = parsedJson.continuationToken
if (continuationToken != null) {
url1 = url + "&continuationToken=" + continuationToken
}
if (source.equalsIgnoreCase('snapshot')) {
Set<String> modVersion = new HashSet<>()
for (def item : rawVer) {
modVersion.add(item.split("-").getAt(0) + "-SNAPSHOT")
}
versions.addAll(modVersion)
} else {
versions.addAll(rawVer)
}
if (continuationToken == null || count > 5) {
break;
}
}
return versions.sort().reverse()
The nexus api search endpoint uses a pagination strategy to return the result. If the global result contains several pages, the current result will contain a continuationToken you can use in your next query to fetch the next result page.
If your search returns more results than the page size, you will need to make several calls to the search url to get all the results in your Jenkins script (i.e. until continuationToken is null).
Ref: Nexus Search API endpoint doc (also referenced in your swagger-ui api view in your nexus admin)

Error occurred creating story: "Cannot parse object reference from", Rally

I am trying to create user stories in rally by java. Am failing to get response and it throwing " Cannot parse object reference from ".
Brief on my rally : Let assume I have Project name "Characters" and its has subs as A, B, C, D. I have to create my user stories in C. Is my following code correct in this prospect? Can anyone please help?
package samplerally;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import com.google.gson.JsonObject;
import com.rallydev.rest.RallyRestApi;
import com.rallydev.rest.request.CreateRequest;
import com.rallydev.rest.request.GetRequest;
import com.rallydev.rest.response.CreateResponse;
import com.rallydev.rest.util.Ref;
public class CreateStory {
public static void main(String[] args) throws URISyntaxException,IOException {
String host = "https://rally1.rallydev.com";
String username = "name#comp.com";
String password = "password";
String wsapiVersion = "v2.0";
String applicationName = "C";
RallyRestApi restApi = new RallyRestApi(new URI(host),username,password);
restApi.setWsapiVersion(wsapiVersion);
restApi.setApplicationName(applicationName);
try {
for (int i = 0; i < 3; i++) {
// Add a story
System.out.println("Creating a story...");
JsonObject newStory = new JsonObject();
newStory.addProperty("Name", "my story");
newStory.addProperty("Project", "Characters");
CreateRequest createRequest = new CreateRequest("hierarchicalrequirement", newStory);
CreateResponse createResponse = restApi.create(createRequest);
System.out.println("Response ::: "+createResponse.wasSuccessful());
if (createResponse.wasSuccessful()) {
System.out.println(String.format("Created %s",createResponse.getObject().get("_ref").getAsString()));
// Read story
String ref = Ref.getRelativeRef(createResponse.getObject().get("_ref").getAsString());
System.out.println(String.format("\nReading Story %s...",ref));
GetRequest getRequest = new GetRequest(ref);
} else {
String[] createErrors;
createErrors = createResponse.getErrors();
System.out.println("Error occurred creating story: ");
for (int j = 0; j < createErrors.length; j++) {
System.out.println(createErrors[j]);
}
}
}
} finally {
// Release all resources
restApi.close();
}
}
}
And I am getting error as :
Creating a story...
Response ::: false
Error occurred creating story:
Could not read: Cannot parse object reference from "BSS HWSE Team Columbia"
Creating a story...
Response ::: false
Error occurred creating story:
Could not read: Cannot parse object reference from "BSS HWSE Team Columbia"
Creating a story...
Response ::: false
Error occurred creating story:
Could not read: Cannot parse object reference from "BSS HWSE Team Columbia"
Picked up JAVA_TOOL_OPTIONS: -agentlib:jvmhook
Picked up _JAVA_OPTIONS: -Xrunjvmhook -Xbootclasspath/a:"C:\Program Files\HP\Unified Functional Testing\bin\java_shared\classes";"C:\Program Files\HP\Unified Functional Testing\bin\java_shared\classes\jasmine.jar"
Please help. New to rally. Thanks in advance
When setting reference fields such as Project, Iteration, Release, WorkProduduct, Parent, etc that point to full objects use the reference,and not the Name.
Find out the unique ObjectID of the Project "C", e.g. 123456.
Replace:
newStory.addProperty("Project", "Characters");
with
newStory.addProperty("Project", "/project/123456");
To find ObjectID of a project you may either query by its name in WS API, or look at the URL of one of Rally pages in the address bar, while in that project, e.g. URL of a Backlog page: https://rally1.rallydev.com/#/123456/backlog
If there is a d or u appended to ObjectID string, e.g. 123456d, do not include it in string. In the URL it indicates if you are currently scoping up or down.

Piglatin jodatime error with StanfordCoreNLP

I am trying to create a Pig UDF that extracts the locations mentioned in a tweet using the Stanford CoreNLP package interfaced through the sista Scala API. It works fine when run locally with 'sbt run', but throws a "java.lang.NoSuchMethodError" exception when called from Pig:
Loading default properties from tagger
edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger
Reading POS tagger model from
edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger
Loading classifier from edu/stanford/nlp/models/ner/english.all.3class.distsim.crf.ser.gz
2013-06-14 10:47:54,952 [communication thread] INFO
org.apache.hadoop.mapred.LocalJobRunner - reduce > reduce done [7.5
sec]. Loading classifier from
edu/stanford/nlp/models/ner/english.muc.7class.distsim.crf.ser.gz ...
2013-06-14 10:48:02,108 [Low Memory Detector] INFO
org.apache.pig.impl.util.SpillableMemoryManager - first memory handler
call - Collection threshold init = 18546688(18112K) used =
358671232(350264K) committed = 366542848(357952K) max =
699072512(682688K) done [5.0 sec]. Loading classifier from
edu/stanford/nlp/models/ner/english.conll.4class.distsim.crf.ser.gz
... 2013-06-14 10:48:10,522 [Low Memory Detector] INFO
org.apache.pig.impl.util.SpillableMemoryManager - first memory handler
call- Usage threshold init = 18546688(18112K) used =
590012928(576184K) committed = 597786624(583776K) max =
699072512(682688K) done [5.6 sec]. 2013-06-14 10:48:11,469 [Thread-11]
WARN org.apache.hadoop.mapred.LocalJobRunner - job_local_0001
java.lang.NoSuchMethodError:
org.joda.time.Duration.compareTo(Lorg/joda/time/ReadableDuration;)I
at edu.stanford.nlp.time.SUTime$Duration.compareTo(SUTime.java:3406)
at edu.stanford.nlp.time.SUTime$Duration.max(SUTime.java:3488) at
edu.stanford.nlp.time.SUTime$Time.difference(SUTime.java:1308) at
edu.stanford.nlp.time.SUTime$Range.(SUTime.java:3793) at
edu.stanford.nlp.time.SUTime.(SUTime.java:570)
Here is the relevant code:
object CountryTokenizer {
def tokenize(text: String): String = {
val locations = TweetEntityExtractor.NERLocationFilter(text)
println(locations)
locations.map(x => Cities.country(x)).flatten.mkString(" ")
}
}
class PigCountryTokenizer extends EvalFunc[String] {
override def exec(tuple: Tuple): java.lang.String = {
val text: java.lang.String = Util.cast[java.lang.String](tuple.get(0))
CountryTokenizer.tokenize(text)
}
}
object TweetEntityExtractor {
val processor:Processor = new CoreNLPProcessor()
def NERLocationFilter(text: String): List[String] = {
val doc = processor.mkDocument(text)
processor.tagPartsOfSpeech(doc)
processor.lemmatize(doc)
processor.recognizeNamedEntities(doc)
val locations = doc.sentences.map(sentence => {
val entities = sentence.entities.map(List.fromArray(_)) match {
case Some(l) => l
case _ => List()
}
val words = List.fromArray(sentence.words)
(words zip entities).filter(x => {
x._1 != "" && x._2 == "LOCATION"
}).map(_._1)
})
List.fromArray(locations).flatten
}
}
I am using sbt-assembly to construct a fat-jar, and so the joda-time jar file should be accessible. What is going on?
Pig ships with its own version of joda-time (1.6), which is incompatible with 2.x.

Jenkins - Email notifications

I'm trying to customize email using Groovy with the email-ext plugin. As I add new features to these emails, I may introduce errors in the scripts and so receive bad mails containing the StackTrace. So, I'd like to be able to send notifications on finished jobs as my jobs may take many hours (more than 4 currently).
Is there a way to ask jenkins to send notifications on finished jobs (using Groovy or any other scripting language)?
find a solution :
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import groovy.text.Template
import groovy.text.SimpleTemplateEngine
import javax.mail.*
import javax.mail.internet.*
//-------------- Job params --------------
projectName="YourProjectName";
buildNum = 10;
templateName="groovy-html-cobertura.template";
recipients="someone#somewhere.com";
sender="jenkins#somewhere.com";
smtpHost="mysmtphost";
//------------End Job params -------------
for (hudson.model.AbstractProject p : hudson.model.Hudson.instance.projects) {
if(p.name.equals(projectName)){
for (hudson.model.AbstractBuild b : p.getBuilds() ) {
if(b.getNumber() == buildNum){
println b;
b.reload();
def binding = [ "build" : b,
"project" : b.getProject(),
"rooturl" : hudson.model.Hudson.getInstance().getRootUrl(),
"it" : new hudson.plugins.emailext.plugins.content.ScriptContentBuildWrapper(b),
"spc" : " " ]
def engine = new SimpleTemplateEngine()
java.io.File file = new java.io.File(hudson.model.Hudson.getInstance ().getRootPath().getBaseName()+"/email-templates/"+templateName);
mailBody = engine.createTemplate(file.getText()).make(binding).toString();
port = 25
props = new Properties()
props.put('mail.smtp.host', smtpHost)
props.put('mail.smtp.port', port.toString())
session = Session.getDefaultInstance(props, null)
// Construct the message
msg = new MimeMessage(session)
msg.from = new InternetAddress(sender)
msg.sentDate = new Date()
msg.subject = 'Template Test'
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients))
msg.setHeader('Organization', 'i-BP')
msg.setContent(mailBody,
'text/html')
// Send the message
Transport.send(msg)
}
}
}
}