Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API to supersede components if not required #601

Merged
merged 12 commits into from
Jan 2, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,10 @@ private static List<Content> appendManifestContents(
ContentContainer contentsContainer = new ContentContainer(contentFilter, components);
for (Component component : components) {
try {
if (components.stream().anyMatch(c -> c.supersedes(component))) {
continue;
}

manifest.append(" * ").append(component.getDisplayName()).append("\n\n");
LOGGER.log(Level.FINE, "Start processing " + component.getDisplayName());
long startTime = System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ public ComponentCategory getCategory() {
return ComponentCategory.UNCATEGORIZED;
}

/**
* Returns true if a component is superseded by this component.
* useful if we write a component that makes another one obsolete.
*/
public boolean supersedes(Component component) {
return false;
}

/**
* Categories supported by this version of support-core
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,61 @@ public long getTime() throws IOException {
assertNotNull(zip.getEntry("about.md"));
assertNotNull(zip.getEntry("nodes.md"));
}

/**
* Test that a component can supersede another component.
* We are superseding AboutJenkins and BuildQueue components.
* even if is added to the list of components to create.
* It will not add its files to the bundle
*/
@Test
public void testSupersedesComponent() throws Exception {
List<Component> components = Arrays.asList(
new Component() {
@NonNull
@Override
public Set<Permission> getRequiredPermissions() {
return Collections.singleton(Jenkins.ADMINISTER);
}

@NonNull
@Override
public String getDisplayName() {
return "Test Component";
}

@Override
public boolean supersedes(Component component) {
return Set.of(AboutJenkins.class, BuildQueue.class).contains(component.getClass());
}

@Override
public void addContents(@NonNull Container container) {
container.add(new Content("test/testWriteBundleWithJenkinsRule.md") {
@Override
public void writeTo(OutputStream os) throws IOException {
os.write("test content".getBytes(StandardCharsets.UTF_8));
}
});
}
},
ExtensionList.lookup(Component.class).get(AboutJenkins.class),
ExtensionList.lookup(Component.class).get(BuildQueue.class),
ExtensionList.lookup(Component.class).get(SystemProperties.class));

File bundleFile = temp.newFile();
try (OutputStream os = Files.newOutputStream(bundleFile.toPath())) {
SupportPlugin.writeBundle(os, components);
}

ZipFile zip = new ZipFile(bundleFile);
assertNotNull(zip.getEntry("test/testWriteBundleWithJenkinsRule.md"));
assertNotNull(zip.getEntry("manifest.md"));
assertNotNull(zip.getEntry("nodes/master/system.properties"));

// Assert null for AboutJenkins.class, BuildQueue.class components
assertNull(zip.getEntry("buildqueue.md"));
assertNull(zip.getEntry("about.md"));
assertNull(zip.getEntry("nodes.md"));
}
}
Loading