57 lines
1.8 KiB
Java
57 lines
1.8 KiB
Java
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;
|
|
}
|
|
}
|