Skip to content

Commit

Permalink
Codeowner probe using bad repository name (#558)
Browse files Browse the repository at this point in the history
  • Loading branch information
alecharp authored Oct 18, 2024
1 parent ffb3bf4 commit e498182
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ protected ProbeResult doApply(Plugin plugin, ProbeContext context) {
return this.error("There is no local repository for plugin " + plugin.getName() + ".");
}
final Path scmRepository = context.getScmRepository().get();
final String repositoryName = context.getRepositoryName().orElse("NOT_VALID");

try (Stream<Path> paths = Files.find(
scmRepository,
Expand All @@ -59,9 +60,7 @@ protected ProbeResult doApply(Plugin plugin, ProbeContext context) {
.map(file -> {
try {
return Files.readAllLines(file).stream()
.anyMatch(line -> line.contains("@jenkinsci/%s-developers"
.formatted(context.getRepositoryName()
.orElse("NOT_VALID"))))
.anyMatch(line -> line.contains("@%s-developers".formatted(repositoryName)))
? this.success("CODEOWNERS file is valid.")
: this.success("CODEOWNERS file is not set correctly.");
} catch (IOException ex) {
Expand All @@ -87,7 +86,7 @@ public String getDescription() {

@Override
public long getVersion() {
return 2;
return 3;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Jenkins Infra
* Copyright (c) 2023-2024 Jenkins Infra
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -21,7 +21,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.jenkins.pluginhealth.scoring.probes;

import java.io.File;
Expand Down Expand Up @@ -76,8 +75,12 @@ public void cloneRepository() {
final String pluginName = this.plugin.getName();
try {
final Path repo = Files.createTempDirectory(pluginName);
try (Git git = Git.cloneRepository().setURI(plugin.getScm()).setDirectory(repo.toFile()).call()) {
this.scmRepository = Paths.get(git.getRepository().getDirectory().getParentFile().toURI());
try (Git git = Git.cloneRepository()
.setURI(plugin.getScm())
.setDirectory(repo.toFile())
.call()) {
this.scmRepository = Paths.get(
git.getRepository().getDirectory().getParentFile().toURI());
} catch (GitAPIException e) {
LOGGER.warn("Could not clone Git repository for plugin {}", pluginName, e);
}
Expand Down Expand Up @@ -114,6 +117,13 @@ public Map<String, String> getPluginDocumentationLinks() {
return pluginDocumentationLinks;
}

/**
* Returns the GitHub repository of the plugin source code.
* This needs to be in the format 'organization/repository' for the GitHub.
* Returns empty if the scm link of the plugin is not set to point to jenkinsci organization.
*
* @return the GitHub repository of the plugin source code or empty if not formatted correctly
*/
public Optional<String> getRepositoryName() {
if (plugin.getScm() == null || plugin.getScm().isBlank()) {
return Optional.empty();
Expand All @@ -133,9 +143,7 @@ public void setScmFolderPath(Path scmFolderPath) {
/* default */ void cleanUp() throws IOException {
if (scmRepository != null) {
try (Stream<Path> paths = Files.walk(this.scmRepository)) {
paths.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
paths.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void shouldDetectCodeOwnershipFileWithInvalidContent() throws IOException {
final Plugin plugin = mock(Plugin.class);
final ProbeContext ctx = mock(ProbeContext.class);

when(ctx.getRepositoryName()).thenReturn(Optional.of("new-super-plugin"));
when(ctx.getRepositoryName()).thenReturn(Optional.of("jenkinsci/new-super-plugin"));

{
final Path repo = Files.createTempDirectory(getClass().getName());
Expand Down Expand Up @@ -137,7 +137,7 @@ void shouldDetectCodeOwnershipFileWithValidTeam() throws IOException {
final Plugin plugin = mock(Plugin.class);
final ProbeContext ctx = mock(ProbeContext.class);

when(ctx.getRepositoryName()).thenReturn(Optional.of("sample-plugin"));
when(ctx.getRepositoryName()).thenReturn(Optional.of("jenkinsci/sample-plugin"));

{
final Path repo = Files.createTempDirectory(getClass().getName());
Expand Down Expand Up @@ -190,7 +190,7 @@ void shouldDetectCodeOwnershipFileWithValidTeamAndMore() throws IOException {
final Plugin plugin = mock(Plugin.class);
final ProbeContext ctx = mock(ProbeContext.class);

when(ctx.getRepositoryName()).thenReturn(Optional.of("sample-plugin"));
when(ctx.getRepositoryName()).thenReturn(Optional.of("jenkinsci/sample-plugin"));

{
final Path repo = Files.createTempDirectory(getClass().getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* MIT License
*
* Copyright (c) 2024 Jenkins Infra
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.jenkins.pluginhealth.scoring.probes;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.Optional;

import io.jenkins.pluginhealth.scoring.model.Plugin;
import io.jenkins.pluginhealth.scoring.model.updatecenter.UpdateCenter;

import org.junit.jupiter.api.Test;

public class ProbeContextTest {
@Test
void shouldBeAbleToReturnCorrectPluginRepositoryName() throws Exception {
final Plugin plugin = mock(Plugin.class);
final UpdateCenter uc = mock(UpdateCenter.class);

when(plugin.getScm()).thenReturn("https://github.com/jenkinsci/git-client-plugin");

final ProbeContext ctx = new ProbeContext(plugin, uc);
assertThat(ctx.getRepositoryName()).isEqualTo(Optional.of("jenkinsci/git-client-plugin"));
}
}

0 comments on commit e498182

Please sign in to comment.