-
-
Notifications
You must be signed in to change notification settings - Fork 117
[JENKINS-60245] Potential fix for @Library annotation in load()'d scripts #87
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,8 +39,15 @@ | |
| class LibrariesAction extends InvisibleAction { | ||
|
|
||
| private final List<LibraryRecord> libraries; | ||
|
|
||
|
|
||
| private final String scope; | ||
|
|
||
| LibrariesAction(List<LibraryRecord> libraries) { | ||
| this(null, libraries); | ||
| } | ||
|
|
||
| LibrariesAction(String scope, List<LibraryRecord> libraries) { | ||
| this.scope = scope; | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New constructor & field added to |
||
| this.libraries = libraries; | ||
| } | ||
|
|
||
|
|
@@ -50,6 +57,13 @@ class LibrariesAction extends InvisibleAction { | |
| public List<LibraryRecord> getLibraries() { | ||
| return libraries; | ||
| } | ||
|
|
||
| /** | ||
| * @return An identifier of the source file that these library definitions are for | ||
| */ | ||
| public String getScope() { | ||
| return scope; | ||
| } | ||
|
|
||
| @Extension public static class LibraryEnvironment extends EnvironmentContributor { | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,7 +65,7 @@ | |
|
|
||
| private static final Logger LOGGER = Logger.getLogger(LibraryAdder.class.getName()); | ||
|
|
||
| @Override public List<Addition> add(CpsFlowExecution execution, List<String> libraries, HashMap<String, Boolean> changelogs) throws Exception { | ||
| @Override public List<Addition> add(String scope, CpsFlowExecution execution, List<String> libraries, HashMap<String, Boolean> changelogs) throws Exception { | ||
| Queue.Executable executable = execution.getOwner().getExecutable(); | ||
| Run<?,?> build; | ||
| if (executable instanceof Run) { | ||
|
|
@@ -85,7 +85,19 @@ | |
| librariesUnparsed.put(parsed[0], library); | ||
| } | ||
| List<Addition> additions = new ArrayList<>(); | ||
| LibrariesAction action = build.getAction(LibrariesAction.class); | ||
| LibrariesAction action = null; | ||
|
|
||
| for( LibrariesAction laction : build.getActions( LibrariesAction.class ) ) { | ||
| if( laction.getScope() != null && laction.getScope().equals( scope ) ) { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously, as there was only ever one Now, there may be multiple actions so we must find the one whose |
||
| // found the action that's absolutely right for this scope | ||
| action = laction; | ||
| break; | ||
| } else if( laction.getScope() == null ) { | ||
| // generic LibrariesAction may be overridden later if there's a perfect match. | ||
| action = laction; | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is possible that a build was suspended before these code changes landed, in which case the "right" LibrariesAction will have a |
||
| } | ||
| } | ||
|
|
||
| if (action != null) { | ||
| // Resuming a build, so just look up what we loaded before. | ||
| for (LibraryRecord record : action.getLibraries()) { | ||
|
|
@@ -131,12 +143,14 @@ | |
| } | ||
| } | ||
| // Record libraries we plan to load. We need LibrariesAction there first so variables can be interpolated. | ||
| build.addAction(new LibrariesAction(new ArrayList<>(librariesAdded.values()))); | ||
| // Now actually try to retrieve the libraries. | ||
| for (LibraryRecord record : librariesAdded.values()) { | ||
| listener.getLogger().println("Loading library " + record.name + "@" + record.version); | ||
| for (URL u : retrieve(record.name, record.version, retrievers.get(record.name), record.trusted, record.changelog, listener, build, execution, record.variables)) { | ||
| additions.add(new Addition(u, record.trusted)); | ||
| if( librariesAdded.size() > 0 ) { | ||
| build.addAction(new LibrariesAction(new ArrayList<>(librariesAdded.values()))); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid adding a This feels more right to me - I couldn't figure any solid reason for adding a |
||
| // Now actually try to retrieve the libraries. | ||
| for (LibraryRecord record : librariesAdded.values()) { | ||
| listener.getLogger().println("Loading library " + record.name + "@" + record.version); | ||
| for (URL u : retrieve(record.name, record.version, retrievers.get(record.name), record.trusted, record.changelog, listener, build, execution, record.variables)) { | ||
| additions.add(new Addition(u, record.trusted)); | ||
| } | ||
| } | ||
| } | ||
| return additions; | ||
|
|
@@ -200,8 +214,7 @@ static List<URL> retrieve(@Nonnull String name, @Nonnull String version, @Nonnul | |
| Queue.Executable executable = execution.getOwner().getExecutable(); | ||
| if (executable instanceof Run) { | ||
| Run<?,?> run = (Run) executable; | ||
| LibrariesAction action = run.getAction(LibrariesAction.class); | ||
| if (action != null) { | ||
| for( LibrariesAction action : run.getActions(LibrariesAction.class)) { | ||
| FilePath libs = new FilePath(run.getRootDir()).child("libs"); | ||
| for (LibraryRecord library : action.getLibraries()) { | ||
| FilePath f = libs.child(library.name + "/resources/" + name); | ||
|
|
@@ -230,16 +243,17 @@ private static String readResource(FilePath file, @CheckForNull String encoding) | |
| if (run == null) { | ||
| return Collections.emptySet(); | ||
| } | ||
| LibrariesAction action = run.getAction(LibrariesAction.class); | ||
| if (action == null) { | ||
| return Collections.emptySet(); | ||
| } | ||
|
|
||
| List<GlobalVariable> vars = new ArrayList<>(); | ||
| for (LibraryRecord library : action.getLibraries()) { | ||
| for (String variable : library.variables) { | ||
| vars.add(new UserDefinedGlobalVariable(variable, new File(run.getRootDir(), "libs/" + library.name + "/vars/" + variable + ".txt"))); | ||
|
|
||
| for( LibrariesAction action : run.getActions(LibrariesAction.class)) { | ||
| for (LibraryRecord library : action.getLibraries()) { | ||
| for (String variable : library.variables) { | ||
| vars.add(new UserDefinedGlobalVariable(variable, new File(run.getRootDir(), "libs/" + library.name + "/vars/" + variable + ".txt"))); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return vars; | ||
| } | ||
|
|
||
|
|
@@ -255,8 +269,7 @@ private static String readResource(FilePath file, @CheckForNull String encoding) | |
| Queue.Executable executable = execution.getOwner().getExecutable(); | ||
| if (executable instanceof Run) { | ||
| Run<?,?> run = (Run) executable; | ||
| LibrariesAction action = run.getAction(LibrariesAction.class); | ||
| if (action != null) { | ||
| for( LibrariesAction action : run.getActions(LibrariesAction.class)) { | ||
| FilePath libs = new FilePath(run.getRootDir()).child("libs"); | ||
| for (LibraryRecord library : action.getLibraries()) { | ||
| if (library.trusted) { | ||
|
|
@@ -273,7 +286,7 @@ private static String readResource(FilePath file, @CheckForNull String encoding) | |
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } catch (IOException | InterruptedException x) { | ||
| LOGGER.log(Level.WARNING, null, x); | ||
|
|
@@ -286,8 +299,7 @@ private static String readResource(FilePath file, @CheckForNull String encoding) | |
| @Extension public static class Copier extends FlowCopier.ByRun { | ||
|
|
||
| @Override public void copy(Run<?,?> original, Run<?,?> copy, TaskListener listener) throws IOException, InterruptedException { | ||
| LibrariesAction action = original.getAction(LibrariesAction.class); | ||
| if (action != null) { | ||
| for( LibrariesAction action : original.getActions(LibrariesAction.class )) { | ||
| copy.addAction(new LibrariesAction(action.getLibraries())); | ||
| FilePath libs = new FilePath(original.getRootDir()).child("libs"); | ||
| if (libs.isDirectory()) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New
add(...)method added to theClasspathAdderinterface, to allow for scoping the source of an addition.