Skip to content

Commit

Permalink
[JENKINS-49632] Also record generated attached artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyrille Le Clerc committed Mar 6, 2018
2 parents 00a6ba4 + abcb33a commit e53f09f
Show file tree
Hide file tree
Showing 7 changed files with 704 additions and 258 deletions.
Expand Up @@ -60,12 +60,7 @@ public void process(@Nonnull StepContext context, @Nonnull Element mavenSpyLogsE
FilePath workspace = context.get(FilePath.class);
final String fileSeparatorOnAgent = XmlUtils.getFileSeparatorOnRemote(workspace);

List<MavenArtifact> mavenArtifacts = listArtifacts(mavenSpyLogsElt);
List<MavenArtifact> attachedMavenArtifacts = listAttachedArtifacts(mavenSpyLogsElt);
List<MavenArtifact> join = new ArrayList<>();
join.addAll(mavenArtifacts);
join.addAll(attachedMavenArtifacts);

List<MavenArtifact> join = XmlUtils.listGeneratedArtifacts(mavenSpyLogsElt, true);

Map<String, String> artifactsToArchive = new HashMap<>(); // artifactPathInArchiveZone -> artifactPathInWorkspace
Map<String, String> artifactsToFingerPrint = new HashMap<>(); // artifactPathInArchiveZone -> artifactMd5
Expand Down Expand Up @@ -146,126 +141,6 @@ public void process(@Nonnull StepContext context, @Nonnull Element mavenSpyLogsE
}
}

/*
<ExecutionEvent type="ProjectSucceeded" class="org.apache.maven.lifecycle.internal.DefaultExecutionEvent" _time="2017-01-31 00:15:42.255">
<project artifactId="pipeline-maven" groupId="org.jenkins-ci.plugins" name="Pipeline Maven Integration Plugin" version="0.6-SNAPSHOT"/>
<no-execution-found/>
<artifact groupId="org.jenkins-ci.plugins" artifactId="pipeline-maven" id="org.jenkins-ci.plugins:pipeline-maven:hpi:0.6-SNAPSHOT" type="hpi" version="0.6-SNAPSHOT">
<file>/Users/cleclerc/git/jenkins/pipeline-maven-plugin/target/pipeline-maven.hpi</file>
</artifact>
<attachedArtifacts>
<artifact groupId="org.jenkins-ci.plugins" artifactId="pipeline-maven" id="org.jenkins-ci.plugins:pipeline-maven:jar:0.6-SNAPSHOT" type="jar" version="0.6-SNAPSHOT">
<file>/Users/cleclerc/git/jenkins/pipeline-maven-plugin/target/pipeline-maven.jar</file>
</artifact>
</attachedArtifacts>
</ExecutionEvent>
*/

/**
* @param mavenSpyLogs Root XML element
* @return list of {@link MavenArtifact}
*/
/*
<artifact artifactId="demo-pom" groupId="com.example" id="com.example:demo-pom:pom:0.0.1-SNAPSHOT" type="pom" version="0.0.1-SNAPSHOT">
<file/>
</artifact>
*/
@Nonnull
public List<MavenArtifact> listArtifacts(Element mavenSpyLogs) {

List<MavenArtifact> result = new ArrayList<>();

for (Element projectSucceededElt : XmlUtils.getExecutionEvents(mavenSpyLogs, "ProjectSucceeded")) {

Element projectElt = XmlUtils.getUniqueChildElement(projectSucceededElt, "project");
MavenArtifact projectArtifact = XmlUtils.newMavenArtifact(projectElt);
MavenArtifact pomArtifact = new MavenArtifact();
pomArtifact.groupId = projectArtifact.groupId;
pomArtifact.artifactId = projectArtifact.artifactId;
pomArtifact.baseVersion = projectArtifact.baseVersion;
pomArtifact.version = projectArtifact.version;
pomArtifact.snapshot = projectArtifact.snapshot;
pomArtifact.type = "pom";
pomArtifact.extension = "pom";
pomArtifact.file = projectElt.getAttribute("file");

result.add(pomArtifact);

Element artifactElt = XmlUtils.getUniqueChildElement(projectSucceededElt, "artifact");
MavenArtifact mavenArtifact = XmlUtils.newMavenArtifact(artifactElt);
if ("pom".equals(mavenArtifact.type)) {
// NO file is generated by Maven for pom projects, skip
continue;
}

Element fileElt = XmlUtils.getUniqueChildElementOrNull(artifactElt, "file");
if (fileElt == null || fileElt.getTextContent() == null || fileElt.getTextContent().isEmpty()) {
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.log(Level.FINE, "listGeneratedArtifacts: Project " + projectArtifact + ": no associated file found for " +
mavenArtifact + " in " + XmlUtils.toString(artifactElt));
}
} else {
mavenArtifact.file = StringUtils.trim(fileElt.getTextContent());
}
result.add(mavenArtifact);
}

