Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,16 @@
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-${jenkins.baseline}.x</artifactId>
<version>5043.v855ff4819a_0f</version>
<version>5659.vecf9e2dc5a_ed</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- TODO until in BOM -->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
<version>721.v54b_c43b_da_1db_</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
Expand Down
51 changes: 42 additions & 9 deletions src/main/java/jenkins/branch/MultiBranchProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,7 @@
state.reset();
}
// optimize lookup of sources by building a temporary map that is equivalent to getSCMSource(id) in results
Map<String, SCMSource> sourceMap = new HashMap<>();
for (BranchSource source : sources) {
SCMSource s = source.getSource();
String id = s.getId();
if (!sourceMap.containsKey(id)) { // only the first match should win
sourceMap.put(id, s);
}
}
Map<String, SCMSource> sourceMap = buildSourceMap();
for (P item : getItems(factory::isProject)) {
Branch oldBranch = factory.getBranch(item);
SCMSource source = sourceMap.get(oldBranch.getSourceId());
Expand Down Expand Up @@ -250,6 +243,28 @@
}
}

private Map<String, SCMSource> buildSourceMap() {
Map<String, SCMSource> sourceMap = new HashMap<>();
int count = 1;
for (BranchSource source : sources) {
SCMSource s = source.getSource();
String id = s.getId();
if (id.isBlank()) {
// Generate ids of the form "1", "2", … on demand
while (sourceMap.containsKey(Integer.toString(count))) {
count++;
}
id = Integer.toString(count);
s.setId(id);
LOGGER.fine(() -> "assigned id to " + s + " in " + this);
}
if (!sourceMap.containsKey(id)) { // only the first match should win

Check warning on line 261 in src/main/java/jenkins/branch/MultiBranchProject.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 261 is only partially covered, one branch is missing
sourceMap.put(id, s);
}
}
return sourceMap;
}

/**
* Consolidated initialization code.
*/
Expand Down Expand Up @@ -426,6 +441,7 @@
if (this.sources.isEmpty() || sources.isEmpty()) {
// easy
this.sources.replaceBy(sources);
buildSourceMap();
return;
}
Set<String> oldIds = sourceIds(this.sources);
Expand Down Expand Up @@ -477,12 +493,29 @@

private Set<String> sourceIds(List<BranchSource> sources) {
Set<String> result = new HashSet<>();
int count = 1;
for (BranchSource s : sources) {
result.add(s.getSource().getId());
var id = s.getSource().getId();
if (id.isBlank()) {
// Generate ids of the form "1", "2", … on demand
while (result.contains(Integer.toString(count))) {
count++;
}
id = Integer.toString(count);
s.getSource().setId(id);
LOGGER.fine(() -> "assigned id to " + s.getSource() + " in " + this);
}
result.add(id);
}
return result;
}

@Override
public synchronized void save() throws IOException {
buildSourceMap();
super.save();
}

private boolean equalButForId(SCMSource a, SCMSource b) {
if (!a.getClass().equals(b.getClass())) {
return false;
Expand Down
10 changes: 9 additions & 1 deletion src/test/java/integration/BrandingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,14 @@ protected List<Action> retrieveActions(@NonNull SCMRevision revision, SCMHeadEve
}

@Extension()
public static class DescriptorImpl extends MockSCMSource.DescriptorImpl {}
public static class DescriptorImpl extends MockSCMSource.DescriptorImpl {

@NonNull
@Override
public String getDisplayName() {
return "Branded Mock SCM";
}

}
}
}
81 changes: 81 additions & 0 deletions src/test/java/jenkins/branch/MultiBranchProjectSourcesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* The MIT License
*
* Copyright 2025 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package jenkins.branch;

import integration.harness.BasicMultiBranchProject;
import java.util.List;
import java.util.logging.Level;
import jenkins.scm.impl.mock.MockSCMSource;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.LoggerRule;
import org.jvnet.hudson.test.recipes.LocalData;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;

