Skip to content

Commit

Permalink
Remove workarounds introduced in jenkinsci#510
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed Aug 28, 2024
1 parent 6ad0a63 commit 4688a33
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 180 deletions.
2 changes: 0 additions & 2 deletions src/main/java/org/jenkins/tools/test/PluginCompatTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ public void testPlugins() throws PluginCompatibilityTesterException {

// Sanity check all plugins in the repository come from the same hash/tag
for (List<Plugin> pluginList : pluginsByRepository.values()) {
// the legacy extractors set the hash to the tag - so we only need to check the hash to cover modern and
// legacy
Set<String> hashes = pluginList.stream().map(Plugin::getGitHash).collect(Collectors.toSet());
if (hashes.size() != 1) {
throw new IllegalArgumentException("Repository "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public boolean check(@NonNull BeforeCheckoutContext context) {
justification = "We are not used Git SHA comparisons for security")
@Override
public void action(@NonNull BeforeCheckoutContext context) throws PluginSourcesUnavailableException {
// legacy extractors set the gitHash to the tag - so we just need to check the tag.
String gitHash = context.getPlugin().getGitHash();
if (gitHash == null || gitHash.equals("HEAD")) {
throw new PluginSourcesUnavailableException("Failed to check out plugin sources for "
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
package org.jenkins.tools.test.model.plugin_metadata;

import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import org.apache.maven.model.Model;
import org.jenkins.tools.test.exception.MetadataExtractionException;

/**
* Extractor that obtains all the information from the plugin's manifest file.
* This requires the plugin to have been built with {@code maven-hpi-plugin} 3.42 or newer.
*
* @author jnord
*/
public interface PluginMetadataExtractor {
public final class PluginMetadataExtractor {

/**
* Determine whether the extractor is applicable to the given plugin.
*/
boolean isApplicable(String pluginId, Manifest manifest, Model model);
private static final Attributes.Name PLUGIN_SCM_CONNECTION = new Attributes.Name("Plugin-ScmConnection");
private static final Attributes.Name PLUGIN_SCM_TAG = new Attributes.Name("Plugin-ScmTag");
private static final Attributes.Name PLUGIN_ID = new Attributes.Name("Short-Name");
private static final Attributes.Name PLUGIN_NAME = new Attributes.Name("Long-Name");
private static final Attributes.Name PLUGIN_VERSION = new Attributes.Name("Plugin-Version");
private static final Attributes.Name IMPLEMENTATION_BUILD = new Attributes.Name("Implementation-Build");

// Suppress default constructor for noninstantiability
private PluginMetadataExtractor() {
throw new AssertionError();
}

/**
* Obtain the metadata for a give plugin from a jar file.
Expand All @@ -21,5 +33,22 @@ public interface PluginMetadataExtractor {
* @param model the plugins' model (from the HPI).
* @return a fully populated {@link Plugin} for the given plugin.
*/
Plugin extractMetadata(String pluginId, Manifest manifest, Model model) throws MetadataExtractionException;
@NonNull
public static Plugin extractMetadata(String pluginId, Manifest manifest, Model model)
throws MetadataExtractionException {
// All the information is stored in the plugin's manifest
Attributes mainAttributes = manifest.getMainAttributes();

assert pluginId.equals(mainAttributes.getValue(PLUGIN_ID));

return new Plugin.Builder()
.withPluginId(mainAttributes.getValue(PLUGIN_ID))
.withName(mainAttributes.getValue(PLUGIN_NAME))
.withScmConnection(mainAttributes.getValue(PLUGIN_SCM_CONNECTION))
.withTag(mainAttributes.getValue(PLUGIN_SCM_TAG))
.withGitHash(mainAttributes.getValue(IMPLEMENTATION_BUILD))
.withModule(":" + mainAttributes.getValue(PLUGIN_ID))
.withVersion(mainAttributes.getValue(PLUGIN_VERSION))
.build();
}
}
12 changes: 1 addition & 11 deletions src/main/java/org/jenkins/tools/test/util/WarExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ public class WarExtractor {
@NonNull
private final File warFile;

@NonNull
private final List<PluginMetadataExtractor> extractors;

@CheckForNull
private final Set<String> includedPlugins;

Expand All @@ -47,7 +44,6 @@ public class WarExtractor {
public WarExtractor(
File warFile, ServiceHelper serviceHelper, Set<String> includedPlugins, Set<String> excludedPlugins) {
this.warFile = warFile;
this.extractors = serviceHelper.loadServices(PluginMetadataExtractor.class);
this.includedPlugins = includedPlugins;
this.excludedPlugins = excludedPlugins;
}
Expand Down Expand Up @@ -140,14 +136,8 @@ private Plugin getPlugin(JarFile jf, JarEntry entry) throws MetadataExtractionEx
} catch (IOException e) {
throw new UncheckedIOException(e);
}
// Once all plugins have adopted https://github.com/jenkinsci/maven-hpi-plugin/pull/436 this can be simplified
LOGGER.log(Level.INFO, "Extracting metadata for {0}", pluginId);
for (PluginMetadataExtractor extractor : extractors) {
if (extractor.isApplicable(pluginId, manifest, model)) {
return extractor.extractMetadata(pluginId, manifest, model);
}
}
throw new MetadataExtractionException("No metadata could be extracted for entry " + entry.getName());
return PluginMetadataExtractor.extractMetadata(pluginId, manifest, model);
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.jenkins.tools.test.model.plugin_metadata;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.io.InputStream;
import java.util.jar.Manifest;
import org.junit.jupiter.api.Test;

class PluginMetadataExtractorTest {

@Test
void extractMetadata() throws Exception {
try (InputStream resourceAsStream =
PluginMetadataExtractorTest.class.getResourceAsStream("PluginMetadataExtractorTest/MANIFEST.MF")) {
assertNotNull(resourceAsStream);
Manifest manifest = new Manifest(resourceAsStream);
Plugin plugin = PluginMetadataExtractor.extractMetadata("aws-java-sdk-ec2", manifest, null);
assertThat(
plugin,
allOf(
hasProperty("pluginId", is("aws-java-sdk-ec2")),
hasProperty("gitUrl", is("https://github.com/jenkinsci/aws-java-sdk-plugin.git")),
hasProperty("module", is(":aws-java-sdk-ec2")),
hasProperty("gitHash", is("938ad577f750694635f3c0160ac2110db5d6eb98")),
hasProperty("name", is("Amazon Web Services SDK :: EC2")),
hasProperty("version", startsWith("1.12.406-373.v59d2b_d41281b_"))));
}
}
}

This file was deleted.

0 comments on commit 4688a33

Please sign in to comment.