Skip to content

Commit af8414f

Browse files
committed
Fixing LoadedLibraries, though not yet actual replay
1 parent 29a8848 commit af8414f

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

src/main/java/org/jenkinsci/plugins/workflow/libs/LibraryAdder.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import edu.umd.cs.findbugs.annotations.CheckForNull;
5151
import edu.umd.cs.findbugs.annotations.NonNull;
5252
import java.net.URI;
53+
import java.nio.charset.StandardCharsets;
5354
import java.util.jar.JarEntry;
5455
import java.util.jar.JarFile;
5556
import java.util.regex.Matcher;
@@ -358,20 +359,24 @@ static URL retrieve(@NonNull LibraryRecord record, @NonNull LibraryRetriever ret
358359
Run<?,?> run = (Run) executable;
359360
LibrariesAction action = run.getAction(LibrariesAction.class);
360361
if (action != null) {
361-
// TODO handle *.jar
362362
FilePath libs = new FilePath(run.getRootDir()).child("libs");
363363
for (LibraryRecord library : action.getLibraries()) {
364364
if (library.trusted) {
365365
continue; // TODO JENKINS-41157 allow replay of trusted libraries if you have ADMINISTER
366366
}
367-
for (String rootName : new String[] {"src", "vars"}) {
368-
FilePath root = libs.child(library.getDirectoryName() + "/" + rootName);
369-
if (!root.isDirectory()) {
370-
continue;
371-
}
372-
for (FilePath groovy : root.list("**/*.groovy")) {
373-
String clazz = className(groovy.getRemote(), root.getRemote());
374-
scripts.put(clazz, groovy.readToString()); // TODO no idea what encoding the Groovy compiler uses
367+
FilePath jar = libs.child(library.getDirectoryName() + ".jar");
368+
if (!jar.exists()) {
369+
continue;
370+
}
371+
try (JarFile jf = new JarFile(jar.getRemote())) {
372+
for (JarEntry je : (Iterable<JarEntry>) jf.stream()::iterator) {
373+
if (je.getName().endsWith(".groovy")) {
374+
String text;
375+
try (InputStream is = jf.getInputStream(je)) {
376+
text = IOUtils.toString(is, StandardCharsets.UTF_8); // TODO no idea what encoding the Groovy compiler uses
377+
}
378+
scripts.put(je.getName().replaceFirst("[.]groovy$", "").replace('/', '.'), text);
379+
}
375380
}
376381
}
377382
}
@@ -383,10 +388,6 @@ static URL retrieve(@NonNull LibraryRecord record, @NonNull LibraryRetriever ret
383388
return scripts;
384389
}
385390

386-
static String className(String groovy, String root) {
387-
return groovy.replaceFirst("^" + Pattern.quote(root) + "[/\\\\](.+)[.]groovy", "$1").replace('/', '.').replace('\\', '.');
388-
}
389-
390391
}
391392

392393
@Extension public static class Copier extends FlowCopier.ByRun {

src/test/java/org/jenkinsci/plugins/workflow/libs/LibraryAdderTest.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import jenkins.scm.impl.subversion.SubversionSCMSource;
4747
import jenkins.scm.impl.subversion.SubversionSampleRepoRule;
4848
import static org.hamcrest.MatcherAssert.assertThat;
49-
import static org.hamcrest.Matchers.is;
5049
import static org.hamcrest.Matchers.nullValue;
5150
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
5251
import org.jenkinsci.plugins.workflow.cps.GlobalVariable;
@@ -64,7 +63,6 @@
6463
import org.jvnet.hudson.test.Issue;
6564
import org.jvnet.hudson.test.JenkinsRule;
6665
import org.jvnet.hudson.test.TestExtension;
67-
import org.jvnet.hudson.test.WithoutJenkins;
6866
import org.jvnet.hudson.test.recipes.LocalData;
6967

7068
public class LibraryAdderTest {
@@ -546,12 +544,4 @@ public void parallelBuildsDontInterfereWithExpiredCache() throws Throwable {
546544

547545
// TODO test migration in LibraryAdder.add after reload build started prior to dir2Jar (@OldData)
548546

549-
@Issue("JENKINS-68544")
550-
@WithoutJenkins
551-
@Test public void className() {
552-
assertThat(LibraryAdder.LoadedLibraries.className("/path/to/lib/src/some/pkg/Type.groovy", "/path/to/lib/src"), is("some.pkg.Type"));
553-
assertThat(LibraryAdder.LoadedLibraries.className("C:\\path\\to\\lib\\src\\some\\pkg\\Type.groovy", "C:\\path\\to\\lib\\src"), is("some.pkg.Type"));
554-
assertThat(LibraryAdder.LoadedLibraries.className("C:\\path\\to\\Extra\\lib\\src\\some\\pkg\\Type.groovy", "C:\\path\\to\\Extra\\lib\\src"), is("some.pkg.Type"));
555-
}
556-
557547
}

0 commit comments

Comments
 (0)