return result;
}


/**
* @param mavenSpyLogs Root XML element
* @return list of {@link FilePath#getRemote()}
*/
/*
<ExecutionEvent type="ProjectSucceeded" class="org.apache.maven.lifecycle.internal.DefaultExecutionEvent" _time="2017-01-31 00:15:42.255">
<project artifactId="pipeline-maven" groupId="org.jenkins-ci.plugins" name="Pipeline Maven Integration Plugin" version="0.6-SNAPSHOT"/>
<no-execution-found/>
<artifact groupId="org.jenkins-ci.plugins" artifactId="pipeline-maven" id="org.jenkins-ci.plugins:pipeline-maven:hpi:0.6-SNAPSHOT" type="hpi" version="0.6-SNAPSHOT">
<file>/Users/cleclerc/git/jenkins/pipeline-maven-plugin/target/pipeline-maven.hpi</file>
</artifact>
<attachedArtifacts>
<artifact groupId="org.jenkins-ci.plugins" artifactId="pipeline-maven" id="org.jenkins-ci.plugins:pipeline-maven:jar:0.6-SNAPSHOT" type="jar" version="0.6-SNAPSHOT">
<file>/Users/cleclerc/git/jenkins/pipeline-maven-plugin/target/pipeline-maven.jar</file>
</artifact>
</attachedArtifacts>
</ExecutionEvent>
*/
@Nonnull
public List<MavenArtifact> listAttachedArtifacts(Element mavenSpyLogs) {
List<MavenArtifact> result = new ArrayList<>();

for (Element projectSucceededElt : XmlUtils.getExecutionEvents(mavenSpyLogs, "ProjectSucceeded")) {

Element projectElt = XmlUtils.getUniqueChildElement(projectSucceededElt, "project");
MavenArtifact projectArtifact = XmlUtils.newMavenArtifact(projectElt);

Element attachedArtifactsParentElt = XmlUtils.getUniqueChildElement(projectSucceededElt, "attachedArtifacts");
List<Element> attachedArtifactsElts = XmlUtils.getChildrenElements(attachedArtifactsParentElt, "artifact");
for (Element attachedArtifactElt : attachedArtifactsElts) {
MavenArtifact attachedMavenArtifact = XmlUtils.newMavenArtifact(attachedArtifactElt);

Element fileElt = XmlUtils.getUniqueChildElementOrNull(attachedArtifactElt, "file");
if (fileElt == null || fileElt.getTextContent() == null || fileElt.getTextContent().isEmpty()) {
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.log(Level.FINER, "Project " + projectArtifact + ", no associated file found for attached artifact " +
attachedMavenArtifact + " in " + XmlUtils.toString(attachedArtifactElt));
}
} else {
attachedMavenArtifact.file = StringUtils.trim(fileElt.getTextContent());
}
result.add(attachedMavenArtifact);
}


}
return result;
}



