Skip to content

Commit

Permalink
Makes sure external downloads happen only once (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
alecharp authored Oct 12, 2023
1 parent 639d92f commit 46203cc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package io.jenkins.pluginhealth.scoring.probes;

import java.io.IOException;
import java.util.Map;

import io.jenkins.pluginhealth.scoring.model.Plugin;
import io.jenkins.pluginhealth.scoring.model.ProbeResult;
Expand Down Expand Up @@ -70,8 +71,9 @@ public ProbeEngine(ProbeService probeService, PluginService pluginService, Updat
public void run() throws IOException {
LOGGER.info("Start running probes on all plugins");
final UpdateCenter updateCenter = updateCenterService.fetchUpdateCenter();
final Map<String, String> pluginDocumentationUrl = pluginDocumentationService.fetchPluginDocumentationUrl();
pluginService.streamAll().parallel()
.forEach(plugin -> this.runOn(plugin, updateCenter));
.forEach(plugin -> this.runOn(plugin, updateCenter, pluginDocumentationUrl));
LOGGER.info("Probe engine has finished");
}

Expand All @@ -84,11 +86,12 @@ public void run() throws IOException {
public void runOn(Plugin plugin) throws IOException {
LOGGER.info("Start running probes on {}", plugin.getName());
final UpdateCenter updateCenter = updateCenterService.fetchUpdateCenter();
runOn(plugin, updateCenter);
final Map<String, String> pluginDocumentationUrl = pluginDocumentationService.fetchPluginDocumentationUrl();
runOn(plugin, updateCenter, pluginDocumentationUrl);
LOGGER.info("Probe engine has finished");
}

private void runOn(Plugin plugin, UpdateCenter updateCenter) {
private void runOn(Plugin plugin, UpdateCenter updateCenter, Map<String, String> pluginDocumentationUrl) {
if (plugin.getScm() == null || plugin.getScm().isBlank()) {
LOGGER.info("Will not run probes on {} because its SCM is not set correctly.", plugin.getName());
return;
Expand All @@ -102,7 +105,7 @@ private void runOn(Plugin plugin, UpdateCenter updateCenter) {
}

probeContext.setGitHub(gitHub);
probeContext.setPluginDocumentationLinks(pluginDocumentationService.fetchPluginDocumentationUrl());
probeContext.setPluginDocumentationLinks(pluginDocumentationUrl);
probeContext.cloneRepository();

probeService.getProbes().forEach(probe -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,28 @@ protected boolean isSourceCodeRelated() {
verify(probe).doApply(plugin, ctx);
verify(plugin).addDetails(newProbeResult);
}

@Test
void shouldRequestUpdateCenterAndDocumentationUrlOnce() throws IOException {
final Plugin p1 = mock(Plugin.class);
final Plugin p2 = mock(Plugin.class);
final Probe probe = mock(Probe.class);
final ProbeContext ctx = mock(ProbeContext.class);

when(p1.getScm()).thenReturn("this-is-ok");
when(p2.getScm()).thenReturn("this-is-ok");

when(probe.apply(p1, ctx)).thenReturn(ProbeResult.success("foo", "this is fine", 1));
when(probe.apply(p2, ctx)).thenReturn(ProbeResult.success("foo", "this is ok too", 1));

when(probeService.getProbes()).thenReturn(List.of(probe));
when(probeService.getProbeContext(any(Plugin.class), any(UpdateCenter.class))).thenReturn(ctx);
when(pluginService.streamAll()).thenReturn(Stream.of(p1, p2));

final ProbeEngine probeEngine = new ProbeEngine(probeService, pluginService, updateCenterService, gitHub, pluginDocumentationService);
probeEngine.run();

verify(pluginDocumentationService).fetchPluginDocumentationUrl();
verify(updateCenterService).fetchUpdateCenter();
}
}

0 comments on commit 46203cc

Please sign in to comment.