Skip to content

Avoid need for idealNames workaround #359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 10, 2025
Merged

Conversation

basil
Copy link
Member

@basil basil commented Apr 8, 2025

Testing with

diff --git a/src/test/java/org/jenkinsci/plugins/workflow/multibranch/WorkflowMultiBranchProjectTest.java b/src/test/java/org/jenkinsci/plugins/workflow/multibranch/WorkflowMultiBranchProjectTest.java
index 86d2af8..501a484 100644
--- a/src/test/java/org/jenkinsci/plugins/workflow/multibranch/WorkflowMultiBranchProjectTest.java
+++ b/src/test/java/org/jenkinsci/plugins/workflow/multibranch/WorkflowMultiBranchProjectTest.java
@@ -233,7 +233,7 @@ public class WorkflowMultiBranchProjectTest {
     @Test public void configuredScriptNameBranches() throws Exception {
         sampleRepo.init();
 
-        WorkflowMultiBranchProject mp = r.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
+        WorkflowMultiBranchProject mp = r.jenkins.createProject(WorkflowMultiBranchProject.class, "ElNiño");
         mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0])));
         for (SCMSource source : mp.getSCMSources()) {
             assertEquals(mp, source.getOwner());
@@ -248,30 +248,30 @@ public class WorkflowMultiBranchProjectTest {
         mp.scheduleBuild2(0).getFuture().get();
         assertNull(mp.getItem("master"));
 
-        sampleRepo.git("checkout", "-b", "feature");
+        sampleRepo.git("checkout", "-b", "feature/1");
         sampleRepo.write("another-Jenkinsfile", "echo(/branch=$BRANCH_NAME/); node {checkout scm; echo readFile('file')}");
         sampleRepo.git("add", "another-Jenkinsfile");
         sampleRepo.write("file", "subsequent content");
         sampleRepo.git("commit", "--all", "--message=feature-create");
-        WorkflowJob p1 = scheduleAndFindBranchProject(mp, "feature");
+        WorkflowJob p1 = scheduleAndFindBranchProject(mp, "feature/1");
         assertEquals(1, mp.getItems().size());
         r.waitUntilNoActivity();
         WorkflowRun b1 = p1.getLastBuild();
         assertEquals(1, b1.getNumber());
         r.assertLogContains("subsequent content", b1);
-        r.assertLogContains("branch=feature", b1);
+        r.assertLogContains("branch=feature/1", b1);
 
-        sampleRepo.git("checkout", "-b", "feature2");
+        sampleRepo.git("checkout", "-b", "feature/2");
         sampleRepo.write("another-Jenkinsfile", "echo(/branch=$BRANCH_NAME/); node {checkout scm; echo readFile('file').toUpperCase()}");
         sampleRepo.write("file", "alternative content");
         sampleRepo.git("commit", "--all", "--message=feature2-create");
-        WorkflowJob p2 = scheduleAndFindBranchProject(mp, "feature2");
+        WorkflowJob p2 = scheduleAndFindBranchProject(mp, "feature/2");
         assertEquals(2, mp.getItems().size());
         r.waitUntilNoActivity();
         WorkflowRun b2 = p2.getLastBuild();
         assertEquals(1, b2.getNumber());
         r.assertLogContains("ALTERNATIVE CONTENT", b2);
-        r.assertLogContains("branch=feature2", b2);
+        r.assertLogContains("branch=feature/2", b2);
     }
 
     @Issue("JENKINS-72613")

I tested to see if the idealNames workaround in cloudbees-folder was still necessary. I found a usage here:

idealNameFromItem:151, ChildNameGenerator (com.cloudbees.hudson.plugins.folder)
dirNameFromItem:248, MultiBranchProjectDescriptor$ChildNameGeneratorImpl (jenkins.branch)
dirNameFromItem:222, MultiBranchProjectDescriptor$ChildNameGeneratorImpl (jenkins.branch)
dirName:198, ChildNameGenerator (com.cloudbees.hudson.plugins.folder)
getRootDirFor:543, AbstractFolder (com.cloudbees.hudson.plugins.folder)
getRootDirFor:878, MultiBranchProject (jenkins.branch)
getRootDirFor:130, MultiBranchProject (jenkins.branch)
getRootDir:196, AbstractItem (hudson.model)
getConfigFile:391, Items (hudson.model)
getConfigFile:624, AbstractItem (hudson.model)
save:619, AbstractItem (hudson.model)
save:203, Job (hudson.model)
setDefinition:172, WorkflowJob (org.jenkinsci.plugins.workflow.job)
setBranch:66, AbstractWorkflowBranchProjectFactory (org.jenkinsci.plugins.workflow.multibranch)
newInstance:55, AbstractWorkflowBranchProjectFactory (org.jenkinsci.plugins.workflow.multibranch)
newInstance:45, AbstractWorkflowBranchProjectFactory (org.jenkinsci.plugins.workflow.multibranch)
observeNew:2109, MultiBranchProject$SCMHeadObserverImpl (jenkins.branch)
observe:2031, MultiBranchProject$SCMHeadObserverImpl (jenkins.branch)
process:357, SCMSourceRequest (jenkins.scm.api.trait)
discoverBranches:728, AbstractGitSCMSource$8 (jenkins.plugins.git)
run:632, AbstractGitSCMSource$8 (jenkins.plugins.git)
run:611, AbstractGitSCMSource$8 (jenkins.plugins.git)
doRetrieve:415, AbstractGitSCMSource (jenkins.plugins.git)
doRetrieve:352, AbstractGitSCMSource (jenkins.plugins.git)
retrieve:611, AbstractGitSCMSource (jenkins.plugins.git)
_retrieve:372, SCMSource (jenkins.scm.api)
fetch:282, SCMSource (jenkins.scm.api)
computeChildren:662, MultiBranchProject (jenkins.branch)
updateChildren:272, ComputedFolder (com.cloudbees.hudson.plugins.folder.computed)
run:171, FolderComputation (com.cloudbees.hudson.plugins.folder.computed)
run:1065, MultiBranchProject$BranchIndexing (jenkins.branch)
execute:101, ResourceController (hudson.model)
run:445, Executor (hudson.model)

This must have been the problem @stephenc had in mind when he wrote:

There are some items which need the Item#getRootDir() during construction (those are bold evil item types that leak side-effects, you should fix them if you find them).

I have found it, and I am fixing it. Here the problem is that WorkflowJob#setDefinition triggers a save (which needs the unmangled name) before we have added the BranchJobProperty to record the unmangled name. By wrapping the whole thing in a BulkChange we avoid the save until after the unmangled name has been recorded in BranchJobProperty, after which idealNames is no longer consulted.

Testing done

PR build, as well as local testing with the abovementioned test diff. Verified in the debugger that idealNames was consulted before this PR but not after this PR.

Submitter checklist

  • Make sure you are opening from a topic/feature/bugfix branch (right side) and not your main branch!
  • Ensure that the pull request title represents the desired changelog entry
  • Please describe what you did
  • Link to relevant issues in GitHub or Jira
  • Link to relevant pull requests, esp. upstream and downstream changes
  • Ensure you have provided tests - that demonstrates feature works or fixes the issue

@basil basil added the internal label Apr 8, 2025
@basil basil requested a review from a team as a code owner April 8, 2025 18:46
@basil basil requested a review from jglick April 8, 2025 18:47
@@ -63,14 +64,19 @@ public abstract class AbstractWorkflowBranchProjectFactory extends BranchProject

@NonNull
@Override public WorkflowJob setBranch(@NonNull WorkflowJob project, @NonNull Branch branch) {
project.setDefinition(createDefinition());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review with Hide whitespace enabled.

@basil basil requested review from alecharp and removed request for jglick April 9, 2025 19:49
basil added a commit to basil/bom that referenced this pull request Apr 10, 2025
@jglick jglick added bug and removed internal labels Apr 10, 2025
@jglick
Copy link
Member

jglick commented Apr 10, 2025

WorkflowMultiBranchProject mp = r.jenkins.createProject(WorkflowMultiBranchProject.class, "ElNiño");

Just to be on the safe side I guess? Since my understanding is that the affected code deals with the relative names of Jobs with the ComputedFolder, not about the name of the ComputedFolder itself.

Copy link
Member

@jglick jglick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be great to get rid of this system if we can.

@basil basil merged commit bb688f6 into jenkinsci:master Apr 10, 2025
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants