Skip to content

Commit a0e3777

Browse files
baymacjetersen
andauthored
[JENKINS-61760] Use git plugin's MergeWithSCMExtension Class and POM cleanup (#81)
* Use git plugin's MergeWithSCMExtension Class * Add bom to dep mgmt, upgrade plugin versions * Remove dep versions to let bom handle it * use handy uri templates api plugin to avoid class loading issues * ignore super class shadowing * remove duplicate branch api dep * remove used deps and variable in pom * move joda-time back to dependency management * indents * use the newer jcasc test-harness Co-authored-by: Joseph Petersen <[email protected]>
1 parent 9d02c79 commit a0e3777

File tree

3 files changed

+34
-146
lines changed

3 files changed

+34
-146
lines changed

pom.xml

Lines changed: 22 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,14 @@
1414
<properties>
1515
<revision>1.4.6</revision>
1616
<changelist>-SNAPSHOT</changelist>
17-
<!-- Baseline Jenkins version you use to build the plugin. Users must have this version or newer to run. -->
1817
<jenkins.version>2.176.2</jenkins.version>
1918
<java.level>8</java.level>
20-
<!-- Other properties you may want to use:
21-
~ jenkins-test-harness.version: Jenkins Test Harness version you use to test the plugin. For Jenkins version >= 1.580.1 use JTH 2.0 or higher.
22-
~ hpi-plugin.version: The HPI Maven Plugin version used by the plugin..
23-
~ stapler-plugin.version: The Stapler Maven plugin version required by the plugin.
24-
-->
25-
<jcasc.version>1.19</jcasc.version>
19+
<jcasc.version>1.36</jcasc.version>
2620
</properties>
2721
<name>GitLab Branch Source Plugin</name>
2822
<description>GitLab Provides branch source and folder organisation functionality for GitLab
2923
Repositories in Jenkins Source Plugin
3024
</description>
31-
<!-- The default licence for Jenkins OSS Plugins is MIT. Substitute for the applicable one if needed. -->
3225
<licenses>
3326
<license>
3427
<name>MIT License</name>
@@ -39,12 +32,10 @@
3932
<dependency>
4033
<groupId>org.jenkins-ci.plugins</groupId>
4134
<artifactId>apache-httpcomponents-client-4-api</artifactId>
42-
<version>4.5.10-2.0</version>
4335
</dependency>
4436
<dependency>
4537
<groupId>org.jenkins-ci.plugins</groupId>
4638
<artifactId>scm-api</artifactId>
47-
<version>2.5.1</version>
4839
</dependency>
4940
<dependency>
5041
<groupId>io.jenkins.plugins</groupId>
@@ -54,27 +45,24 @@
5445
<dependency>
5546
<groupId>org.jenkins-ci.plugins</groupId>
5647
<artifactId>credentials</artifactId>
57-
<version>2.2.0</version>
5848
</dependency>
5949
<dependency>
6050
<groupId>org.jenkins-ci.plugins</groupId>
6151
<artifactId>plain-credentials</artifactId>
62-
<version>1.3</version>
6352
</dependency>
6453
<dependency>
6554
<groupId>javax.servlet</groupId>
6655
<artifactId>javax.servlet-api</artifactId>
6756
<version>4.0.1</version>
6857
</dependency>
6958
<dependency>
70-
<groupId>com.damnhandy</groupId>
71-
<artifactId>handy-uri-templates</artifactId>
72-
<version>2.1.6</version>
59+
<groupId>org.jenkins-ci.plugins</groupId>
60+
<artifactId>handy-uri-templates-2-api</artifactId>
61+
<version>2.1.6-1.0</version>
7362
</dependency>
7463
<dependency>
7564
<groupId>org.jenkins-ci.plugins</groupId>
7665
<artifactId>git</artifactId>
77-
<version>3.10.0</version>
7866
</dependency>
7967
<dependency>
8068
<groupId>org.hamcrest</groupId>
@@ -85,49 +73,41 @@
8573
<dependency>
8674
<groupId>org.jenkins-ci.plugins</groupId>
8775
<artifactId>pipeline-build-step</artifactId>
88-
<version>2.4</version>
8976
<scope>test</scope>
9077
</dependency>
9178
<dependency>
9279
<groupId>org.jenkins-ci.plugins.workflow</groupId>
9380
<artifactId>workflow-api</artifactId>
94-
<version>2.33</version>
9581
<scope>test</scope>
9682
</dependency>
9783
<dependency>
9884
<groupId>org.jenkins-ci.plugins.workflow</groupId>
9985
<artifactId>workflow-basic-steps</artifactId>
100-
<version>2.4</version>
10186
<scope>test</scope>
10287
</dependency>
10388
<dependency>
10489
<groupId>org.jenkins-ci.plugins.workflow</groupId>
10590
<artifactId>workflow-cps</artifactId>
106-
<version>2.61</version>
10791
<scope>test</scope>
10892
</dependency>
10993
<dependency>
11094
<groupId>org.jenkins-ci.plugins.workflow</groupId>
11195
<artifactId>workflow-job</artifactId>
112-
<version>2.31</version>
11396
<scope>test</scope>
11497
</dependency>
11598
<dependency>
11699
<groupId>org.jenkins-ci.plugins.workflow</groupId>
117100
<artifactId>workflow-scm-step</artifactId>
118-
<version>2.7</version>
119101
<scope>test</scope>
120102
</dependency>
121103
<dependency>
122104
<groupId>org.jenkins-ci.plugins.workflow</groupId>
123105
<artifactId>workflow-step-api</artifactId>
124-
<version>2.19</version>
125106
<scope>test</scope>
126107
</dependency>
127108
<dependency>
128109
<groupId>org.jenkins-ci.plugins.workflow</groupId>
129110
<artifactId>workflow-support</artifactId>
130-
<version>3.2</version>
131111
<scope>test</scope>
132112
</dependency>
133113
<dependency>
@@ -145,7 +125,6 @@
145125
<dependency>
146126
<groupId>org.jenkins-ci.plugins.workflow</groupId>
147127
<artifactId>workflow-durable-task-step</artifactId>
148-
<version>2.19</version>
149128
<scope>test</scope>
150129
</dependency>
151130
<dependency>
@@ -173,51 +152,41 @@
173152
<scope>test</scope>
174153
</dependency>
175154
<dependency>
176-
<groupId>io.jenkins</groupId>
177-
<artifactId>configuration-as-code</artifactId>
155+
<groupId>io.jenkins.configuration-as-code</groupId>
156+
<artifactId>test-harness</artifactId>
178157
<version>${jcasc.version}</version>
179-
<classifier>tests</classifier>
180158
<scope>test</scope>
159+
<exclusions>
160+
<exclusion>
161+
<groupId>com.fasterxml.jackson.core</groupId>
162+
<artifactId>jackson-databind</artifactId>
163+
</exclusion>
164+
<exclusion>
165+
<groupId>com.fasterxml.jackson.core</groupId>
166+
<artifactId>jackson-core</artifactId>
167+
</exclusion>
168+
</exclusions>
181169
</dependency>
182170
<dependency>
183171
<groupId>org.jenkins-ci.plugins</groupId>
184172
<artifactId>branch-api</artifactId>
185-
<scope>compile</scope>
173+
<scope>test</scope>
186174
</dependency>
187175
</dependencies>
188176
<dependencyManagement>
189177
<dependencies>
190178
<dependency>
191-
<groupId>org.jenkins-ci.plugins</groupId>
192-
<artifactId>structs</artifactId>
193-
<version>1.19</version>
179+
<groupId>io.jenkins.tools.bom</groupId>
180+
<artifactId>bom-2.176.x</artifactId>
181+
<version>4</version>
182+
<scope>import</scope>
183+
<type>pom</type>
194184
</dependency>
195185
<dependency>
196186
<groupId>joda-time</groupId>
197187
<artifactId>joda-time</artifactId>
198188
<version>2.9.5</version>
199189
</dependency>
200-
<dependency>
201-
<groupId>org.jenkins-ci.plugins</groupId>
202-
<artifactId>script-security</artifactId>
203-
<version>1.48</version>
204-
</dependency>
205-
<dependency>
206-
<groupId>org.jenkins-ci.plugins</groupId>
207-
<artifactId>branch-api</artifactId>
208-
<version>2.5.4</version>
209-
<scope>test</scope>
210-
</dependency>
211-
<dependency>
212-
<groupId>org.jenkins-ci.plugins</groupId>
213-
<artifactId>cloudbees-folder</artifactId>
214-
<version>6.9</version>
215-
</dependency>
216-
<dependency>
217-
<groupId>org.jenkins-ci.plugins</groupId>
218-
<artifactId>junit</artifactId>
219-
<version>1.3</version>
220-
</dependency>
221190
</dependencies>
222191
</dependencyManagement>
223192

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Random;
2020
import java.util.Set;
2121
import jenkins.plugins.git.GitSCMBuilder;
22+
import jenkins.plugins.git.MergeWithGitSCMExtension;
2223
import jenkins.scm.api.SCMHead;
2324
import jenkins.scm.api.SCMRevision;
2425
import jenkins.scm.api.SCMSourceOwner;

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

