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

update to v4 of gitlab api #80

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
39 changes: 20 additions & 19 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

<properties>
<jenkins.version>2.7.1</jenkins.version>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.level>8</java.level>
</properties>

<groupId>argelbargel.jenkins.plugins</groupId>
Expand All @@ -36,47 +37,47 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>branch-api</artifactId>
<version>2.0.9</version>
<version>2.0.20</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>cloudbees-folder</artifactId>
<version>6.0.4</version>
<version>6.4</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials</artifactId>
<version>2.1.13</version>
<version>2.1.16</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git</artifactId>
<version>3.3.0</version>
<version>3.9.1</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git-client</artifactId>
<version>2.4.5</version>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>gitlab-plugin</artifactId>
<version>1.5.3</version>
<version>1.5.5</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
<version>2.1.1</version>
<version>2.2.7</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-api</artifactId>
<version>2.13</version>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>2.30</version>
<version>2.53</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand All @@ -86,53 +87,53 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-multibranch</artifactId>
<version>2.14</version>
<version>2.19</version>
</dependency>
<dependency>
<groupId>org.gitlab</groupId>
<artifactId>java-gitlab-api</artifactId>
<version>1.2.7</version>
<version>4.0.0</version>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>pipeline-stage-step</artifactId>
<version>2.2</version>
<version>2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<version>2.4</version>
<version>2.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-durable-task-step</artifactId>
<version>2.11</version>
<version>2.19</version>
<scope>test</scope>
</dependency>

<!-- without this the build fails because of cyclic-dependencies -->
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-scm-step</artifactId>
<version>2.4</version>
<version>2.6</version>
</dependency>

<!-- by default 1.4 is installed which causes an StackOverflowError -->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.6</version>
<version>1.14</version>
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>github-branch-source</artifactId>
<version>2.0.5</version>
<version>2.3.6</version>
<scope>test</scope>
</dependency>
<!-- TODO: i'd like to have those for local development but not when building with travis...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMSource;
import jenkins.scm.api.SCMSourceDescriptor;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.mixin.TagSCMHead;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.StaplerRequest;
Expand Down Expand Up @@ -47,6 +48,16 @@ private boolean isAutomaticBuild(GitLabSCMSource source, SCMHead head) {
return true;
}

@Override
public boolean isAutomaticBuild(SCMSource source, SCMHead head, SCMRevision var3, SCMRevision var4)
{
if (source instanceof GitLabSCMSource) {
return isAutomaticBuild((GitLabSCMSource) source, head);
}

return !TagSCMHead.class.isInstance(head);
}

private boolean isAutomaticBuild(GitLabSCMSource source, GitLabSCMMergeRequestHead head) {
if (!head.isMerged()) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ static String iconFilePathPattern(String name) {

private static String groupAvatarUrl(GitlabProject project, String connectionName) throws GitLabAPIException {
GitlabNamespace namespace = project.getNamespace();
if (namespace.getOwnerId() != null) {
return null;
}
// if (namespace.getOwnerId() != null) {
// return null;
// }

GitLabGroup group = gitLabAPI(connectionName).getGroup(namespace.getId());
return group.getAvatarUrl();
Expand Down
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 @@ -111,7 +116,7 @@ private void retrieveMergeRequest(SCMSourceCriteria criteria, @Nonnull SCMHeadOb
String targetBranch = attributes.getTargetBranch();

if (!source.isExcluded(targetBranch)) {
int mrId = attributes.getId();
int mrId = attributes.getIid();
log(listener, Messages.GitLabSCMSource_retrievingMergeRequest(mrId));
try {
GitLabMergeRequest mr = api().getMergeRequest(source.getProjectId(), mrId);
Expand All @@ -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 @@ -233,12 +233,11 @@ private void observe(SCMSourceCriteria criteria, @Nonnull SCMHeadObserver observ

String targetBranch = mergeRequest.getTargetBranch();
GitLabSCMMergeRequestHead head = createMergeRequest(
mergeRequest.getId(),
mergeRequest.getIid(),
mergeRequest.getTitle(),
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 All @@ -251,7 +250,7 @@ private void observe(SCMSourceCriteria criteria, @Nonnull SCMHeadObserver observ
}

if (!source.getSourceSettings().getBranchMonitorStrategy().getBuildBranchesWithMergeRequests() && head.fromOrigin()) {
branchesWithMergeRequests(listener).put(mergeRequest.getId(), mergeRequest.getSourceBranch());
branchesWithMergeRequests(listener).put(mergeRequest.getIid(), mergeRequest.getSourceBranch());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public final class GitLabSCMAcceptMergeRequestAction extends InvisibleAction imp

public GitLabSCMAcceptMergeRequestAction(GitLabMergeRequest MR, int mergeRequestScopedId, String commitMessage, boolean removeSourceBranch) {
this.mergeRequestPID = MR.getProjectId();
this.mergeRequestID = MR.getId();
this.mergeRequestID = MR.getIid();
this.mergeRequestScopedId = mergeRequestScopedId;
this.commitMessage = commitMessage;
this.removeSourceBranch = removeSourceBranch;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,15 @@ public void run() {

public void publishResult(Run<?, ?> build, GitLabSCMHeadMetadataAction metadata) {
Result buildResult = build.getResult();

if ((buildResult == SUCCESS) || ((buildResult == UNSTABLE) && markUnstableAsSuccess)) {

updateRunningContexts(build, metadata, success);
} else if (buildResult == ABORTED) {

updateRunningContexts(build, metadata, canceled);
} else {

updateRunningContexts(build, metadata, failed);
}
}
Expand Down Expand Up @@ -135,7 +139,14 @@ public void onNewHead(FlowNode node) {
if (isNamedStageStartNode(node)) {
publishBuildStatus(build, metadata, running, getRunningContexts().push(node), "");
} else if (isStageEndNode(node, getRunningContexts().peekNodeId())) {
publishBuildStatus(build, metadata, success, getRunningContexts().pop(), "");
Result buildResult = build.getResult();
if(buildResult==UNSTABLE ) {
publishBuildStatus(build, metadata,failed, getRunningContexts().pop(), "");
}
else
{
publishBuildStatus(build, metadata, success, getRunningContexts().pop(), "");
}
}
}

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 @@ -187,10 +188,11 @@ public List<GitlabRepositoryTree> getTree(int id, String ref, String path) throw
try {
Query query = new Query()
.appendIf("path", path)
.appendIf("ref_name", ref);

.appendIf("ref", ref);

query.append("per_page","10000");
String tailUrl = GitlabProject.URL + "/" + id + "/repository" + GitlabRepositoryTree.URL + query.toString();
LOGGER.fine("tailurl: " + tailUrl);
GitlabRepositoryTree[] tree = delegate.retrieve().to(tailUrl, GitlabRepositoryTree[].class);
return Arrays.asList(tree);
} catch (Exception e) {
Expand Down Expand Up @@ -282,18 +284,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 +304,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
Expand Up @@ -7,7 +7,7 @@
public enum GitLabProjectSelector {
OWNED("owned"),
STARRED("starred"),
VISIBLE("visible");
VISIBLE("");

public static GitLabProjectSelector byId(String id) {
for (GitLabProjectSelector value : values()) {
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();
}
}
Loading