Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-47069] record jar generated by org.apache.felix:maven-bundle…
…-plugin:bundle with both types "jar" and "bundle"
  • Loading branch information
Cyrille Le Clerc committed Oct 2, 2017
1 parent 56088fa commit f0f6ed4
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 1 deletion.
Expand Up @@ -24,6 +24,8 @@

package org.jenkinsci.plugins.pipeline.maven.dao;

import hudson.model.Item;
import hudson.model.Run;
import org.apache.commons.io.IOUtils;
import org.h2.api.ErrorCode;
import org.h2.jdbcx.JdbcConnectionPool;
Expand All @@ -42,7 +44,9 @@
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -434,4 +438,49 @@ public List<String> listDownstreamJobs(@Nonnull String jobFullName, int buildNum

return downstreamJobsFullNames;
}

/**
* List the artifacts generated by the given build
*
* @param jobFullName see {@link Item#getFullName()}
* @param buildNumber see {@link Run#getNumber()}
* @return list of artifact details stored as maps ("gav", "type", "skip_downstream_triggers")
*/
@Nonnull
public List<Map<String, String>> getGeneratedArtifacts(@Nonnull String jobFullName, @Nonnull int buildNumber) {
LOGGER.log(Level.FINER, "getGeneratedArtifacts({0}, {1})", new Object[]{jobFullName, buildNumber});
String generatedArtifactsSql = "SELECT DISTINCT MAVEN_ARTIFACT.*, GENERATED_MAVEN_ARTIFACT.* " +
" FROM MAVEN_ARTIFACT " +
" INNER JOIN GENERATED_MAVEN_ARTIFACT ON MAVEN_ARTIFACT.ID = GENERATED_MAVEN_ARTIFACT.ARTIFACT_ID" +
" INNER JOIN JENKINS_BUILD AS UPSTREAM_BUILD ON GENERATED_MAVEN_ARTIFACT.BUILD_ID = UPSTREAM_BUILD.ID " +
" INNER JOIN JENKINS_JOB AS UPSTREAM_JOB ON UPSTREAM_BUILD.JOB_ID = UPSTREAM_JOB.ID " +
" WHERE " +
" UPSTREAM_JOB.FULL_NAME = ? AND" +
" UPSTREAM_BUILD.NUMBER = ? ";

List<Map<String, String>> results = new ArrayList<>();
try (Connection cnn = this.jdbcConnectionPool.getConnection()) {
PreparedStatement stmt = cnn.prepareStatement(generatedArtifactsSql);
stmt.setString(1, jobFullName);
stmt.setInt(2, buildNumber);
try (ResultSet rst = stmt.executeQuery()) {
while (rst.next()) {
Map<String, String> artifact = new HashMap<>();

String gav =
rst.getString("maven_artifact.group_id") + ":" +
rst.getString("maven_artifact.artifact_id") + ":" +
rst.getString("maven_artifact.version");
artifact.put("gav", gav);
artifact.put("type", rst.getString("maven_artifact.type"));
artifact.put("skip_downstream_triggers", rst.getString("generated_maven_artifact.skip_downstream_triggers"));
results.add(artifact);
}
}
} catch(SQLException e) {
throw new RuntimeSqlException(e);
}

return results;
}
}
Expand Up @@ -177,6 +177,13 @@ protected void recordGeneratedArtifacts(List<MavenSpyLogProcessor.MavenArtifact>
dao.recordGeneratedArtifact(run.getParent().getFullName(), run.getNumber(),
artifact.groupId, artifact.artifactId, artifact.version, artifact.type, artifact.baseVersion,
skipDownstreamPipelines);
if ("bundle".equals(artifact.type) && "jar".equals(artifact.extension)) {
// JENKINS-47069 org.apache.felix:maven-bundle-plugin:bundle uses the type "bundle" for "jar" files
// record artifact as both "bundle" and "jar"
dao.recordGeneratedArtifact(run.getParent().getFullName(), run.getNumber(),
artifact.groupId, artifact.artifactId, artifact.version, "jar", artifact.baseVersion,
skipDownstreamPipelines);
}
}
}

