-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detects if a plugin repository is archived or not (#572)
- Loading branch information
Showing
5 changed files
with
334 additions
and
43 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
core/src/main/java/io/jenkins/pluginhealth/scoring/probes/RepositoryArchivedStatusProbe.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* 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 java.io.IOException; | ||
import java.util.Optional; | ||
|
||
import io.jenkins.pluginhealth.scoring.model.Plugin; | ||
import io.jenkins.pluginhealth.scoring.model.ProbeResult; | ||
|
||
import org.kohsuke.github.GHRepository; | ||
import org.kohsuke.github.GitHub; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Order(value = RepositoryArchivedStatusProbe.ORDER) | ||
public class RepositoryArchivedStatusProbe extends Probe { | ||
public static final int ORDER = SCMLinkValidationProbe.ORDER + 100; | ||
public static final String KEY = "repository-archived"; | ||
|
||
@Override | ||
protected ProbeResult doApply(Plugin plugin, ProbeContext context) { | ||
if (plugin.getScm() == null) { | ||
return this.error("Plugin SCM is unknown, cannot fetch the number of open pull requests."); | ||
} | ||
final GitHub gh = context.getGitHub(); | ||
final Optional<String> repositoryName = context.getRepositoryName(); | ||
if (repositoryName.isEmpty()) { | ||
return this.error("Cannot find repository for " + plugin.getName()); | ||
} | ||
|
||
try { | ||
final GHRepository repository = gh.getRepository(repositoryName.get()); | ||
return this.success("%b".formatted(repository.isArchived())); | ||
} catch (IOException e) { | ||
return this.error("Cannot access repository " + repositoryName.get()); | ||
} | ||
} | ||
|
||
@Override | ||
public String key() { | ||
return KEY; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Learn if the plugin repository is archived or not."; | ||
} | ||
|
||
@Override | ||
public long getVersion() { | ||
return 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
...c/test/java/io/jenkins/pluginhealth/scoring/probes/RepositoryArchivedStatusProbeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/* | ||
* 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.AssertionsForClassTypes.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.Optional; | ||
|
||
import io.jenkins.pluginhealth.scoring.model.Plugin; | ||
import io.jenkins.pluginhealth.scoring.model.ProbeResult; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.kohsuke.github.GHRepository; | ||
import org.kohsuke.github.GitHub; | ||
|
||
class RepositoryArchivedStatusProbeTest extends AbstractProbeTest<RepositoryArchivedStatusProbe> { | ||
|
||
@Override | ||
RepositoryArchivedStatusProbe getSpy() { | ||
return spy(RepositoryArchivedStatusProbe.class); | ||
} | ||
|
||
@Test | ||
void shouldNotRequireRelease() { | ||
assertFalse(getSpy().requiresRelease()); | ||
} | ||
|
||
@Test | ||
void shouldNotBeRelatedToSourceCode() { | ||
assertFalse(getSpy().requiresRelease()); | ||
} | ||
|
||
@Test | ||
void shouldFailWithNoSCM() { | ||
final Plugin pl = mock(Plugin.class); | ||
final ProbeContext ctx = mock(ProbeContext.class); | ||
|
||
final RepositoryArchivedStatusProbe probe = getSpy(); | ||
|
||
assertThat(probe.apply(pl, ctx)) | ||
.usingRecursiveComparison() | ||
.comparingOnlyFields("id", "status", "message") | ||
.isEqualTo(ProbeResult.error( | ||
RepositoryArchivedStatusProbe.KEY, | ||
"Plugin SCM is unknown, cannot fetch the number of open pull requests.", | ||
1)); | ||
} | ||
|
||
@Test | ||
void shouldFailWithNoRepositoryName() { | ||
final Plugin pl = mock(Plugin.class); | ||
final ProbeContext ctx = mock(ProbeContext.class); | ||
|
||
when(pl.getName()).thenReturn("_test_"); | ||
when(pl.getScm()).thenReturn("valid-url"); | ||
when(ctx.getRepositoryName()).thenReturn(Optional.empty()); | ||
|
||
final RepositoryArchivedStatusProbe probe = getSpy(); | ||
|
||
assertThat(probe.apply(pl, ctx)) | ||
.usingRecursiveComparison() | ||
.comparingOnlyFields("id", "status", "message") | ||
.isEqualTo( | ||
ProbeResult.error(RepositoryArchivedStatusProbe.KEY, "Cannot find repository for _test_", 1)); | ||
} | ||
|
||
@Test | ||
void shouldSucceedToFindArchivedRepository() throws Exception { | ||
final Plugin pl = mock(Plugin.class); | ||
final ProbeContext ctx = mock(ProbeContext.class); | ||
final GitHub gh = mock(GitHub.class); | ||
final GHRepository ghRepository = mock(GHRepository.class); | ||
|
||
when(pl.getName()).thenReturn("_test_"); | ||
when(pl.getScm()).thenReturn("valid-url"); | ||
when(ctx.getRepositoryName()).thenReturn(Optional.of("jenkinsci/_test_")); | ||
|
||
when(ctx.getGitHub()).thenReturn(gh); | ||
when(gh.getRepository(anyString())).thenReturn(ghRepository); | ||
|
||
when(ghRepository.isArchived()).thenReturn(true); | ||
|
||
final RepositoryArchivedStatusProbe probe = getSpy(); | ||
|
||
assertThat(probe.apply(pl, ctx)) | ||
.usingRecursiveComparison() | ||
.comparingOnlyFields("id", "status", "message") | ||
.isEqualTo(ProbeResult.success(RepositoryArchivedStatusProbe.KEY, "true", 1)); | ||
} | ||
|
||
@Test | ||
void shouldSucceedToFindNotArchivedRepository() throws Exception { | ||
final Plugin pl = mock(Plugin.class); | ||
final ProbeContext ctx = mock(ProbeContext.class); | ||
final GitHub gh = mock(GitHub.class); | ||
final GHRepository ghRepository = mock(GHRepository.class); | ||
|
||
when(pl.getName()).thenReturn("_test_"); | ||
when(pl.getScm()).thenReturn("valid-url"); | ||
when(ctx.getRepositoryName()).thenReturn(Optional.of("jenkinsci/_test_")); | ||
|
||
when(ctx.getGitHub()).thenReturn(gh); | ||
when(gh.getRepository(anyString())).thenReturn(ghRepository); | ||
|
||
when(ghRepository.isArchived()).thenReturn(false); | ||
|
||
final RepositoryArchivedStatusProbe probe = getSpy(); | ||
|
||
assertThat(probe.apply(pl, ctx)) | ||
.usingRecursiveComparison() | ||
.comparingOnlyFields("id", "status", "message") | ||
.isEqualTo(ProbeResult.success(RepositoryArchivedStatusProbe.KEY, "false", 1)); | ||
} | ||
} |
Oops, something went wrong.