package io.gitlab.jfronny.woodpecker.include; import com.amihaiemil.eoyaml.*; import io.gitlab.jfronny.commons.HttpUtils; import io.gitlab.jfronny.commons.log.Logger; 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 { public static final Logger LOG = Logger.forName("Woodpecker-Include"); public static void main(String[] args) throws IOException { if (args.length != 1) { LOG.error("Usage: woodpecker-include "); 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); } LOG.info("Processed includes for pipeline for " + request.repo.namespace + "/" + request.repo.name); return 0; } else return 204; } catch (UncheckedIOException e) { throw e.getCause(); } }, "POST"); server.start(); LOG.info("Running Woodpecker-Include"); } }