Woodpecker-Include/src/main/java/io/gitlab/jfronny/woodpecker/include/Main.java

51 lines
2.2 KiB
Java
Raw Normal View History

2022-10-20 21:57:43 +02:00
package io.gitlab.jfronny.woodpecker.include;
import com.amihaiemil.eoyaml.*;
import io.gitlab.jfronny.commons.HttpUtils;
2022-10-22 13:55:42 +02:00
import io.gitlab.jfronny.commons.log.Logger;
2022-10-20 21:57:43 +02:00
import io.gitlab.jfronny.commons.serialize.Serializer;
import io.gitlab.jfronny.commons.serialize.gson.api.v1.GsonHolders;
import io.gitlab.jfronny.woodpecker.include.model.*;
import io.gitlab.jfronny.woodpecker.include.util.YamlTypeAdapter;
import net.freeutils.httpserver.HTTPServer;
import java.io.*;
import java.util.concurrent.atomic.AtomicBoolean;
public class Main {
2022-10-22 13:55:42 +02:00
public static final Logger LOG = Logger.forName("Woodpecker-Include");
2022-10-20 21:57:43 +02:00
public static void main(String[] args) throws IOException {
if (args.length != 1) {
2022-10-22 13:55:42 +02:00
LOG.error("Usage: woodpecker-include <port>");
2022-10-20 21:57:43 +02:00
return;
}
HTTPServer server = new HTTPServer(Integer.parseInt(args[0]));
HTTPServer.VirtualHost host = server.getVirtualHost(null);
GsonHolders.registerSerializer();
GsonHolders.registerTypeAdapter(YamlMapping.class, new YamlTypeAdapter());
HttpUtils.setUserAgent("Woodpecker-Include/1.0");
host.addContext("/ciconfig", (req, resp) -> {
try {
RequestModel request;
try (var isr = new InputStreamReader(req.getBody())) {
request = Serializer.getInstance().deserialize(isr, RequestModel.class);
}
AtomicBoolean changed = new AtomicBoolean(false);
ResponseModel response = new ResponseModel(request.configs.stream().mapMulti(new PipelineUnpacker(changed)).toList());
if (changed.get()) {
try (OutputStreamWriter writer = new OutputStreamWriter(resp.getBody())) {
Serializer.getInstance().serialize(response, writer);
}
2022-10-22 13:55:42 +02:00
LOG.info("Processed includes for pipeline for " + request.repo.namespace + "/" + request.repo.name);
2022-10-20 21:57:43 +02:00
return 0;
} else return 204;
} catch (UncheckedIOException e) {
throw e.getCause();
}
}, "POST");
server.start();
2022-10-22 13:55:42 +02:00
LOG.info("Running Woodpecker-Include");
2022-10-20 21:57:43 +02:00
}
}