From 2dbcb560e23ced02a16bd852e6eef510c35e38fc Mon Sep 17 00:00:00 2001 From: Ilya Beyrak Date: Mon, 16 Apr 2018 08:59:20 -0500 Subject: [PATCH 01/10] add membership=true to gitlab project listings --- .../gitlab_branch_source/api/GitLabAPI.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/api/GitLabAPI.java b/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/api/GitLabAPI.java index 0247c80..19c8bca 100644 --- a/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/api/GitLabAPI.java +++ b/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/api/GitLabAPI.java @@ -282,18 +282,18 @@ private boolean unregisterProjectHook(String url, int projectId) throws IOExcept } private String projectUrl(String group, GitLabProjectSelector selector, GitLabProjectVisibility visibility, String searchPattern) { - StringBuilder urlBuilder = new StringBuilder(GitlabGroup.URL).append(PATH_SEP).append(group).append(GitLabProject.URL); + StringBuilder urlBuilder = new StringBuilder(GitlabGroup.URL).append(PATH_SEP).append(group).append(GitLabProject.URL).append("?membership=true"); if (!VISIBLE.equals(selector)) { - urlBuilder.append("?").append(selector.id()).append("=true"); + urlBuilder.append("&").append(selector.id()).append("=true"); } if (!ALL.equals(visibility)) { - urlBuilder.append(VISIBLE.equals(selector) ? "?" : "&").append("visibility=").append(visibility.id()); + urlBuilder.append("&").append("visibility=").append(visibility.id()); } if (!StringUtils.isEmpty(searchPattern)) { - urlBuilder.append(VISIBLE.equals(selector) && ALL.equals(visibility) ? "?" : "&").append("search=").append(searchPattern); + urlBuilder.append("&").append("search=").append(searchPattern); } return urlBuilder.toString(); @@ -302,14 +302,14 @@ private String projectUrl(String group, GitLabProjectSelector selector, GitLabPr private String projectUrl(GitLabProjectSelector selector, GitLabProjectVisibility visibility, String searchPattern) { StringBuilder urlBuilder = new StringBuilder(GitlabProject.URL) - .append(PATH_SEP).append(selector.id()); + .append(PATH_SEP).append(selector.id()).append("?membership=true"); if (!ALL.equals(visibility)) { - urlBuilder.append("?visibility=").append(visibility.id()); + urlBuilder.append("&visibility=").append(visibility.id()); } if (!StringUtils.isEmpty(searchPattern)) { - urlBuilder.append(ALL.equals(visibility) ? "?" : "&").append("search=").append(searchPattern); + urlBuilder.append("&").append("search=").append(searchPattern); } return urlBuilder.toString(); From a8a2aa6b7a43c6a7b2e7b914a95cd4a4021e6c19 Mon Sep 17 00:00:00 2001 From: Ilya Beyrak Date: Tue, 17 Apr 2018 13:25:15 -0500 Subject: [PATCH 02/10] add removal of double slashes which occurs and makes api fail --- .../jenkins/plugins/gitlab_branch_source/api/GitLabAPI.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/api/GitLabAPI.java b/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/api/GitLabAPI.java index 19c8bca..69611b7 100644 --- a/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/api/GitLabAPI.java +++ b/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/api/GitLabAPI.java @@ -90,6 +90,7 @@ private List getBranches(Serializable nameOrId) throws GitLabAPIEx public GitlabBranch getBranch(int projectId, String branch) throws GitLabAPIException { try { String tailUrl = GitlabProject.URL + PATH_SEP + projectId + GitlabBranch.URL + PATH_SEP + URLEncoder.encode(branch, "UTF-8"); + tailUrl = tailUrl.replaceAll("//", "/"); return delegate.retrieve().to(tailUrl, GitlabBranch.class); } catch (FileNotFoundException e) { throw new NoSuchElementException("unknown branch " + branch); From 2d85b291cc206d03b4bac5e1de74fda3fd7ee2e5 Mon Sep 17 00:00:00 2001 From: Ilya Beyrak Date: Tue, 17 Apr 2018 13:26:33 -0500 Subject: [PATCH 03/10] decode MergeRequestHook objectAttributes action as string and do it outside of gitlab-plugin as it is wrong there. Also add support for reopen action to do same as Open. --- .../api/Hooks/MergeRequestHook.java | 49 +++++++++++++++ .../Hooks/MergeRequestObjectAttributes.java | 50 ++++++++++++++++ .../api/Hooks/WebHook.java | 52 ++++++++++++++++ .../events/GitLabSCMMergeRequestEvent.java | 19 ++++-- .../hooks/HookHandler.java | 59 ++++++++++++------- 5 files changed, 204 insertions(+), 25 deletions(-) create mode 100644 src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/api/Hooks/MergeRequestHook.java create mode 100644 src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/api/Hooks/MergeRequestObjectAttributes.java create mode 100644 src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/api/Hooks/WebHook.java diff --git a/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/api/Hooks/MergeRequestHook.java b/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/api/Hooks/MergeRequestHook.java new file mode 100644 index 0000000..0b3c6ad --- /dev/null +++ b/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/api/Hooks/MergeRequestHook.java @@ -0,0 +1,49 @@ +package argelbargel.jenkins.plugins.gitlab_branch_source.api.Hooks; + + +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; +import org.apache.commons.lang.builder.ToStringBuilder; + +public class MergeRequestHook extends WebHook { + + private MergeRequestObjectAttributes objectAttributes; + + public MergeRequestObjectAttributes getObjectAttributes() { + return objectAttributes; + } + + public void setObjectAttributes(MergeRequestObjectAttributes objectAttributes) { + this.objectAttributes = objectAttributes; + } + + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + MergeRequestHook that = (MergeRequestHook) o; + return new EqualsBuilder() + .append(objectAttributes, that.objectAttributes) + .isEquals(); + } + + @Override + public int hashCode() { + return new HashCodeBuilder(17, 37) + + .append(objectAttributes) + .toHashCode(); + } + + @Override + public String toString() { + return new ToStringBuilder(this) + .append("objectAttributes", objectAttributes) + .toString(); + } +} \ No newline at end of file diff --git a/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/api/Hooks/MergeRequestObjectAttributes.java b/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/api/Hooks/MergeRequestObjectAttributes.java new file mode 100644 index 0000000..2015c13 --- /dev/null +++ b/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/api/Hooks/MergeRequestObjectAttributes.java @@ -0,0 +1,50 @@ +package argelbargel.jenkins.plugins.gitlab_branch_source.api.Hooks; + +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; +import org.apache.commons.lang.builder.ToStringBuilder; + +import java.util.Date; +import java.util.List; + +public class MergeRequestObjectAttributes { + + private String action; + + + public String getAction() { + return action; + } + + public void setAction(String action) { + this.action = action; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + MergeRequestObjectAttributes that = (MergeRequestObjectAttributes) o; + return new EqualsBuilder() + .append(action, that.action) + .isEquals(); + } + + @Override + public int hashCode() { + return new HashCodeBuilder(17, 37) + .append(action) + .toHashCode(); + } + + @Override + public String toString() { + return new ToStringBuilder(this) + .append("action", action) + .toString(); + } +} \ No newline at end of file diff --git a/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/api/Hooks/WebHook.java b/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/api/Hooks/WebHook.java new file mode 100644 index 0000000..07624c4 --- /dev/null +++ b/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/api/Hooks/WebHook.java @@ -0,0 +1,52 @@ +package argelbargel.jenkins.plugins.gitlab_branch_source.api.Hooks; + + +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; +import org.apache.commons.lang.builder.ToStringBuilder; + +/** + * @author Robin Müller + */ +public abstract class WebHook { + + private String objectKind; + + public String getObjectKind() { + return objectKind; + } + + public void setObjectKind(String objectKind) { + this.objectKind = objectKind; + } + + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + WebHook webHook = (WebHook) o; + return new EqualsBuilder() + .append(objectKind, webHook.objectKind) + .isEquals(); + } + + @Override + public int hashCode() { + return new HashCodeBuilder(17, 37) + + .append(objectKind) + .toHashCode(); + } + + @Override + public String toString() { + return new ToStringBuilder(this) + .append("objectKind", objectKind) + .toString(); + } +} \ No newline at end of file diff --git a/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/events/GitLabSCMMergeRequestEvent.java b/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/events/GitLabSCMMergeRequestEvent.java index 3fef178..181a2c7 100644 --- a/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/events/GitLabSCMMergeRequestEvent.java +++ b/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/events/GitLabSCMMergeRequestEvent.java @@ -21,13 +21,24 @@ import static jenkins.scm.api.SCMEvent.Type.REMOVED; import static jenkins.scm.api.SCMEvent.Type.UPDATED; +import java.util.logging.Logger; +import java.util.logging.Level; + public final class GitLabSCMMergeRequestEvent extends GitLabSCMHeadEvent { - public static GitLabSCMMergeRequestEvent create(String id, MergeRequestHook hook, String origin) { - switch (hook.getObjectAttributes().getAction()) { - case open: + public static GitLabSCMMergeRequestEvent create(String id, MergeRequestHook hook, String action, String origin) { + Logger LOGGER = Logger.getLogger(GitLabSCMMergeRequestEvent.class.getName()); + + if(hook.getObjectAttributes().getAction()==null) + { + LOGGER.warning("hook ObjectAttributes action is null"); + } + switch (action) { + case "open": + return new GitLabSCMMergeRequestEvent(CREATED, id, hook, origin); + case "reopen": return new GitLabSCMMergeRequestEvent(CREATED, id, hook, origin); - case update: + case "update": return new GitLabSCMMergeRequestEvent(UPDATED, id, hook, origin); default: // other actions are "merged" and "closed". in both cases we can remove the head diff --git a/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/hooks/HookHandler.java b/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/hooks/HookHandler.java index d71a587..7150041 100644 --- a/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/hooks/HookHandler.java +++ b/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/hooks/HookHandler.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.util.NoSuchElementException; import java.util.logging.Logger; +import java.util.logging.Level; import static argelbargel.jenkins.plugins.gitlab_branch_source.api.GitLabHookEventType.byHeader; import static jenkins.scm.api.SCMEvent.originOf; @@ -35,39 +36,55 @@ void handle(String id, HttpServletRequest request) throws IOException { } private void handle(String id, GitLabHookEventType eventType, HttpServletRequest request) throws IOException { - switch (eventType) { - case PUSH: - SCMHeadEvent.fireNow(new GitLabSCMPushEvent(id, readHook(PushHook.class, request), originOf(request))); - break; - case TAG_PUSH: - SCMHeadEvent.fireNow(new GitLabSCMTagPushEvent(id, readHook(PushHook.class, request), originOf(request))); - break; - case MERGE_REQUEST: - SCMHeadEvent.fireNow(GitLabSCMMergeRequestEvent.create(id, readHook(MergeRequestHook.class, request), originOf(request))); - break; - case SYSTEM_HOOK: - handleSystemHook(id, request); - break; - default: - throw new IllegalArgumentException("cannot handle hook-event of type " + eventType); + LOGGER.fine("handling hook for " + id + " for eventType " + eventType); + try + { + + String requestBody = getRequestBody(request); + switch (eventType) { + case PUSH: + SCMHeadEvent.fireNow(new GitLabSCMPushEvent(id, readHook(PushHook.class, requestBody), originOf(request))); + break; + case TAG_PUSH: + SCMHeadEvent.fireNow(new GitLabSCMTagPushEvent(id, readHook(PushHook.class, requestBody), originOf(request))); + break; + case MERGE_REQUEST: + argelbargel.jenkins.plugins.gitlab_branch_source.api.Hooks.MergeRequestHook hookAction= readHookTemp(argelbargel.jenkins.plugins.gitlab_branch_source.api.Hooks.MergeRequestHook.class, requestBody); + SCMHeadEvent.fireNow(GitLabSCMMergeRequestEvent.create(id, readHook(MergeRequestHook.class, requestBody),hookAction.getObjectAttributes().getAction(), originOf(request))); + break; + case SYSTEM_HOOK: + handleSystemHook(id, request,requestBody); + break; + default: + LOGGER.warning("ignoring hook: " + eventType); + throw new IllegalArgumentException("cannot handle hook-event of type " + eventType); + } + } catch (Exception e) { + LOGGER.log(Level.WARNING, "GitLabHookEventType", e); } } - private void handleSystemHook(String id, HttpServletRequest request) throws IOException { + private void handleSystemHook(String id, HttpServletRequest request, String requestBody) throws IOException { try { - LOGGER.fine("handling system-hook for " + id); - SystemHook hook = readHook(SystemHook.class, request); + SystemHook hook = readHook(SystemHook.class, requestBody); SCMSourceEvent.fireNow(GitLabSCMSourceEvent.create(id, hook, originOf(request))); } catch (IllegalArgumentException e) { LOGGER.warning("ignoring system hook: " + e.getMessage()); } } - private T readHook(Class type, HttpServletRequest req) { + private T readHook(Class type, String requestBody) { + try { + return JsonUtil.read(requestBody, type); + } catch (Exception e) { + throw new IllegalArgumentException("could not read payload"); + } + } + private T readHookTemp(Class type, String requestBody) { try { - return JsonUtil.read(getRequestBody(req), type); + return JsonUtil.read(requestBody, type); } catch (Exception e) { - throw new IllegalArgumentException("ould not read payload"); + throw new IllegalArgumentException("could not read payload"); } } From b93258e5df0e5a0bd923fa892cac138eb857b54b Mon Sep 17 00:00:00 2001 From: Ilya Beyrak Date: Thu, 19 Apr 2018 10:23:53 -0500 Subject: [PATCH 04/10] fix settings screen not storing option for Build Merged because of wrong variable --- .../settings/GitLabSCMOriginMonitorStrategy/config.jelly | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/argelbargel/jenkins/plugins/gitlab_branch_source/settings/GitLabSCMOriginMonitorStrategy/config.jelly b/src/main/resources/argelbargel/jenkins/plugins/gitlab_branch_source/settings/GitLabSCMOriginMonitorStrategy/config.jelly index e4fc5db..4f726dc 100644 --- a/src/main/resources/argelbargel/jenkins/plugins/gitlab_branch_source/settings/GitLabSCMOriginMonitorStrategy/config.jelly +++ b/src/main/resources/argelbargel/jenkins/plugins/gitlab_branch_source/settings/GitLabSCMOriginMonitorStrategy/config.jelly @@ -4,7 +4,7 @@ - From 062ef0c84036c4e5b173dba85c3f673b7658282f Mon Sep 17 00:00:00 2001 From: Ilya Beyrak Date: Thu, 19 Apr 2018 23:14:11 -0500 Subject: [PATCH 05/10] remove duplicative call to build after merge --- .../plugins/gitlab_branch_source/SourceHeads.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/SourceHeads.java b/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/SourceHeads.java index 453e808..af1b793 100644 --- a/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/SourceHeads.java +++ b/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/SourceHeads.java @@ -39,8 +39,13 @@ import static hudson.model.TaskListener.NULL; import static java.util.Collections.emptyMap; +import java.util.logging.Logger; +import java.util.logging.Level; + class SourceHeads { + + private static final Logger LOGGER = Logger.getLogger(SourceHeads.class.getName()); private static final SCMHeadObserver NOOP_OBSERVER = new SCMHeadObserver() { @Override public void observe(@Nonnull jenkins.scm.api.SCMHead head, @Nonnull SCMRevision revision) { /* NOOP */ } @@ -120,11 +125,6 @@ private void retrieveMergeRequest(SCMSourceCriteria criteria, @Nonnull SCMHeadOb log(listener, Messages.GitLabSCMSource_removedMergeRequest(mrId)); branchesWithMergeRequests(listener).remove(mrId); } - - int sourceProjectId = attributes.getSourceProjectId(); - if (sourceProjectId == source.getProjectId()) { - observe(criteria, observer, createBranch(source.getProjectId(), attributes.getSourceBranch(), attributes.getLastCommit().getId()), listener); - } } } @@ -238,7 +238,6 @@ private void observe(SCMSourceCriteria criteria, @Nonnull SCMHeadObserver observ mergeRequest.getIid(), createBranch(mergeRequest.getSourceProjectId(), mergeRequest.getSourceBranch(), mergeRequest.getSha()), createBranch(mergeRequest.getTargetProjectId(), targetBranch, retrieveBranchRevision(targetBranch)), Objects.equals(mergeRequest.getMergeStatus(), CAN_BE_MERGED)); - if (source.getSourceSettings().buildUnmerged(head)) { observe(criteria, observer, head, listener); } From 86b092ea2c0f0ec7b42ed0fb95d4a2691fc19fff Mon Sep 17 00:00:00 2001 From: Ilya Beyrak Date: Mon, 7 May 2018 07:26:48 -0500 Subject: [PATCH 06/10] fix test code --- .../plugins/gitlab_branch_source/heads/GitLabSCMHead.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/heads/GitLabSCMHead.java b/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/heads/GitLabSCMHead.java index f30ab62..b12d752 100644 --- a/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/heads/GitLabSCMHead.java +++ b/src/main/java/argelbargel/jenkins/plugins/gitlab_branch_source/heads/GitLabSCMHead.java @@ -14,11 +14,11 @@ public abstract class GitLabSCMHead extends SCMHead implements SCMHeadMixin { public static final String REVISION_HEAD = "HEAD"; public static GitLabSCMBranchHead createBranch(int projectId, String name, String hash) { - return createBranch(projectId, name, hash, false); + return createBranch(projectId, name+"t2", hash, false); } public static GitLabSCMTagHead createTag(int projectId, String name, String hash, long timestamp) { - return new GitLabSCMTagHead(projectId, name, hash, timestamp); + return new GitLabSCMTagHead(projectId, name+"t3", hash, timestamp); } public static GitLabSCMMergeRequestHead createMergeRequest(int id, String name, int iid, GitLabSCMHead source, GitLabSCMBranchHead target) { From 59444ff365765fdf7882cddd66a99280bcdb59ca Mon Sep 17 00:00:00 2001 From: Ilya Beyrak Date: Thu, 7 Jun 2018 21:41:38 -0500 Subject: [PATCH 07/10] update to gitlab v4 api --- pom.xml | 39 ++++++++++--------- .../GitLabSCMBranchBuildStrategy.java | 11 ++++++ .../gitlab_branch_source/GitLabSCMIcons.java | 6 +-- .../gitlab_branch_source/api/GitLabAPI.java | 5 ++- .../api/GitLabProjectSelector.java | 2 +- 5 files changed, 38 insertions(+), 25 deletions(-) diff --git a/pom.xml b/pom.xml index f227e4a..81359b5 100644 --- a/pom.xml +++ b/pom.xml @@ -12,8 +12,9 @@ 2.7.1 - 1.7 - 1.7 + 1.8 + 1.8 + 8 argelbargel.jenkins.plugins @@ -36,47 +37,47 @@ org.jenkins-ci.plugins branch-api - 2.0.9 + 2.0.20 org.jenkins-ci.plugins cloudbees-folder - 6.0.4 + 6.4 org.jenkins-ci.plugins credentials - 2.1.13 + 2.1.16 org.jenkins-ci.plugins git - 3.3.0 + 3.9.1 org.jenkins-ci.plugins git-client - 2.4.5 + 2.7.2 org.jenkins-ci.plugins gitlab-plugin - 1.5.3 + 1.5.6 org.jenkins-ci.plugins scm-api - 2.1.1 + 2.2.7 org.jenkins-ci.plugins.workflow workflow-api - 2.13 + 2.27 org.jenkins-ci.plugins.workflow workflow-cps - 2.30 + 2.53 org.jenkins-ci.plugins.workflow @@ -86,30 +87,30 @@ org.jenkins-ci.plugins.workflow workflow-multibranch - 2.14 + 2.19 org.gitlab java-gitlab-api - 1.2.7 + 4.0.0 org.jenkins-ci.plugins pipeline-stage-step - 2.2 + 2.3 test org.jenkins-ci.plugins.workflow workflow-basic-steps - 2.4 + 2.7 test org.jenkins-ci.plugins.workflow workflow-durable-task-step - 2.11 + 2.19 test @@ -117,14 +118,14 @@ org.jenkins-ci.plugins.workflow workflow-scm-step - 2.4 + 2.6 org.jenkins-ci.plugins structs - 1.6 + 1.14 test @@ -132,7 +133,7 @@ org.jenkins-ci.plugins github-branch-source - 2.0.5 + 2.3.6 test