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());
|
||||
HttpUtils.setUserAgent("Woodpecker-Include/1.0");
|
||||
host.addContext("/ciconfig", (req, resp) -> {
|
||||
Ref ref = new Ref();
|
||||
try {
|
||||
RequestModel request;
|
||||
try (var isr = new InputStreamReader(req.getBody())) {
|
||||
request = Serializer.getInstance().deserialize(isr, RequestModel.class);
|
||||
try {
|
||||
return processRequest(req, resp, ref);
|
||||
} catch (UncheckedIOException e) {
|
||||
throw e.getCause();
|
||||
}
|
||||
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();
|
||||
} catch (Throwable e) {
|
||||
if (ref.name == null) LOG.info("Could not process pipeline", e);
|
||||
else LOG.info("Could not process pipeline for " + ref.name, e);
|
||||
throw e;
|
||||
}
|
||||
}, "POST");
|
||||
server.start();
|
||||
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.URISyntaxException;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.BiConsumer;
|
||||
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
|
||||
public void accept(Pipeline pipeline, Consumer<Pipeline> pipelineConsumer) {
|
||||
try {
|
||||
|
@ -45,14 +53,18 @@ record PipelineUnpacker(AtomicBoolean changed) implements BiConsumer<Pipeline, C
|
|||
download(node.asScalar().value(), null, pipelineConsumer);
|
||||
}
|
||||
} else throw new IllegalStateException("Unexpected value: " + include);
|
||||
// Remove include from original
|
||||
Pipeline filtered = new Pipeline();
|
||||
filtered.name = pipeline.name;
|
||||
pipeline.data = new FilteringYamlMapping(pipeline.data, s -> !s.asScalar().value().equals("include"));
|
||||
pipelineConsumer.accept(filtered);
|
||||
if (pipeline.data.keys().size() > 1) {
|
||||
// More than just includes: generate override without include node
|
||||
Pipeline filtered = new Pipeline();
|
||||
filtered.name = pipeline.name;
|
||||
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 {
|
||||
if (included.contains(url)) return;
|
||||
included.add(url);
|
||||
URI uri = new URI(url.replace(" ", "%20"));
|
||||
if (!"http".equalsIgnoreCase(uri.getScheme()) && !"https".equalsIgnoreCase(uri.getScheme())) {
|
||||
throw new URISyntaxException(url, "Could not find scheme");
|
||||
|
|
|
@ -3,34 +3,34 @@ package io.gitlab.jfronny.woodpecker.include.model;
|
|||
import java.util.List;
|
||||
|
||||
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 String link_url;
|
||||
public String message;
|
||||
public Long number;
|
||||
public String author;
|
||||
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 refspec;
|
||||
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 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 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 java.util.List;
|
||||
|
||||
public class Repo {
|
||||
public Long id;
|
||||
public String uid;
|
||||
public Long user_id;
|
||||
public String namespace;
|
||||
public String owner;
|
||||
public String name;
|
||||
public String slug;
|
||||
public String scm;
|
||||
public String git_http_url;
|
||||
public String git_ssh_url;
|
||||
public String link;
|
||||
public String full_name;
|
||||
public String avatar_url;
|
||||
public String link_url;
|
||||
public String clone_url;
|
||||
public String default_branch;
|
||||
public String scm;
|
||||
public Long timeout;
|
||||
public String visibility;
|
||||
@SerializedName("private")
|
||||
public Boolean private$;
|
||||
public String visibility;
|
||||
public Boolean active;
|
||||
public String config;
|
||||
public Boolean trusted;
|
||||
@SerializedName("protected")
|
||||
public Boolean protected$;
|
||||
public Boolean ignore_forks;
|
||||
public Boolean ignore_pulls;
|
||||
public Boolean cancel_pulls;
|
||||
public Long timeout;
|
||||
public Long counter;
|
||||
public Long synced;
|
||||
public Long created;
|
||||
public Long updated;
|
||||
public Long version;
|
||||
public Boolean gated;
|
||||
public Boolean active;
|
||||
public Boolean allow_pr;
|
||||
public String config_file;
|
||||
public List<String> cancel_previous_pipeline_events;
|
||||
}
|
||||
|
|
|
@ -4,6 +4,6 @@ import java.util.List;
|
|||
|
||||
public class RequestModel {
|
||||
public Repo repo;
|
||||
public Build build;
|
||||
public Build pipeline;
|
||||
public List<Pipeline> configs;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue