From 0842bf6e3a19e451d3930c405ebbd67122559cf8 Mon Sep 17 00:00:00 2001 From: Aaron Echavarria Date: Sun, 15 Sep 2024 23:09:23 -0600 Subject: [PATCH] resolver codenarc issues --- .../jte/job/TemplateFlowDefinition.groovy | 30 +++++++---------- .../jte/util/FileSystemCacheKey.groovy | 33 ++++++++++++++----- .../jte/util/FileSystemWrapperFactory.groovy | 24 ++++++++++---- 3 files changed, 53 insertions(+), 34 deletions(-) diff --git a/src/main/groovy/org/boozallen/plugins/jte/job/TemplateFlowDefinition.groovy b/src/main/groovy/org/boozallen/plugins/jte/job/TemplateFlowDefinition.groovy index 1cc9115c..95b32918 100644 --- a/src/main/groovy/org/boozallen/plugins/jte/job/TemplateFlowDefinition.groovy +++ b/src/main/groovy/org/boozallen/plugins/jte/job/TemplateFlowDefinition.groovy @@ -15,31 +15,25 @@ */ package org.boozallen.plugins.jte.job -import org.boozallen.plugins.jte.util.FileSystemWrapperFactory - -import static org.jenkinsci.plugins.workflow.cps.persistence.PersistenceContext.JOB - -import org.boozallen.plugins.jte.init.primitives.TemplatePrimitiveCollector -import org.jenkinsci.plugins.pipeline.modeldefinition.causes.RestartDeclarativePipelineCause -import org.jenkinsci.plugins.workflow.cps.replay.ReplayCause -import org.boozallen.plugins.jte.init.PipelineConfigurationAggregator -import org.boozallen.plugins.jte.init.PipelineTemplateResolver -import org.boozallen.plugins.jte.init.governance.config.dsl.PipelineConfigurationObject -import org.boozallen.plugins.jte.init.primitives.TemplatePrimitiveInjector import hudson.model.Action import hudson.model.Item import hudson.model.Queue import hudson.model.TaskListener +import org.boozallen.plugins.jte.init.PipelineConfigurationAggregator +import org.boozallen.plugins.jte.init.PipelineTemplateResolver +import org.boozallen.plugins.jte.init.governance.config.dsl.PipelineConfigurationObject +import org.boozallen.plugins.jte.init.primitives.TemplatePrimitiveCollector +import org.boozallen.plugins.jte.init.primitives.TemplatePrimitiveInjector + +import org.jenkinsci.plugins.pipeline.modeldefinition.causes.RestartDeclarativePipelineCause import org.jenkinsci.plugins.workflow.cps.CpsFlowExecution +import org.jenkinsci.plugins.workflow.cps.CpsFlowFactoryAction2 import org.jenkinsci.plugins.workflow.cps.persistence.PersistIn -import org.jenkinsci.plugins.workflow.flow.FlowDefinition -import org.jenkinsci.plugins.workflow.flow.FlowExecution -import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner -import org.jenkinsci.plugins.workflow.flow.FlowDurabilityHint -import org.jenkinsci.plugins.workflow.flow.DurabilityHintProvider -import org.jenkinsci.plugins.workflow.flow.GlobalDefaultFlowDurabilityLevel +import org.jenkinsci.plugins.workflow.cps.replay.ReplayCause +import org.jenkinsci.plugins.workflow.flow.* import org.jenkinsci.plugins.workflow.job.WorkflowRun -import org.jenkinsci.plugins.workflow.cps.CpsFlowFactoryAction2 + +import static org.jenkinsci.plugins.workflow.cps.persistence.PersistenceContext.JOB /** * Defines a JTE Pipeline. diff --git a/src/main/groovy/org/boozallen/plugins/jte/util/FileSystemCacheKey.groovy b/src/main/groovy/org/boozallen/plugins/jte/util/FileSystemCacheKey.groovy index 0b9ff486..84ed8cd3 100644 --- a/src/main/groovy/org/boozallen/plugins/jte/util/FileSystemCacheKey.groovy +++ b/src/main/groovy/org/boozallen/plugins/jte/util/FileSystemCacheKey.groovy @@ -78,21 +78,36 @@ class FileSystemCacheKey { * @return {@code true} if the provided object is equal to this instance, {@code false} otherwise. */ @Override - boolean equals(o) { - if (this.is(o)) return true - if (!(o instanceof FileSystemCacheKey)) return false + boolean equals(Object o) { + if (this == o) { + return true + } + if (!(o instanceof FileSystemCacheKey)) { + return false + } FileSystemCacheKey that = (FileSystemCacheKey) o - if (owner != that.owner) return false - if (scm != that.scm) return false - if (scmHead != that.scmHead) return false - if (scmRevision != that.scmRevision) return false - if (scmSource != that.scmSource) return false + if (owner != that.owner) { + return false + } + if (scm != that.scm) { + return false + } + if (scmHead != that.scmHead) { + return false + } + if (scmRevision != that.scmRevision) { + return false + } + if (scmSource != that.scmSource) { + return false + } return true } + /** * Generates a hash code for this FileSystemCacheKey instance. * The hash code is computed based on the {@code owner}, {@code scm}, {@code scmSource}, @@ -113,4 +128,4 @@ class FileSystemCacheKey { result = 31 * result + (scmRevision != null ? scmRevision.hashCode() : 0) return result } -} +} \ No newline at end of file diff --git a/src/main/groovy/org/boozallen/plugins/jte/util/FileSystemWrapperFactory.groovy b/src/main/groovy/org/boozallen/plugins/jte/util/FileSystemWrapperFactory.groovy index 8a3abcab..16f955c8 100644 --- a/src/main/groovy/org/boozallen/plugins/jte/util/FileSystemWrapperFactory.groovy +++ b/src/main/groovy/org/boozallen/plugins/jte/util/FileSystemWrapperFactory.groovy @@ -54,7 +54,23 @@ class FileSystemWrapperFactory { *

This cache is a static attribute of the class, shared across all instances of {@code FileSystemCacheKey}, * and is initialized as an empty map, represented by {@code [:]}. It can grow dynamically as new entries are added during runtime.

*/ - private static Map cache = [:] + private final static Map cache = [:] + + /** + * Clears the cache for a specific {@link FlowExecutionOwner}. + * + *

This method iterates through the cache entries and removes any entries where the + * {@link FileSystemCacheKey#getOwner()} matches the provided {@code owner}. This is useful + * for invalidating or cleaning up cache entries associated with a particular pipeline execution.

+ * + *

It uses the {@link Map#entrySet()} combined with {@link java.util.Map.Entry#removeIf(Predicate)} + * to efficiently remove the matching entries from the cache.

+ * + * @param owner the {@link FlowExecutionOwner} whose associated cache entries should be removed. + */ + static void clearCache(FlowExecutionOwner owner) { + cache.entrySet().removeIf { entry -> entry.getKey().getOwner() == owner } + } /** * Creates a FileSystemWrapper. Can either be provided an SCM directly @@ -115,7 +131,6 @@ class FileSystemWrapperFactory { } Branch branch = property.getBranch() - SCMSource scmSource = parent.getSCMSource(branch.getSourceId()) if (!scmSource) { throw new IllegalStateException("${branch.getSourceId()} not found") @@ -166,9 +181,4 @@ class FileSystemWrapperFactory { return fsw } } - - static void clearCache(FlowExecutionOwner owner) { - cache.entrySet().removeIf { entry -> entry.getKey().getOwner() == owner } - } - }