From f1d9d9c218cb0cffa7d6e4467e7acc4adb35f2d1 Mon Sep 17 00:00:00 2001 From: Adrien Lecharpentier Date: Tue, 16 Jan 2024 16:31:10 +0100 Subject: [PATCH] Better detect dependency bot configuration file in plugin repository (#443) --- ...stractDependencyBotConfigurationProbe.java | 10 +++++++++- .../scoring/probes/DependabotProbe.java | 7 ++++++- .../scoring/probes/RenovateProbe.java | 7 ++++++- .../scoring/scores/AdoptionScoring.java | 2 +- .../scoring/probes/DependabotProbeTest.java | 19 +++++++++++++++++++ 5 files changed, 41 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/io/jenkins/pluginhealth/scoring/probes/AbstractDependencyBotConfigurationProbe.java b/core/src/main/java/io/jenkins/pluginhealth/scoring/probes/AbstractDependencyBotConfigurationProbe.java index db40d71c5..af9779563 100644 --- a/core/src/main/java/io/jenkins/pluginhealth/scoring/probes/AbstractDependencyBotConfigurationProbe.java +++ b/core/src/main/java/io/jenkins/pluginhealth/scoring/probes/AbstractDependencyBotConfigurationProbe.java @@ -55,7 +55,7 @@ protected ProbeResult doApply(Plugin plugin, ProbeContext context) { } try (Stream paths = Files.find(githubConfig, 1, (path, $) -> - Files.isRegularFile(path) && path.getFileName().toString().startsWith(getBotName()))) { + Files.isRegularFile(path) && isPathBotConfigFile(path.getFileName().toString()))) { return paths.findFirst() .map(file -> this.success(String.format("%s is configured.", capitalize(getBotName())))) .orElseGet(() -> this.success(String.format("%s is not configured.", capitalize(getBotName())))); @@ -77,6 +77,14 @@ private String capitalize(String str) { */ protected abstract String getBotName(); + /** + * Tests if the provided filename is the same as the bot configuration filename. + * + * @param filename the path + * @return true if the filename is the same as the required bot configuration filename. + */ + protected abstract boolean isPathBotConfigFile(String filename); + @Override protected boolean isSourceCodeRelated() { return true; diff --git a/core/src/main/java/io/jenkins/pluginhealth/scoring/probes/DependabotProbe.java b/core/src/main/java/io/jenkins/pluginhealth/scoring/probes/DependabotProbe.java index 7ed799a44..44a137547 100644 --- a/core/src/main/java/io/jenkins/pluginhealth/scoring/probes/DependabotProbe.java +++ b/core/src/main/java/io/jenkins/pluginhealth/scoring/probes/DependabotProbe.java @@ -40,6 +40,11 @@ protected String getBotName() { return KEY; } + @Override + protected boolean isPathBotConfigFile(String filename) { + return "dependabot.yml".equals(filename) || "dependabot.yaml".equals(filename); + } + @Override public String key() { return KEY; @@ -52,6 +57,6 @@ public String getDescription() { @Override public long getVersion() { - return 1; + return 2; } } diff --git a/core/src/main/java/io/jenkins/pluginhealth/scoring/probes/RenovateProbe.java b/core/src/main/java/io/jenkins/pluginhealth/scoring/probes/RenovateProbe.java index ae2033f8b..5494d69b0 100644 --- a/core/src/main/java/io/jenkins/pluginhealth/scoring/probes/RenovateProbe.java +++ b/core/src/main/java/io/jenkins/pluginhealth/scoring/probes/RenovateProbe.java @@ -16,6 +16,11 @@ protected String getBotName() { return KEY; } + @Override + protected boolean isPathBotConfigFile(String filename) { + return "renovate.json".equals(filename); + } + @Override public String key() { return KEY; @@ -28,6 +33,6 @@ public String getDescription() { @Override public long getVersion() { - return 1; + return 2; } } diff --git a/core/src/main/java/io/jenkins/pluginhealth/scoring/scores/AdoptionScoring.java b/core/src/main/java/io/jenkins/pluginhealth/scoring/scores/AdoptionScoring.java index 80597adbb..252447d02 100644 --- a/core/src/main/java/io/jenkins/pluginhealth/scoring/scores/AdoptionScoring.java +++ b/core/src/main/java/io/jenkins/pluginhealth/scoring/scores/AdoptionScoring.java @@ -142,6 +142,6 @@ public String description() { @Override public int version() { - return 1; + return 2; } } diff --git a/core/src/test/java/io/jenkins/pluginhealth/scoring/probes/DependabotProbeTest.java b/core/src/test/java/io/jenkins/pluginhealth/scoring/probes/DependabotProbeTest.java index 9f910eead..a7209cae8 100644 --- a/core/src/test/java/io/jenkins/pluginhealth/scoring/probes/DependabotProbeTest.java +++ b/core/src/test/java/io/jenkins/pluginhealth/scoring/probes/DependabotProbeTest.java @@ -124,4 +124,23 @@ void shouldDetectDependabotFile() throws Exception { .isEqualTo(ProbeResult.success(DependabotProbe.KEY, "Dependabot is configured.", probe.getVersion())); verify(probe).doApply(any(Plugin.class), any(ProbeContext.class)); } + + @Test + void shouldDetectDependabotFileWithFullFileExtension() throws Exception { + final Plugin plugin = mock(Plugin.class); + final ProbeContext ctx = mock(ProbeContext.class); + final DependabotProbe probe = getSpy(); + + final Path repo = Files.createTempDirectory("foo"); + final Path github = Files.createDirectories(repo.resolve(".github")); + + Files.createFile(github.resolve("dependabot.yaml")); + when(ctx.getScmRepository()).thenReturn(Optional.of(repo)); + + assertThat(probe.apply(plugin, ctx)) + .usingRecursiveComparison() + .comparingOnlyFields("id", "message", "status") + .isEqualTo(ProbeResult.success(DependabotProbe.KEY, "Dependabot is configured.", probe.getVersion())); + verify(probe).doApply(any(Plugin.class), any(ProbeContext.class)); + } }