Skip to content

Commit 7ddd131

Browse files
committed
Set sequential ids for SCMSources in MultiBranchProject
1 parent 598ecbb commit 7ddd131

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/main/java/jenkins/branch/MultiBranchProject.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,18 @@ public void onLoad(ItemGroup<? extends Item> parent, String name) throws IOExcep
205205
}
206206
// optimize lookup of sources by building a temporary map that is equivalent to getSCMSource(id) in results
207207
Map<String, SCMSource> sourceMap = new HashMap<>();
208+
int count = 1;
208209
for (BranchSource source : sources) {
209210
SCMSource s = source.getSource();
210211
String id = s.getId();
212+
if (id.isBlank()) {
213+
// Generate ids of the form "1", "2", … on demand
214+
while (sourceMap.containsKey(Integer.toString(count))) {
215+
count++;
216+
}
217+
id = Integer.toString(count);
218+
s.setId(id);
219+
}
211220
if (!sourceMap.containsKey(id)) { // only the first match should win
212221
sourceMap.put(id, s);
213222
}
@@ -477,8 +486,18 @@ public void setSourcesList(List<BranchSource> sources) throws IOException {
477486

478487
private Set<String> sourceIds(List<BranchSource> sources) {
479488
Set<String> result = new HashSet<>();
489+
int count = 1;
480490
for (BranchSource s : sources) {
481-
result.add(s.getSource().getId());
491+
var id = s.getSource().getId();
492+
if (id.isBlank()) {
493+
// Generate ids of the form "1", "2", … on demand
494+
while (result.contains(Integer.toString(count))) {
495+
count++;
496+
}
497+
id = Integer.toString(count);
498+
s.getSource().setId(id);
499+
}
500+
result.add(id);
482501
}
483502
return result;
484503
}

0 commit comments

Comments
 (0)