@Symbol("artifactsPublisher")
@Extension public static class DescriptorImpl extends MavenPublisher.DescriptorImpl {
@Nonnull
Expand Down
Expand Up @@ -58,16 +58,16 @@ public Collection<MavenArtifact> getGeneratedArtifacts() {
if (generatedArtifacts == null) {
List<MavenArtifact> generatedArtifacts = GlobalPipelineMavenConfig.getDao().getGeneratedArtifacts(run.getParent().getFullName(), run.getNumber());
if (run.getResult() == null) {
LOGGER.log(Level.FINE, "Load generated artifacts for build {0}#{1} but don't cache them as the build is not finished", new Object[]{run.getParent().getName(), run.getNumber()});
LOGGER.log(Level.FINE, "Load generated artifacts for build {0}#{1} but don't cache them as the build is not finished: {2} entries", new Object[]{run.getParent().getName(), run.getNumber(), generatedArtifacts.size()});
} else {
LOGGER.log(Level.FINE, "Load generated artifacts for build {0}#{1} and cache them", new Object[]{run.getParent().getName(), run.getNumber()});
LOGGER.log(Level.FINE, "Load generated artifacts for build {0}#{1} and cache them: {2} entries", new Object[]{run.getParent().getName(), run.getNumber(), generatedArtifacts.size()});

// build is finished, we can cache the result
this.generatedArtifacts = generatedArtifacts;
}
return generatedArtifacts;
} else {
LOGGER.log(Level.FINE, "Use cached generated artifacts for build {0}#{1}", new Object[]{run.getParent().getName(), run.getNumber()});
LOGGER.log(Level.FINE, "Use cached generated artifacts for build {0}#{1}: {2} entries", new Object[]{run.getParent().getName(), run.getNumber(), generatedArtifacts.size()});
return generatedArtifacts;
}
}
Expand Down
Expand Up @@ -88,7 +88,7 @@ public void process(@Nonnull StepContext context, @Nonnull Element mavenSpyLogsE

List<MavenArtifact> parentProjects = listParentProjects(mavenSpyLogsElt, LOGGER);
List<MavenSpyLogProcessor.MavenDependency> dependencies = listDependencies(mavenSpyLogsElt, LOGGER);
List<MavenArtifact> generatedArtifacts = listGeneratedArtifacts(mavenSpyLogsElt);
List<MavenArtifact> generatedArtifacts = XmlUtils.listGeneratedArtifacts(mavenSpyLogsElt, true);
List<String> executedLifecyclePhases = XmlUtils.getExecutedLifecyclePhases(mavenSpyLogsElt);

recordParentProject(parentProjects, generatedArtifacts, run,listener, dao);
Expand Down Expand Up @@ -241,68 +241,6 @@ protected void recordGeneratedArtifacts(List<MavenArtifact> generatedArtifacts,
}
}

/**
* @param mavenSpyLogs Root XML element
* @return list of {@link MavenArtifact}
*/
/*
<artifact artifactId="demo-pom" groupId="com.example" id="com.example:demo-pom:pom:0.0.1-SNAPSHOT" type="pom" version="0.0.1-SNAPSHOT">
<file/>
</artifact>
*/
@Nonnull
public List<MavenArtifact> listGeneratedArtifacts(Element mavenSpyLogs) {

List<MavenArtifact> result = new ArrayList<>();

List<Element> artifactDeployedEvents = XmlUtils.getArtifactDeployedEvents(mavenSpyLogs);

for (Element projectSucceededElt : XmlUtils.getExecutionEvents(mavenSpyLogs, "ProjectSucceeded")) {

Element projectElt = XmlUtils.getUniqueChildElement(projectSucceededElt, "project");
MavenArtifact projectArtifact = XmlUtils.newMavenArtifact(projectElt);

MavenArtifact pomArtifact = new MavenArtifact();
pomArtifact.groupId = projectArtifact.groupId;
pomArtifact.artifactId = projectArtifact.artifactId;
pomArtifact.baseVersion = projectArtifact.baseVersion;
pomArtifact.version = projectArtifact.version;
pomArtifact.snapshot = projectArtifact.snapshot;
pomArtifact.type = "pom";
pomArtifact.extension = "pom";
pomArtifact.file = projectElt.getAttribute("file");

result.add(pomArtifact);

Element artifactElt = XmlUtils.getUniqueChildElement(projectSucceededElt, "artifact");
MavenArtifact mavenArtifact = XmlUtils.newMavenArtifact(artifactElt);
if ("pom".equals(mavenArtifact.type)) {
// NO file is generated by Maven for pom projects, skip
continue;
}

Element fileElt = XmlUtils.getUniqueChildElementOrNull(artifactElt, "file");
if (fileElt == null || fileElt.getTextContent() == null || fileElt.getTextContent().isEmpty()) {
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.log(Level.FINE, "listGeneratedArtifacts: Project " + projectArtifact + ": no associated file found for " +
mavenArtifact + " in " + XmlUtils.toString(artifactElt));
}
} else {
mavenArtifact.file = StringUtils.trim(fileElt.getTextContent());
Element artifactDeployedEvent = XmlUtils.getArtifactDeployedEvent(artifactDeployedEvents, mavenArtifact.file);
if(artifactDeployedEvent == null) {
// artifact has not been deployed ("mvn deploy")
} else {
mavenArtifact.repositoryUrl = XmlUtils.getUniqueChildElement(artifactDeployedEvent, "repository").getAttribute("url");
}
}
result.add(mavenArtifact);
}

return result;
}