Lines changed: 11 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -2,109 +2,27 @@
22

33
import edu.umd.cs.findbugs.annotations.CheckForNull;
44
import edu.umd.cs.findbugs.annotations.NonNull;
5-
import hudson.model.Run;
6-
import hudson.model.TaskListener;
7-
import hudson.plugins.git.GitException;
8-
import hudson.plugins.git.GitSCM;
9-
import hudson.plugins.git.Revision;
10-
import hudson.plugins.git.extensions.GitSCMExtension;
11-
import hudson.plugins.git.extensions.impl.PreBuildMerge;
12-
import hudson.plugins.git.util.MergeRecord;
13-
import java.io.IOException;
14-
import org.apache.commons.lang.StringUtils;
15-
import org.eclipse.jgit.lib.Constants;
16-
import org.eclipse.jgit.lib.ObjectId;
17-
import org.jenkinsci.plugins.gitclient.CheckoutCommand;
18-
import org.jenkinsci.plugins.gitclient.GitClient;
19-
import org.jenkinsci.plugins.gitclient.MergeCommand;
5+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
6+
import java.io.ObjectStreamException;
207
import org.kohsuke.accmod.Restricted;
218
import org.kohsuke.accmod.restrictions.NoExternalUse;
229

2310
/**
24-
* Similar to {@link PreBuildMerge}, but we cannot use that unmodified: we need to specify the exact
25-
* base branch hash. The hash is specified so that we are not subject to a race condition between
26-
* the {@code baseHash} we think we are merging with and a possibly newer one that was just pushed.
11+
* Retained for data migration.
12+
*
13+
* @deprecated use {@link jenkins.plugins.git.MergeWithGitSCMExtension}
2714
*/
15+
@Deprecated
2816
@Restricted(NoExternalUse.class)
29-
public class MergeWithGitSCMExtension extends GitSCMExtension {
30-
31-
@NonNull
32-
private final String baseName;
33-
@CheckForNull
34-
private final String baseHash;
17+
@SuppressFBWarnings(value = "NM_SAME_SIMPLE_NAME_AS_SUPERCLASS")
18+
public class MergeWithGitSCMExtension extends jenkins.plugins.git.MergeWithGitSCMExtension {
3519

3620
MergeWithGitSCMExtension(@NonNull String baseName, @CheckForNull String baseHash) {
37-
this.baseName = baseName;
38-
this.baseHash = baseHash;
39-
}
40-
41-
@NonNull
42-
public String getBaseName() {
43-
return baseName;
44-
}
45-
46-
@CheckForNull
47-
public String getBaseHash() {
48-
return baseHash;
49-
}
50-
51-
@Override
52-
public Revision decorateRevisionToBuild(GitSCM scm, Run<?, ?> build, GitClient git,
53-
TaskListener listener,
54-
Revision marked, Revision rev)
55-
throws IOException, InterruptedException, GitException {
56-
ObjectId baseObjectId;
57-
if (StringUtils.isBlank(baseHash)) {
58-
try {
59-
baseObjectId = git.revParse(Constants.R_REFS + baseName);
60-
} catch (GitException e) {
61-
listener.getLogger()
62-
.printf("Unable to determine head revision of %s prior to merge with MR%n",
63-
baseName);
64-
throw e;
65-
}
66-
} else {
67-
baseObjectId = ObjectId.fromString(baseHash);
68-
}
69-
listener.getLogger().printf("Merging %s commit %s into MR head commit %s%n",
70-
baseName, baseObjectId.name(), rev.getSha1String()
71-
);
72-
checkout(scm, build, git, listener, rev);
73-
try {
74-
/* could parse out of JenkinsLocationConfiguration.get().getAdminAddress() but seems overkill */
75-
git.setAuthor("Jenkins", "nobody@nowhere");
76-
git.setCommitter("Jenkins", "nobody@nowhere");
77-
MergeCommand cmd = git.merge().setRevisionToMerge(baseObjectId);
78-
for (GitSCMExtension ext : scm.getExtensions()) {
79-
// By default we do a regular merge, allowing it to fast-forward.
80-
ext.decorateMergeCommand(scm, build, git, listener, cmd);
81-
}
82-
cmd.execute();
83-
} catch (GitException x) {
84-
// Try to revert merge conflict markers.
85-
// TODO IGitAPI offers a reset(hard) method yet GitClient does not. Why?
86-
checkout(scm, build, git, listener, rev);
87-
// TODO would be nicer to throw an AbortException with just the message, but this is actually worse
88-
// until git-client 1.19.7+
89-
throw x;
90-
}
91-
build.addAction(
92-
new MergeRecord(baseName,
93-
baseObjectId.getName())); // does not seem to be used, but just in case
94-
ObjectId mergeRev = git.revParse(Constants.HEAD);
95-
listener.getLogger().println("Merge succeeded, producing " + mergeRev.name());
96-
return new Revision(mergeRev,
97-
rev.getBranches()); // note that this ensures Build.revision != Build.marked
21+
super(baseName, baseHash);
9822
}
9923

100-
private void checkout(GitSCM scm, Run<?, ?> build, GitClient git, TaskListener listener,
101-
Revision rev)
102-
throws InterruptedException, IOException, GitException {
103-
CheckoutCommand checkoutCommand = git.checkout().ref(rev.getSha1String());
104-
for (GitSCMExtension ext : scm.getExtensions()) {
105-
ext.decorateCheckoutCommand(scm, build, git, listener, checkoutCommand);
106-
}
107-
checkoutCommand.execute();
24+
private Object readResolve() throws ObjectStreamException {
25+
return new jenkins.plugins.git.MergeWithGitSCMExtension(getBaseName(), getBaseHash());
10826
}
10927

11028
}

0 commit comments

Comments
 (0)