Skip to content

Commit eafa7c1

Browse files
lmifemosaic-nick
andauthored
JENKINS-62343: Correctly handle lightweight checkout of MRs (#364)
Co-authored-by: Nick Chadwick <nick@mosaicsmartdata.com>
1 parent 588456f commit eafa7c1

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMFileSystem.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
import hudson.scm.SCM;
1010
import hudson.scm.SCMDescriptor;
1111
import java.io.IOException;
12-
import jenkins.plugins.git.GitTagSCMRevision;
1312
import jenkins.scm.api.SCMFile;
1413
import jenkins.scm.api.SCMFileSystem;
1514
import jenkins.scm.api.SCMHead;
1615
import jenkins.scm.api.SCMRevision;
1716
import jenkins.scm.api.SCMSource;
1817
import jenkins.scm.api.SCMSourceDescriptor;
18+
import jenkins.scm.api.mixin.ChangeRequestCheckoutStrategy;
1919
import org.gitlab4j.api.GitLabApi;
2020
import org.gitlab4j.api.GitLabApiException;
2121

@@ -30,19 +30,7 @@ protected GitLabSCMFileSystem(GitLabApi gitLabApi, String projectPath, String re
3030
super(rev);
3131
this.gitLabApi = gitLabApi;
3232
this.projectPath = projectPath;
33-
if (rev != null) {
34-
if (rev.getHead() instanceof MergeRequestSCMHead) {
35-
this.ref = ((MergeRequestSCMRevision) rev).getOrigin().getHash();
36-
} else if (rev instanceof BranchSCMRevision) {
37-
this.ref = ((BranchSCMRevision) rev).getHash();
38-
} else if (rev instanceof GitTagSCMRevision) {
39-
this.ref = ((GitTagSCMRevision) rev).getHash();
40-
} else {
41-
this.ref = ref;
42-
}
43-
} else {
44-
this.ref = ref;
45-
}
33+
this.ref = ref;
4634
}
4735

4836
@Override
@@ -109,7 +97,17 @@ public SCMFileSystem build(
10997
throws IOException, InterruptedException {
11098
String ref;
11199
if (head instanceof MergeRequestSCMHead) {
112-
ref = ((MergeRequestSCMHead) head).getOriginName();
100+
MergeRequestSCMHead mrHead = (MergeRequestSCMHead) head;
101+
ChangeRequestCheckoutStrategy checkoutStrategy = mrHead.getCheckoutStrategy();
102+
String mrRef;
103+
if (checkoutStrategy == ChangeRequestCheckoutStrategy.HEAD) {
104+
mrRef = "head";
105+
} else if (checkoutStrategy == ChangeRequestCheckoutStrategy.MERGE) {
106+
mrRef = "merge";
107+
} else {
108+
return null;
109+
}
110+
ref = String.format("merge-requests/%s/%s", mrHead.getId(), mrRef);
113111
} else if (head instanceof BranchSCMHead) {
114112
ref = head.getName();
115113
} else if (head instanceof GitLabTagSCMHead) {

0 commit comments

Comments
 (0)