From 1d99442828cc011f6c7f4ac299671c0c6c743959 Mon Sep 17 00:00:00 2001 From: Michael Kriese Date: Mon, 8 Jul 2024 12:28:15 +0200 Subject: [PATCH] Modernize code (#153) --- pom.xml | 1 + .../checks/gitea/GiteaChecksDetails.java | 14 ++++++--- .../checks/gitea/GiteaChecksPublisher.java | 6 +--- .../plugins/checks/gitea/SCMFacade.java | 4 +-- .../gitea/GitSCMChecksContextITest.java | 2 -- .../gitea/GiteaPublisherFactoryTest.java | 16 +++++----- .../GiteaSCMSourceChecksContextTest.java | 30 +++++++++---------- 7 files changed, 36 insertions(+), 37 deletions(-) diff --git a/pom.xml b/pom.xml index a163457..495bc73 100644 --- a/pom.xml +++ b/pom.xml @@ -65,6 +65,7 @@ false true + true diff --git a/src/main/java/io/jenkins/plugins/checks/gitea/GiteaChecksDetails.java b/src/main/java/io/jenkins/plugins/checks/gitea/GiteaChecksDetails.java index 4cc1a48..80dee87 100644 --- a/src/main/java/io/jenkins/plugins/checks/gitea/GiteaChecksDetails.java +++ b/src/main/java/io/jenkins/plugins/checks/gitea/GiteaChecksDetails.java @@ -11,7 +11,8 @@ import org.jenkinsci.plugin.gitea.client.api.GiteaCommitState; /** - * An adaptor which adapts the generic checks objects of {@link ChecksDetails} to the specific Gitea checks run. + * An adaptor which adapts the generic checks objects of {@link ChecksDetails} + * to the specific Gitea checks run. */ class GiteaChecksDetails { private final ChecksDetails details; @@ -44,7 +45,8 @@ public String getName() { } /** - * Returns the name of a Gitea commit status. This is displayed on a PR page on gitea, together with + * Returns the name of a Gitea commit status. This is displayed on a PR page on + * gitea, together with * the description. * * @return the name of the check @@ -57,9 +59,10 @@ public String getContextString() { * Returns the {@link GiteaCommitState} of a Gitea check run. * * @return the status of a check run - * @throws IllegalArgumentException if the status of the {@code details} is not one of {@link ChecksStatus} + * @throws IllegalArgumentException if the status of the {@code details} is not + * one of {@link ChecksStatus} */ - @SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.SwitchStmtsShouldHaveDefault", "PMD.ImplicitSwitchFallThrough"}) + @SuppressWarnings({"PMD.CyclomaticComplexity"}) public GiteaCommitState getStatus() { switch (details.getStatus()) { case NONE: @@ -79,7 +82,10 @@ public GiteaCommitState getStatus() { return GiteaCommitState.FAILURE; case TIME_OUT: return GiteaCommitState.ERROR; + default: + throw new IllegalArgumentException("Unsupported checks status: " + details.getStatus()); } + // fallthrough - Can't happen happen default: throw new IllegalArgumentException("Unsupported checks status: " + details.getStatus()); } diff --git a/src/main/java/io/jenkins/plugins/checks/gitea/GiteaChecksPublisher.java b/src/main/java/io/jenkins/plugins/checks/gitea/GiteaChecksPublisher.java index 55f99ca..1cd5e7b 100644 --- a/src/main/java/io/jenkins/plugins/checks/gitea/GiteaChecksPublisher.java +++ b/src/main/java/io/jenkins/plugins/checks/gitea/GiteaChecksPublisher.java @@ -51,12 +51,8 @@ public GiteaChecksPublisher(final GiteaChecksContext context, final PluginLogger * the details of a check run */ @Override - @SuppressWarnings("PMD.CloseResource") public void publish(final ChecksDetails details) { - try { - // TODO: close connection - GiteaConnection giteaConnection = connect(giteaServerUrl, context.getCredentials()); - + try (GiteaConnection giteaConnection = connect(giteaServerUrl, context.getCredentials())) { GiteaChecksDetails giteaDetails = new GiteaChecksDetails(details); publishGiteaCommitStatus(giteaConnection, giteaDetails); diff --git a/src/main/java/io/jenkins/plugins/checks/gitea/SCMFacade.java b/src/main/java/io/jenkins/plugins/checks/gitea/SCMFacade.java index 741c750..89443f5 100644 --- a/src/main/java/io/jenkins/plugins/checks/gitea/SCMFacade.java +++ b/src/main/java/io/jenkins/plugins/checks/gitea/SCMFacade.java @@ -14,7 +14,6 @@ import hudson.security.ACL; import java.io.IOException; import java.util.Collection; -import java.util.Collections; import java.util.List; import java.util.Optional; import jenkins.authentication.tokens.api.AuthenticationTokens; @@ -128,8 +127,7 @@ UserRemoteConfig getUserRemoteConfig(final GitSCM scm) { */ public Optional findGiteaAppCredentials(final Job job, final String credentialsId) { StandardCredentials credential = CredentialsMatchers.firstOrNull( - CredentialsProvider.lookupCredentials( - StandardCredentials.class, job, ACL.SYSTEM, Collections.emptyList()), + CredentialsProvider.lookupCredentialsInItem(StandardCredentials.class, job, ACL.SYSTEM2), CredentialsMatchers.allOf( AuthenticationTokens.matcher(GiteaAuth.class), CredentialsMatchers.withId(credentialsId))); diff --git a/src/test/java/io/jenkins/plugins/checks/gitea/GitSCMChecksContextITest.java b/src/test/java/io/jenkins/plugins/checks/gitea/GitSCMChecksContextITest.java index 2df218d..7c79d97 100644 --- a/src/test/java/io/jenkins/plugins/checks/gitea/GitSCMChecksContextITest.java +++ b/src/test/java/io/jenkins/plugins/checks/gitea/GitSCMChecksContextITest.java @@ -36,8 +36,6 @@ public void shouldRetrieveContextFromFreeStyleBuild() throws IOException { GitSCM scm = new GitSCM( GitSCM.createRepoList(HTTP_URL, CREDENTIALS_ID), Collections.singletonList(branchSpec), - false, - Collections.emptyList(), null, null, Collections.emptyList()); diff --git a/src/test/java/io/jenkins/plugins/checks/gitea/GiteaPublisherFactoryTest.java b/src/test/java/io/jenkins/plugins/checks/gitea/GiteaPublisherFactoryTest.java index 7e12217..4f98bfc 100644 --- a/src/test/java/io/jenkins/plugins/checks/gitea/GiteaPublisherFactoryTest.java +++ b/src/test/java/io/jenkins/plugins/checks/gitea/GiteaPublisherFactoryTest.java @@ -21,8 +21,8 @@ class GiteaPublisherFactoryTest { @Test void shouldCreateGiteaChecksPublisherFromRunForProjectWithValidGiteaSCMSource() { - Run run = mock(Run.class); - Job job = mock(Job.class); + var run = mock(Run.class); + var job = mock(Job.class); GiteaSCMSource source = mock(GiteaSCMSource.class); StandardCredentials credentials = mock(StandardCredentials.class); PullRequestSCMRevision revision = mock(PullRequestSCMRevision.class); @@ -42,8 +42,8 @@ void shouldCreateGiteaChecksPublisherFromRunForProjectWithValidGiteaSCMSource() @Test void shouldReturnGiteaChecksPublisherFromJobProjectWithValidGiteaSCMSource() { - Run run = mock(Run.class); - Job job = mock(Job.class); + var run = mock(Run.class); + var job = mock(Job.class); GiteaSCMSource source = mock(GiteaSCMSource.class); StandardCredentials credentials = mock(StandardCredentials.class); PullRequestSCMRevision revision = mock(PullRequestSCMRevision.class); @@ -65,8 +65,8 @@ void shouldReturnGiteaChecksPublisherFromJobProjectWithValidGiteaSCMSource() { @Test void shouldCreateGiteaChecksPublisherFromRunForProjectWithValidGitSCM() throws IOException, InterruptedException { - Job job = mock(Job.class); - Run run = mock(Run.class); + var job = mock(Job.class); + var run = mock(Run.class); GitSCM gitSCM = mock(GitSCM.class); UserRemoteConfig config = mock(UserRemoteConfig.class); StandardCredentials credentials = mock(StandardCredentials.class); @@ -89,7 +89,7 @@ void shouldCreateGiteaChecksPublisherFromRunForProjectWithValidGitSCM() throws I @Test void shouldReturnEmptyFromRunForInvalidProject() { - Run run = mock(Run.class); + var run = mock(Run.class); SCMFacade facade = mock(SCMFacade.class); DisplayURLProvider urlProvider = mock(DisplayURLProvider.class); @@ -99,7 +99,7 @@ void shouldReturnEmptyFromRunForInvalidProject() { @Test void shouldCreateNullPublisherFromJobForInvalidProject() { - Job job = mock(Job.class); + var job = mock(Job.class); SCMFacade facade = mock(SCMFacade.class); DisplayURLProvider urlProvider = mock(DisplayURLProvider.class); diff --git a/src/test/java/io/jenkins/plugins/checks/gitea/GiteaSCMSourceChecksContextTest.java b/src/test/java/io/jenkins/plugins/checks/gitea/GiteaSCMSourceChecksContextTest.java index 9a9d8f2..6e55649 100644 --- a/src/test/java/io/jenkins/plugins/checks/gitea/GiteaSCMSourceChecksContextTest.java +++ b/src/test/java/io/jenkins/plugins/checks/gitea/GiteaSCMSourceChecksContextTest.java @@ -22,7 +22,7 @@ class GiteaSCMSourceChecksContextTest { @Test void shouldGetHeadShaFromMasterBranch() { - Job job = mock(Job.class); + var job = mock(Job.class); SCMHead head = mock(SCMHead.class); AbstractGitSCMSource.SCMRevisionImpl revision = mock(AbstractGitSCMSource.SCMRevisionImpl.class); GiteaSCMSource source = mock(GiteaSCMSource.class); @@ -35,7 +35,7 @@ job, URL, createGiteaSCMFacadeWithRevision(job, source, head, revision, "a1b2c3" @Test void shouldGetHeadShaFromPullRequest() { - Job job = mock(Job.class); + var job = mock(Job.class); SCMHead head = mock(SCMHead.class); PullRequestSCMRevision revision = mock(PullRequestSCMRevision.class); GiteaSCMSource source = mock(GiteaSCMSource.class); @@ -48,8 +48,8 @@ job, URL, createGiteaSCMFacadeWithRevision(job, source, head, revision, "a1b2c3" @Test void shouldGetHeadShaFromRun() { - Job job = mock(Job.class); - Run run = mock(Run.class); + var job = mock(Job.class); + var run = mock(Run.class); PullRequestSCMRevision revision = mock(PullRequestSCMRevision.class); GiteaSCMSource source = mock(GiteaSCMSource.class); @@ -64,7 +64,7 @@ run, URL, createGiteaSCMFacadeWithRevision(run, source, revision, "a1b2c3")) @Test void shouldThrowIllegalStateExceptionWhenGetHeadShaButNoSCMHeadAvailable() { - Job job = mock(Job.class); + var job = mock(Job.class); GiteaSCMSource source = mock(GiteaSCMSource.class); when(job.getName()).thenReturn("gitea-checks-plugin"); @@ -78,7 +78,7 @@ void shouldThrowIllegalStateExceptionWhenGetHeadShaButNoSCMHeadAvailable() { @Test void shouldThrowIllegalStateExceptionWhenGetHeadShaButNoSCMRevisionAvailable() { - Job job = mock(Job.class); + var job = mock(Job.class); SCMHead head = mock(SCMHead.class); GiteaSCMSource source = mock(GiteaSCMSource.class); @@ -95,7 +95,7 @@ job, URL, createGiteaSCMFacadeWithRevision(job, source, head, null, null))::getH @Test void shouldThrowIllegalStateExceptionWhenGetHeadShaButNoSuitableSCMRevisionAvailable() { - Job job = mock(Job.class); + var job = mock(Job.class); SCMHead head = mock(SCMHead.class); SCMRevision revision = mock(SCMRevision.class); GiteaSCMSource source = mock(GiteaSCMSource.class); @@ -110,7 +110,7 @@ job, URL, createGiteaSCMFacadeWithRevision(job, source, head, revision, null)):: @Test void shouldGetRepositoryName() { - Job job = mock(Job.class); + var job = mock(Job.class); GiteaSCMSource source = mock(GiteaSCMSource.class); when(source.getRepoOwner()).thenReturn("jenkinsci"); @@ -123,7 +123,7 @@ void shouldGetRepositoryName() { @Test void shouldThrowIllegalStateExceptionWhenGetRepositoryButNoGiteaSCMSourceAvailable() { - Job job = mock(Job.class); + var job = mock(Job.class); when(job.getName()).thenReturn("gitea-checks-plugin"); assertThatThrownBy( @@ -135,7 +135,7 @@ void shouldThrowIllegalStateExceptionWhenGetRepositoryButNoGiteaSCMSourceAvailab @Test void shouldGetCredentials() { - Job job = mock(Job.class); + var job = mock(Job.class); GiteaSCMSource source = mock(GiteaSCMSource.class); StandardCredentials credentials = mock(StandardCredentials.class); @@ -147,7 +147,7 @@ job, URL, createGiteaSCMFacadeWithCredentials(job, source, credentials, "1")) @Test void shouldThrowIllegalStateExceptionWhenGetCredentialsButNoCredentialsAvailable() { - Job job = mock(Job.class); + var job = mock(Job.class); GiteaSCMSource source = mock(GiteaSCMSource.class); when(job.getName()).thenReturn("gitea-checks-plugin"); @@ -160,7 +160,7 @@ job, URL, createGiteaSCMFacadeWithCredentials(job, source, null, null))::getCred @Test void shouldThrowIllegalStateExceptionWhenGetCredentialsButNoSourceAvailable() { - Job job = mock(Job.class); + var job = mock(Job.class); SCMFacade scmFacade = mock(SCMFacade.class); when(job.getName()).thenReturn("gitea-checks-plugin"); @@ -172,7 +172,7 @@ void shouldThrowIllegalStateExceptionWhenGetCredentialsButNoSourceAvailable() { @Test void shouldGetURLForJob() { - Job job = mock(Job.class); + var job = mock(Job.class); assertThat(GiteaSCMSourceChecksContext.fromJob(job, URL, createGiteaSCMFacadeWithSource(job, null)) .getURL()) @@ -223,8 +223,8 @@ void shouldReturnFalseWhenValidateContextButHasNoValidGiteaAppCredentials() { @Test void shouldReturnFalseWhenValidateContextButHasNoValidSHA() { - Run run = mock(Run.class); - Job job = mock(Job.class); + var run = mock(Run.class); + var job = mock(Job.class); GiteaSCMSource source = mock(GiteaSCMSource.class); StandardCredentials credentials = mock(StandardCredentials.class); FilteredLog logger = new FilteredLog("");