Skip to content

Commit 159d214

Browse files
committed
We also need to update ids in case branch sources are added via getSourcesList().add(…), for example in a test
1 parent c0f8b2e commit 159d214

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

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

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -204,23 +204,7 @@ public void onLoad(ItemGroup<? extends Item> parent, String name) throws IOExcep
204204
state.reset();
205205
}
206206
// optimize lookup of sources by building a temporary map that is equivalent to getSCMSource(id) in results
207-
Map<String, SCMSource> sourceMap = new HashMap<>();
208-
int count = 1;
209-
for (BranchSource source : sources) {
210-
SCMSource s = source.getSource();
211-
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-
}
220-
if (!sourceMap.containsKey(id)) { // only the first match should win
221-
sourceMap.put(id, s);
222-
}
223-
}
207+
Map<String, SCMSource> sourceMap = buildSourceMap();
224208
for (P item : getItems(factory::isProject)) {
225209
Branch oldBranch = factory.getBranch(item);
226210
SCMSource source = sourceMap.get(oldBranch.getSourceId());
@@ -259,6 +243,27 @@ public void onLoad(ItemGroup<? extends Item> parent, String name) throws IOExcep
259243
}
260244
}
261245

246+
private Map<String, SCMSource> buildSourceMap() {
247+
Map<String, SCMSource> sourceMap = new HashMap<>();
248+
int count = 1;
249+
for (BranchSource source : sources) {
250+
SCMSource s = source.getSource();
251+
String id = s.getId();
252+
if (id.isBlank()) {
253+
// Generate ids of the form "1", "2", … on demand
254+
while (sourceMap.containsKey(Integer.toString(count))) {
255+
count++;
256+
}
257+
id = Integer.toString(count);
258+
s.setId(id);
259+
}
260+
if (!sourceMap.containsKey(id)) { // only the first match should win
261+
sourceMap.put(id, s);
262+
}
263+
}
264+
return sourceMap;
265+
}
266+
262267
/**
263268
* Consolidated initialization code.
264269
*/
@@ -435,6 +440,7 @@ public void setSourcesList(List<BranchSource> sources) throws IOException {
435440
if (this.sources.isEmpty() || sources.isEmpty()) {
436441
// easy
437442
this.sources.replaceBy(sources);
443+
buildSourceMap();
438444
return;
439445
}
440446
Set<String> oldIds = sourceIds(this.sources);
@@ -502,6 +508,12 @@ private Set<String> sourceIds(List<BranchSource> sources) {
502508
return result;
503509
}
504510

511+
@Override
512+
public synchronized void save() throws IOException {
513+
buildSourceMap();
514+
super.save();
515+
}
516+
505517
private boolean equalButForId(SCMSource a, SCMSource b) {
506518
if (!a.getClass().equals(b.getClass())) {
507519
return false;

0 commit comments

Comments
 (0)