Browse Source

add patch trace handler and jobrequest response

master
Antoine 6 months ago
parent
commit
3262307ab7
Signed by: antoine <antoinroux@hotmail.fr> GPG Key ID: 098FB66FC0475E70
23 changed files with 279 additions and 52 deletions
  1. +16
    -0
      misc/requests/patch_job_trace.txt
  2. +0
    -0
      misc/requests/post_webhook_gitea.json
  3. +33
    -7
      src/main/java/tk/antoine_roux/wiki/ControllerHandlers.java
  4. +9
    -0
      src/main/java/tk/antoine_roux/wiki/configuration/Exception/InvalidObjectIdException.java
  5. +1
    -1
      src/main/java/tk/antoine_roux/wiki/configuration/WebConfiguration.java
  6. +0
    -20
      src/main/java/tk/antoine_roux/wiki/model/HookEvent.java
  7. +0
    -10
      src/main/java/tk/antoine_roux/wiki/model/UserReduced.java
  8. +1
    -1
      src/main/java/tk/antoine_roux/wiki/model/internal/Commit.java
  9. +56
    -0
      src/main/java/tk/antoine_roux/wiki/model/internal/HookEvent.java
  10. +1
    -1
      src/main/java/tk/antoine_roux/wiki/model/internal/InternalTracker.java
  11. +93
    -0
      src/main/java/tk/antoine_roux/wiki/model/internal/Job.java
  12. +12
    -0
      src/main/java/tk/antoine_roux/wiki/model/internal/JobStatus.java
  13. +1
    -1
      src/main/java/tk/antoine_roux/wiki/model/internal/Permission.java
  14. +1
    -2
      src/main/java/tk/antoine_roux/wiki/model/internal/Repository.java
  15. +1
    -1
      src/main/java/tk/antoine_roux/wiki/model/internal/Runner.java
  16. +5
    -1
      src/main/java/tk/antoine_roux/wiki/model/internal/User.java
  17. +16
    -0
      src/main/java/tk/antoine_roux/wiki/model/internal/UserReduced.java
  18. +0
    -1
      src/main/java/tk/antoine_roux/wiki/model/request/AddRunner.java
  19. +0
    -2
      src/main/java/tk/antoine_roux/wiki/model/request/JobRequest.java
  20. +1
    -1
      src/main/java/tk/antoine_roux/wiki/model/request/RunnerInfo.java
  21. +27
    -0
      src/main/java/tk/antoine_roux/wiki/service/JobManager.java
  22. +2
    -2
      src/main/java/tk/antoine_roux/wiki/service/RunnerRegistrar.java
  23. +3
    -1
      src/main/java/tk/antoine_roux/wiki/utilitary/Constant.java

+ 16
- 0
misc/requests/patch_job_trace.txt View 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

misc/requests/post_webhook_gitea.txt → misc/requests/post_webhook_gitea.json View File


+ 33
- 7
src/main/java/tk/antoine_roux/wiki/ControllerHandlers.java View File

@@ -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();
}
}

+ 9
- 0
src/main/java/tk/antoine_roux/wiki/configuration/Exception/InvalidObjectIdException.java View File

@@ -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");
}
}

+ 1
- 1
src/main/java/tk/antoine_roux/wiki/configuration/WebConfiguration.java View File

@@ -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


+ 0
- 20
src/main/java/tk/antoine_roux/wiki/model/HookEvent.java View File

@@ -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;
}

+ 0
- 10
src/main/java/tk/antoine_roux/wiki/model/UserReduced.java View File

@@ -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;
}

src/main/java/tk/antoine_roux/wiki/model/Commit.java → src/main/java/tk/antoine_roux/wiki/model/internal/Commit.java View File

@@ -1,4 +1,4 @@
package tk.antoine_roux.wiki.model;
package tk.antoine_roux.wiki.model.internal;

import com.fasterxml.jackson.annotation.JsonFormat;


+ 56
- 0
src/main/java/tk/antoine_roux/wiki/model/internal/HookEvent.java View File

@@ -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;
}
}

src/main/java/tk/antoine_roux/wiki/model/InternalTracker.java → src/main/java/tk/antoine_roux/wiki/model/internal/InternalTracker.java View File

@@ -1,4 +1,4 @@
package tk.antoine_roux.wiki.model;
package tk.antoine_roux.wiki.model.internal;

import com.fasterxml.jackson.annotation.JsonProperty;


+ 93
- 0
src/main/java/tk/antoine_roux/wiki/model/internal/Job.java View 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;
}
}
}

+ 12
- 0
src/main/java/tk/antoine_roux/wiki/model/internal/JobStatus.java View File

@@ -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
}

src/main/java/tk/antoine_roux/wiki/Permission.java → src/main/java/tk/antoine_roux/wiki/model/internal/Permission.java View File

@@ -1,4 +1,4 @@
package tk.antoine_roux.wiki;
package tk.antoine_roux.wiki.model.internal;

/**
* model about repository permission

src/main/java/tk/antoine_roux/wiki/model/Repository.java → src/main/java/tk/antoine_roux/wiki/model/internal/Repository.java View File

@@ -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;


src/main/java/tk/antoine_roux/wiki/model/Runner.java → src/main/java/tk/antoine_roux/wiki/model/internal/Runner.java View File

@@ -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;


src/main/java/tk/antoine_roux/wiki/model/User.java → src/main/java/tk/antoine_roux/wiki/model/internal/User.java View File

@@ -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);
}
}

+ 16
- 0
src/main/java/tk/antoine_roux/wiki/model/internal/UserReduced.java View File

@@ -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;
}
}

+ 0
- 1
src/main/java/tk/antoine_roux/wiki/model/request/AddRunner.java View File

@@ -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;


+ 0
- 2
src/main/java/tk/antoine_roux/wiki/model/request/JobRequest.java View File

@@ -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
*/


src/main/java/tk/antoine_roux/wiki/model/RunnerInfo.java → src/main/java/tk/antoine_roux/wiki/model/request/RunnerInfo.java View File

@@ -1,4 +1,4 @@
package tk.antoine_roux.wiki.model;
package tk.antoine_roux.wiki.model.request;

import java.util.Map;


+ 27
- 0
src/main/java/tk/antoine_roux/wiki/service/JobManager.java View 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());
}
}

src/main/java/tk/antoine_roux/wiki/RunnerRegistrar.java → src/main/java/tk/antoine_roux/wiki/service/RunnerRegistrar.java View File

@@ -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;


src/main/java/tk/antoine_roux/wiki/Constant.java → src/main/java/tk/antoine_roux/wiki/utilitary/Constant.java View File

@@ -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…
Cancel
Save