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
37 changes: 19 additions & 18 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,27 +37,27 @@
<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>
Expand All @@ -66,17 +67,17 @@
<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,51 +87,51 @@
<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>
<!-- Fix NoClassDefFoundError org/jenkinsci/plugins/workflow/steps/SynchronousStepExecution on startup -->
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
@@ -1,24 +1,22 @@
package argelbargel.jenkins.plugins.gitlab_branch_source;


import argelbargel.jenkins.plugins.gitlab_branch_source.api.GitLabAPI;
import argelbargel.jenkins.plugins.gitlab_branch_source.api.GitLabAPIException;
import argelbargel.jenkins.plugins.gitlab_branch_source.api.GitLabMergeRequest;
import argelbargel.jenkins.plugins.gitlab_branch_source.api.filters.GitLabMergeRequestFilter;
import argelbargel.jenkins.plugins.gitlab_branch_source.events.GitLabSCMMergeRequestEvent;
import argelbargel.jenkins.plugins.gitlab_branch_source.events.GitLabSCMNoteEvent;
import argelbargel.jenkins.plugins.gitlab_branch_source.events.GitLabSCMPushEvent;
import argelbargel.jenkins.plugins.gitlab_branch_source.events.GitLabSCMTagPushEvent;
import argelbargel.jenkins.plugins.gitlab_branch_source.heads.GitLabSCMHead;
import argelbargel.jenkins.plugins.gitlab_branch_source.heads.GitLabSCMMergeRequestHead;
import argelbargel.jenkins.plugins.gitlab_branch_source.heads.GitLabSCMTagHead;
import com.dabsquared.gitlabjenkins.gitlab.hook.model.MergeRequestObjectAttributes;
import hudson.model.TaskListener;
import jenkins.branch.MultiBranchProject;
import jenkins.model.ParameterizedJobMixIn;
import jenkins.plugins.git.AbstractGitSCMSource.SCMRevisionImpl;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMHeadEvent;
import jenkins.scm.api.SCMHeadObserver;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.SCMSourceCriteria;
import jenkins.scm.api.*;
import org.gitlab.api.models.GitlabBranch;
import org.gitlab.api.models.GitlabTag;

Expand Down Expand Up @@ -70,6 +68,8 @@ public boolean isHead(@Nonnull Probe probe, @Nonnull TaskListener taskListener)
void retrieve(@CheckForNull SCMSourceCriteria criteria, @Nonnull SCMHeadObserver observer, @CheckForNull SCMHeadEvent<?> event, @Nonnull TaskListener listener) throws IOException, InterruptedException {
if (event instanceof GitLabSCMMergeRequestEvent) {
retrieveMergeRequest(criteria, observer, (GitLabSCMMergeRequestEvent) event, listener);
} else if (event instanceof GitLabSCMNoteEvent) {
retrieveMergeRequest(criteria, observer, (GitLabSCMNoteEvent) event, listener);
} else if (event instanceof GitLabSCMTagPushEvent) {
retrieveTag(criteria, observer, (GitLabSCMTagPushEvent) event, listener);
} else if (event instanceof GitLabSCMPushEvent) {
Expand Down Expand Up @@ -116,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 @@ -127,6 +127,11 @@ private void retrieveMergeRequest(SCMSourceCriteria criteria, @Nonnull SCMHeadOb
}
}
}
private void retrieveMergeRequest(SCMSourceCriteria criteria, @Nonnull SCMHeadObserver observer, @Nonnull GitLabSCMNoteEvent event, @Nonnull TaskListener listener) throws IOException, InterruptedException {
MergeRequestObjectAttributes attributes = event.getPayload().getMergeRequest();
String targetBranch = attributes.getTargetBranch();

}

private void retrieveBranch(SCMSourceCriteria criteria, @Nonnull SCMHeadObserver observer, @Nonnull GitLabSCMPushEvent event, @Nonnull TaskListener listener) throws IOException, InterruptedException {
retrieveBranch(criteria, observer, BRANCHES.remoteName(event.getPayload().getRef()), listener);
Expand Down Expand Up @@ -233,12 +238,13 @@ 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 @@ -250,12 +256,15 @@ 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());
}
}

private void observe(SCMSourceCriteria criteria, @Nonnull SCMHeadObserver observer, GitLabSCMHead head, TaskListener listener) throws IOException, InterruptedException {

if (criteria == null || matches(criteria, head, listener)) {


observer.observe(head, head.getRevision());
}
}
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 @@ -102,6 +102,9 @@ public void run() {
public void publishResult(Run<?, ?> build, GitLabSCMHeadMetadataAction metadata) {
Result buildResult = build.getResult();
updateRunningContexts(build, metadata, toBuildStateFromResult(buildResult));



}

private void updateRunningContexts(Run<?, ?> build, GitLabSCMHeadMetadataAction metadata, BuildState state) {
Expand Down Expand Up @@ -142,7 +145,7 @@ public void onNewHead(FlowNode node) {

} else if (isStageEndNode(node, getRunningContexts().peekNodeId())) {

// If this or a prior stage failed then build.result is set to 'FAILED'
// If this or a prior stage failed then build.result is set to 'FAILED'
// otherwise build.result is still null and we assume success.
BuildState state = success;
if(build.getResult() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public enum GitLabHookEventType {
MERGE_REQUEST("Merge Request Hook"),
PUSH("Push Hook"),
SYSTEM_HOOK("System Hook"),
TAG_PUSH("Tag Push Hook");
TAG_PUSH("Tag Push Hook"),
NOTE("Note Hook");

public static GitLabHookEventType byHeader(String header) {
if (StringUtils.isBlank(header)) {
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
@@ -1,10 +1,7 @@
package argelbargel.jenkins.plugins.gitlab_branch_source.events;

import com.dabsquared.gitlabjenkins.cause.CauseData;
import com.dabsquared.gitlabjenkins.gitlab.hook.model.Commit;
import com.dabsquared.gitlabjenkins.gitlab.hook.model.MergeRequestHook;
import com.dabsquared.gitlabjenkins.gitlab.hook.model.MergeRequestObjectAttributes;
import com.dabsquared.gitlabjenkins.gitlab.hook.model.PushHook;
import com.dabsquared.gitlabjenkins.gitlab.hook.model.*;
import org.eclipse.jgit.util.StringUtils;

import java.util.List;
Expand Down Expand Up @@ -47,6 +44,9 @@ static CauseData buildCauseData(PushHook hook) {
static CauseData buildCauseData(MergeRequestHook hook) {
return buildCauseData(hook.getObjectAttributes());
}
static CauseData buildCauseData(NoteHook hook) {
return buildCauseData(hook.getMergeRequest());
}

private static CauseData buildCauseData(MergeRequestObjectAttributes attributes) {
return causeData()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Collection<? extends GitLabSCMHead> heads(@Nonnull GitLabSCMSource source) throw
String sourceBranch = attributes.getSourceBranch();
String hash = attributes.getLastCommit().getId();
GitLabSCMMergeRequestHead head = createMergeRequest(
attributes.getId(), attributes.getTitle(), attributes.getIid(),
attributes.getIid(), attributes.getTitle(), attributes.getIid(),
createBranch(sourceProjectId, sourceBranch, hash),
createBranch(attributes.getTargetProjectId(), attributes.getTargetBranch(), REVISION_HEAD));

Expand Down
Loading