-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from Nookure/dev
Dev
- Loading branch information
Showing
13 changed files
with
299 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
NookureStaff-API/src/main/java/com/nookure/staff/api/jenkins/JenkinsBaseClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.nookure.staff.api.jenkins; | ||
|
||
import com.google.auto.service.AutoService; | ||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.inject.Singleton; | ||
import com.nookure.staff.api.jenkins.json.JenkinsArtifact; | ||
import com.nookure.staff.api.jenkins.json.JenkinsBuild; | ||
import com.nookure.staff.api.jenkins.json.JenkinsJob; | ||
import com.nookure.staff.api.jenkins.json.JenkinsRun; | ||
import com.nookure.staff.api.jenkins.json.serializer.JenkinsArtifactDeserializer; | ||
import com.nookure.staff.api.jenkins.json.serializer.JenkinsBuildDeserializer; | ||
import com.nookure.staff.api.jenkins.json.serializer.JenkinsJobDeserializer; | ||
import com.nookure.staff.api.jenkins.json.serializer.JenkinsRunDeserializer; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
@Singleton | ||
@AutoService(JenkinsBaseClient.class) | ||
public class JenkinsBaseClient { | ||
private static final String agentName = "NookureStaff-API"; | ||
|
||
private final String jenkinsUrl; | ||
private final URI jenkinsUri; | ||
private final HttpClient httpClient; | ||
|
||
private final Gson gson = new GsonBuilder() | ||
.registerTypeAdapter(JenkinsBuild.class, new JenkinsBuildDeserializer()) | ||
.registerTypeAdapter(JenkinsArtifact.class, new JenkinsArtifactDeserializer()) | ||
.registerTypeAdapter(JenkinsRun.class, new JenkinsRunDeserializer()) | ||
.registerTypeAdapter(JenkinsJob.class, new JenkinsJobDeserializer()) | ||
.create(); | ||
|
||
public JenkinsBaseClient(@NotNull final String jenkinsUrl) { | ||
this.jenkinsUrl = jenkinsUrl; | ||
this.jenkinsUri = URI.create(jenkinsUrl); | ||
this.httpClient = HttpClient.newBuilder() | ||
.version(HttpClient.Version.HTTP_2) | ||
.build(); | ||
} | ||
|
||
public @NotNull CompletableFuture<JenkinsJob> getJob(@NotNull final String jobName) { | ||
return httpClient.sendAsync(HttpRequest.newBuilder() | ||
.uri(URI.create(jenkinsUrl + "/job/" + jobName + "/api/json")) | ||
.header("User-Agent", agentName) | ||
.build(), HttpResponse.BodyHandlers.ofString()) | ||
.thenApply(response -> gson.fromJson(response.body(), JenkinsJob.class)); | ||
} | ||
|
||
public @NotNull CompletableFuture<JenkinsRun> getRun(@NotNull final String jobName, final int runNumber) { | ||
return httpClient.sendAsync(HttpRequest.newBuilder() | ||
.uri(URI.create(jenkinsUrl + "/job/" + jobName + "/" + runNumber + "/api/json")) | ||
.header("User-Agent", agentName) | ||
.build(), HttpResponse.BodyHandlers.ofString()) | ||
.thenApply(response -> gson.fromJson(response.body(), JenkinsRun.class)); | ||
} | ||
|
||
public @NotNull CompletableFuture<JenkinsRun> getRun(@NotNull JenkinsJob job, final int runNumber) { | ||
return getRun(job.name(), runNumber); | ||
} | ||
|
||
public @NotNull HttpClient getHttpClient() { | ||
return httpClient; | ||
} | ||
|
||
public @NotNull String getJenkinsUrl() { | ||
return jenkinsUrl; | ||
} | ||
|
||
public @NotNull URI getJenkinsUri() { | ||
return jenkinsUri; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
NookureStaff-API/src/main/java/com/nookure/staff/api/jenkins/json/JenkinsArtifact.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.nookure.staff.api.jenkins.json; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
public record JenkinsArtifact( | ||
@NotNull String displayPath, | ||
@NotNull String fileName, | ||
@NotNull String relativePath | ||
) { | ||
} |
10 changes: 10 additions & 0 deletions
10
NookureStaff-API/src/main/java/com/nookure/staff/api/jenkins/json/JenkinsBuild.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.nookure.staff.api.jenkins.json; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
public record JenkinsBuild( | ||
@NotNull String _class, | ||
int number, | ||
@NotNull String url | ||
) { | ||
} |
13 changes: 13 additions & 0 deletions
13
NookureStaff-API/src/main/java/com/nookure/staff/api/jenkins/json/JenkinsJob.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.nookure.staff.api.jenkins.json; | ||
|
||
public record JenkinsJob( | ||
String displayName, | ||
String fullDisplayName, | ||
String name, | ||
String url, | ||
JenkinsBuild[] builds, | ||
JenkinsBuild lastCompletedBuild, | ||
JenkinsBuild lastStableBuild, | ||
JenkinsBuild lastSuccessfulBuild | ||
) { | ||
} |
15 changes: 15 additions & 0 deletions
15
NookureStaff-API/src/main/java/com/nookure/staff/api/jenkins/json/JenkinsRun.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.nookure.staff.api.jenkins.json; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public record JenkinsRun( | ||
@NotNull String _class, | ||
boolean building, | ||
@Nullable String description, | ||
long duration, | ||
@NotNull String fullDisplayName, | ||
@NotNull String url, | ||
@NotNull JenkinsArtifact[] artifacts | ||
) { | ||
} |
24 changes: 24 additions & 0 deletions
24
.../main/java/com/nookure/staff/api/jenkins/json/serializer/JenkinsArtifactDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.nookure.staff.api.jenkins.json.serializer; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.google.gson.*; | ||
import com.nookure.staff.api.jenkins.json.JenkinsArtifact; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
public class JenkinsArtifactDeserializer implements JsonDeserializer<JenkinsArtifact> { | ||
@Override | ||
public JenkinsArtifact deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { | ||
final JsonObject jsonObject = jsonElement.getAsJsonObject(); | ||
|
||
final String displayPath = jsonObject.get("displayPath").getAsString(); | ||
final String fileName = jsonObject.get("fileName").getAsString(); | ||
final String relativePath = jsonObject.get("relativePath").getAsString(); | ||
|
||
Preconditions.checkNotNull(displayPath, "displayPath cannot be null"); | ||
Preconditions.checkNotNull(fileName, "fileName cannot be null"); | ||
Preconditions.checkNotNull(relativePath, "relativePath cannot be null"); | ||
|
||
return new JenkinsArtifact(displayPath, fileName, relativePath); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...src/main/java/com/nookure/staff/api/jenkins/json/serializer/JenkinsBuildDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.nookure.staff.api.jenkins.json.serializer; | ||
|
||
import com.google.gson.*; | ||
import com.nookure.staff.api.jenkins.json.JenkinsBuild; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public class JenkinsBuildDeserializer implements JsonDeserializer<JenkinsBuild> { | ||
@Override | ||
public JenkinsBuild deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { | ||
JsonObject jsonObject = jsonElement.getAsJsonObject(); | ||
|
||
return new JenkinsBuild( | ||
requireNonNull(jsonObject.get("_class").getAsString()), | ||
jsonObject.get("number").getAsInt(), | ||
requireNonNull(jsonObject.get("url").getAsString()) | ||
); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...I/src/main/java/com/nookure/staff/api/jenkins/json/serializer/JenkinsJobDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.nookure.staff.api.jenkins.json.serializer; | ||
|
||
import com.google.gson.*; | ||
import com.nookure.staff.api.jenkins.json.JenkinsBuild; | ||
import com.nookure.staff.api.jenkins.json.JenkinsJob; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public class JenkinsJobDeserializer implements JsonDeserializer<JenkinsJob> { | ||
@Override | ||
public JenkinsJob deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { | ||
JsonObject jsonObject = jsonElement.getAsJsonObject(); | ||
|
||
return new JenkinsJob( | ||
requireNonNull(jsonObject.get("displayName").getAsString()), | ||
requireNonNull(jsonObject.get("fullDisplayName").getAsString()), | ||
requireNonNull(jsonObject.get("name").getAsString()), | ||
requireNonNull(jsonObject.get("url").getAsString()), | ||
jsonDeserializationContext.deserialize(jsonObject.get("builds"), JenkinsBuild[].class), | ||
jsonDeserializationContext.deserialize(jsonObject.get("lastCompletedBuild"), JenkinsBuild.class), | ||
jsonDeserializationContext.deserialize(jsonObject.get("lastStableBuild"), JenkinsBuild.class), | ||
jsonDeserializationContext.deserialize(jsonObject.get("lastSuccessfulBuild"), JenkinsBuild.class) | ||
); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...I/src/main/java/com/nookure/staff/api/jenkins/json/serializer/JenkinsRunDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.nookure.staff.api.jenkins.json.serializer; | ||
|
||
import com.google.gson.*; | ||
import com.nookure.staff.api.jenkins.json.JenkinsArtifact; | ||
import com.nookure.staff.api.jenkins.json.JenkinsRun; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public class JenkinsRunDeserializer implements JsonDeserializer<JenkinsRun> { | ||
@Override | ||
public JenkinsRun deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { | ||
JsonObject jsonObject = jsonElement.getAsJsonObject(); | ||
|
||
String description = jsonObject.has("description") && !jsonObject.get("description").isJsonNull() | ||
? jsonObject.get("description").getAsString() | ||
: null; | ||
|
||
return new JenkinsRun( | ||
requireNonNull(jsonObject.get("_class").getAsString()), | ||
jsonObject.get("building").getAsBoolean(), | ||
description, | ||
jsonObject.get("duration").getAsLong(), | ||
requireNonNull(jsonObject.get("fullDisplayName").getAsString()), | ||
requireNonNull(jsonObject.get("url").getAsString()), | ||
jsonDeserializationContext.deserialize(jsonObject.get("artifacts"), JenkinsArtifact[].class) | ||
); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
NookureStaff-API/src/test/java/com/nookure/staff/api/jenkins/JenkinsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.nookure.staff.api.jenkins; | ||
|
||
import com.nookure.staff.Constants; | ||
import com.nookure.staff.api.jenkins.json.JenkinsJob; | ||
import com.nookure.staff.api.jenkins.json.JenkinsRun; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.condition.EnabledIf; | ||
|
||
public class JenkinsTest { | ||
private final String host = System.getenv("JENKINS_HOST") != null ? System.getenv("JENKINS_HOST") : "https://ci.nookure.com"; | ||
private final JenkinsBaseClient jenkinsBaseClient = new JenkinsBaseClient(host); | ||
|
||
@Test | ||
@EnabledIf("canRun") | ||
public void testGetJob() { | ||
final JenkinsJob jenkinsJob = jenkinsBaseClient.getJob("NookureStaff").join(); | ||
assert jenkinsJob != null; | ||
} | ||
|
||
@Test | ||
@EnabledIf("canRun") | ||
public void testGetRun() { | ||
final JenkinsJob jenkinsJob = jenkinsBaseClient.getJob(Constants.JENKINS_JOB_NAME).join(); | ||
final JenkinsRun jenkinsRun = jenkinsBaseClient.getRun(jenkinsJob, jenkinsJob.lastCompletedBuild().number()).join(); | ||
|
||
assert jenkinsRun != null; | ||
} | ||
|
||
public static boolean canRun() { | ||
return System.getenv("JENKINS_HOST") != null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters