Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.net.URL;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

/**
* Provides a {@link GiteaChecksContext} for a Jenkins job that uses a supported
Expand Down Expand Up @@ -56,7 +55,7 @@ public String getHeadSha() {
} catch (IOException | InterruptedException e) {
// ignore and return a default
}
return StringUtils.EMPTY;
return "";
}

public String getGitCommitEnvironment() throws IOException, InterruptedException {
Expand All @@ -71,14 +70,14 @@ private String getLastBuiltRevisionFromBuildData() {
return lastBuiltRevision.getSha1().getName();
}
}
return StringUtils.EMPTY;
return "";
}

@Override
public String getRepository() {
String repositoryURL = getUserRemoteConfig().getUrl();
if (repositoryURL == null) {
return StringUtils.EMPTY;
return "";
}

return getRepository(repositoryURL);
Expand Down Expand Up @@ -106,7 +105,7 @@ public String getGiteaServerUrl() {
try {
url = new URL(repoUrl);
} catch (MalformedURLException e) {
return StringUtils.EMPTY;
return "";
}

return "https://" + url.getHost();
Expand All @@ -115,26 +114,26 @@ public String getGiteaServerUrl() {
@VisibleForTesting
String getRepository(final String repositoryUrl) {
if (StringUtils.isBlank(repositoryUrl)) {
return StringUtils.EMPTY;
return "";
}

if (repositoryUrl.startsWith("http")) {
URL url;
try {
url = new URL(repositoryUrl);
} catch (MalformedURLException e) {
return StringUtils.EMPTY;
return "";
}

String[] pathParts = StringUtils.removeStart(url.getPath(), "/").split("/");
if (pathParts.length == NumberUtils.INTEGER_TWO) {
if (pathParts.length == 2) {
return pathParts[0] + "/" + StringUtils.removeEnd(pathParts[1], ".git");
}
} else if (repositoryUrl.matches("git@.+:.+\\/.+")) {
return StringUtils.removeEnd(repositoryUrl.split(":")[1], ".git");
}

return StringUtils.EMPTY;
return "";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import hudson.model.Job;
import hudson.model.Run;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;

/**
* Base class for a context that publishes Gitea checks.
Expand Down Expand Up @@ -37,7 +36,8 @@ protected GiteaChecksContext(final Job<?, ?> job, final String url, final SCMFac
public abstract String getRepoOwner();

/**
* Returns the source repository's name of the run. The name consists of the repository's name, e.g. jenkins
* Returns the source repository's name of the run. The name consists of the
* repository's name, e.g. jenkins
*
* @return the source repository's name
*/
Expand All @@ -51,7 +51,8 @@ protected GiteaChecksContext(final Job<?, ?> job, final String url, final SCMFac
public abstract String getGiteaServerUrl();

/**
* Returns the source repository's full name of the run. The full name consists of the owner's name and the
* Returns the source repository's full name of the run. The full name consists
* of the owner's name and the
* repository's name, e.g. jenkins-ci/jenkins
*
* @return the source repository's full name
Expand All @@ -62,7 +63,7 @@ protected GiteaChecksContext(final Job<?, ?> job, final String url, final SCMFac
* Returns whether the context is valid (with all properties functional) to use.
*
* @param logger
* the filtered logger
* the filtered logger
* @return whether the context is valid to use
*/
public abstract boolean isValid(FilteredLog logger);
Expand All @@ -76,11 +77,13 @@ protected GiteaChecksContext(final Job<?, ?> job, final String url, final SCMFac
* @return the credentials
*/
public StandardCredentials getCredentials() {
return getGiteaAppCredentials(StringUtils.defaultIfEmpty(getCredentialsId(), ""));
var id = getCredentialsId();
return getGiteaAppCredentials(id == null || id.isEmpty() ? "" : id);
}

/**
* Returns the URL of the run's summary page, e.g. https://ci.jenkins.io/job/Core/job/jenkins/job/master/2000/.
* Returns the URL of the run's summary page, e.g.
* https://ci.jenkins.io/job/Core/job/jenkins/job/master/2000/.
*
* @return the URL of the summary page
*/
Expand All @@ -98,17 +101,19 @@ protected SCMFacade getScmFacade() {

protected StandardCredentials getGiteaAppCredentials(final String credentialsId) {
return findGiteaAppCredentials(credentialsId)
.orElseThrow(() ->
new IllegalStateException("No Gitea APP credentials available for job: " + getJob().getName()));
.orElseThrow(() -> new IllegalStateException(
"No Gitea APP credentials available for job: " + getJob().getName()));
}

protected boolean hasGiteaAppCredentials() {
return findGiteaAppCredentials(StringUtils.defaultIfEmpty(getCredentialsId(), ""))
var id = getCredentialsId();
return findGiteaAppCredentials(id == null || id.isEmpty() ? "" : id)
.isPresent();
}

protected boolean hasCredentialsId() {
return StringUtils.isNoneBlank(getCredentialsId());
var id = getCredentialsId();
return id != null && !id.isBlank();
}

protected boolean hasValidCredentials(final FilteredLog logger) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.Optional;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMRevision;
import org.apache.commons.lang3.StringUtils;
import org.jenkinsci.plugin.gitea.GiteaSCMSource;

/**
Expand Down Expand Up @@ -49,7 +48,7 @@ private GiteaSCMSourceChecksContext(

@Override
public String getHeadSha() {
if (StringUtils.isBlank(sha)) {
if (sha == null || sha.isBlank()) {
throw new IllegalStateException("No SHA found for job: " + getJob().getName());
}

Expand Down Expand Up @@ -109,7 +108,7 @@ public boolean isValid(final FilteredLog logger) {
return false;
}

if (StringUtils.isBlank(sha)) {
if (sha == null || sha.isBlank()) {
logger.logError("No HEAD SHA found for %s", getRepository());

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import io.jenkins.plugins.checks.api.ChecksDetails;
import io.jenkins.plugins.checks.api.ChecksDetails.ChecksDetailsBuilder;
import io.jenkins.plugins.checks.api.ChecksStatus;
import org.apache.commons.lang3.StringUtils;
import org.jenkinsci.plugin.gitea.client.api.GiteaCommitState;
import org.junit.jupiter.api.Test;

Expand All @@ -30,7 +29,7 @@ void shouldReturnAllGiteaObjectsCorrectly() {
@Test
void shouldReturnEmptyWhenDetailsURLIsBlank() {
GiteaChecksDetails giteaChecksDetails = new GiteaChecksDetails(
new ChecksDetailsBuilder().withDetailsURL(StringUtils.EMPTY).build());
new ChecksDetailsBuilder().withDetailsURL("").build());
assertThat(giteaChecksDetails.getDetailsURL()).isEmpty();
}

Expand Down
Loading