Skip to content
This repository has been archived by the owner on Dec 10, 2019. It is now read-only.

Commit

Permalink
add membership=true to gitlab project listings (#66)
Browse files Browse the repository at this point in the history
* add membership=true to gitlab project listings

* add removal of double slashes which occurs and makes api fail

* 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.

* fix settings screen not storing option for Build Merged because of wrong variable

* remove duplicative call to build after merge

* fix test code
  • Loading branch information
ilushka85 authored and Argelbargel committed May 16, 2018
1 parent 19b02a2 commit b2831d3
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 */ }
Expand Down Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ private List<GitlabBranch> 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);
Expand Down Expand Up @@ -282,18 +283,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();
Expand All @@ -302,14 +303,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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<MergeRequestHook> {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 extends WebHook> T readHook(Class<T> type, HttpServletRequest req) {
private <T extends WebHook> T readHook(Class<T> type, String requestBody) {
try {
return JsonUtil.read(requestBody, type);
} catch (Exception e) {
throw new IllegalArgumentException("could not read payload");
}
}
private <T extends argelbargel.jenkins.plugins.gitlab_branch_source.api.Hooks.WebHook> T readHookTemp(Class<T> 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");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<f:optionalBlock title="${%Monitor &amp; Build Merge Requests from Origin}" field="monitored" inline="true">
<f:nested>
<table>
<f:optionalBlock title="${%Build merged with base branch}" field="buildMerged"
<f:optionalBlock title="${%Build merged with base branch}" field="build"
checked="${descriptor.defaults.build}" inline="true">
<f:entry title="${%Build only mergeable requests}" field="buildOnlyMergeableMerged">
<f:checkbox default="${descriptor.defaults.buildOnlyMergeableMerged}"/>
Expand Down

0 comments on commit b2831d3

Please sign in to comment.