Expand Down
Expand Up @@ -61,6 +61,10 @@ protected void loadMavenJarWithFlattenPomProjectInGitRepo(GitSampleRepoRule gitR
loadSourceCodeInGitRepository(gitRepo, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_jar_with_flatten_pom_project/");
}

protected void loadOsgiBundleProjectInGitRepo(GitSampleRepoRule gitRepo) throws Exception {
loadSourceCodeInGitRepository(gitRepo, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/multi_module_bundle_project/");
}

protected void loadMavenPluginProjectInGitRepo(GitSampleRepoRule gitRepo) throws Exception {
loadSourceCodeInGitRepository(gitRepo, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_plugin_project/");
}
Expand Down
@@ -1,11 +1,17 @@
package org.jenkinsci.plugins.pipeline.maven;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import hudson.model.Cause;
import hudson.model.CauseAction;
import hudson.model.Result;
import jenkins.branch.BranchSource;
import jenkins.plugins.git.GitSCMSource;
import jenkins.plugins.git.GitSampleRepoRule;
import org.hamcrest.Matchers;
import org.jenkinsci.plugins.pipeline.maven.dao.PipelineMavenPluginDao;
import org.jenkinsci.plugins.pipeline.maven.dao.PipelineMavenPluginH2Dao;
import org.jenkinsci.plugins.pipeline.maven.publishers.PipelineGraphPublisher;
import org.jenkinsci.plugins.pipeline.maven.trigger.WorkflowJobDependencyTrigger;
import org.jenkinsci.plugins.pipeline.maven.util.WorkflowMultibranchProjectTestsUtils;
Expand All @@ -19,9 +25,10 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

import javax.inject.Inject;
import javax.annotation.Nullable;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
Expand All @@ -34,8 +41,11 @@ public class DependencyGraphTest extends AbstractIntegrationTest {
@Rule
public GitSampleRepoRule mavenWarRepoRule = new GitSampleRepoRule();

/*
Does not work
@Inject
public GlobalPipelineMavenConfig globalPipelineMavenConfig;
*/

@Before
@Override
Expand Down Expand Up @@ -175,4 +185,57 @@ public void verify_downstream_multi_branch_pipeline_trigger() throws Exception {
assertThat(upstreamCause, notNullValue());

}

@Test
public void verify_osgi_bundle_recorded_as_bundle_and_as_jar() throws Exception {
loadOsgiBundleProjectInGitRepo(gitRepoRule);


String pipelineScript = "node('master') {\n" +
" git($/" + gitRepoRule.toString() + "/$)\n" +
" withMaven() {\n" +
" sh 'mvn package'\n" +
" }\n" +
"}";

// TRIGGER maven-jar#1 to record that "build-maven-jar"
WorkflowJob multiModuleBundleProjectPipeline = jenkinsRule.createProject(WorkflowJob.class, "build-multi-module-bundle");
multiModuleBundleProjectPipeline.setDefinition(new CpsFlowDefinition(pipelineScript, true));
WorkflowRun build = jenkinsRule.assertBuildStatus(Result.SUCCESS, multiModuleBundleProjectPipeline.scheduleBuild2(0));

PipelineMavenPluginDao dao = GlobalPipelineMavenConfig.getDao();
if (!(dao instanceof PipelineMavenPluginH2Dao))
throw new IllegalStateException();

PipelineMavenPluginH2Dao h2Dao = (PipelineMavenPluginH2Dao) dao;
List<Map<String, String>> generatedArtifacts = h2Dao.getGeneratedArtifacts(multiModuleBundleProjectPipeline.getFullName(), build.getNumber());

/*
[{skip_downstream_triggers=TRUE, type=pom, gav=jenkins.mvn.test.bundle:bundle-parent:0.0.1-SNAPSHOT},
{skip_downstream_triggers=TRUE, type=bundle, gav=jenkins.mvn.test.bundle:print-api:0.0.1-SNAPSHOT},
{skip_downstream_triggers=TRUE, type=jar, gav=jenkins.mvn.test.bundle:print-impl:0.0.1-SNAPSHOT},
{skip_downstream_triggers=TRUE, type=jar, gav=jenkins.mvn.test.bundle:print-api:0.0.1-SNAPSHOT},
{skip_downstream_triggers=TRUE, type=pom, gav=jenkins.mvn.test.bundle:print-api:0.0.1-SNAPSHOT},
{skip_downstream_triggers=TRUE, type=pom, gav=jenkins.mvn.test.bundle:print-impl:0.0.1-SNAPSHOT}]
*/
System.out.println("generated artifacts" + generatedArtifacts);

Iterable<Map<String, String>> matchingGeneratedArtifacts =Iterables.filter(generatedArtifacts, new Predicate<Map<String, String>>() {
@Override
public boolean apply(@Nullable Map<String, String> input) {
return input != null && "jenkins.mvn.test.bundle:print-api:0.0.1-SNAPSHOT".equals(input.get("gav"));
}
});

Iterable<String> matchingArtifactTypes = Iterables.transform(matchingGeneratedArtifacts, new Function<Map<String, String>, String>() {
@Override
public String apply(@Nullable Map<String, String> input) {
return input.get("type");
}
});


assertThat(matchingArtifactTypes, Matchers.containsInAnyOrder("jar", "bundle", "pom"));
}
}

0 comments on commit f0f6ed4

Please sign in to comment.