Convert the GitLab web hook data within the trigger handlers to the new CauseData

- this reduce the null checks so the API model classes can be simple POJOs again
This commit is contained in:
Robin Müller 2016-03-20 16:21:09 +01:00
parent c2a12d5e0e
commit 108ad736c4
31 changed files with 874 additions and 1081 deletions

View File

@ -4,12 +4,9 @@ import com.dabsquared.gitlabjenkins.gitlab.api.GitLabApi;
import com.dabsquared.gitlabjenkins.gitlab.api.model.Branch;
import com.dabsquared.gitlabjenkins.util.LoggerUtil;
import com.dabsquared.gitlabjenkins.util.ProjectIdUtil;
import com.google.common.base.Optional;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.WebApplicationException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
@ -71,10 +68,7 @@ public class GitLabProjectBranchesService {
List<String> result = new ArrayList<>();
String projectId = ProjectIdUtil.retrieveProjectId(sourceRepository);
for (Branch branch : client.getBranches(projectId)) {
Optional<String> name = branch.optName();
if (name.isPresent()) {
result.add(name.get());
}
result.add(branch.getName());
}
LOGGER.log(Level.FINEST, "found these branches for repo {0} : {1}", LoggerUtil.toArray(sourceRepository, result));
return result;

View File

@ -1,130 +0,0 @@
package com.dabsquared.gitlabjenkins.cause;
import com.dabsquared.gitlabjenkins.gitlab.api.model.MergeRequestHook;
import com.dabsquared.gitlabjenkins.gitlab.api.model.ObjectAttributes;
import hudson.init.InitMilestone;
import hudson.init.Initializer;
import hudson.model.Run;
import java.io.File;
import java.io.IOException;
/**
* @author Robin Müller
*/
public class GitLabMergeCause extends GitLabWebHookCause<MergeRequestHook> {
public GitLabMergeCause(MergeRequestHook mergeRequestHook) {
this(mergeRequestHook, "");
}
public GitLabMergeCause(MergeRequestHook mergeRequestHook, String pollingLog) {
super(mergeRequestHook, pollingLog);
}
public GitLabMergeCause(MergeRequestHook mergeRequestHook, File logFile) throws IOException {
super(mergeRequestHook, logFile);
}
@Override
public String getBranch() {
return getObjectAttributes().optSourceBranch().orNull();
}
@Override
public String getSourceBranch() {
return getObjectAttributes().optSourceBranch().orNull();
}
@Override
public GitLabWebHookCause.ActionType getActionType() {
return ActionType.MERGE;
}
@Override
public String getUserName() {
return getObjectAttributes().getLastCommit().getAuthor().optName().orNull();
}
@Override
public String getUserEmail() {
return getObjectAttributes().getLastCommit().getAuthor().optEmail().orNull();
}
@Override
public String getSourceRepoHomepage() {
return getObjectAttributes().getSource().optHomepage().orNull();
}
@Override
public String getSourceRepoName() {
return getObjectAttributes().getSource().optName().orNull();
}
@Override
public String getSourceRepoUrl() {
return getObjectAttributes().getSource().optUrl().orNull();
}
@Override
public String getSourceRepoSshUrl() {
return getObjectAttributes().getSource().optSshUrl().orNull();
}
@Override
public String getSourceRepoHttpUrl() {
return getObjectAttributes().getSource().optHttpUrl().orNull();
}
@Override
public String getShortDescription() {
ObjectAttributes objectAttribute = getObjectAttributes();
return "GitLab Merge Request #" + objectAttribute.optIid().orNull() + " : " + objectAttribute.optSourceBranch().orNull() +
" => " + objectAttribute.optTargetBranch().orNull();
}
@Override
public String getMergeRequestTitle() {
return getObjectAttributes().optTitle().orNull();
}
@Override
public String getMergeRequestDescription() {
return getObjectAttributes().optDescription().orNull();
}
@Override
public String getMergeRequestId() {
return getObjectAttributes().optId().isPresent() ? getObjectAttributes().optId().toString() : null;
}
@Override
public String getTargetBranch() {
return getObjectAttributes().optTargetBranch().orNull();
}
@Override
public String getTargetRepoName() {
return getObjectAttributes().getTarget().optName().orNull();
}
@Override
public String getTargetRepoSshUrl() {
return getObjectAttributes().getTarget().optSshUrl().orNull();
}
@Override
public String getTargetRepoHttpUrl() {
return getObjectAttributes().getTarget().optHttpUrl().orNull();
}
private ObjectAttributes getObjectAttributes() {
return getRequest().getObjectAttributes();
}
@Initializer(before = InitMilestone.PLUGINS_STARTED)
public static void addAliases() {
Run.XSTREAM2.addCompatibilityAlias("com.dabsquared.gitlabjenkins.GitLabMergeCause", GitLabMergeCause.class);
}
}

View File

@ -1,103 +0,0 @@
package com.dabsquared.gitlabjenkins.cause;
import com.dabsquared.gitlabjenkins.gitlab.api.model.Commit;
import com.dabsquared.gitlabjenkins.gitlab.api.model.PushHook;
import hudson.init.InitMilestone;
import hudson.init.Initializer;
import hudson.model.Run;
import java.io.File;
import java.io.IOException;
import java.util.List;
/**
* @author Robin Müller
*/
public class GitLabPushCause extends GitLabWebHookCause<PushHook> {
public GitLabPushCause(PushHook pushHook) {
this(pushHook, "");
}
public GitLabPushCause(PushHook pushHook, File logFile) throws IOException {
super(pushHook, logFile);
}
public GitLabPushCause(PushHook pushHook, String pollingLog) {
super(pushHook, pollingLog);
}
@Override
public String getBranch() {
return getRequest().optRef().or("").replaceFirst("^refs/heads/", "");
}
@Override
public String getSourceBranch() {
return getBranch();
}
@Override
public ActionType getActionType() {
return ActionType.PUSH;
}
@Override
public String getUserName() {
return getRequest().optUserName().orNull();
}
@Override
public String getUserEmail() {
return getRequest().optUserEmail().orNull();
}
@Override
public String getSourceRepoHomepage() {
return getRequest().getProject().optHomepage().orNull();
}
@Override
public String getSourceRepoName() {
return getRequest().getProject().optName().orNull();
}
@Override
public String getSourceRepoUrl() {
return getRequest().getProject().optUrl().orNull();
}
@Override
public String getSourceRepoSshUrl() {
return getRequest().getProject().optSshUrl().orNull();
}
@Override
public String getSourceRepoHttpUrl() {
return getRequest().getProject().optHttpUrl().orNull();
}
@Override
public String getShortDescription() {
String pushedBy = retrievePushedBy();
if (pushedBy == null) {
return "Started by GitLab push";
} else {
return String.format("Started by GitLab push by %s", pushedBy);
}
}
private String retrievePushedBy() {
List<Commit> commits = getRequest().getCommits();
if (commits.size() > 0) {
return commits.get(0).getAuthor().optName().orNull();
} else {
return getRequest().optUserName().orNull();
}
}
@Initializer(before = InitMilestone.PLUGINS_STARTED)
public static void addAliases() {
Run.XSTREAM2.addCompatibilityAlias("com.dabsquared.gitlabjenkins.GitLabPushCause", GitLabPushCause.class);
}
}

View File

@ -1,109 +1,27 @@
package com.dabsquared.gitlabjenkins.cause;
import com.dabsquared.gitlabjenkins.gitlab.api.model.WebHook;
import hudson.triggers.SCMTrigger;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* @author Robin Müller
*/
public abstract class GitLabWebHookCause<T extends WebHook> extends SCMTrigger.SCMTriggerCause {
public class GitLabWebHookCause extends SCMTrigger.SCMTriggerCause {
private final T request;
private final CauseData data;
public GitLabWebHookCause(T request, String pollingLog) {
super(pollingLog);
this.request = request;
public GitLabWebHookCause(CauseData data) {
super("");
this.data = checkNotNull(data, "data must not be null");
}
public GitLabWebHookCause(T request, File logFile) throws IOException {
super(logFile);
this.request = request;
public CauseData getData() {
return data;
}
public T getRequest() {
return request;
}
public Map<String, String> getBuildVariables() {
HashMap<String, String> variables = new HashMap<String, String>();
putWithNullCheck(variables, "gitlabBranch", getBranch());
putWithNullCheck(variables, "gitlabSourceBranch", getSourceBranch());
putWithNullCheck(variables, "gitlabActionType", getActionType().name());
putWithNullCheck(variables, "gitlabUserName", getUserName());
putWithNullCheck(variables, "gitlabUserEmail", getUserEmail());
putWithNullCheck(variables, "gitlabSourceRepoHomepage", getSourceRepoHomepage());
putWithNullCheck(variables, "gitlabSourceRepoName", getSourceRepoName());
putWithNullCheck(variables, "gitlabSourceRepoURL", getSourceRepoUrl());
putWithNullCheck(variables, "gitlabSourceRepoSshUrl", getSourceRepoSshUrl());
putWithNullCheck(variables, "gitlabSourceRepoHttpUrl", getSourceRepoHttpUrl());
putWithNullCheck(variables, "gitlabMergeRequestTitle", getMergeRequestTitle());
putWithNullCheck(variables, "gitlabMergeRequestDescription", getMergeRequestDescription());
putWithNullCheck(variables, "gitlabMergeRequestId", getMergeRequestId());
putWithNullCheck(variables, "gitlabTargetBranch", getTargetBranch());
putWithNullCheck(variables, "gitlabTargetRepoName", getTargetRepoName());
putWithNullCheck(variables, "gitlabTargetRepoSshUrl", getTargetRepoSshUrl());
putWithNullCheck(variables, "gitlabTargetRepoHttpUrl", getTargetRepoHttpUrl());
return variables;
}
protected void putWithNullCheck(Map<String, String> variables, String name, String value) {
variables.put(name, value == null ? "" : value);
}
public abstract String getBranch();
public abstract String getSourceBranch();
public abstract ActionType getActionType();
public abstract String getUserName();
public abstract String getUserEmail();
public abstract String getSourceRepoHomepage();
public abstract String getSourceRepoName();
public abstract String getSourceRepoUrl();
public abstract String getSourceRepoSshUrl();
public abstract String getSourceRepoHttpUrl();
public String getMergeRequestTitle() {
return null;
}
public String getMergeRequestDescription() {
return null;
}
public String getMergeRequestId() {
return null;
}
public String getTargetBranch() {
return null;
}
public String getTargetRepoName() {
return null;
}
public String getTargetRepoSshUrl() {
return null;
}
public String getTargetRepoHttpUrl() {
return null;
}
public enum ActionType {
PUSH, MERGE
@Override
public String getShortDescription() {
return data.getShortDescription();
}
}

View File

@ -2,7 +2,7 @@ package com.dabsquared.gitlabjenkins.gitlab;
import com.dabsquared.gitlabjenkins.connection.GitLabConnection;
import com.dabsquared.gitlabjenkins.gitlab.api.GitLabApi;
import com.dabsquared.gitlabjenkins.util.GsonUtil;
import com.dabsquared.gitlabjenkins.util.JsonUtil;
import com.dabsquared.gitlabjenkins.util.LoggerUtil;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import com.google.common.base.Function;
@ -34,7 +34,6 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.RuntimeDelegate;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
@ -141,7 +140,7 @@ public class GitLabClientBuilder {
private String getPrettyPrintResponseBody(ClientResponseContext responseContext) {
String responseBody = getResponseBody(responseContext);
if (StringUtils.isNotEmpty(responseBody) && responseContext.getMediaType().equals(MediaType.APPLICATION_JSON_TYPE)) {
return GsonUtil.toPrettyPrint(responseBody);
return JsonUtil.toPrettyPrint(responseBody);
}
return responseBody;
}

View File

@ -19,6 +19,7 @@ import javax.ws.rs.ext.Provider;
public class JacksonConfig implements ContextResolver<ObjectMapper> {
public ObjectMapper getContext(Class<?> type) {
return new ObjectMapper()
.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
}

View File

@ -1,8 +1,5 @@
package com.dabsquared.gitlabjenkins.gitlab.api.model;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Optional;
import net.karneim.pojobuilder.GeneratePojoBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
@ -11,36 +8,35 @@ import org.apache.commons.lang.builder.ToStringBuilder;
/**
* @author Robin Müller
*/
@GeneratePojoBuilder(intoPackage = "*.builder.generated", withFactoryMethod = "*")
public class Branch {
private final String name;
private final Boolean protectedBranch;
private final Commit commit;
private String name;
private Boolean protectedBranch;
private Commit commit;
@JsonCreator
@GeneratePojoBuilder(intoPackage = "*.generated.builder", withFactoryMethod = "*")
public Branch(@JsonProperty("name") String name,
@JsonProperty("protected") Boolean protectedBranch,
@JsonProperty("commit") Commit commit) {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getProtectedBranch() {
return protectedBranch;
}
public void setProtectedBranch(Boolean protectedBranch) {
this.protectedBranch = protectedBranch;
this.commit = commit;
}
public Optional<String> optName() {
return Optional.fromNullable(name);
}
public Optional<Boolean> optProtectedBranch() {
return Optional.fromNullable(protectedBranch);
}
public Optional<Commit> optCommit() {
return Optional.fromNullable(commit);
}
public Commit getCommit() {
return commit == null ? new Commit() : commit;
return commit;
}
public void setCommit(Commit commit) {
this.commit = commit;
}
@Override

View File

@ -1,101 +1,90 @@
package com.dabsquared.gitlabjenkins.gitlab.api.model;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Optional;
import net.karneim.pojobuilder.GeneratePojoBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import java.util.Collections;
import java.util.Date;
import java.util.List;
/**
* @author Robin Müller
*/
@GeneratePojoBuilder(intoPackage = "*.builder.generated", withFactoryMethod = "*")
public class Commit {
private final String id;
private final String message;
private final Date timestamp;
private final String url;
private final User author;
private final List<String> added;
private final List<String> modified;
private final List<String> removed;
private String id;
private String message;
private Date timestamp;
private String url;
private User author;
private List<String> added;
private List<String> modified;
private List<String> removed;
@JsonCreator
@GeneratePojoBuilder(intoPackage = "*.builder.generated", withFactoryMethod = "*")
public Commit(@JsonProperty("id") String id,
@JsonProperty("message") String message,
@JsonProperty("timestamp") Date timestamp,
@JsonProperty("url") String url,
@JsonProperty("author") User author,
@JsonProperty("added") List<String> added,
@JsonProperty("modified") List<String> modified,
@JsonProperty("removed") List<String> removed) {
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
this.author = author;
this.added = added;
this.modified = modified;
this.removed = removed;
}
Commit() {
this(null, null, null, null, null, null, null, null);
}
public Optional<String> optId() {
return Optional.fromNullable(id);
}
public Optional<String> optMessage() {
return Optional.fromNullable(message);
}
public Optional<Date> optTimestamp() {
return Optional.fromNullable(timestamp);
}
public Optional<String> optUrl() {
return Optional.fromNullable(url);
}
public Optional<User> optAuthor() {
return Optional.fromNullable(author);
}
public User getAuthor() {
return author == null ? new User() : author;
return author;
}
public Optional<List<String>> optAdded() {
return Optional.fromNullable(added);
public void setAuthor(User author) {
this.author = author;
}
public List<String> getAdded() {
return added == null ? Collections.<String>emptyList() : added;
return added;
}
public Optional<List<String>> optModified() {
return Optional.fromNullable(modified);
public void setAdded(List<String> added) {
this.added = added;
}
public List<String> getModified() {
return modified == null ? Collections.<String>emptyList() : modified;
return modified;
}
public Optional<List<String>> optRemoved() {
return Optional.fromNullable(removed);
public void setModified(List<String> modified) {
this.modified = modified;
}
public List<String> getRemoved() {
return removed == null ? Collections.<String>emptyList() : removed;
return removed;
}
public void setRemoved(List<String> removed) {
this.removed = removed;
}
@Override

View File

@ -1,8 +1,5 @@
package com.dabsquared.gitlabjenkins.gitlab.api.model;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Optional;
import net.karneim.pojobuilder.GeneratePojoBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
@ -13,145 +10,170 @@ import java.util.List;
/**
* @author Robin Müller
*/
@GeneratePojoBuilder(intoPackage = "*.builder.generated", withFactoryMethod = "*")
public class MergeRequest {
private final Integer id;
private final Integer iid;
private final String sourceBranch;
private final String targetBranch;
private final Integer projectId;
private final String title;
private final State state;
private final Integer upvotes;
private final Integer downvotes;
private final User author;
private final User assignee;
private final Integer sourceProjectId;
private final Integer targetProjectId;
private final List<String> labels;
private final String description;
private final Boolean workInProgress;
private final Boolean mergeWhenBuildSucceeds;
private final String mergeStatus;
private Integer id;
private Integer iid;
private String sourceBranch;
private String targetBranch;
private Integer projectId;
private String title;
private State state;
private Integer upvotes;
private Integer downvotes;
private User author;
private User assignee;
private Integer sourceProjectId;
private Integer targetProjectId;
private List<String> labels;
private String description;
private Boolean workInProgress;
private Boolean mergeWhenBuildSucceeds;
private String mergeStatus;
@JsonCreator
@GeneratePojoBuilder(intoPackage = "*.generated.builder", withFactoryMethod = "*")
public MergeRequest(@JsonProperty("id") Integer id,
@JsonProperty("iid") Integer iid,
@JsonProperty("source_branch") String sourceBranch,
@JsonProperty("target_branch") String targetBranch,
@JsonProperty("project_id") Integer projectId,
@JsonProperty("title") String title,
@JsonProperty("state") State state,
@JsonProperty("upvotes") Integer upvotes,
@JsonProperty("downvotes") Integer downvotes,
@JsonProperty("author") User author,
@JsonProperty("assignee") User assignee,
@JsonProperty("source_project_id") Integer sourceProjectId,
@JsonProperty("target_project_id") Integer targetProjectId,
@JsonProperty("labels") List<String> labels,
@JsonProperty("description") String description,
@JsonProperty("work_in_progress") Boolean workInProgress,
@JsonProperty("merge_when_build_succeeds") Boolean mergeWhenBuildSucceeds,
@JsonProperty("merge_status") String mergeStatus) {
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getIid() {
return iid;
}
public void setIid(Integer iid) {
this.iid = iid;
}
public String getSourceBranch() {
return sourceBranch;
}
public void setSourceBranch(String sourceBranch) {
this.sourceBranch = sourceBranch;
}
public String getTargetBranch() {
return targetBranch;
}
public void setTargetBranch(String targetBranch) {
this.targetBranch = targetBranch;
}
public Integer getProjectId() {
return projectId;
}
public void setProjectId(Integer projectId) {
this.projectId = projectId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
public Integer getUpvotes() {
return upvotes;
}
public void setUpvotes(Integer upvotes) {
this.upvotes = upvotes;
}
public Integer getDownvotes() {
return downvotes;
}
public void setDownvotes(Integer downvotes) {
this.downvotes = downvotes;
this.author = author;
this.assignee = assignee;
this.sourceProjectId = sourceProjectId;
this.targetProjectId = targetProjectId;
this.labels = labels;
this.description = description;
this.workInProgress = workInProgress;
this.mergeWhenBuildSucceeds = mergeWhenBuildSucceeds;
this.mergeStatus = mergeStatus;
}
public Optional<Integer> optId() {
return Optional.fromNullable(id);
}
public Optional<Integer> optIid() {
return Optional.fromNullable(iid);
}
public Optional<String> optSourceBranch() {
return Optional.fromNullable(sourceBranch);
}
public Optional<String> optTargetBranch() {
return Optional.fromNullable(targetBranch);
}
public Optional<Integer> optProjectId() {
return Optional.fromNullable(projectId);
}
public Optional<String> optTitle() {
return Optional.fromNullable(title);
}
public Optional<State> optState() {
return Optional.fromNullable(state);
}
public Optional<Integer> optUpvotes() {
return Optional.fromNullable(upvotes);
}
public Optional<Integer> optDownvotes() {
return Optional.fromNullable(downvotes);
}
public Optional<User> optAuthor() {
return Optional.fromNullable(author);
}
public User getAuthor() {
return author == null ? new User() : author;
return author;
}
public Optional<User> optAssignee() {
return Optional.fromNullable(assignee);
public void setAuthor(User author) {
this.author = author;
}
public User getAssignee() {
return assignee == null ? new User() : assignee;
return assignee;
}
public Optional<Integer> optSourceProjectId() {
return Optional.fromNullable(sourceProjectId);
public void setAssignee(User assignee) {
this.assignee = assignee;
}
public Optional<Integer> optTargetProjectId() {
return Optional.fromNullable(targetProjectId);
public Integer getSourceProjectId() {
return sourceProjectId;
}
public Optional<List<String>> optLabels() {
return Optional.fromNullable(labels);
public void setSourceProjectId(Integer sourceProjectId) {
this.sourceProjectId = sourceProjectId;
}
public Optional<String> optDescription() {
return Optional.fromNullable(description);
public Integer getTargetProjectId() {
return targetProjectId;
}
public Optional<Boolean> optWorkInProgress() {
return Optional.fromNullable(workInProgress);
public void setTargetProjectId(Integer targetProjectId) {
this.targetProjectId = targetProjectId;
}
public Optional<Boolean> optMergeWhenBuildSucceeds() {
return Optional.fromNullable(mergeWhenBuildSucceeds);
public List<String> getLabels() {
return labels;
}
public Optional<String> optMergeStatus() {
return Optional.fromNullable(mergeStatus);
public void setLabels(List<String> labels) {
this.labels = labels;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Boolean getWorkInProgress() {
return workInProgress;
}
public void setWorkInProgress(Boolean workInProgress) {
this.workInProgress = workInProgress;
}
public Boolean getMergeWhenBuildSucceeds() {
return mergeWhenBuildSucceeds;
}
public void setMergeWhenBuildSucceeds(Boolean mergeWhenBuildSucceeds) {
this.mergeWhenBuildSucceeds = mergeWhenBuildSucceeds;
}
public String getMergeStatus() {
return mergeStatus;
}
public void setMergeStatus(String mergeStatus) {
this.mergeStatus = mergeStatus;
}
@Override

View File

@ -1,7 +1,6 @@
package com.dabsquared.gitlabjenkins.gitlab.api.model;
import com.google.common.base.Optional;
import net.karneim.pojobuilder.GeneratePojoBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
@ -10,56 +9,35 @@ import org.apache.commons.lang.builder.ToStringBuilder;
/**
* @author Robin Müller
*/
@GeneratePojoBuilder(intoPackage = "*.builder.generated", withFactoryMethod = "*")
public class MergeRequestHook extends WebHook {
private final User user;
private final Project project;
private final ObjectAttributes objectAttributes;
private final Repository repository;
@GeneratePojoBuilder(intoPackage = "*.builder.generated", withFactoryMethod = "*")
public MergeRequestHook(String objectKind, User user, Project project, ObjectAttributes objectAttributes, Repository repository) {
super(objectKind);
this.user = user;
this.project = project;
this.objectAttributes = objectAttributes;
this.repository = repository;
}
MergeRequestHook() {
this(null, null, null, null, null);
}
public Optional<User> optUser() {
return Optional.fromNullable(user);
}
private User user;
private Project project;
private ObjectAttributes objectAttributes;
public User getUser() {
return user == null ? new User() : user;
return user;
}
public Optional<Project> optProject() {
return Optional.fromNullable(project);
public void setUser(User user) {
this.user = user;
}
public Project getProject() {
return project == null ? new Project() : project;
return project;
}
public Optional<ObjectAttributes> optObjectAttributes() {
return Optional.fromNullable(objectAttributes);
public void setProject(Project project) {
this.project = project;
}
public ObjectAttributes getObjectAttributes() {
return objectAttributes == null ? new ObjectAttributes() : objectAttributes;
return objectAttributes;
}
public Optional<Repository> optRepository() {
return Optional.fromNullable(repository);
}
public Repository getRepository() {
return repository == null ? new Repository() : repository;
public void setObjectAttributes(ObjectAttributes objectAttributes) {
this.objectAttributes = objectAttributes;
}
@Override
@ -75,7 +53,6 @@ public class MergeRequestHook extends WebHook {
.append(user, that.user)
.append(project, that.project)
.append(objectAttributes, that.objectAttributes)
.append(repository, that.repository)
.isEquals();
}
@ -85,7 +62,6 @@ public class MergeRequestHook extends WebHook {
.append(user)
.append(project)
.append(objectAttributes)
.append(repository)
.toHashCode();
}
@ -95,7 +71,6 @@ public class MergeRequestHook extends WebHook {
.append("user", user)
.append("project", project)
.append("objectAttributes", objectAttributes)
.append("repository", repository)
.toString();
}
}

View File

@ -1,6 +1,5 @@
package com.dabsquared.gitlabjenkins.gitlab.api.model;
import com.google.common.base.Optional;
import net.karneim.pojobuilder.GeneratePojoBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
@ -11,149 +10,188 @@ import java.util.Date;
/**
* @author Robin Müller
*/
@GeneratePojoBuilder(intoPackage = "*.builder.generated", withFactoryMethod = "*")
public class ObjectAttributes {
private final Integer id;
private final Integer iid;
private final String sourceBranch;
private final String targetBranch;
private final Integer sourceProjectId;
private final Integer targetProjectId;
private final Integer authorId;
private final Integer assigneeId;
private final String title;
private final Date createdAt;
private final Date updatedAt;
private final State state;
private final String description;
private final Project source;
private final Project target;
private final Commit lastCommit;
private final String mergeStatus;
private final String url;
private final Action action;
private final Boolean workInProgress;
@GeneratePojoBuilder(intoPackage = "*.builder.generated", withFactoryMethod = "*")
public ObjectAttributes(Integer id, Integer iid, String sourceBranch, String targetBranch, Integer sourceProjectId, Integer targetProjectId,
Integer authorId, Integer assigneeId, String title, Date createdAt, Date updatedAt, State state, String description,
Project source, Project target, Commit lastCommit, String mergeStatus, String url, Action action, Boolean workInProgress) {
private Integer id;
private Integer iid;
private String sourceBranch;
private String targetBranch;
private Integer sourceProjectId;
private Integer targetProjectId;
private Integer authorId;
private Integer assigneeId;
private String title;
private Date createdAt;
private Date updatedAt;
private State state;
private String description;
private Project source;
private Project target;
private Commit lastCommit;
private String mergeStatus;
private String url;
private Action action;
private Boolean workInProgress;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getIid() {
return iid;
}
public void setIid(Integer iid) {
this.iid = iid;
}
public String getSourceBranch() {
return sourceBranch;
}
public void setSourceBranch(String sourceBranch) {
this.sourceBranch = sourceBranch;
}
public String getTargetBranch() {
return targetBranch;
}
public void setTargetBranch(String targetBranch) {
this.targetBranch = targetBranch;
}
public Integer getSourceProjectId() {
return sourceProjectId;
}
public void setSourceProjectId(Integer sourceProjectId) {
this.sourceProjectId = sourceProjectId;
}
public Integer getTargetProjectId() {
return targetProjectId;
}
public void setTargetProjectId(Integer targetProjectId) {
this.targetProjectId = targetProjectId;
}
public Integer getAuthorId() {
return authorId;
}
public void setAuthorId(Integer authorId) {
this.authorId = authorId;
}
public Integer getAssigneeId() {
return assigneeId;
}
public void setAssigneeId(Integer assigneeId) {
this.assigneeId = assigneeId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
this.source = source;
this.target = target;
this.lastCommit = lastCommit;
this.mergeStatus = mergeStatus;
this.url = url;
this.action = action;
this.workInProgress = workInProgress;
}
ObjectAttributes() {
this(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
}
public Optional<Integer> optId() {
return Optional.fromNullable(id);
}
public Optional<Integer> optIid() {
return Optional.fromNullable(iid);
}
public Optional<String> optSourceBranch() {
return Optional.fromNullable(sourceBranch);
}
public Optional<String> optTargetBranch() {
return Optional.fromNullable(targetBranch);
}
public Optional<Integer> optSourceProjectId() {
return Optional.fromNullable(sourceProjectId);
}
public Optional<Integer> optTargetProjectId() {
return Optional.fromNullable(targetProjectId);
}
public Optional<Integer> optAuthorId() {
return Optional.fromNullable(authorId);
}
public Optional<Integer> optAssigneeId() {
return Optional.fromNullable(assigneeId);
}
public Optional<String> optTitle() {
return Optional.fromNullable(title);
}
public Optional<Date> optCreatedAt() {
return Optional.fromNullable(createdAt);
}
public Optional<Date> optUpdatedAt() {
return Optional.fromNullable(updatedAt);
}
public Optional<State> optState() {
return Optional.fromNullable(state);
}
public Optional<String> optDescription() {
return Optional.fromNullable(description);
}
public Optional<Project> optSource() {
return Optional.fromNullable(source);
}
public Project getSource() {
return source == null ? new Project() : source;
return source;
}
public Optional<Project> optTarget() {
return Optional.fromNullable(target);
public void setSource(Project source) {
this.source = source;
}
public Project getTarget() {
return target == null ? new Project() : target;
return target;
}
public Optional<Commit> optLastCommit() {
return Optional.fromNullable(lastCommit);
public void setTarget(Project target) {
this.target = target;
}
public Commit getLastCommit() {
return lastCommit == null ? new Commit() : lastCommit;
return lastCommit;
}
public Optional<String> optMergeStatus() {
return Optional.fromNullable(mergeStatus);
public void setLastCommit(Commit lastCommit) {
this.lastCommit = lastCommit;
}
public Optional<String> optUrl() {
return Optional.fromNullable(url);
public String getMergeStatus() {
return mergeStatus;
}
public Optional<Action> optAction() {
return Optional.fromNullable(action);
public void setMergeStatus(String mergeStatus) {
this.mergeStatus = mergeStatus;
}
public Optional<Boolean> optWorkInProgress() {
return Optional.fromNullable(workInProgress);
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Action getAction() {
return action;
}
public void setAction(Action action) {
this.action = action;
}
public Boolean getWorkInProgress() {
return workInProgress;
}
public void setWorkInProgress(Boolean workInProgress) {
this.workInProgress = workInProgress;
}
@Override

View File

@ -1,6 +1,5 @@
package com.dabsquared.gitlabjenkins.gitlab.api.model;
import com.google.common.base.Optional;
import net.karneim.pojobuilder.GeneratePojoBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
@ -9,90 +8,118 @@ import org.apache.commons.lang.builder.ToStringBuilder;
/**
* @author Robin Müller
*/
@GeneratePojoBuilder(intoPackage = "*.builder.generated", withFactoryMethod = "*")
public class Project {
private final String name;
private final String description;
private final String webUrl;
private final String avatarUrl;
private final String namespace;
private final Integer visibilityLevel;
private final String pathWithNamespace;
private final String defaultBranch;
private final String homepage;
private final String url;
private final String sshUrl;
private final String httpUrl;
private String name;
private String description;
private String webUrl;
private String avatarUrl;
private String namespace;
private Integer visibilityLevel;
private String pathWithNamespace;
private String defaultBranch;
private String homepage;
private String url;
private String sshUrl;
private String httpUrl;
@GeneratePojoBuilder(intoPackage = "*.builder.generated", withFactoryMethod = "*")
public Project(String name, String description, String webUrl, String avatarUrl, String namespace, Integer visibilityLevel,
String pathWithNamespace, String defaultBranch, String homepage, String url, String sshUrl, String httpUrl) {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getWebUrl() {
return webUrl;
}
public void setWebUrl(String webUrl) {
this.webUrl = webUrl;
}
public String getAvatarUrl() {
return avatarUrl;
}
public void setAvatarUrl(String avatarUrl) {
this.avatarUrl = avatarUrl;
}
public String getNamespace() {
return namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
public Integer getVisibilityLevel() {
return visibilityLevel;
}
public void setVisibilityLevel(Integer visibilityLevel) {
this.visibilityLevel = visibilityLevel;
}
public String getPathWithNamespace() {
return pathWithNamespace;
}
public void setPathWithNamespace(String pathWithNamespace) {
this.pathWithNamespace = pathWithNamespace;
}
public String getDefaultBranch() {
return defaultBranch;
}
public void setDefaultBranch(String defaultBranch) {
this.defaultBranch = defaultBranch;
}
public String getHomepage() {
return homepage;
}
public void setHomepage(String homepage) {
this.homepage = homepage;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getSshUrl() {
return sshUrl;
}
public void setSshUrl(String sshUrl) {
this.sshUrl = sshUrl;
}
public String getHttpUrl() {
return httpUrl;
}
public void setHttpUrl(String httpUrl) {
this.httpUrl = httpUrl;
}
Project() {
this(null, null, null, null, null, null, null, null, null, null, null, null);
}
public Optional<String> optName() {
return Optional.fromNullable(name);
}
public Optional<String> optDescription() {
return Optional.fromNullable(description);
}
public Optional<String> optWebUrl() {
return Optional.fromNullable(webUrl);
}
public Optional<String> optAvatarUrl() {
return Optional.fromNullable(avatarUrl);
}
public Optional<String> optNamespace() {
return Optional.fromNullable(namespace);
}
public Optional<Integer> optVisibilityLevel() {
return Optional.fromNullable(visibilityLevel);
}
public Optional<String> optPathWithNamespace() {
return Optional.fromNullable(pathWithNamespace);
}
public Optional<String> optDefaultBranch() {
return Optional.fromNullable(defaultBranch);
}
public Optional<String> optHomepage() {
return Optional.fromNullable(homepage);
}
public Optional<String> optUrl() {
return Optional.fromNullable(url);
}
public Optional<String> optSshUrl() {
return Optional.fromNullable(sshUrl);
}
public Optional<String> optHttpUrl() {
return Optional.fromNullable(httpUrl);
}
@Override
public boolean equals(Object o) {
if (this == o) {

View File

@ -1,112 +1,116 @@
package com.dabsquared.gitlabjenkins.gitlab.api.model;
import com.google.common.base.Optional;
import net.karneim.pojobuilder.GeneratePojoBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import java.util.Collections;
import java.util.List;
/**
* @author Robin Müller
*/
@GeneratePojoBuilder(intoPackage = "*.builder.generated", withFactoryMethod = "*")
public class PushHook extends WebHook {
private final String before;
private final String after;
private final String ref;
private final Integer userId;
private final String userName;
private final String userEmail;
private final String userAvatar;
private final Integer projectId;
private final Project project;
private final Repository repository;
private final List<Commit> commits;
private final Integer totalCommitsCount;
private String before;
private String after;
private String ref;
private Integer userId;
private String userName;
private String userEmail;
private String userAvatar;
private Integer projectId;
private Project project;
private List<Commit> commits;
private Integer totalCommitsCount;
@GeneratePojoBuilder(intoPackage = "*.builder.generated", withFactoryMethod = "*")
public PushHook(String objectKind, String before, String after, String ref, Integer userId, String userName, String userEmail, String userAvatar,
Integer projectId, Project project, Repository repository, List<Commit> commits, Integer totalCommitsCount) {
super(objectKind);
public String getBefore() {
return before;
}
public void setBefore(String before) {
this.before = before;
}
public String getAfter() {
return after;
}
public void setAfter(String after) {
this.after = after;
}
public String getRef() {
return ref;
}
public void setRef(String ref) {
this.ref = ref;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
public String getUserAvatar() {
return userAvatar;
}
public void setUserAvatar(String userAvatar) {
this.userAvatar = userAvatar;
}
public Integer getProjectId() {
return projectId;
}
public void setProjectId(Integer projectId) {
this.projectId = projectId;
this.project = project;
this.repository = repository;
this.commits = commits;
this.totalCommitsCount = totalCommitsCount;
}
PushHook() {
this(null, null, null, null, null, null, null, null, null, null, null, null, null);
}
public Optional<Project> optProject() {
return Optional.fromNullable(project);
}
public Optional<Integer> optProjectId() {
return Optional.fromNullable(projectId);
}
public Optional<String> optUserAvatar() {
return Optional.fromNullable(userAvatar);
}
public Optional<String> optUserEmail() {
return Optional.fromNullable(userEmail);
}
public Optional<String> optUserName() {
return Optional.fromNullable(userName);
}
public Optional<Integer> optUserId() {
return Optional.fromNullable(userId);
}
public Optional<String> optRef() {
return Optional.fromNullable(ref);
}
public Optional<String> optAfter() {
return Optional.fromNullable(after);
}
public Optional<String> optBefore() {
return Optional.fromNullable(before);
}
public Project getProject() {
return project == null ? new Project() : project;
return project;
}
public Optional<Repository> optRepository() {
return Optional.fromNullable(repository);
}
public Repository getRepository() {
return repository == null ? new Repository() : repository;
}
public Optional<List<Commit>> optCommits() {
return Optional.fromNullable(commits);
public void setProject(Project project) {
this.project = project;
}
public List<Commit> getCommits() {
return commits == null ? Collections.<Commit>emptyList() : commits;
return commits;
}
public Optional<Integer> optTotalCommitsCount() {
return Optional.fromNullable(totalCommitsCount);
public void setCommits(List<Commit> commits) {
this.commits = commits;
}
public Integer getTotalCommitsCount() {
return totalCommitsCount;
}
public void setTotalCommitsCount(Integer totalCommitsCount) {
this.totalCommitsCount = totalCommitsCount;
}
@Override
@ -128,7 +132,6 @@ public class PushHook extends WebHook {
.append(userAvatar, pushHook.userAvatar)
.append(projectId, pushHook.projectId)
.append(project, pushHook.project)
.append(repository, pushHook.repository)
.append(commits, pushHook.commits)
.append(totalCommitsCount, pushHook.totalCommitsCount)
.isEquals();
@ -146,7 +149,6 @@ public class PushHook extends WebHook {
.append(userAvatar)
.append(projectId)
.append(project)
.append(repository)
.append(commits)
.append(totalCommitsCount)
.toHashCode();
@ -164,7 +166,6 @@ public class PushHook extends WebHook {
.append("userAvatar", userAvatar)
.append("projectId", projectId)
.append("project", project)
.append("repository", repository)
.append("commits", commits)
.append("totalCommitsCount", totalCommitsCount)
.toString();

View File

@ -1,7 +1,5 @@
package com.dabsquared.gitlabjenkins.gitlab.api.model;
import com.google.common.base.Optional;
import com.google.common.base.Supplier;
import net.karneim.pojobuilder.GeneratePojoBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
@ -10,68 +8,73 @@ import org.apache.commons.lang.builder.ToStringBuilder;
/**
* @author Robin Müller
*/
@GeneratePojoBuilder(intoPackage = "*.builder.generated", withFactoryMethod = "*")
public class Repository {
private final String name;
private final String description;
private final String url;
private final String homepage;
private final String gitSshUrl;
private final String gitHttpUrl;
private final Integer visibilityLevel;
private String name;
private String description;
private String url;
private String homepage;
private String gitSshUrl;
private String gitHttpUrl;
private Integer visibilityLevel;
public static Supplier<Repository> nullRepository() {
return new Supplier<Repository>() {
@Override
public Repository get() {
return new Repository();
}
};
public String getName() {
return name;
}
@GeneratePojoBuilder(intoPackage = "*.builder.generated", withFactoryMethod = "*")
public Repository(String name, String description, String url, String homepage, String gitSshUrl, String gitHttpUrl, Integer visibilityLevel) {
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getHomepage() {
return homepage;
}
public void setHomepage(String homepage) {
this.homepage = homepage;
}
public String getGitSshUrl() {
return gitSshUrl;
}
public void setGitSshUrl(String gitSshUrl) {
this.gitSshUrl = gitSshUrl;
}
public String getGitHttpUrl() {
return gitHttpUrl;
}
public void setGitHttpUrl(String gitHttpUrl) {
this.gitHttpUrl = gitHttpUrl;
}
public Integer getVisibilityLevel() {
return visibilityLevel;
}
public void setVisibilityLevel(Integer visibilityLevel) {
this.visibilityLevel = visibilityLevel;
}
Repository() {
this(null, null, null, null, null, null, null);
}
public Optional<String> optName() {
return Optional.fromNullable(name);
}
public Optional<String> optDescription() {
return Optional.fromNullable(description);
}
public Optional<String> optUrl() {
return Optional.fromNullable(url);
}
public Optional<String> optHomepage() {
return Optional.fromNullable(homepage);
}
public Optional<String> optGitSshUrl() {
return Optional.fromNullable(gitSshUrl);
}
public Optional<String> optGitHttpUrl() {
return Optional.fromNullable(gitHttpUrl);
}
public Optional<Integer> optVisibilityLevel() {
return Optional.fromNullable(visibilityLevel);
}
@Override
public boolean equals(Object o) {
if (this == o) {

View File

@ -1,9 +1,5 @@
package com.dabsquared.gitlabjenkins.gitlab.api.model;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Optional;
import com.google.common.base.Supplier;
import net.karneim.pojobuilder.GeneratePojoBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
@ -12,61 +8,55 @@ import org.apache.commons.lang.builder.ToStringBuilder;
/**
* @author Robin Müller
*/
@GeneratePojoBuilder(intoPackage = "*.builder.generated", withFactoryMethod = "*")
public class User {
private final Integer id;
private final String name;
private final String username;
private final String email;
private final String avatarUrl;
private Integer id;
private String name;
private String username;
private String email;
private String avatarUrl;
public static Supplier<User> nullUser() {
return new Supplier<User>() {
@Override
public User get() {
return new User();
}
};
public Integer getId() {
return id;
}
@JsonCreator
@GeneratePojoBuilder(intoPackage = "*.builder.generated", withFactoryMethod = "*")
public User(@JsonProperty("id") Integer id,
@JsonProperty("name") String name,
@JsonProperty("username") String username,
@JsonProperty("email") String email,
@JsonProperty("avatar_url") String avatarUrl) {
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAvatarUrl() {
return avatarUrl;
}
public void setAvatarUrl(String avatarUrl) {
this.avatarUrl = avatarUrl;
}
User() {
this(null, null, null, null, null);
}
public Optional<Integer> optId() {
return Optional.fromNullable(id);
}
public Optional<String> optName() {
return Optional.fromNullable(name);
}
public Optional<String> optUsername() {
return Optional.fromNullable(username);
}
public Optional<String> optEmail() {
return Optional.fromNullable(email);
}
public Optional<String> optAvatarUrl() {
return Optional.fromNullable(avatarUrl);
}
@Override
public boolean equals(Object o) {
if (this == o) {

View File

@ -1,6 +1,5 @@
package com.dabsquared.gitlabjenkins.gitlab.api.model;
import com.google.common.base.Optional;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
@ -10,14 +9,23 @@ import org.apache.commons.lang.builder.ToStringBuilder;
*/
public abstract class WebHook {
private final String objectKind;
private Repository repository;
private String objectKind;
protected WebHook(String objectKind) {
public String getObjectKind() {
return objectKind;
}
public void setObjectKind(String objectKind) {
this.objectKind = objectKind;
}
public Optional<String> optObjectKind() {
return Optional.fromNullable(objectKind);
public Repository getRepository() {
return repository;
}
public void setRepository(Repository repository) {
this.repository = repository;
}
@Override
@ -30,6 +38,7 @@ public abstract class WebHook {
}
WebHook webHook = (WebHook) o;
return new EqualsBuilder()
.append(repository, webHook.repository)
.append(objectKind, webHook.objectKind)
.isEquals();
}
@ -37,6 +46,7 @@ public abstract class WebHook {
@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(repository)
.append(objectKind)
.toHashCode();
}
@ -44,6 +54,7 @@ public abstract class WebHook {
@Override
public String toString() {
return new ToStringBuilder(this)
.append("repository", repository)
.append("objectKind", objectKind)
.toString();
}

View File

@ -24,9 +24,9 @@ public class GitLabEnvironmentRunListener extends RunListener<AbstractBuild<?, ?
@Override
@SuppressWarnings("unchecked")
public void buildEnvVars(Map<String, String> env) {
GitLabWebHookCause<WebHook> cause = (GitLabWebHookCause<WebHook>) build.getCause(GitLabWebHookCause.class);
GitLabWebHookCause cause = (GitLabWebHookCause) build.getCause(GitLabWebHookCause.class);
if (cause != null) {
env.putAll(cause.getBuildVariables());
env.putAll(cause.getData().getBuildVariables());
}
}
};

View File

@ -1,10 +1,10 @@
package com.dabsquared.gitlabjenkins.listener;
import com.dabsquared.gitlabjenkins.GitLabPushTrigger;
import com.dabsquared.gitlabjenkins.cause.GitLabMergeCause;
import com.dabsquared.gitlabjenkins.cause.CauseData;
import com.dabsquared.gitlabjenkins.cause.GitLabWebHookCause;
import com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty;
import com.dabsquared.gitlabjenkins.gitlab.api.GitLabApi;
import com.dabsquared.gitlabjenkins.gitlab.api.model.ObjectAttributes;
import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.Result;
@ -25,14 +25,13 @@ public class GitLabMergeRequestRunListener extends RunListener<AbstractBuild<?,
@Override
public void onCompleted(AbstractBuild<?, ?> build, @Nonnull TaskListener listener) {
GitLabPushTrigger trigger = build.getProject().getTrigger(GitLabPushTrigger.class);
GitLabMergeCause gitLabMergeCause = build.getCause(GitLabMergeCause.class);
GitLabWebHookCause cause = build.getCause(GitLabWebHookCause.class);
if (trigger != null && gitLabMergeCause != null) {
if (trigger != null && cause != null && cause.getData().getActionType() == CauseData.ActionType.MERGE) {
String buildUrl = getBuildUrl(build);
Result buildResult = build.getResult();
ObjectAttributes objectAttributes = gitLabMergeCause.getRequest().getObjectAttributes();
Integer projectId = objectAttributes.optSourceProjectId().orNull();
Integer mergeRequestId = objectAttributes.optId().orNull();
Integer projectId = cause.getData().getProjectId();
Integer mergeRequestId = cause.getData().getMergeRequestId();
if (buildResult == Result.SUCCESS) {
acceptMergeRequestIfNecessary(build, trigger, listener, projectId.toString(), mergeRequestId);
}

View File

@ -96,7 +96,7 @@ public class GitLabCommitStatusPublisher extends Notifier {
private String getBuildBranch(AbstractBuild<?, ?> build) {
GitLabWebHookCause cause = build.getCause(GitLabWebHookCause.class);
return cause == null ? null : cause.getSourceBranch();
return cause == null ? null : cause.getData().getSourceBranch();
}
private String getBuildUrl(AbstractBuild<?, ?> build) {

View File

@ -1,5 +1,7 @@
package com.dabsquared.gitlabjenkins.trigger.handler;
import com.dabsquared.gitlabjenkins.cause.CauseData;
import com.dabsquared.gitlabjenkins.cause.GitLabWebHookCause;
import com.dabsquared.gitlabjenkins.gitlab.api.model.WebHook;
import com.dabsquared.gitlabjenkins.trigger.exception.NoRevisionToBuildException;
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilter;
@ -9,7 +11,9 @@ import hudson.model.CauseAction;
import hudson.model.Job;
import hudson.plugins.git.RevisionParameterAction;
import jenkins.model.ParameterizedJobMixIn;
import org.eclipse.jgit.transport.URIish;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -38,9 +42,9 @@ public abstract class AbstractWebHookTriggerHandler<H extends WebHook> implement
protected abstract boolean isCiSkip(H hook);
protected Action[] createActions(Job<?, ?> job, H hook) {
ArrayList<Action> actions = new ArrayList<Action>();
actions.add(createCauseAction(job, hook));
private Action[] createActions(Job<?, ?> job, H hook) {
ArrayList<Action> actions = new ArrayList<>();
actions.add(new CauseAction(new GitLabWebHookCause(retrieveCauseData(hook))));
try {
actions.add(createRevisionParameter(hook));
} catch (NoRevisionToBuildException e) {
@ -50,12 +54,23 @@ public abstract class AbstractWebHookTriggerHandler<H extends WebHook> implement
return actions.toArray(new Action[actions.size()]);
}
protected abstract CauseAction createCauseAction(Job<?, ?> job, H hook);
protected abstract CauseData retrieveCauseData(H hook);
protected abstract String getTargetBranch(H hook);
protected abstract RevisionParameterAction createRevisionParameter(H hook) throws NoRevisionToBuildException;
protected URIish retrieveUrIish(WebHook hook) {
try {
if (hook.getRepository() != null) {
return new URIish(hook.getRepository().getUrl());
}
} catch (URISyntaxException e) {
LOGGER.log(Level.WARNING, "could not parse URL");
}
return null;
}
private void scheduleBuild(Job<?, ?> job, Action[] actions) {
int projectBuildDelay = 0;
if (job instanceof ParameterizedJobMixIn.ParameterizedJob) {

View File

@ -1,6 +1,7 @@
package com.dabsquared.gitlabjenkins.trigger.handler.merge;
import com.dabsquared.gitlabjenkins.cause.GitLabMergeCause;
import com.dabsquared.gitlabjenkins.cause.CauseData;
import com.dabsquared.gitlabjenkins.cause.GitLabWebHookCause;
import com.dabsquared.gitlabjenkins.gitlab.api.model.MergeRequestHook;
import com.dabsquared.gitlabjenkins.gitlab.api.model.ObjectAttributes;
import com.dabsquared.gitlabjenkins.gitlab.api.model.State;
@ -8,20 +9,17 @@ import com.dabsquared.gitlabjenkins.trigger.exception.NoRevisionToBuildException
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilter;
import com.dabsquared.gitlabjenkins.trigger.handler.AbstractWebHookTriggerHandler;
import com.dabsquared.gitlabjenkins.util.BuildUtil;
import hudson.model.CauseAction;
import hudson.model.Job;
import hudson.model.Run;
import hudson.plugins.git.RevisionParameterAction;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.transport.URIish;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.dabsquared.gitlabjenkins.cause.CauseDataBuilder.causeData;
/**
* @author Robin Müller
*/
@ -37,7 +35,7 @@ class MergeRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<M
@Override
public void handle(Job<?, ?> job, MergeRequestHook hook, boolean ciSkip, BranchFilter branchFilter) {
if (allowedStates.contains(hook.getObjectAttributes().optState().orNull()) && isLastCommitNotYetBuild(job, hook)) {
if (allowedStates.contains(hook.getObjectAttributes().getState()) && isLastCommitNotYetBuild(job, hook)) {
super.handle(job, hook, ciSkip, branchFilter);
}
}
@ -49,12 +47,14 @@ class MergeRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<M
@Override
protected boolean isCiSkip(MergeRequestHook hook) {
return hook.getObjectAttributes().optDescription().or("").contains("[ci-skip]");
return hook.getObjectAttributes() != null
&& hook.getObjectAttributes().getDescription() != null
&& hook.getObjectAttributes().getDescription().contains("[ci-skip]");
}
@Override
protected String getTargetBranch(MergeRequestHook hook) {
return hook.getObjectAttributes().optTargetBranch().orNull();
return hook.getObjectAttributes() == null ? null : hook.getObjectAttributes().getTargetBranch();
}
@Override
@ -63,8 +63,28 @@ class MergeRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<M
}
@Override
protected CauseAction createCauseAction(Job<?, ?> job, MergeRequestHook hook) {
return new CauseAction(createGitLabMergeCause(job, hook));
protected CauseData retrieveCauseData(MergeRequestHook hook) {
return causeData()
.withActionType(CauseData.ActionType.MERGE)
.withProjectId(hook.getObjectAttributes().getTargetProjectId())
.withBranch(hook.getObjectAttributes().getSourceBranch())
.withSourceBranch(hook.getObjectAttributes().getSourceBranch())
.withUserName(hook.getObjectAttributes().getLastCommit().getAuthor().getName())
.withUserEmail(hook.getObjectAttributes().getLastCommit().getAuthor().getEmail())
.withSourceRepoHomepage(hook.getObjectAttributes().getSource().getHomepage())
.withSourceRepoName(hook.getObjectAttributes().getSource().getName())
.withSourceRepoUrl(hook.getObjectAttributes().getSource().getUrl())
.withSourceRepoSshUrl(hook.getObjectAttributes().getSource().getSshUrl())
.withSourceRepoHttpUrl(hook.getObjectAttributes().getSource().getHttpUrl())
.withMergeRequestTitle(hook.getObjectAttributes().getTitle())
.withMergeRequestDescription(hook.getObjectAttributes().getDescription())
.withMergeRequestId(hook.getObjectAttributes().getIid())
.withTargetBranch(hook.getObjectAttributes().getTargetBranch())
.withTargetRepoName(hook.getObjectAttributes().getTarget().getName())
.withTargetRepoSshUrl(hook.getObjectAttributes().getTarget().getSshUrl())
.withTargetRepoHttpUrl(hook.getObjectAttributes().getTarget().getHttpUrl())
.withTriggeredByUser(hook.getObjectAttributes().getLastCommit().getAuthor().getName())
.build();
}
@Override
@ -72,36 +92,22 @@ class MergeRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<M
return new RevisionParameterAction(retrieveRevisionToBuild(hook), retrieveUrIish(hook));
}
private GitLabMergeCause createGitLabMergeCause(Job<?, ?> job, MergeRequestHook mergeRequestHook) {
try {
return new GitLabMergeCause(mergeRequestHook, new File(job.getRootDir(), "gitlab-polling.log"));
} catch (IOException ex) {
return new GitLabMergeCause(mergeRequestHook);
}
}
private String retrieveRevisionToBuild(MergeRequestHook hook) throws NoRevisionToBuildException {
if (hook.getObjectAttributes().getLastCommit().optId().isPresent()) {
return hook.getObjectAttributes().getLastCommit().optId().get();
if (hook.getObjectAttributes() != null
&& hook.getObjectAttributes().getLastCommit() != null
&& hook.getObjectAttributes().getLastCommit().getId() != null) {
return hook.getObjectAttributes().getLastCommit().getId();
} else {
throw new NoRevisionToBuildException();
}
}
private URIish retrieveUrIish(MergeRequestHook hook) {
try {
return new URIish(hook.getRepository().optUrl().orNull());
} catch (URISyntaxException e) {
LOGGER.log(Level.WARNING, "could not parse URL");
return null;
}
}
private boolean isLastCommitNotYetBuild(Job<?, ?> project, MergeRequestHook hook) {
ObjectAttributes objectAttributes = hook.getObjectAttributes();
if (objectAttributes.optLastCommit().isPresent()) {
Run<?, ?> mergeBuild = BuildUtil.getBuildBySHA1(project, objectAttributes.optLastCommit().get().optId().get(), true);
if (mergeBuild != null && StringUtils.equals(getTargetBranchFromBuild(mergeBuild), objectAttributes.optTargetBranch().get())) {
if (objectAttributes != null && objectAttributes.getLastCommit() != null) {
Run<?, ?> mergeBuild = BuildUtil.getBuildBySHA1(project, objectAttributes.getLastCommit().getId(), true);
if (mergeBuild != null && StringUtils.equals(getTargetBranchFromBuild(mergeBuild), objectAttributes.getTargetBranch())) {
LOGGER.log(Level.INFO, "Last commit in Merge Request has already been built in build #" + mergeBuild.getNumber());
return false;
}
@ -110,7 +116,7 @@ class MergeRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<M
}
private String getTargetBranchFromBuild(Run<?, ?> mergeBuild) {
GitLabMergeCause cause = mergeBuild.getCause(GitLabMergeCause.class);
return cause == null ? null : cause.getTargetBranch();
GitLabWebHookCause cause = mergeBuild.getCause(GitLabWebHookCause.class);
return cause == null ? null : cause.getData().getTargetBranch();
}
}

View File

@ -1,17 +1,16 @@
package com.dabsquared.gitlabjenkins.trigger.handler.push;
import com.dabsquared.gitlabjenkins.GitLabPushTrigger;
import com.dabsquared.gitlabjenkins.cause.GitLabMergeCause;
import com.dabsquared.gitlabjenkins.cause.CauseData;
import com.dabsquared.gitlabjenkins.cause.GitLabWebHookCause;
import com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty;
import com.dabsquared.gitlabjenkins.gitlab.api.GitLabApi;
import com.dabsquared.gitlabjenkins.gitlab.api.model.Branch;
import com.dabsquared.gitlabjenkins.gitlab.api.model.MergeRequest;
import com.dabsquared.gitlabjenkins.gitlab.api.model.MergeRequestHook;
import com.dabsquared.gitlabjenkins.gitlab.api.model.PushHook;
import com.dabsquared.gitlabjenkins.gitlab.api.model.State;
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilter;
import com.dabsquared.gitlabjenkins.util.LoggerUtil;
import com.google.common.base.Optional;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.CauseAction;
@ -27,9 +26,7 @@ import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.dabsquared.gitlabjenkins.gitlab.api.model.builder.generated.CommitBuilder.commit;
import static com.dabsquared.gitlabjenkins.gitlab.api.model.builder.generated.MergeRequestHookBuilder.mergeRequestHook;
import static com.dabsquared.gitlabjenkins.gitlab.api.model.builder.generated.ObjectAttributesBuilder.objectAttributes;
import static com.dabsquared.gitlabjenkins.cause.CauseDataBuilder.causeData;
/**
* @author Robin Müller
@ -45,11 +42,11 @@ class OpenMergeRequestPushHookTriggerHandler implements PushHookTriggerHandler {
AbstractProject<?, ?> project = (AbstractProject<?, ?>) job;
GitLabConnectionProperty property = job.getProperty(GitLabConnectionProperty.class);
final GitLabPushTrigger trigger = project.getTrigger(GitLabPushTrigger.class);
Optional<Integer> projectId = hook.optProjectId();
if (property != null && property.getClient() != null && projectId.isPresent() && trigger != null) {
Integer projectId = hook.getProjectId();
if (property != null && property.getClient() != null && projectId != null && trigger != null) {
GitLabApi client = property.getClient();
for (MergeRequest mergeRequest : getOpenMergeRequests(client, projectId.get().toString())) {
handleMergeRequest(job, hook, ciSkip, branchFilter, client, projectId.get(), mergeRequest);
for (MergeRequest mergeRequest : getOpenMergeRequests(client, projectId.toString())) {
handleMergeRequest(job, hook, ciSkip, branchFilter, client, projectId, mergeRequest);
}
}
}
@ -78,24 +75,42 @@ class OpenMergeRequestPushHookTriggerHandler implements PushHookTriggerHandler {
}
private void handleMergeRequest(Job<?, ?> job, PushHook hook, boolean ciSkip, BranchFilter branchFilter, GitLabApi client, Integer projectId, MergeRequest mergeRequest) {
if (ciSkip && mergeRequest.optDescription().or("").contains("[ci-skip]")) {
LOGGER.log(Level.INFO, "Skipping MR " + mergeRequest.optTitle().or("") + " due to ci-skip.");
if (ciSkip && mergeRequest.getDescription() != null && mergeRequest.getDescription().contains("[ci-skip]")) {
LOGGER.log(Level.INFO, "Skipping MR " + mergeRequest.getTitle() + " due to ci-skip.");
return;
}
Optional<String> targetBranch = mergeRequest.optTargetBranch();
Optional<String> sourceBranch = mergeRequest.optSourceBranch();
if (targetBranch.isPresent()
&& branchFilter.isBranchAllowed(targetBranch.get())
&& hook.optRef().or("").endsWith(targetBranch.get())
&& sourceBranch.isPresent()) {
String targetBranch = mergeRequest.getTargetBranch();
String sourceBranch = mergeRequest.getSourceBranch();
if (targetBranch != null && branchFilter.isBranchAllowed(targetBranch) && hook.getRef().endsWith(targetBranch) && sourceBranch != null) {
LOGGER.log(Level.INFO, "{0} triggered for push to target branch of open merge request #{1}.",
LoggerUtil.toArray(job.getFullName(), mergeRequest.optIid().orNull()));
LoggerUtil.toArray(job.getFullName(), mergeRequest.getId()));
Branch branch = client.getBranch(projectId.toString(), sourceBranch.get());
scheduleBuild(job, new CauseAction(new GitLabMergeCause(createMergeRequest(projectId, mergeRequest, branch))));
Branch branch = client.getBranch(projectId.toString(), sourceBranch);
scheduleBuild(job, new CauseAction(new GitLabWebHookCause(retrieveCauseData(hook, mergeRequest, branch))));
}
}
private CauseData retrieveCauseData(PushHook hook, MergeRequest mergeRequest, Branch branch) {
return causeData()
.withActionType(CauseData.ActionType.MERGE)
.withProjectId(hook.getProjectId())
.withBranch(branch.getName())
.withSourceBranch(branch.getName())
.withUserName(branch.getCommit().getAuthor().getName())
.withUserEmail(branch.getCommit().getAuthor().getEmail())
//.withSourceRepoHomepage()
//.withSourceRepoUrl()
.withMergeRequestTitle(mergeRequest.getTitle())
.withMergeRequestDescription(mergeRequest.getDescription())
.withMergeRequestId(mergeRequest.getIid())
.withTargetBranch(mergeRequest.getTargetBranch())
.withTargetRepoName(hook.getRepository().getName())
.withTargetRepoSshUrl(hook.getRepository().getGitSshUrl())
.withTargetRepoHttpUrl(hook.getRepository().getGitHttpUrl())
.withTriggeredByUser(hook.getCommits().get(0).getAuthor().getName())
.build();
}
@Override
public boolean isEnabled() {
return false;
@ -121,28 +136,4 @@ class OpenMergeRequestPushHookTriggerHandler implements PushHookTriggerHandler {
}
};
}
private MergeRequestHook createMergeRequest(Integer projectId, MergeRequest mergeRequest, Branch branch) {
return mergeRequestHook()
.withObjectKind("merge_request")
.withObjectAttributes(objectAttributes()
.withAssigneeId(mergeRequest.getAssignee().optId().orNull())
.withAuthorId(mergeRequest.getAuthor().optId().orNull())
.withDescription(mergeRequest.optDescription().orNull())
.withId(mergeRequest.optId().orNull())
.withIid(mergeRequest.optIid().orNull())
.withMergeStatus(mergeRequest.optMergeStatus().orNull())
.withSourceBranch(mergeRequest.optSourceBranch().orNull())
.withSourceProjectId(mergeRequest.optSourceProjectId().orNull())
.withTargetBranch(mergeRequest.optTargetBranch().orNull())
.withTargetProjectId(projectId)
.withTitle(mergeRequest.optTitle().orNull())
.withLastCommit(commit()
.withId(branch.getCommit().optId().orNull())
.withMessage(branch.getCommit().optMessage().orNull())
.withUrl("/projects/" + projectId + "/repository/commits/" + branch.getCommit().optId().orNull())
.build())
.build())
.build();
}
}

View File

@ -1,29 +1,21 @@
package com.dabsquared.gitlabjenkins.trigger.handler.push;
import com.dabsquared.gitlabjenkins.cause.GitLabPushCause;
import com.dabsquared.gitlabjenkins.cause.CauseData;
import com.dabsquared.gitlabjenkins.gitlab.api.model.Commit;
import com.dabsquared.gitlabjenkins.gitlab.api.model.PushHook;
import com.dabsquared.gitlabjenkins.trigger.exception.NoRevisionToBuildException;
import com.dabsquared.gitlabjenkins.trigger.handler.AbstractWebHookTriggerHandler;
import hudson.model.CauseAction;
import hudson.model.Job;
import hudson.plugins.git.RevisionParameterAction;
import org.eclipse.jgit.transport.URIish;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.dabsquared.gitlabjenkins.cause.CauseDataBuilder.causeData;
/**
* @author Robin Müller
*/
class PushHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<PushHook> implements PushHookTriggerHandler {
private final static Logger LOGGER = Logger.getLogger(PushHookTriggerHandlerImpl.class.getName());
@Override
public boolean isEnabled() {
return true;
@ -32,17 +24,37 @@ class PushHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<PushHook>
@Override
protected boolean isCiSkip(PushHook hook) {
List<Commit> commits = hook.getCommits();
return !commits.isEmpty() && commits.get(0).optMessage().or("").contains("[ci-skip]");
return commits != null && !commits.isEmpty() && commits.get(0).getMessage() != null && commits.get(0).getMessage().contains("[ci-skip]");
}
@Override
protected CauseAction createCauseAction(Job<?, ?> job, PushHook hook) {
return new CauseAction(createGitLabPushCause(job, hook));
protected CauseData retrieveCauseData(PushHook hook) {
return causeData()
.withActionType(CauseData.ActionType.PUSH)
.withProjectId(hook.getProjectId())
.withBranch(getTargetBranch(hook))
.withSourceBranch(getTargetBranch(hook))
.withUserName(hook.getUserName())
.withUserEmail(hook.getUserEmail())
.withSourceRepoHomepage(hook.getRepository().getHomepage())
.withSourceRepoName(hook.getRepository().getName())
.withSourceRepoUrl(hook.getRepository().getUrl())
.withSourceRepoSshUrl(hook.getRepository().getGitSshUrl())
.withSourceRepoHttpUrl(hook.getRepository().getGitHttpUrl())
.withMergeRequestTitle("")
.withMergeRequestDescription("")
.withMergeRequestId(null)
.withTargetBranch(getTargetBranch(hook))
.withTargetRepoName("")
.withTargetRepoSshUrl("")
.withTargetRepoHttpUrl("")
.withTriggeredByUser(retrievePushedBy(hook))
.build();
}
@Override
protected String getTargetBranch(PushHook hook) {
return hook.optRef().or("").replaceFirst("^refs/heads/", "");
return hook.getRef() == null ? null : hook.getRef().replaceFirst("^refs/heads/", "");
}
@Override
@ -50,42 +62,34 @@ class PushHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<PushHook>
return "push";
}
private GitLabPushCause createGitLabPushCause(Job<?, ?> job, PushHook hook) {
try {
return new GitLabPushCause(hook, new File(job.getRootDir(), "gitlab-polling.log"));
} catch (IOException ex) {
return new GitLabPushCause(hook);
}
}
@Override
protected RevisionParameterAction createRevisionParameter(PushHook hook) throws NoRevisionToBuildException {
return new RevisionParameterAction(retrieveRevisionToBuild(hook), retrieveUrIish(hook));
}
private String retrievePushedBy(PushHook hook) {
List<Commit> commits = hook.getCommits();
if (commits != null && commits.size() > 0) {
return commits.get(0).getAuthor().getName();
} else {
return hook.getUserName();
}
}
private String retrieveRevisionToBuild(PushHook hook) throws NoRevisionToBuildException {
if (hook.getCommits().isEmpty()) {
if (hook.getCommits() == null || hook.getCommits().isEmpty()) {
if (isNewBranchPush(hook)) {
return hook.optAfter().orNull();
return hook.getAfter();
} else {
throw new NoRevisionToBuildException();
}
} else {
List<Commit> commits = hook.getCommits();
return commits.get(commits.size() - 1).optId().orNull();
}
}
private URIish retrieveUrIish(PushHook hook) {
try {
return new URIish(hook.getRepository().optUrl().orNull());
} catch (URISyntaxException e) {
LOGGER.log(Level.WARNING, "could not parse URL");
return null;
return commits.get(commits.size() - 1).getId();
}
}
private boolean isNewBranchPush(PushHook pushHook) {
return pushHook.optBefore().or("").contains("0000000000000000000000000000000000000000");
return pushHook.getBefore() != null && pushHook.getBefore().contains("0000000000000000000000000000000000000000");
}
}

View File

@ -19,7 +19,7 @@ import java.util.Locale;
/**
* @author Robin Müller
*/
public final class GsonUtil {
public final class JsonUtil {
private static final Gson prettyPrint = new GsonBuilder().setPrettyPrinting().create();
private static final Gson gson = new GsonBuilder()
@ -27,17 +27,17 @@ public final class GsonUtil {
.registerTypeAdapter(Date.class, new DateSerializer())
.create();
private GsonUtil() { }
public static Gson getGson() {
return gson;
}
private JsonUtil() { }
public static String toPrettyPrint(String json) {
JsonParser parser = new JsonParser();
return prettyPrint.toJson(parser.parse(json));
}
public static <T> T read(String json, Class<T> type) {
return gson.fromJson(json, type);
}
private static final String[] DATE_FORMATS = new String[] {
"yyyy-MM-dd HH:mm:ss Z", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "yyyy-MM-dd'T'HH:mm:ssX" };

View File

@ -2,7 +2,7 @@ package com.dabsquared.gitlabjenkins.webhook.build;
import com.dabsquared.gitlabjenkins.GitLabPushTrigger;
import com.dabsquared.gitlabjenkins.gitlab.api.model.MergeRequestHook;
import com.dabsquared.gitlabjenkins.util.GsonUtil;
import com.dabsquared.gitlabjenkins.util.JsonUtil;
import com.dabsquared.gitlabjenkins.webhook.WebHookAction;
import hudson.model.AbstractProject;
import hudson.security.ACL;
@ -12,7 +12,7 @@ import org.kohsuke.stapler.StaplerResponse;
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.dabsquared.gitlabjenkins.util.GsonUtil.toPrettyPrint;
import static com.dabsquared.gitlabjenkins.util.JsonUtil.toPrettyPrint;
/**
* @author Robin Müller
@ -26,7 +26,7 @@ public class MergeRequestBuildAction implements WebHookAction {
public MergeRequestBuildAction(AbstractProject<?, ?> project, String json) {
LOGGER.log(Level.FINE, "MergeRequest: {0}", toPrettyPrint(json));
this.project = project;
this.mergeRequestHook = GsonUtil.getGson().fromJson(json, MergeRequestHook.class);
this.mergeRequestHook = JsonUtil.read(json, MergeRequestHook.class);
}
public void execute(StaplerResponse response) {

View File

@ -2,7 +2,7 @@ package com.dabsquared.gitlabjenkins.webhook.build;
import com.dabsquared.gitlabjenkins.GitLabPushTrigger;
import com.dabsquared.gitlabjenkins.gitlab.api.model.PushHook;
import com.dabsquared.gitlabjenkins.util.GsonUtil;
import com.dabsquared.gitlabjenkins.util.JsonUtil;
import com.dabsquared.gitlabjenkins.webhook.WebHookAction;
import hudson.model.AbstractProject;
import hudson.security.ACL;
@ -12,7 +12,7 @@ import org.kohsuke.stapler.StaplerResponse;
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.dabsquared.gitlabjenkins.util.GsonUtil.toPrettyPrint;
import static com.dabsquared.gitlabjenkins.util.JsonUtil.toPrettyPrint;
/**
* @author Robin Müller
@ -27,12 +27,11 @@ public class PushBuildAction implements WebHookAction {
public PushBuildAction(AbstractProject<?, ?> project, String json) {
LOGGER.log(Level.FINE, "Push: {0}", toPrettyPrint(json));
this.project = project;
this.pushHook = GsonUtil.getGson().fromJson(json, PushHook.class);
this.pushHook = JsonUtil.read(json, PushHook.class);
}
public void execute(StaplerResponse response) {
String repositoryUrl = pushHook.getRepository().optUrl().orNull();
if (repositoryUrl == null) {
if (pushHook.getRepository() != null && pushHook.getRepository().getUrl() == null) {
LOGGER.log(Level.WARNING, "No repository url found.");
return;
}

View File

@ -12,7 +12,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static com.dabsquared.gitlabjenkins.gitlab.api.model.generated.builder.BranchBuilder.branch;
import static com.dabsquared.gitlabjenkins.gitlab.api.model.builder.generated.BranchBuilder.branch;
import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

View File

@ -25,8 +25,11 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import static com.dabsquared.gitlabjenkins.gitlab.api.model.builder.generated.CommitBuilder.commit;
import static com.dabsquared.gitlabjenkins.gitlab.api.model.builder.generated.MergeRequestHookBuilder.mergeRequestHook;
import static com.dabsquared.gitlabjenkins.gitlab.api.model.builder.generated.ObjectAttributesBuilder.objectAttributes;
import static com.dabsquared.gitlabjenkins.gitlab.api.model.builder.generated.ProjectBuilder.project;
import static com.dabsquared.gitlabjenkins.gitlab.api.model.builder.generated.UserBuilder.user;
import static com.dabsquared.gitlabjenkins.trigger.filter.BranchFilterConfig.BranchFilterConfigBuilder.branchFilterConfig;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@ -92,6 +95,27 @@ public class MergeRequestHookTriggerHandlerImplTest {
.withObjectAttributes(objectAttributes()
.withTargetBranch("refs/heads/" + git.nameRev().add(head).call().get(head))
.withState(State.opened)
.withIid(1)
.withTitle("test")
.withTargetProjectId(1)
.withSourceProjectId(1)
.withSourceBranch("feature")
.withTargetBranch("master")
.withLastCommit(commit().withAuthor(user().withName("test").build()).build())
.withSource(project()
.withName("test")
.withHomepage("https://gitlab.org/test")
.withUrl("git@gitlab.org:test.git")
.withSshUrl("git@gitlab.org:test.git")
.withHttpUrl("https://gitlab.org/test.git")
.build())
.withTarget(project()
.withName("test")
.withHomepage("https://gitlab.org/test")
.withUrl("git@gitlab.org:test.git")
.withSshUrl("git@gitlab.org:test.git")
.withHttpUrl("https://gitlab.org/test.git")
.build())
.build())
.build(), true, BranchFilterFactory.newBranchFilter(branchFilterConfig().build(BranchFilterType.All)));

View File

@ -46,7 +46,7 @@ public class PushHookTriggerHandlerGitlabServerTest {
RevisionParameterAction revisionParameterAction = new PushHookTriggerHandlerImpl().createRevisionParameter(hook);
assertThat(revisionParameterAction, is(notNullValue()));
assertThat(revisionParameterAction.commit, is(hook.optAfter().get()));
assertThat(revisionParameterAction.commit, is(hook.getAfter()));
assertFalse(revisionParameterAction.canOriginateFrom(new ArrayList<RemoteConfig>()));
}
@ -57,7 +57,7 @@ public class PushHookTriggerHandlerGitlabServerTest {
RevisionParameterAction revisionParameterAction = new PushHookTriggerHandlerImpl().createRevisionParameter(hook);
assertThat(revisionParameterAction, is(notNullValue()));
assertThat(revisionParameterAction.commit, is(hook.optAfter().get()));
assertThat(revisionParameterAction.commit, is(hook.getAfter()));
assertFalse(revisionParameterAction.canOriginateFrom(new ArrayList<RemoteConfig>()));
}
@ -68,7 +68,7 @@ public class PushHookTriggerHandlerGitlabServerTest {
RevisionParameterAction revisionParameterAction = new PushHookTriggerHandlerImpl().createRevisionParameter(hook);
assertThat(revisionParameterAction, is(notNullValue()));
assertThat(revisionParameterAction.commit, is(hook.optAfter().get()));
assertThat(revisionParameterAction.commit, is(hook.getAfter()));
assertFalse(revisionParameterAction.canOriginateFrom(new ArrayList<RemoteConfig>()));
}
@ -79,7 +79,7 @@ public class PushHookTriggerHandlerGitlabServerTest {
RevisionParameterAction revisionParameterAction = new PushHookTriggerHandlerImpl().createRevisionParameter(hook);
assertThat(revisionParameterAction, is(notNullValue()));
assertThat(revisionParameterAction.commit, is(hook.optAfter().get()));
assertThat(revisionParameterAction.commit, is(hook.getAfter()));
assertFalse(revisionParameterAction.canOriginateFrom(new ArrayList<RemoteConfig>()));
}
@ -90,7 +90,7 @@ public class PushHookTriggerHandlerGitlabServerTest {
RevisionParameterAction revisionParameterAction = new PushHookTriggerHandlerImpl().createRevisionParameter(hook);
assertThat(revisionParameterAction, is(notNullValue()));
assertThat(revisionParameterAction.commit, is(hook.optAfter().get()));
assertThat(revisionParameterAction.commit, is(hook.getAfter()));
assertFalse(revisionParameterAction.canOriginateFrom(new ArrayList<RemoteConfig>()));
}

View File

@ -92,9 +92,17 @@ public class PushHookTriggerHandlerImplTest {
});
pushHookTriggerHandler.handle(project, pushHook()
.withBefore("0000000000000000000000000000000000000000")
.withProjectId(1)
.withUserName("test")
.withRepository(repository()
.withName("test")
.withHomepage("https://gitlab.org/test")
.withUrl("git@gitlab.org:test.git")
.withGitSshUrl("git@gitlab.org:test.git")
.withGitHttpUrl("https://gitlab.org/test.git")
.build())
.withAfter(commit.name())
.withRef("refs/heads/" + git.nameRev().add(head).call().get(head))
.withRepository(repository().withUrl(repositoryUrl).build())
.build(), webHookTriggerConfig(true).getCiSkip(), webHookTriggerConfig(true).getBranchFilter());
buildTriggered.block();

View File

@ -1,7 +1,8 @@
package com.dabsquared.gitlabjenkins.webhook.build;
import com.dabsquared.gitlabjenkins.GitLabPushTrigger;
import com.dabsquared.gitlabjenkins.cause.GitLabMergeCause;
import com.dabsquared.gitlabjenkins.cause.CauseData;
import com.dabsquared.gitlabjenkins.cause.GitLabWebHookCause;
import com.dabsquared.gitlabjenkins.gitlab.api.model.MergeRequestHook;
import hudson.model.FreeStyleProject;
import hudson.model.ParametersAction;
@ -26,8 +27,7 @@ import org.mockito.runners.MockitoJUnitRunner;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import static com.dabsquared.gitlabjenkins.gitlab.api.model.builder.generated.MergeRequestHookBuilder.mergeRequestHook;
import static com.dabsquared.gitlabjenkins.gitlab.api.model.builder.generated.ObjectAttributesBuilder.objectAttributes;
import static com.dabsquared.gitlabjenkins.cause.CauseDataBuilder.causeData;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
@ -109,8 +109,24 @@ public class MergeRequestBuildActionTest {
FreeStyleProject testProject = jenkins.createFreeStyleProject("test");
testProject.addTrigger(trigger);
testProject.setScm(new GitSCM(gitRepoUrl));
QueueTaskFuture<?> future = testProject.scheduleBuild2(0, new GitLabMergeCause(mergeRequestHook()
.withObjectAttributes(objectAttributes().withTargetBranch("master").build())
QueueTaskFuture<?> future = testProject.scheduleBuild2(0, new GitLabWebHookCause(causeData()
.withActionType(CauseData.ActionType.MERGE)
.withProjectId(1)
.withBranch("feature")
.withSourceBranch("feature")
.withUserName("")
.withSourceRepoHomepage("https://gitlab.org/test")
.withSourceRepoName("test")
.withSourceRepoUrl("git@gitlab.org:test.git")
.withSourceRepoSshUrl("git@gitlab.org:test.git")
.withSourceRepoHttpUrl("https://gitlab.org/test.git")
.withMergeRequestTitle("Test")
.withMergeRequestId(1)
.withTargetBranch("master")
.withTargetRepoName("test")
.withTargetRepoSshUrl("git@gitlab.org:test.git")
.withTargetRepoHttpUrl("https://gitlab.org/test.git")
.withTriggeredByUser("test")
.build()));
future.get();