Skip to content
Open
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 @@ -3,6 +3,8 @@
import java.util.Optional;

import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import hudson.EnvVars;
import hudson.model.TaskListener;
import org.apache.commons.lang3.StringUtils;

import edu.hm.hafner.util.FilteredLog;
Expand Down Expand Up @@ -144,4 +146,17 @@ void addActionIfMissing(final long id, final String name) {
getRun().get().addAction(new GitHubChecksAction(id, name));
}
}

EnvVars getEnvironmentVariables() {
if (getRun().isEmpty()) {
return new EnvVars();
}
try {
return getRun().get().getEnvironment(TaskListener.NULL);
}
catch (Exception e) {
// If we cannot get the environment variables, we return an empty EnvVars object
return new EnvVars();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Optional;
import java.util.stream.Collectors;

import hudson.EnvVars;
import org.apache.commons.lang3.StringUtils;

import org.kohsuke.github.GHCheckRun.AnnotationLevel;
Expand All @@ -33,6 +34,7 @@
*/
class GitHubChecksDetails {
private final ChecksDetails details;
private final EnvVars envVars;

private static final int MAX_MESSAGE_SIZE_TO_CHECKS_API = 65_535;

Expand All @@ -41,7 +43,7 @@ class GitHubChecksDetails {
*
* @param details the details of a generic check run
*/
GitHubChecksDetails(final ChecksDetails details) {
GitHubChecksDetails(final ChecksDetails details, final EnvVars envVars) {
if (details.getConclusion() == ChecksConclusion.NONE) {
if (details.getStatus() == ChecksStatus.COMPLETED) {
throw new IllegalArgumentException("No conclusion has been set when status is completed.");
Expand All @@ -53,6 +55,7 @@ class GitHubChecksDetails {
}

this.details = details;
this.envVars = envVars;
}

/**
Expand All @@ -61,9 +64,9 @@ class GitHubChecksDetails {
* @return the name of the check
*/
public String getName() {
return details.getName()
return envVars.expand(details.getName()
.filter(StringUtils::isNotBlank)
.orElseThrow(() -> new IllegalArgumentException("The check name is blank."));
.orElseThrow(() -> new IllegalArgumentException("The check name is blank.")));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import hudson.EnvVars;
import org.apache.commons.lang3.StringUtils;

import edu.hm.hafner.util.VisibleForTesting;
Expand Down Expand Up @@ -63,6 +64,7 @@ public GitHubChecksPublisher(final GitHubChecksContext context, final PluginLogg
public void publish(final ChecksDetails details) {
try {
final var credentials = context.getCredentials();
final EnvVars envVars = context.getEnvironmentVariables();

// Prevent publication with unsupported credential types
switch (credentials.getClass().getSimpleName()) {
Expand All @@ -81,7 +83,7 @@ public void publish(final ChecksDetails details) {
GitHub gitHub = Connector.connect(StringUtils.defaultIfBlank(apiUri, gitHubUrl),
credentials);

GitHubChecksDetails gitHubDetails = new GitHubChecksDetails(details);
GitHubChecksDetails gitHubDetails = new GitHubChecksDetails(details, envVars);

Optional<Long> existingId = context.getId(gitHubDetails.getName());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.jenkins.plugins.checks.github;

import hudson.EnvVars;
import io.jenkins.plugins.checks.api.ChecksConclusion;
import io.jenkins.plugins.checks.api.ChecksDetails;
import io.jenkins.plugins.checks.api.ChecksDetails.ChecksDetailsBuilder;
Expand All @@ -13,6 +14,7 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class GitHubChecksDetailsTest {
private final EnvVars envVars = new EnvVars("JOB_NAME", "job1");

@Test
void shouldReturnAllGitHubObjectsCorrectly() {
Expand All @@ -23,24 +25,40 @@ void shouldReturnAllGitHubObjectsCorrectly() {
.withDetailsURL("https://ci.jenkins.io")
.build();

GitHubChecksDetails gitHubDetails = new GitHubChecksDetails(details);
GitHubChecksDetails gitHubDetails = new GitHubChecksDetails(details, envVars);
assertThat(gitHubDetails.getName()).isEqualTo("checks");
assertThat(gitHubDetails.getStatus()).isEqualTo(Status.COMPLETED);
assertThat(gitHubDetails.getConclusion()).isPresent().hasValue(Conclusion.SUCCESS);
assertThat(gitHubDetails.getDetailsURL()).isPresent().hasValue("https://ci.jenkins.io");
}

@Test
void shouldReturnAllGitHubObjectsCorrectlyWithEnvInterpolation() {
ChecksDetails details = new ChecksDetailsBuilder()
.withName("checks:${JOB_NAME}")
.withStatus(ChecksStatus.COMPLETED)
.withConclusion(ChecksConclusion.SUCCESS)
.withDetailsURL("https://ci.jenkins.io")
.build();

GitHubChecksDetails gitHubDetails = new GitHubChecksDetails(details, envVars);
assertThat(gitHubDetails.getName()).isEqualTo("checks:job1");
assertThat(gitHubDetails.getStatus()).isEqualTo(Status.COMPLETED);
assertThat(gitHubDetails.getConclusion()).isPresent().hasValue(Conclusion.SUCCESS);
assertThat(gitHubDetails.getDetailsURL()).isPresent().hasValue("https://ci.jenkins.io");
}

@Test
void shouldReturnEmptyWhenDetailsURLIsBlank() {
GitHubChecksDetails gitHubChecksDetails =
new GitHubChecksDetails(new ChecksDetailsBuilder().withDetailsURL(StringUtils.EMPTY).build());
new GitHubChecksDetails(new ChecksDetailsBuilder().withDetailsURL(StringUtils.EMPTY).build(), envVars);
assertThat(gitHubChecksDetails.getDetailsURL()).isEmpty();
}

@Test
void shouldThrowIllegalStateExceptionWhenDetailsURLIsNotHttpOrHttpsScheme() {
GitHubChecksDetails gitHubChecksDetails =
new GitHubChecksDetails(new ChecksDetailsBuilder().withDetailsURL("ci.jenkins.io").build());
new GitHubChecksDetails(new ChecksDetailsBuilder().withDetailsURL("ci.jenkins.io").build(), envVars);
assertThatThrownBy(gitHubChecksDetails::getDetailsURL)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("The details url is not http or https scheme: ci.jenkins.io");
Expand Down