add patch trace handler and jobrequest response
This commit is contained in:
parent
4a4f5323a7
commit
3262307ab7
16
misc/requests/patch_job_trace.txt
Normal file
16
misc/requests/patch_job_trace.txt
Normal file
@ -0,0 +1,16 @@
|
||||
[PATCH /api/v4/jobs/0/trace HTTP/1.1
|
||||
Host: 172.17.0.1:8080
|
||||
User-Agent: gitlab-runner 13.3.1 (13-3-stable; go1.13.8; linux/amd64)
|
||||
Content-Length: 363
|
||||
Content-Range: 0-362
|
||||
Content-Type: text/plain
|
||||
Job-Token:
|
||||
Accept-Encoding: gzip]
|
||||
|
||||
Running with gitlab-runner 13.3.1 (738bbe5a)
|
||||
on manualy registered gitlab runner f336d593
|
||||
Preparing the "shell" executor
|
||||
Using Shell executor...
|
||||
Preparing environment
|
||||
Running on 94afd2dbc667...
|
||||
ERROR: Job failed: panic: runtime error: slice bounds out of range [:8] with length 0
|
@ -9,19 +9,23 @@ import org.springframework.web.bind.annotation.*;
|
||||
import tk.antoine_roux.wiki.annotation.ApiPrefix;
|
||||
import tk.antoine_roux.wiki.annotation.ApiVersion;
|
||||
import tk.antoine_roux.wiki.configuration.Exception.DeleteRunnerException;
|
||||
import tk.antoine_roux.wiki.model.HookEvent;
|
||||
import tk.antoine_roux.wiki.model.Runner;
|
||||
import tk.antoine_roux.wiki.model.internal.HookEvent;
|
||||
import tk.antoine_roux.wiki.model.internal.Job;
|
||||
import tk.antoine_roux.wiki.model.internal.Runner;
|
||||
import tk.antoine_roux.wiki.model.request.AddRunner;
|
||||
import tk.antoine_roux.wiki.model.request.JobRequest;
|
||||
import tk.antoine_roux.wiki.model.request.TokenRunner;
|
||||
import tk.antoine_roux.wiki.model.response.RegisterRunnerResponse;
|
||||
import tk.antoine_roux.wiki.service.JobManager;
|
||||
import tk.antoine_roux.wiki.service.RunnerRegistrar;
|
||||
import tk.antoine_roux.wiki.utilitary.Boolean;
|
||||
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import static tk.antoine_roux.wiki.Constant.*;
|
||||
import static tk.antoine_roux.wiki.utilitary.Constant.*;
|
||||
|
||||
@RestController
|
||||
@ApiPrefix(API_PREFIX)
|
||||
@ -30,10 +34,12 @@ public class ControllerHandlers {
|
||||
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
|
||||
|
||||
private final RunnerRegistrar runnerRegistrar;
|
||||
private final JobManager jobManager;
|
||||
|
||||
@Autowired
|
||||
public ControllerHandlers(RunnerRegistrar runnerRegistrar) {
|
||||
public ControllerHandlers(RunnerRegistrar runnerRegistrar, JobManager jobManager) {
|
||||
this.runnerRegistrar = runnerRegistrar;
|
||||
this.jobManager = jobManager;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -96,13 +102,33 @@ public class ControllerHandlers {
|
||||
|
||||
@ApiVersion(API_VERSION)
|
||||
@PostMapping("/jobs/request")
|
||||
public ResponseEntity<Void> jobRequest(@RequestBody JobRequest jobRequest) {
|
||||
return ResponseEntity.ok().build();
|
||||
public ResponseEntity<Job> jobRequest(@RequestBody JobRequest jobRequest) {
|
||||
Optional<Job> currentJob = this.jobManager.popJob(jobRequest);
|
||||
return currentJob
|
||||
.map(job -> ResponseEntity.status(HttpStatus.CREATED).body(job))
|
||||
.orElseGet(() -> ResponseEntity.noContent().build());
|
||||
}
|
||||
|
||||
@ApiVersion(API_VERSION)
|
||||
@PatchMapping("jobs/0/trace")
|
||||
public ResponseEntity<Void> receiveTrace(@RequestBody String traceContent) {
|
||||
logger.info(traceContent);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).build();
|
||||
}
|
||||
|
||||
@ApiVersion(API_VERSION)
|
||||
@PostMapping("/webhook")
|
||||
public ResponseEntity<Void> webhook(@RequestBody HookEvent webHookData) {
|
||||
return ResponseEntity.ok().build();
|
||||
ResponseEntity.BodyBuilder responseEntity;
|
||||
Optional<Job> job = webHookData.toJob();
|
||||
|
||||
if (job.isPresent()) {
|
||||
this.jobManager.stackJob(job.get());
|
||||
responseEntity = ResponseEntity.ok();
|
||||
} else {
|
||||
responseEntity = ResponseEntity.unprocessableEntity();
|
||||
}
|
||||
|
||||
return responseEntity.build();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,9 @@
|
||||
package tk.antoine_roux.wiki.configuration.Exception;
|
||||
|
||||
public class InvalidObjectIdException extends RuntimeException {
|
||||
private static final long serialVersionUID = 6274838148439186894L;
|
||||
|
||||
public InvalidObjectIdException(String commitStr) {
|
||||
super("Try to convert invalid string (" + commitStr + ") to commit id");
|
||||
}
|
||||
}
|
@ -3,7 +3,7 @@ package tk.antoine_roux.wiki.configuration;
|
||||
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||
import tk.antoine_roux.wiki.Constant;
|
||||
import tk.antoine_roux.wiki.utilitary.Constant;
|
||||
|
||||
/**
|
||||
* spring web configuration
|
||||
|
@ -1,20 +0,0 @@
|
||||
package tk.antoine_roux.wiki.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class HookEvent {
|
||||
public String secret;
|
||||
public String ref;
|
||||
public String before;
|
||||
public String after;
|
||||
@JsonProperty("compare_url")
|
||||
public String compareUrl;
|
||||
public List<Commit> commits;
|
||||
@JsonProperty("head_commit")
|
||||
public String headCommit;
|
||||
public Repository repository;
|
||||
public User pusher;
|
||||
public User sender;
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
package tk.antoine_roux.wiki.model;
|
||||
|
||||
/**
|
||||
* Reduced user information
|
||||
*/
|
||||
public class UserReduced {
|
||||
public String name;
|
||||
public String email;
|
||||
public String username;
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package tk.antoine_roux.wiki.model;
|
||||
package tk.antoine_roux.wiki.model.internal;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
|
@ -0,0 +1,56 @@
|
||||
package tk.antoine_roux.wiki.model.internal;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class HookEvent {
|
||||
public String secret;
|
||||
public String ref;
|
||||
public String before;
|
||||
public String after;
|
||||
@JsonProperty("compare_url")
|
||||
public String compareUrl;
|
||||
public List<Commit> commits;
|
||||
@JsonProperty("head_commit")
|
||||
public String headCommit;
|
||||
public Repository repository;
|
||||
public User pusher;
|
||||
public User sender;
|
||||
|
||||
AtomicInteger idIncrementer = new AtomicInteger();
|
||||
|
||||
/**
|
||||
* convert {@link HookEvent} to {@link Job} if possible
|
||||
* else return and {@link Optional#empty()}
|
||||
*/
|
||||
public Optional<Job> toJob() {
|
||||
Optional<Job> optJob;
|
||||
|
||||
if (this.commits.isEmpty()) {
|
||||
optJob = Optional.empty();
|
||||
} else {
|
||||
// search for head commit or take first in event's list of commit
|
||||
Commit commit = this.commits.stream().filter(co -> co.id.equals(this.headCommit))
|
||||
.findFirst().orElse(this.commits.get(0));
|
||||
|
||||
Job.Commit co = new Job.Commit(
|
||||
commit.author.email, commit.author.name, commit.timestamp,
|
||||
commit.id, commit.message, commit.id.substring(0, 8), commit.message
|
||||
);
|
||||
|
||||
Job job = new Job(
|
||||
null, co, null, ZonedDateTime.now(), null,
|
||||
this.idIncrementer.getAndIncrement(), UUID.randomUUID().toString(), this.ref,
|
||||
null, "root", null, JobStatus.CREATED, false, this.pusher.toReducedUser()
|
||||
);
|
||||
optJob = Optional.of(job);
|
||||
}
|
||||
|
||||
return optJob;
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package tk.antoine_roux.wiki.model;
|
||||
package tk.antoine_roux.wiki.model.internal;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
93
src/main/java/tk/antoine_roux/wiki/model/internal/Job.java
Normal file
93
src/main/java/tk/antoine_roux/wiki/model/internal/Job.java
Normal file
@ -0,0 +1,93 @@
|
||||
package tk.antoine_roux.wiki.model.internal;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
// ID int `json:"id"`
|
||||
// Token string `json:"token"`
|
||||
// AllowGitFetch bool `json:"allow_git_fetch"`
|
||||
// JobInfo JobInfo `json:"job_info"`
|
||||
// GitInfo GitInfo `json:"git_info"`
|
||||
// RunnerInfo RunnerInfo `json:"runner_info"`
|
||||
// Variables JobVariables `json:"variables"`
|
||||
// Steps Steps `json:"steps"`
|
||||
// Image Image `json:"image"`
|
||||
// Services Services `json:"services"`
|
||||
// Artifacts Artifacts `json:"artifacts"`
|
||||
// Cache Caches `json:"cache"`
|
||||
// Credentials []Credentials `json:"credentials"`
|
||||
// Dependencies Dependencies `json:"dependencies"`
|
||||
// Features GitlabFeatures `json:"features"`
|
||||
// Secrets Secrets `json:"secrets,omitempty"`
|
||||
|
||||
public class Job {
|
||||
@JsonProperty("artifacts_file")
|
||||
public String artifactsFile;
|
||||
public Commit commit;
|
||||
public String coverage;
|
||||
@JsonProperty("created_at")
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssSSSXXX")
|
||||
public ZonedDateTime createdAt;
|
||||
@JsonProperty("finished_at")
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssSSSXXX")
|
||||
public ZonedDateTime finishedAt;
|
||||
public Integer id;
|
||||
public String name;
|
||||
public String ref;
|
||||
public Runner runner;
|
||||
public String stage;
|
||||
@JsonProperty("started_at")
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssSSSXXX")
|
||||
public ZonedDateTime startedAt;
|
||||
public JobStatus status;
|
||||
public boolean tag;
|
||||
public UserReduced user;
|
||||
|
||||
public Job(
|
||||
String artifactsFile, Commit commit, String coverage, ZonedDateTime createdAt, ZonedDateTime finishedAt,
|
||||
Integer id, String name, String ref, Runner runner, String stage, ZonedDateTime startedAt, JobStatus status,
|
||||
boolean tag, UserReduced user
|
||||
) {
|
||||
this.artifactsFile = artifactsFile;
|
||||
this.commit = commit;
|
||||
this.coverage = coverage;
|
||||
this.createdAt = createdAt;
|
||||
this.finishedAt = finishedAt;
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.ref = ref;
|
||||
this.runner = runner;
|
||||
this.stage = stage;
|
||||
this.startedAt = startedAt;
|
||||
this.status = status;
|
||||
this.tag = tag;
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public static class Commit {
|
||||
@JsonProperty("author_email")
|
||||
public String authorEmail;
|
||||
@JsonProperty("author_name")
|
||||
public String authorName;
|
||||
@JsonProperty("created_at")
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssSSSXXX")
|
||||
public ZonedDateTime createdAt;
|
||||
public String id;
|
||||
public String message;
|
||||
@JsonProperty("short_id")
|
||||
public String shortId;
|
||||
public String title;
|
||||
|
||||
public Commit(String authorEmail, String authorName, ZonedDateTime createdAt, String id, String message, String shortId, String title) {
|
||||
this.authorEmail = authorEmail;
|
||||
this.authorName = authorName;
|
||||
this.createdAt = createdAt;
|
||||
this.id = id;
|
||||
this.message = message;
|
||||
this.shortId = shortId;
|
||||
this.title = title;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package tk.antoine_roux.wiki.model.internal;
|
||||
|
||||
/**
|
||||
* This enum represent all state a job could take during
|
||||
* his lifecycle
|
||||
*/
|
||||
public enum JobStatus {
|
||||
CREATED,
|
||||
STARTED,
|
||||
STOPPED,
|
||||
FINISHED
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package tk.antoine_roux.wiki;
|
||||
package tk.antoine_roux.wiki.model.internal;
|
||||
|
||||
/**
|
||||
* model about repository permission
|
@ -1,7 +1,6 @@
|
||||
package tk.antoine_roux.wiki.model;
|
||||
package tk.antoine_roux.wiki.model.internal;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import tk.antoine_roux.wiki.Permission;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package tk.antoine_roux.wiki.model;
|
||||
package tk.antoine_roux.wiki.model.internal;
|
||||
|
||||
import tk.antoine_roux.wiki.model.response.RegisterRunnerResponse;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package tk.antoine_roux.wiki.model;
|
||||
package tk.antoine_roux.wiki.model.internal;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
@ -22,4 +22,8 @@ public class User {
|
||||
@JsonProperty("last_login")
|
||||
public ZonedDateTime lastLogin;
|
||||
public ZonedDateTime created;
|
||||
|
||||
public UserReduced toReducedUser() {
|
||||
return new UserReduced(this.login, this.email, this.username);
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package tk.antoine_roux.wiki.model.internal;
|
||||
|
||||
/**
|
||||
* Reduced user information
|
||||
*/
|
||||
public class UserReduced {
|
||||
public String name;
|
||||
public String email;
|
||||
public String username;
|
||||
|
||||
public UserReduced(String name, String email, String username) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
this.username = username;
|
||||
}
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
package tk.antoine_roux.wiki.model.request;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import tk.antoine_roux.wiki.model.RunnerInfo;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
@ -1,7 +1,5 @@
|
||||
package tk.antoine_roux.wiki.model.request;
|
||||
|
||||
import tk.antoine_roux.wiki.model.RunnerInfo;
|
||||
|
||||
/**
|
||||
* Job Request compose from {@link RunnerInfo} and token field
|
||||
*/
|
||||
|
@ -1,4 +1,4 @@
|
||||
package tk.antoine_roux.wiki.model;
|
||||
package tk.antoine_roux.wiki.model.request;
|
||||
|
||||
import java.util.Map;
|
||||
|
27
src/main/java/tk/antoine_roux/wiki/service/JobManager.java
Normal file
27
src/main/java/tk/antoine_roux/wiki/service/JobManager.java
Normal file
@ -0,0 +1,27 @@
|
||||
package tk.antoine_roux.wiki.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import tk.antoine_roux.wiki.model.internal.HookEvent;
|
||||
import tk.antoine_roux.wiki.model.internal.Job;
|
||||
import tk.antoine_roux.wiki.model.request.JobRequest;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
@Service
|
||||
public class JobManager {
|
||||
|
||||
/**
|
||||
* concurrent list of {@link Job} fill by {@link tk.antoine_roux.wiki.ControllerHandlers#webhook(HookEvent)}
|
||||
* and pop by {@link tk.antoine_roux.wiki.ControllerHandlers#jobRequest(JobRequest)}
|
||||
*/
|
||||
ConcurrentLinkedQueue<Job> jobQueue = new ConcurrentLinkedQueue<>();
|
||||
|
||||
public void stackJob(Job newJob) {
|
||||
this.jobQueue.add(newJob);
|
||||
}
|
||||
|
||||
public Optional<Job> popJob(JobRequest jobRequest) {
|
||||
return Optional.ofNullable(this.jobQueue.poll());
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
package tk.antoine_roux.wiki;
|
||||
package tk.antoine_roux.wiki.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import tk.antoine_roux.wiki.model.Runner;
|
||||
import tk.antoine_roux.wiki.model.internal.Runner;
|
||||
import tk.antoine_roux.wiki.model.request.AddRunner;
|
||||
import tk.antoine_roux.wiki.model.request.TokenRunner;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package tk.antoine_roux.wiki;
|
||||
package tk.antoine_roux.wiki.utilitary;
|
||||
|
||||
/**
|
||||
* Application level constant
|
||||
@ -8,4 +8,6 @@ public final class Constant {
|
||||
public static final String API_NAME = "gitlab-runner-gateway";
|
||||
public static final String API_VERSION = "4";
|
||||
public static final String VERSION_PREFIX = "v";
|
||||
|
||||
public static final int OBJECT_ID_STRING_LENGTH = 40;
|
||||
}
|
Loading…
Reference in New Issue
Block a user