Refactor some things
This commit is contained in:
parent
11c0a8a144
commit
4710d20b7d
|
@ -26,25 +26,45 @@ public class Main {
|
||||||
GsonHolders.registerTypeAdapter(YamlMapping.class, new YamlTypeAdapter());
|
GsonHolders.registerTypeAdapter(YamlMapping.class, new YamlTypeAdapter());
|
||||||
HttpUtils.setUserAgent("Woodpecker-Include/1.0");
|
HttpUtils.setUserAgent("Woodpecker-Include/1.0");
|
||||||
host.addContext("/ciconfig", (req, resp) -> {
|
host.addContext("/ciconfig", (req, resp) -> {
|
||||||
|
Ref ref = new Ref();
|
||||||
try {
|
try {
|
||||||
RequestModel request;
|
try {
|
||||||
try (var isr = new InputStreamReader(req.getBody())) {
|
return processRequest(req, resp, ref);
|
||||||
request = Serializer.getInstance().deserialize(isr, RequestModel.class);
|
} catch (UncheckedIOException e) {
|
||||||
|
throw e.getCause();
|
||||||
}
|
}
|
||||||
AtomicBoolean changed = new AtomicBoolean(false);
|
} catch (Throwable e) {
|
||||||
ResponseModel response = new ResponseModel(request.configs.stream().mapMulti(new PipelineUnpacker(changed)).toList());
|
if (ref.name == null) LOG.info("Could not process pipeline", e);
|
||||||
if (changed.get()) {
|
else LOG.info("Could not process pipeline for " + ref.name, e);
|
||||||
try (OutputStreamWriter writer = new OutputStreamWriter(resp.getBody())) {
|
throw e;
|
||||||
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");
|
}, "POST");
|
||||||
server.start();
|
server.start();
|
||||||
LOG.info("Running Woodpecker-Include");
|
LOG.info("Running Woodpecker-Include");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static int processRequest(HTTPServer.Request req, HTTPServer.Response resp, Ref ref) throws IOException {
|
||||||
|
RequestModel request;
|
||||||
|
try (var is = req.getBody()) {
|
||||||
|
String body = new String(is.readAllBytes());
|
||||||
|
request = Serializer.getInstance().deserialize(body, RequestModel.class);
|
||||||
|
}
|
||||||
|
ref.name = request.repo.full_name;
|
||||||
|
AtomicBoolean changed = new AtomicBoolean(false);
|
||||||
|
ResponseModel response = new ResponseModel(request.configs.stream().mapMulti(new PipelineUnpacker(changed)).toList());
|
||||||
|
if (changed.get()) {
|
||||||
|
resp.sendHeaders(200);
|
||||||
|
try (OutputStreamWriter writer = new OutputStreamWriter(resp.getBody())) {
|
||||||
|
Serializer.getInstance().serialize(response, writer);
|
||||||
|
}
|
||||||
|
LOG.info("Processed includes for pipeline for " + ref.name);
|
||||||
|
} else {
|
||||||
|
resp.sendHeaders(204);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class Ref {
|
||||||
|
public String name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,11 +9,19 @@ import java.io.*;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
import java.util.*;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
record PipelineUnpacker(AtomicBoolean changed) implements BiConsumer<Pipeline, Consumer<Pipeline>> {
|
final class PipelineUnpacker implements BiConsumer<Pipeline, Consumer<Pipeline>> {
|
||||||
|
private final AtomicBoolean changed;
|
||||||
|
private final Set<String> included = new HashSet<>();
|
||||||
|
|
||||||
|
public PipelineUnpacker(AtomicBoolean changed) {
|
||||||
|
this.changed = changed;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void accept(Pipeline pipeline, Consumer<Pipeline> pipelineConsumer) {
|
public void accept(Pipeline pipeline, Consumer<Pipeline> pipelineConsumer) {
|
||||||
try {
|
try {
|
||||||
|
@ -45,14 +53,18 @@ record PipelineUnpacker(AtomicBoolean changed) implements BiConsumer<Pipeline, C
|
||||||
download(node.asScalar().value(), null, pipelineConsumer);
|
download(node.asScalar().value(), null, pipelineConsumer);
|
||||||
}
|
}
|
||||||
} else throw new IllegalStateException("Unexpected value: " + include);
|
} else throw new IllegalStateException("Unexpected value: " + include);
|
||||||
// Remove include from original
|
if (pipeline.data.keys().size() > 1) {
|
||||||
Pipeline filtered = new Pipeline();
|
// More than just includes: generate override without include node
|
||||||
filtered.name = pipeline.name;
|
Pipeline filtered = new Pipeline();
|
||||||
pipeline.data = new FilteringYamlMapping(pipeline.data, s -> !s.asScalar().value().equals("include"));
|
filtered.name = pipeline.name;
|
||||||
pipelineConsumer.accept(filtered);
|
pipeline.data = new FilteringYamlMapping(pipeline.data, s -> !s.asScalar().value().equals("include"));
|
||||||
|
pipelineConsumer.accept(filtered);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void download(String url, String fileName, Consumer<Pipeline> pipelineConsumer) throws URISyntaxException, IOException {
|
private void download(String url, String fileName, Consumer<Pipeline> pipelineConsumer) throws URISyntaxException, IOException {
|
||||||
|
if (included.contains(url)) return;
|
||||||
|
included.add(url);
|
||||||
URI uri = new URI(url.replace(" ", "%20"));
|
URI uri = new URI(url.replace(" ", "%20"));
|
||||||
if (!"http".equalsIgnoreCase(uri.getScheme()) && !"https".equalsIgnoreCase(uri.getScheme())) {
|
if (!"http".equalsIgnoreCase(uri.getScheme()) && !"https".equalsIgnoreCase(uri.getScheme())) {
|
||||||
throw new URISyntaxException(url, "Could not find scheme");
|
throw new URISyntaxException(url, "Could not find scheme");
|
||||||
|
|
|
@ -3,34 +3,34 @@ package io.gitlab.jfronny.woodpecker.include.model;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Build {
|
public class Build {
|
||||||
public String author;
|
|
||||||
public String author_avatar;
|
|
||||||
public String author_email;
|
|
||||||
public String branch;
|
|
||||||
public List<String> changed_files;
|
|
||||||
public String commit;
|
|
||||||
public Long created_at;
|
|
||||||
public String deploy_to;
|
|
||||||
public Long enqueued_at;
|
|
||||||
public String error;
|
|
||||||
public String event;
|
|
||||||
public Long finished_at;
|
|
||||||
public Long id;
|
public Long id;
|
||||||
public String link_url;
|
|
||||||
public String message;
|
|
||||||
public Long number;
|
public Long number;
|
||||||
|
public String author;
|
||||||
public Long parent;
|
public Long parent;
|
||||||
|
public String event;
|
||||||
|
public String status;
|
||||||
|
public String error;
|
||||||
|
public Long enqueued_at;
|
||||||
|
public Long created_at;
|
||||||
|
public Long updated_at;
|
||||||
|
public Long started_at;
|
||||||
|
public Long finished_at;
|
||||||
|
public String deploy_to;
|
||||||
|
public String commit;
|
||||||
|
public String branch;
|
||||||
public String ref;
|
public String ref;
|
||||||
public String refspec;
|
public String refspec;
|
||||||
public String remote;
|
public String remote;
|
||||||
public Long reviewed_at;
|
|
||||||
public String reviewed_by;
|
|
||||||
public String sender;
|
|
||||||
public Boolean signed;
|
|
||||||
public Long started_at;
|
|
||||||
public String status;
|
|
||||||
public Long timestamp;
|
|
||||||
public String title;
|
public String title;
|
||||||
public Long updated_at;
|
public String message;
|
||||||
|
public Long timestamp;
|
||||||
|
public String sender;
|
||||||
|
public String author_avatar;
|
||||||
|
public String author_email;
|
||||||
|
public String link_url;
|
||||||
|
public Boolean signed;
|
||||||
public Boolean verified;
|
public Boolean verified;
|
||||||
|
public String reviewed_by;
|
||||||
|
public Long reviewed_at;
|
||||||
|
public List<String> changed_files;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,33 +2,26 @@ package io.gitlab.jfronny.woodpecker.include.model;
|
||||||
|
|
||||||
import io.gitlab.jfronny.gson.annotations.SerializedName;
|
import io.gitlab.jfronny.gson.annotations.SerializedName;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class Repo {
|
public class Repo {
|
||||||
public Long id;
|
public Long id;
|
||||||
public String uid;
|
public String owner;
|
||||||
public Long user_id;
|
|
||||||
public String namespace;
|
|
||||||
public String name;
|
public String name;
|
||||||
public String slug;
|
public String full_name;
|
||||||
public String scm;
|
public String avatar_url;
|
||||||
public String git_http_url;
|
public String link_url;
|
||||||
public String git_ssh_url;
|
public String clone_url;
|
||||||
public String link;
|
|
||||||
public String default_branch;
|
public String default_branch;
|
||||||
|
public String scm;
|
||||||
|
public Long timeout;
|
||||||
|
public String visibility;
|
||||||
@SerializedName("private")
|
@SerializedName("private")
|
||||||
public Boolean private$;
|
public Boolean private$;
|
||||||
public String visibility;
|
|
||||||
public Boolean active;
|
|
||||||
public String config;
|
|
||||||
public Boolean trusted;
|
public Boolean trusted;
|
||||||
@SerializedName("protected")
|
public Boolean gated;
|
||||||
public Boolean protected$;
|
public Boolean active;
|
||||||
public Boolean ignore_forks;
|
public Boolean allow_pr;
|
||||||
public Boolean ignore_pulls;
|
public String config_file;
|
||||||
public Boolean cancel_pulls;
|
public List<String> cancel_previous_pipeline_events;
|
||||||
public Long timeout;
|
|
||||||
public Long counter;
|
|
||||||
public Long synced;
|
|
||||||
public Long created;
|
|
||||||
public Long updated;
|
|
||||||
public Long version;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,6 @@ import java.util.List;
|
||||||
|
|
||||||
public class RequestModel {
|
public class RequestModel {
|
||||||
public Repo repo;
|
public Repo repo;
|
||||||
public Build build;
|
public Build pipeline;
|
||||||
public List<Pipeline> configs;
|
public List<Pipeline> configs;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue