Skip to content

Commit

Permalink
Modernize code (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice authored Jul 8, 2024
1 parent 18456c0 commit 1d99442
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 37 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@

<spotless.check.skip>false</spotless.check.skip>
<checkstyle.skip>true</checkstyle.skip>
<pmd.printFailingErrors>true</pmd.printFailingErrors>
</properties>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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());

Check warning on line 86 in src/main/java/io/jenkins/plugins/checks/gitea/GiteaChecksDetails.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 86 is not covered by tests
}
// fallthrough - Can't happen happen
default:
throw new IllegalArgumentException("Unsupported checks status: " + details.getStatus());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions src/main/java/io/jenkins/plugins/checks/gitea/SCMFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -128,8 +127,7 @@ UserRemoteConfig getUserRemoteConfig(final GitSCM scm) {
*/
public Optional<StandardCredentials> 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),

Check warning on line 130 in src/main/java/io/jenkins/plugins/checks/gitea/SCMFacade.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 130 is not covered by tests
CredentialsMatchers.allOf(
AuthenticationTokens.matcher(GiteaAuth.class), CredentialsMatchers.withId(credentialsId)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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");
Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -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");
Expand All @@ -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(
Expand All @@ -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);

Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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())
Expand Down Expand Up @@ -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("");
Expand Down

0 comments on commit 1d99442

Please sign in to comment.