Skip to content

Commit 54bc43b

Browse files
authored
Merge pull request #357 from jglick/SCMSource.id
Stop assigning random `SCMSource.id`s
2 parents 45496a4 + 0ea1d82 commit 54bc43b

File tree

3 files changed

+10
-17
lines changed

3 files changed

+10
-17
lines changed

src/main/java/jenkins/scm/api/SCMSource.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import java.util.List;
4848
import java.util.Map;
4949
import java.util.Set;
50-
import java.util.UUID;
5150
import java.util.logging.Level;
5251
import java.util.logging.Logger;
5352
import jenkins.model.TransientActionFactory;
@@ -136,7 +135,7 @@ public boolean equals(Object obj) {
136135
*/
137136
@Deprecated
138137
protected SCMSource(@CheckForNull String id) {
139-
this.id = id == null ? UUID.randomUUID().toString() : id;
138+
this.id = id == null ? "" : id;
140139
}
141140

142141
/**
@@ -148,18 +147,16 @@ protected SCMSource() {
148147
/**
149148
* Sets the ID that is used to ensure this {@link SCMSource} can be retrieved from its {@link SCMSourceOwner}
150149
* provided that this {@link SCMSource} does not already {@link #hasId()}.
151-
* <p>
152-
* Note this is a <strong>one-shot</strong> setter. If {@link #getId()} is called first, then its value will stick,
153-
* otherwise the first call to {@link #setId(String)} will stick.
154-
*
150+
* This should be called from the {@link SCMSourceOwner} when saving the list of {@link SCMSource}s,
151+
* though for compatibility reasons it may also be called directly.
155152
* @param id the ID, this is an opaque token expected to be unique within any one {@link SCMSourceOwner}.
156153
* @see #hasId()
157154
* @see #getId()
158155
* @since 2.2.0
159156
*/
160157
@DataBoundSetter
161158
public final synchronized void setId(@CheckForNull String id) {
162-
if (this.id == null) {
159+
if (this.id == null || this.id.isEmpty()) {
163160
this.id = id;
164161
} else if (!this.id.equals(id)) {
165162
throw new IllegalStateException("The ID cannot be changed after it has been set.");
@@ -186,20 +183,19 @@ public final SCMSource withId(@CheckForNull String id) {
186183
* @since 2.2.0
187184
*/
188185
public final synchronized boolean hasId() {
189-
return this.id != null;
186+
return this.id != null && !this.id.isEmpty();
190187
}
191188
/**
192-
* The ID of this source. The ID is not related to anything at all. If this {@link SCMSource} does not have an ID
193-
* then one will be generated.
189+
* The ID of this source.
194190
*
195-
* @return the ID of this source.
191+
* @return the ID of this source; the empty string until assigned
196192
* @see #setId(String)
197193
* @see #hasId()
198194
*/
199195
@NonNull
200196
public final synchronized String getId() {
201197
if (id == null) {
202-
id = UUID.randomUUID().toString();
198+
id = "";
203199
}
204200
return id;
205201
}

src/main/java/jenkins/scm/api/SCMSourceDescriptor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939

4040
import java.util.ArrayList;
4141
import java.util.List;
42-
import java.util.UUID;
4342

4443
/**
4544
* A {@link Descriptor} for {@link SCMSource}s.
@@ -59,12 +58,12 @@ public abstract class SCMSourceDescriptor extends Descriptor<SCMSource> implemen
5958
* Return or generate the ID for a source instance.
6059
*
6160
* @param source the source or {@code null} if a new source.
62-
* @return the ID of the supplied source or a newly generated ID to use for a new source instance.
61+
* @return the ID of the supplied source or the empty string
6362
*/
6463
@NonNull
6564
@SuppressWarnings("unused") // use by stapler as well as elsewhere
6665
public String getId(@CheckForNull SCMSource source) {
67-
return source == null ? UUID.randomUUID().toString() : source.getId();
66+
return source == null ? "" : source.getId();
6867
}
6968

7069
/**

src/test/java/jenkins/scm/impl/SymbolAnnotationsTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public class SymbolAnnotationsTest {
9494
));
9595
assertThat(DescribableModel.uninstantiate2_(instance).toString(), is("@mockScm("
9696
+ "controllerId=" + c.getId() + ","
97-
+ "id=" + instance.getId() + ","
9897
+ "repository=test,"
9998
+ "traits=["
10099
+ "@discoverBranches$MockSCMDiscoverBranches(), "
@@ -158,7 +157,6 @@ public class SymbolAnnotationsTest {
158157
assertThat(DescribableModel.uninstantiate2_(instance).toString(), is("@fromSource(name=foo,"
159158
+ "sources=[@mockScm$MockSCMSource("
160159
+ "controllerId=" + c.getId() + ","
161-
+ "id=" + instance.getSources().get(0).getId() + ","
162160
+ "repository=test,"
163161
+ "traits=[]"
164162
+ ")"

0 commit comments

Comments
 (0)