@Override
public String toString() {
return getClass().getName() + "[" +
Expand Down
Expand Up @@ -478,4 +478,85 @@ public static String join(@NonNull Iterable<String> elements, @NonNull String de
}
return result.toString();
}


@Nonnull
public static List<MavenArtifact> listGeneratedArtifacts(Element mavenSpyLogs, boolean includeAttachedArtifacts) {

List<Element> artifactDeployedEvents = XmlUtils.getArtifactDeployedEvents(mavenSpyLogs);

List<MavenArtifact> result = new ArrayList<>();

for (Element projectSucceededElt : XmlUtils.getExecutionEvents(mavenSpyLogs, "ProjectSucceeded")) {

Element projectElt = XmlUtils.getUniqueChildElement(projectSucceededElt, "project");
MavenArtifact projectArtifact = XmlUtils.newMavenArtifact(projectElt);
MavenArtifact pomArtifact = new MavenArtifact();
pomArtifact.groupId = projectArtifact.groupId;
pomArtifact.artifactId = projectArtifact.artifactId;
pomArtifact.baseVersion = projectArtifact.baseVersion;
pomArtifact.version = projectArtifact.version;
pomArtifact.snapshot = projectArtifact.snapshot;
pomArtifact.type = "pom";
pomArtifact.extension = "pom";
pomArtifact.file = projectElt.getAttribute("file");

result.add(pomArtifact);

Element artifactElt = XmlUtils.getUniqueChildElement(projectSucceededElt, "artifact");
MavenArtifact mavenArtifact = XmlUtils.newMavenArtifact(artifactElt);
if ("pom".equals(mavenArtifact.type)) {
// NO file is generated by Maven for pom projects, skip
continue;
}

{
Element fileElt = XmlUtils.getUniqueChildElementOrNull(artifactElt, "file");
if (fileElt == null || fileElt.getTextContent() == null || fileElt.getTextContent().isEmpty()) {
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.log(Level.FINE, "listGeneratedArtifacts: Project " + projectArtifact + ": no associated file found for " +
mavenArtifact + " in " + XmlUtils.toString(artifactElt));
}
} else {
mavenArtifact.file = StringUtils.trim(fileElt.getTextContent());

Element artifactDeployedEvent = XmlUtils.getArtifactDeployedEvent(artifactDeployedEvents, mavenArtifact.file);
if(artifactDeployedEvent == null) {
// artifact has not been deployed ("mvn deploy")
} else {
mavenArtifact.repositoryUrl = XmlUtils.getUniqueChildElement(artifactDeployedEvent, "repository").getAttribute("url");
}
}
}
result.add(mavenArtifact);
if (includeAttachedArtifacts) {
Element attachedArtifactsParentElt = XmlUtils.getUniqueChildElement(projectSucceededElt, "attachedArtifacts");
List<Element> attachedArtifactsElts = XmlUtils.getChildrenElements(attachedArtifactsParentElt, "artifact");
for (Element attachedArtifactElt : attachedArtifactsElts) {
MavenArtifact attachedMavenArtifact = XmlUtils.newMavenArtifact(attachedArtifactElt);

Element fileElt = XmlUtils.getUniqueChildElementOrNull(attachedArtifactElt, "file");
if (fileElt == null || fileElt.getTextContent() == null || fileElt.getTextContent().isEmpty()) {
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.log(Level.FINER, "Project " + projectArtifact + ", no associated file found for attached artifact " +
attachedMavenArtifact + " in " + XmlUtils.toString(attachedArtifactElt));
}
} else {
attachedMavenArtifact.file = StringUtils.trim(fileElt.getTextContent());

Element attachedArtifactDeployedEvent = XmlUtils.getArtifactDeployedEvent(artifactDeployedEvents, attachedMavenArtifact.file);
if(attachedArtifactDeployedEvent == null) {
// artifact has not been deployed ("mvn deploy")
} else {
attachedMavenArtifact.repositoryUrl = XmlUtils.getUniqueChildElement(attachedArtifactDeployedEvent, "repository").getAttribute("url");
}

}
result.add(attachedMavenArtifact);
}
}
}

return result;
}
}

0 comments on commit e53f09f

Please sign in to comment.