Working prototype!

This commit is contained in:
2022-11-16 23:10:43 -05:00
commit a88e191a9f
27 changed files with 1304 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
package tech.nevets.codebadgerestapi;
public enum BadgeColor {
RED,
ORANGE,
YELLOW,
GREEN,
BLUE,
PURPLE,
LIGHTGRAY,
GRAY
}

View File

@@ -0,0 +1,156 @@
package tech.nevets.codebadgerestapi;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class BadgeFactory {
public static byte[] generateBadge(String leftStr, String rightStr, BadgeColor secondaryColor) {
// TODO: 11/16/2022 Make this automatically adjust the width of the badge depending on how long both strings are
// TODO: 11/16/2022 Make the secondary color always adjust to half the total width of the badge
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics2D = img.createGraphics();
Font font = new Font("Calibri", Font.PLAIN, 6);
graphics2D.setFont(font);
FontMetrics fontMetrics = graphics2D.getFontMetrics();
int width = fontMetrics.stringWidth(leftStr + rightStr);
int height = fontMetrics.getHeight();
graphics2D.dispose();
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
graphics2D = img.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics2D.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
graphics2D.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
graphics2D.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics2D.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
graphics2D.setFont(font);
fontMetrics = graphics2D.getFontMetrics();
graphics2D.setColor(Color.BLACK);
graphics2D.drawString(leftStr + " " + rightStr, 0, fontMetrics.getAscent());
graphics2D.dispose();
ByteArrayOutputStream baos;
try {
baos = new ByteArrayOutputStream();
ImageIO.write(img, "png", baos);
} catch (IOException e) {
e.printStackTrace();
LoggerFactory.getLogger(BadgeFactory.class).info("not good thingy happened...");
return new byte[]{};
}
return baos.toByteArray();
}
private static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth) {
BufferedImage resizedImage = new BufferedImage(targetWidth, 25, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics2D = resizedImage.createGraphics();
graphics2D.drawImage(originalImage, 0, 0, targetWidth, 25, null);
graphics2D.dispose();
return resizedImage;
}
public static String getVersionFromGitRepo(String gitUrl, String org, String repo, String branch) {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.GET()
.header("accept", "text/plain")
.uri(URI.create("https://" + gitUrl + "/" + org + "/" + repo + "/raw/branch/" + branch + "/build.gradle"))
.build();
String responseString = "";
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
responseString = response.body();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
String[] split = responseString.split("\n");
String version = null;
for (String str : split) {
if (str.startsWith("version")) {
version = str.substring(9, str.length() - 1);
}
}
return version;
}
public static String getLatestBuildStatusFromJenkins(String jenkinsUrl, String projectName) {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.GET()
.header("accept", "application/json")
.uri(URI.create("https://" + jenkinsUrl + "/job/" + projectName + "/lastBuild/api/json"))
.build();
String responseString = "";
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
responseString = response.body();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
JsonObject json = new Gson().fromJson(responseString, JsonObject.class);
return json.get("result").getAsString();
}
public static String getLatestSuccessfulVersionFromJenkins(String jenkinsUrl, String projectName) {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.GET()
.header("accept", "application/json")
.uri(URI.create("https://" + jenkinsUrl + "/job/" + projectName + "/lastSuccessfulBuild/api/json"))
.build();
String responseString = "";
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
responseString = response.body();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
JsonObject json = new Gson().fromJson(responseString, JsonObject.class);
String gitUrl = json.get("actions").getAsJsonArray().get(2).getAsJsonObject().get("remoteUrls").getAsJsonArray().get(0).getAsString();
gitUrl = gitUrl.substring(0, gitUrl.length() - 4);
String commit = json.get("changeSet").getAsJsonObject().get("items").getAsJsonArray().get(0).getAsJsonObject().get("commitId").getAsString();
HttpClient gitClient = HttpClient.newHttpClient();
HttpRequest gitRequest = HttpRequest.newBuilder()
.GET()
.header("accept", "text/plain")
.uri(URI.create(gitUrl + "/raw/commit/" + commit + "/build.gradle"))
.build();
String gitResponseString = "";
try {
HttpResponse<String> response = gitClient.send(gitRequest, HttpResponse.BodyHandlers.ofString());
gitResponseString = response.body();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
String[] split = gitResponseString.split("\n");
String version = null;
for (String str : split) {
if (str.startsWith("version")) {
version = str.substring(9, str.length() - 1);
}
}
return version;
}
}

View File

@@ -0,0 +1,69 @@
package tech.nevets.codebadgerestapi;
import static spark.Spark.*;
public class Server {
public static void main(String[] args) {
path("/badge", () -> {
before("/*");
get("/version/*/:gitUrl/:org/:repo/:branch", (req, res) -> {
switch (req.splat()[0]) {
case "gitea" -> {
return BadgeFactory.getVersionFromGitRepo(req.params(":gitUrl"), req.params(":org"), req.params(":repo"), req.params(":branch"));
}
case "github" -> {
res.status(405);
return "Not yet Implemented!";
}
case "gitlab" -> {
res.status(405);
return "Not yet Implemented!";
}
case "test" -> {
res.type("image/png");
String version = BadgeFactory.getVersionFromGitRepo(req.params(":gitUrl"), req.params(":org"), req.params(":repo"), req.params(":branch"));
return BadgeFactory.generateBadge("Version", version, BadgeColor.GREEN);
}
}
res.status(404);
return "Not Found!";
});
get("/buildStatus/*/:ciUrl/:projectName", (req, res) -> {
switch (req.splat()[0]) {
case "jenkins" -> {
return BadgeFactory.getLatestBuildStatusFromJenkins(req.params(":ciUrl"), req.params(":projectName"));
}
case "circleci" -> {
res.status(405);
return "Not yet Implemented!";
}
case "teamcity" -> {
res.status(405);
return "Not yet Implemented!";
}
}
res.status(404);
return "Not Found!";
});
get("/latestSuccessfulVersion/*/:ciUrl/:projectName", (req, res) -> {
switch (req.splat()[0]) {
case "jenkins" -> {
return BadgeFactory.getLatestSuccessfulVersionFromJenkins(req.params(":ciUrl"), req.params(":projectName"));
}
case "circleci" -> {
res.status(405);
return "Not yet Implemented!";
}
case "teamcity" -> {
res.status(405);
return "Not yet Implemented!";
}
}
res.status(404);
return "Not Found!";
});
});
}
}

View File

@@ -0,0 +1,11 @@
package tech.nevets.codebadgerestapi;
import java.awt.*;
public class askjdf {
public static void main(String[] args) {
for (Font font : GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts()) {
System.out.println(font.getName());
}
}
}