From 801d6b3a2bd375708fb2692cc0b86f6fd0931c9b Mon Sep 17 00:00:00 2001 From: Vincent Latombe Date: Sat, 13 Nov 2021 17:59:26 +0100 Subject: [PATCH] [JENKINS-67099] Make trim labels more selective when we're operating on selected nodes (#5882) Co-authored-by: James Nord (cherry picked from commit 4d5a979995f1ae1dfd56fa0e45b07604c06bbd1e) --- core/src/main/java/jenkins/model/Jenkins.java | 32 ++++++++++++++++--- core/src/main/java/jenkins/model/Nodes.java | 11 ++++--- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/jenkins/model/Jenkins.java b/core/src/main/java/jenkins/model/Jenkins.java index 49d8416c7ee6..495d7b5629fb 100644 --- a/core/src/main/java/jenkins/model/Jenkins.java +++ b/core/src/main/java/jenkins/model/Jenkins.java @@ -2239,14 +2239,38 @@ public DescribableList, NodePropertyDescriptor> getGlobalNodePro * but we also call this periodically to self-heal any data out-of-sync issue. */ /*package*/ void trimLabels() { + trimLabels((Set) null); + } + + /** + * Reset labels and remove invalid ones for the given nodes. + * @param nodes the nodes taken as reference to update labels + */ + void trimLabels(Node... nodes) { + Set includedLabels = new HashSet<>(); + Arrays.asList(nodes).stream().filter(Objects::nonNull).forEach(n -> includedLabels.addAll(n.getAssignedLabels())); + trimLabels(includedLabels); + } + + /** + * Reset labels and remove invalid ones for the given nodes. + * @param includedLabels the labels taken as reference to update labels. If {@code null}, all labels are considered. + */ + private void trimLabels(@CheckForNull Set includedLabels) { Set