From 46203ccc2f44014d83ec8eea2a1d3544e70a3949 Mon Sep 17 00:00:00 2001 From: Adrien Lecharpentier Date: Thu, 12 Oct 2023 15:01:57 +0200 Subject: [PATCH] Makes sure external downloads happen only once (#386) --- .../scoring/probes/ProbeEngine.java | 11 +++++---- .../scoring/probes/ProbeEngineTest.java | 24 +++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/war/src/main/java/io/jenkins/pluginhealth/scoring/probes/ProbeEngine.java b/war/src/main/java/io/jenkins/pluginhealth/scoring/probes/ProbeEngine.java index c46de8cba..f24e1efa0 100644 --- a/war/src/main/java/io/jenkins/pluginhealth/scoring/probes/ProbeEngine.java +++ b/war/src/main/java/io/jenkins/pluginhealth/scoring/probes/ProbeEngine.java @@ -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; @@ -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 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"); } @@ -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 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 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; @@ -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 -> { diff --git a/war/src/test/java/io/jenkins/pluginhealth/scoring/probes/ProbeEngineTest.java b/war/src/test/java/io/jenkins/pluginhealth/scoring/probes/ProbeEngineTest.java index ddf9afd83..749db8492 100644 --- a/war/src/test/java/io/jenkins/pluginhealth/scoring/probes/ProbeEngineTest.java +++ b/war/src/test/java/io/jenkins/pluginhealth/scoring/probes/ProbeEngineTest.java @@ -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(); + } }