Skip to content

Commit

Permalink
fix more jenknis tests
Browse files Browse the repository at this point in the history
  • Loading branch information
krusche committed Jan 3, 2025
1 parent cd964b3 commit f2f4fa9
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static de.tum.cit.aet.artemis.core.config.Constants.PROFILE_JENKINS;

import java.io.IOException;
import java.net.URL;
import java.net.URI;

import jakarta.validation.constraints.NotNull;

Expand Down Expand Up @@ -37,7 +37,7 @@ public class JenkinsAuthorizationInterceptor implements ClientHttpRequestInterce
private String password;

@Value("${artemis.continuous-integration.url}")
private URL jenkinsURL;
private URI jenkinsServerUri;

@Value("${jenkins.use-crumb:#{true}}")
private boolean useCrumb;
Expand All @@ -64,7 +64,7 @@ private void setCrumb(final HttpHeaders headersToAuthenticate) {
final var entity = new HttpEntity<>(headers);

try {
final var response = restTemplate.exchange(jenkinsURL.toString() + "/crumbIssuer/api/json", HttpMethod.GET, entity, JsonNode.class);
final var response = restTemplate.exchange(jenkinsServerUri.toString() + "/crumbIssuer/api/json", HttpMethod.GET, entity, JsonNode.class);
final var sessionId = response.getHeaders().get("Set-Cookie").getFirst();
headersToAuthenticate.add("Jenkins-Crumb", response.getBody().get("crumb").asText());
headersToAuthenticate.add("Cookie", sessionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static de.tum.cit.aet.artemis.core.config.Constants.PROFILE_JENKINS;

import java.net.URL;
import java.net.URI;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.info.Info;
Expand All @@ -17,11 +17,11 @@
public class JenkinsInfoContributor implements InfoContributor {

@Value("${artemis.continuous-integration.url}")
private URL JENKINS_SERVER_URL;
private URI jenkinsServerUri;

@Override
public void contribute(Info.Builder builder) {
final var buildPlanURLTemplate = JENKINS_SERVER_URL + "/job/{projectKey}/job/{buildPlanId}";
final var buildPlanURLTemplate = jenkinsServerUri + "/job/{projectKey}/job/{buildPlanId}";
builder.withDetail(Constants.INFO_BUILD_PLAN_URL_DETAIL, buildPlanURLTemplate);

// Store name of the continuous integration system
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class JenkinsService extends AbstractContinuousIntegrationService {
private final ObjectMapper mapper = new ObjectMapper();

@Value("${artemis.continuous-integration.url}")
protected URI serverUri;
private URI jenkinsServerUri;

private final JenkinsBuildPlanService jenkinsBuildPlanService;

Expand Down Expand Up @@ -168,7 +168,7 @@ public String getPlanKey(Object requestBody) throws JenkinsException {
@Override
public Optional<String> getWebHookUrl(String projectKey, String buildPlanId) {
// TODO: use UriComponentsBuilder
var urlString = serverUri + "/project/" + projectKey + "/" + buildPlanId;
var urlString = jenkinsServerUri + "/project/" + projectKey + "/" + buildPlanId;
return Optional.of(jenkinsInternalUrlService.toInternalCiUrl(urlString));
}

Expand Down Expand Up @@ -231,9 +231,9 @@ public void removeAllDefaultProjectPermissions(String projectKey) {

@Override
public ConnectorHealth health() {
Map<String, Object> additionalInfo = Map.of("url", serverUri);
Map<String, Object> additionalInfo = Map.of("url", jenkinsServerUri);
try {
URI uri = JenkinsEndpoints.HEALTH.buildEndpoint(serverUri).build(true).toUri();
URI uri = JenkinsEndpoints.HEALTH.buildEndpoint(jenkinsServerUri).build(true).toUri();
// Note: we simply check if the login page is reachable
shortTimeoutRestTemplate.getForObject(uri, String.class);
return new ConnectorHealth(true, additionalInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static de.tum.cit.aet.artemis.core.config.Constants.PROFILE_JENKINS;

import java.net.URL;
import java.net.URI;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.info.Info;
Expand All @@ -17,11 +17,11 @@
public class JenkinsBuildPlanLinkInfoContributor implements InfoContributor {

@Value("${artemis.continuous-integration.url}")
private URL jenkinsServerUrl;
private URI jenkinsServerUri;

@Override
public void contribute(Info.Builder builder) {
final var buildPlanURLTemplate = jenkinsServerUrl + "/job/{projectKey}/job/{buildPlanId}";
final var buildPlanURLTemplate = jenkinsServerUri + "/job/{projectKey}/job/{buildPlanId}";
builder.withDetail(Constants.INFO_BUILD_PLAN_URL_DETAIL, buildPlanURLTemplate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class JenkinsBuildPlanService {
private static final Logger log = LoggerFactory.getLogger(JenkinsBuildPlanService.class);

@Value("${artemis.continuous-integration.url}")
protected URI serverUri;
private URI jenkinsServerUri;

private final RestTemplate restTemplate;

Expand Down Expand Up @@ -308,7 +308,7 @@ private String getCleanPlanName(String name) {
*/
public void triggerBuild(String projectKey, String planKey) {
try {
URI uri = JenkinsEndpoints.TRIGGER_BUILD.buildEndpoint(serverUri, projectKey, planKey).build(true).toUri();
URI uri = JenkinsEndpoints.TRIGGER_BUILD.buildEndpoint(jenkinsServerUri, projectKey, planKey).build(true).toUri();
restTemplate.postForEntity(uri, new HttpEntity<>(null, new HttpHeaders()), Void.class);
}
catch (RestClientException e) {
Expand Down Expand Up @@ -337,7 +337,7 @@ public ContinuousIntegrationService.BuildStatus getBuildStatusOfPlan(String proj
}

try {
URI uri = JenkinsEndpoints.LAST_BUILD.buildEndpoint(serverUri, projectKey, planKey).build(true).toUri();
URI uri = JenkinsEndpoints.LAST_BUILD.buildEndpoint(jenkinsServerUri, projectKey, planKey).build(true).toUri();
var buildStatus = restTemplate.getForObject(uri, JenkinsBuildStatusDTO.class);
return buildStatus != null && buildStatus.building ? ContinuousIntegrationService.BuildStatus.BUILDING : ContinuousIntegrationService.BuildStatus.INACTIVE;
}
Expand Down Expand Up @@ -422,7 +422,7 @@ public void givePlanPermissions(ProgrammingExercise programmingExercise, String
*/
public void enablePlan(String projectKey, String planKey) {
try {
URI uri = JenkinsEndpoints.ENABLE.buildEndpoint(serverUri, projectKey, planKey).build(true).toUri();
URI uri = JenkinsEndpoints.ENABLE.buildEndpoint(jenkinsServerUri, projectKey, planKey).build(true).toUri();
restTemplate.postForEntity(uri, new HttpEntity<>(null, new HttpHeaders()), Void.class);
}
catch (HttpClientErrorException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class JenkinsJobService {
private final RestTemplate restTemplate;

@Value("${artemis.continuous-integration.url}")
protected URI serverUri;
private URI jenkinsServerUri;

public JenkinsJobService(@Qualifier("jenkinsRestTemplate") RestTemplate restTemplate) {
this.restTemplate = restTemplate;
Expand All @@ -57,7 +57,7 @@ public JobWithDetails getJob(String folderJobName, String jobName) {
}

try {
URI uri = JenkinsEndpoints.GET_JOB.buildEndpoint(serverUri, folderJobName, jobName).build(true).toUri();
URI uri = JenkinsEndpoints.GET_JOB.buildEndpoint(jenkinsServerUri, folderJobName, jobName).build(true).toUri();
return restTemplate.getForObject(uri, JobWithDetails.class);
}
catch (HttpClientErrorException.NotFound notFound) {
Expand Down Expand Up @@ -88,7 +88,7 @@ public record FolderJob(String name, String description, String url) {
*/
public FolderJob getFolderJob(String folderName) {
try {
URI uri = JenkinsEndpoints.GET_FOLDER_JOB.buildEndpoint(serverUri, folderName).build(true).toUri();
URI uri = JenkinsEndpoints.GET_FOLDER_JOB.buildEndpoint(jenkinsServerUri, folderName).build(true).toUri();
return restTemplate.getForObject(uri, FolderJob.class);
}
catch (HttpClientErrorException.NotFound notFound) {
Expand All @@ -114,7 +114,7 @@ public Document getJobConfig(String folderName, String jobName) {
throw new JenkinsException("The folder " + folderName + " does not exist.");
}

URI uri = JenkinsEndpoints.PLAN_CONFIG.buildEndpoint(serverUri, folderName, jobName).build(true).toUri();
URI uri = JenkinsEndpoints.PLAN_CONFIG.buildEndpoint(jenkinsServerUri, folderName, jobName).build(true).toUri();
String xmlString = restTemplate.getForObject(uri, String.class);

// Replace the old reference to the master and main branch by a reference to the default branch
Expand Down Expand Up @@ -145,14 +145,14 @@ public Document getFolderConfig(String folderName) throws IOException {
return null;
}

URI uri = JenkinsEndpoints.FOLDER_CONFIG.buildEndpoint(serverUri, folderName).build(true).toUri();
URI uri = JenkinsEndpoints.FOLDER_CONFIG.buildEndpoint(jenkinsServerUri, folderName).build(true).toUri();
String folderXml = restTemplate.getForObject(uri, String.class);
return JenkinsXmlFileUtils.readFromString(folderXml);
}

public void createFolder(String projectKey) {
//@formatter:off
URI uri = JenkinsEndpoints.NEW_FOLDER.buildEndpoint(serverUri)
URI uri = JenkinsEndpoints.NEW_FOLDER.buildEndpoint(jenkinsServerUri)
.queryParam("name", projectKey)
.queryParam("mode", "com.cloudbees.hudson.plugins.folder.Folder")
.queryParam("from", "")
Expand Down Expand Up @@ -182,7 +182,7 @@ public void createJobInFolder(Document jobConfig, String folderName, String jobN
return;
}

URI uri = JenkinsEndpoints.NEW_PLAN.buildEndpoint(serverUri, folderName).queryParam("name", jobName).build(true).toUri();
URI uri = JenkinsEndpoints.NEW_PLAN.buildEndpoint(jenkinsServerUri, folderName).queryParam("name", jobName).build(true).toUri();

final var headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_XML);
Expand All @@ -207,7 +207,7 @@ public void createJobInFolder(Document jobConfig, String folderName, String jobN
public void updateJob(String folderName, String jobName, Document jobConfig) {
final var errorMessage = "Error trying to configure build plan in Jenkins " + jobName;
try {
URI uri = JenkinsEndpoints.PLAN_CONFIG.buildEndpoint(serverUri, folderName, jobName).build(true).toUri();
URI uri = JenkinsEndpoints.PLAN_CONFIG.buildEndpoint(jenkinsServerUri, folderName, jobName).build(true).toUri();

final var headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_XML);
Expand Down Expand Up @@ -236,7 +236,7 @@ public void updateJob(String folderName, String jobName, Document jobConfig) {
*/
public void updateFolderJob(String folderName, Document folderConfig) throws IOException {
try {
URI uri = JenkinsEndpoints.FOLDER_CONFIG.buildEndpoint(serverUri, folderName).build(true).toUri();
URI uri = JenkinsEndpoints.FOLDER_CONFIG.buildEndpoint(jenkinsServerUri, folderName).build(true).toUri();

final var headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_XML);
Expand All @@ -257,7 +257,7 @@ public void updateFolderJob(String folderName, Document folderConfig) throws IOE

public void deleteJob(String folderName, String jobName) {
try {
URI uri = JenkinsEndpoints.DELETE_JOB.buildEndpoint(serverUri, folderName, jobName).build(true).toUri();
URI uri = JenkinsEndpoints.DELETE_JOB.buildEndpoint(jenkinsServerUri, folderName, jobName).build(true).toUri();
restTemplate.postForEntity(uri, new HttpEntity<>(null, new HttpHeaders()), Void.class);
}
catch (HttpClientErrorException.NotFound e) {
Expand All @@ -277,7 +277,7 @@ public void deleteJob(String folderName, String jobName) {
*/
public void deleteFolderJob(String folderName) {
try {
URI uri = JenkinsEndpoints.DELETE_FOLDER.buildEndpoint(serverUri, folderName).build(true).toUri();
URI uri = JenkinsEndpoints.DELETE_FOLDER.buildEndpoint(jenkinsServerUri, folderName).build(true).toUri();
restTemplate.postForEntity(uri, new HttpEntity<>(null, new HttpHeaders()), Void.class);
}
catch (HttpClientErrorException.NotFound e) {
Expand Down
Loading

0 comments on commit f2f4fa9

Please sign in to comment.