public final class MultiBranchProjectSourcesTest {

@Rule public final JenkinsRule r = new JenkinsRule();
@Rule public final LoggerRule logging = new LoggerRule().record(MultiBranchProject.class, Level.FINE);

@LocalData
@Test public void oldMBPWithIds() throws Exception {
assertThat(idsOf(r.jenkins.getItemByFullName("mbp", BasicMultiBranchProject.class)), contains("30d15538-4f60-437f-81cd-9c46ee6e7ec6", "01d26901-3b39-45ac-915e-e65d806fbcba"));
}

@LocalData
@Test public void oldMBPWithoutIds() throws Exception {
assertThat(idsOf(r.jenkins.getItemByFullName("mbp", BasicMultiBranchProject.class)), contains("1", "2"));
}

@Test public void setSourcesList() throws Exception {
var mbp = r.createProject(BasicMultiBranchProject.class, "mbp");
var bs1 = new BranchSource(new MockSCMSource("c", "r1"));
var bs2 = new BranchSource(new MockSCMSource("c", "r2"));
mbp.setSourcesList(List.of(bs1, bs2));
assertThat(idsOf(mbp), contains("1", "2"));
mbp.setSourcesList(List.of(bs2));
assertThat(idsOf(mbp), contains("2"));
var bs3 = new BranchSource(new MockSCMSource("c", "r3"));
mbp.setSourcesList(List.of(bs1, bs2, bs3));
assertThat(idsOf(mbp), contains("1", "2", "3"));
}

@Test public void getSourcesListAdd() throws Exception {
var mbp = r.createProject(BasicMultiBranchProject.class, "mbp");
var bs1 = new BranchSource(new MockSCMSource("c", "r1"));
var bs2 = new BranchSource(new MockSCMSource("c", "r2"));
mbp.getSourcesList().add(bs1);
assertThat(idsOf(mbp), contains("1"));
mbp.getSourcesList().add(bs2);
assertThat(idsOf(mbp), contains("1", "2"));
}

private static List<String> idsOf(MultiBranchProject<?, ?> mbp) {
return mbp.getSources().stream().map(bs -> bs.getSource().getId()).toList();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version='1.1' encoding='UTF-8'?>
<integration.harness.BasicMultiBranchProject>
<actions/>
<description></description>
<properties/>
<folderViews class="jenkins.branch.MultiBranchProjectViewHolder">
<owner class="integration.harness.BasicMultiBranchProject" reference="../.."/>
</folderViews>
<healthMetrics/>
<icon class="jenkins.branch.MetadataActionFolderIcon">
<owner class="integration.harness.BasicMultiBranchProject" reference="../.."/>
</icon>
<orphanedItemStrategy class="com.cloudbees.hudson.plugins.folder.computed.DefaultOrphanedItemStrategy" plugin="cloudbees-folder@6.1053.vd62fb_b_f7367b_">
<pruneDeadBranches>true</pruneDeadBranches>
<daysToKeep>-1</daysToKeep>
<numToKeep>-1</numToKeep>
<abortBuilds>false</abortBuilds>
</orphanedItemStrategy>
<triggers/>
<disabled>false</disabled>
<sources class="jenkins.branch.MultiBranchProject$BranchSourceList">
<data>
<jenkins.branch.BranchSource>
<source class="jenkins.scm.impl.mock.MockSCMSource">
<id>30d15538-4f60-437f-81cd-9c46ee6e7ec6</id>
<controllerId>c6fb4b23-2907-4f7c-9337-e07d687ecccb</controllerId>
<repository>xxx</repository>
<traits>
<jenkins.scm.impl.mock.MockSCMDiscoverBranches/>
</traits>
</source>
<strategy class="jenkins.branch.DefaultBranchPropertyStrategy">
<properties class="empty-list"/>
</strategy>
</jenkins.branch.BranchSource>
<jenkins.branch.BranchSource>
<source class="jenkins.scm.impl.mock.MockSCMSource">
<id>01d26901-3b39-45ac-915e-e65d806fbcba</id>
<controllerId>c6fb4b23-2907-4f7c-9337-e07d687ecccb</controllerId>
<repository>yyy</repository>
<traits>
<jenkins.scm.impl.mock.MockSCMDiscoverBranches/>
</traits>
</source>
<strategy class="jenkins.branch.DefaultBranchPropertyStrategy">
<properties class="empty-list"/>
</strategy>
</jenkins.branch.BranchSource>
</data>
<owner class="integration.harness.BasicMultiBranchProject" reference="../.."/>
</sources>
<factory class="integration.harness.BasicBranchProjectFactory">
<owner class="integration.harness.BasicMultiBranchProject" reference="../.."/>
</factory>
</integration.harness.BasicMultiBranchProject>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version='1.1' encoding='UTF-8'?>
<integration.harness.BasicMultiBranchProject>
<actions/>
<description></description>
<properties/>
<folderViews class="jenkins.branch.MultiBranchProjectViewHolder">
<owner class="integration.harness.BasicMultiBranchProject" reference="../.."/>
</folderViews>
<healthMetrics/>
<icon class="jenkins.branch.MetadataActionFolderIcon">
<owner class="integration.harness.BasicMultiBranchProject" reference="../.."/>
</icon>
<orphanedItemStrategy class="com.cloudbees.hudson.plugins.folder.computed.DefaultOrphanedItemStrategy" plugin="cloudbees-folder@6.1053.vd62fb_b_f7367b_">
<pruneDeadBranches>true</pruneDeadBranches>
<daysToKeep>-1</daysToKeep>
<numToKeep>-1</numToKeep>
<abortBuilds>false</abortBuilds>
</orphanedItemStrategy>
<triggers/>
<disabled>false</disabled>
<sources class="jenkins.branch.MultiBranchProject$BranchSourceList">
<data>
<jenkins.branch.BranchSource>
<source class="jenkins.scm.impl.mock.MockSCMSource">
<controllerId>c6fb4b23-2907-4f7c-9337-e07d687ecccb</controllerId>
<repository>xxx</repository>
<traits>
<jenkins.scm.impl.mock.MockSCMDiscoverBranches/>
</traits>
</source>
<strategy class="jenkins.branch.DefaultBranchPropertyStrategy">
<properties class="empty-list"/>
</strategy>
</jenkins.branch.BranchSource>
<jenkins.branch.BranchSource>
<source class="jenkins.scm.impl.mock.MockSCMSource">
<controllerId>c6fb4b23-2907-4f7c-9337-e07d687ecccb</controllerId>
<repository>yyy</repository>
<traits>
<jenkins.scm.impl.mock.MockSCMDiscoverBranches/>
</traits>
</source>
<strategy class="jenkins.branch.DefaultBranchPropertyStrategy">
<properties class="empty-list"/>
</strategy>
</jenkins.branch.BranchSource>
</data>
<owner class="integration.harness.BasicMultiBranchProject" reference="../.."/>
</sources>
<factory class="integration.harness.BasicBranchProjectFactory">
<owner class="integration.harness.BasicMultiBranchProject" reference="../.."/>
</factory>
</integration.harness.BasicMultiBranchProject>
Loading