diff --git a/octopus-server/src/main/java/octopus/teamcity/server/OctopusBuildInformationRunType.java b/octopus-server/src/main/java/octopus/teamcity/server/OctopusBuildInformationRunType.java index 9cc54b05..3998b9be 100644 --- a/octopus-server/src/main/java/octopus/teamcity/server/OctopusBuildInformationRunType.java +++ b/octopus-server/src/main/java/octopus/teamcity/server/OctopusBuildInformationRunType.java @@ -16,6 +16,9 @@ package octopus.teamcity.server; +import static octopus.teamcity.server.PropertiesValidator.checkCredentialsUnlessUsingConnection; +import static octopus.teamcity.server.PropertiesValidator.checkNotEmpty; + import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -25,7 +28,6 @@ import jetbrains.buildServer.serverSide.PropertiesProcessor; import jetbrains.buildServer.serverSide.RunType; import jetbrains.buildServer.serverSide.RunTypeRegistry; -import jetbrains.buildServer.util.StringUtil; import jetbrains.buildServer.web.openapi.PluginDescriptor; import octopus.teamcity.common.OctopusConstants; import octopus.teamcity.server.connection.ConnectionInlineFieldCleaner; @@ -62,27 +64,13 @@ public String getDescription() { public PropertiesProcessor getRunnerPropertiesProcessor() { final OctopusConstants c = new OctopusConstants(); return new PropertiesProcessor() { - private void checkNotEmpty( - @NotNull final Map properties, - @NotNull final String key, - @NotNull final String message, - @NotNull final Collection res) { - if (jetbrains.buildServer.util.StringUtil.isEmptyOrSpaces(properties.get(key))) { - res.add(new InvalidProperty(key, message)); - } - } - @Override @NotNull public Collection process(@Nullable final Map p) { final Collection result = new ArrayList(); if (p == null) return result; - final boolean usingConnection = !StringUtil.isEmptyOrSpaces(p.get(c.getConnectionIdKey())); - if (!usingConnection) { - checkNotEmpty(p, c.getServerKey(), "Server must be specified", result); - checkNotEmpty(p, c.getApiKey(), "API key must be specified", result); - } + checkCredentialsUnlessUsingConnection(p, c, result); checkNotEmpty(p, c.getPackageIdKey(), "Package ID must be specified", result); checkNotEmpty(p, c.getPackageVersionKey(), "Package version be specified", result); diff --git a/octopus-server/src/main/java/octopus/teamcity/server/OctopusCreateReleaseRunType.java b/octopus-server/src/main/java/octopus/teamcity/server/OctopusCreateReleaseRunType.java index acd959ec..3158931a 100644 --- a/octopus-server/src/main/java/octopus/teamcity/server/OctopusCreateReleaseRunType.java +++ b/octopus-server/src/main/java/octopus/teamcity/server/OctopusCreateReleaseRunType.java @@ -1,5 +1,8 @@ package octopus.teamcity.server; +import static octopus.teamcity.server.PropertiesValidator.checkCredentialsUnlessUsingConnection; +import static octopus.teamcity.server.PropertiesValidator.checkNotEmpty; + import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -9,7 +12,6 @@ import jetbrains.buildServer.serverSide.PropertiesProcessor; import jetbrains.buildServer.serverSide.RunType; import jetbrains.buildServer.serverSide.RunTypeRegistry; -import jetbrains.buildServer.util.StringUtil; import jetbrains.buildServer.web.openapi.PluginDescriptor; import octopus.teamcity.common.OctopusConstants; import octopus.teamcity.server.connection.ConnectionInlineFieldCleaner; @@ -46,27 +48,13 @@ public String getDescription() { public PropertiesProcessor getRunnerPropertiesProcessor() { final OctopusConstants c = new OctopusConstants(); return new PropertiesProcessor() { - private void checkNotEmpty( - @NotNull final Map properties, - @NotNull final String key, - @NotNull final String message, - @NotNull final Collection res) { - if (jetbrains.buildServer.util.StringUtil.isEmptyOrSpaces(properties.get(key))) { - res.add(new InvalidProperty(key, message)); - } - } - @Override @NotNull public Collection process(@Nullable final Map p) { final Collection result = new ArrayList(); if (p == null) return result; - final boolean usingConnection = !StringUtil.isEmptyOrSpaces(p.get(c.getConnectionIdKey())); - if (!usingConnection) { - checkNotEmpty(p, c.getApiKey(), "API key must be specified", result); - checkNotEmpty(p, c.getServerKey(), "Server must be specified", result); - } + checkCredentialsUnlessUsingConnection(p, c, result); checkNotEmpty(p, c.getProjectNameKey(), "Project name must be specified", result); if (result.isEmpty()) { diff --git a/octopus-server/src/main/java/octopus/teamcity/server/OctopusDeployReleaseRunType.java b/octopus-server/src/main/java/octopus/teamcity/server/OctopusDeployReleaseRunType.java index cf24f63e..f2524e87 100644 --- a/octopus-server/src/main/java/octopus/teamcity/server/OctopusDeployReleaseRunType.java +++ b/octopus-server/src/main/java/octopus/teamcity/server/OctopusDeployReleaseRunType.java @@ -16,6 +16,9 @@ package octopus.teamcity.server; +import static octopus.teamcity.server.PropertiesValidator.checkCredentialsUnlessUsingConnection; +import static octopus.teamcity.server.PropertiesValidator.checkNotEmpty; + import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -25,7 +28,6 @@ import jetbrains.buildServer.serverSide.PropertiesProcessor; import jetbrains.buildServer.serverSide.RunType; import jetbrains.buildServer.serverSide.RunTypeRegistry; -import jetbrains.buildServer.util.StringUtil; import jetbrains.buildServer.web.openapi.PluginDescriptor; import octopus.teamcity.common.OctopusConstants; import octopus.teamcity.server.connection.ConnectionInlineFieldCleaner; @@ -62,27 +64,13 @@ public String getDescription() { public PropertiesProcessor getRunnerPropertiesProcessor() { final OctopusConstants c = new OctopusConstants(); return new PropertiesProcessor() { - private void checkNotEmpty( - @NotNull final Map properties, - @NotNull final String key, - @NotNull final String message, - @NotNull final Collection res) { - if (jetbrains.buildServer.util.StringUtil.isEmptyOrSpaces(properties.get(key))) { - res.add(new InvalidProperty(key, message)); - } - } - @Override @NotNull public Collection process(@Nullable final Map p) { final Collection result = new ArrayList(); if (p == null) return result; - final boolean usingConnection = !StringUtil.isEmptyOrSpaces(p.get(c.getConnectionIdKey())); - if (!usingConnection) { - checkNotEmpty(p, c.getApiKey(), "API key must be specified", result); - checkNotEmpty(p, c.getServerKey(), "Server must be specified", result); - } + checkCredentialsUnlessUsingConnection(p, c, result); checkNotEmpty(p, c.getProjectNameKey(), "Project name must be specified", result); checkNotEmpty(p, c.getReleaseNumberKey(), "Release number must be specified", result); checkNotEmpty(p, c.getDeployToKey(), "Deploy to must be specified", result); diff --git a/octopus-server/src/main/java/octopus/teamcity/server/OctopusPackPackageRunType.java b/octopus-server/src/main/java/octopus/teamcity/server/OctopusPackPackageRunType.java index 610dc292..6e0e65a9 100644 --- a/octopus-server/src/main/java/octopus/teamcity/server/OctopusPackPackageRunType.java +++ b/octopus-server/src/main/java/octopus/teamcity/server/OctopusPackPackageRunType.java @@ -16,6 +16,8 @@ package octopus.teamcity.server; +import static octopus.teamcity.server.PropertiesValidator.checkNotEmpty; + import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -60,16 +62,6 @@ public String getDescription() { public PropertiesProcessor getRunnerPropertiesProcessor() { final OctopusConstants c = new OctopusConstants(); return new PropertiesProcessor() { - private void checkNotEmpty( - @NotNull final Map properties, - @NotNull final String key, - @NotNull final String message, - @NotNull final Collection res) { - if (jetbrains.buildServer.util.StringUtil.isEmptyOrSpaces(properties.get(key))) { - res.add(new InvalidProperty(key, message)); - } - } - @Override @NotNull public Collection process(@Nullable final Map p) { diff --git a/octopus-server/src/main/java/octopus/teamcity/server/OctopusPromoteReleaseRunType.java b/octopus-server/src/main/java/octopus/teamcity/server/OctopusPromoteReleaseRunType.java index 70bbae5c..11725211 100644 --- a/octopus-server/src/main/java/octopus/teamcity/server/OctopusPromoteReleaseRunType.java +++ b/octopus-server/src/main/java/octopus/teamcity/server/OctopusPromoteReleaseRunType.java @@ -16,6 +16,9 @@ package octopus.teamcity.server; +import static octopus.teamcity.server.PropertiesValidator.checkCredentialsUnlessUsingConnection; +import static octopus.teamcity.server.PropertiesValidator.checkNotEmpty; + import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -25,7 +28,6 @@ import jetbrains.buildServer.serverSide.PropertiesProcessor; import jetbrains.buildServer.serverSide.RunType; import jetbrains.buildServer.serverSide.RunTypeRegistry; -import jetbrains.buildServer.util.StringUtil; import jetbrains.buildServer.web.openapi.PluginDescriptor; import octopus.teamcity.common.OctopusConstants; import octopus.teamcity.server.connection.ConnectionInlineFieldCleaner; @@ -62,27 +64,13 @@ public String getDescription() { public PropertiesProcessor getRunnerPropertiesProcessor() { final OctopusConstants c = new OctopusConstants(); return new PropertiesProcessor() { - private void checkNotEmpty( - @NotNull final Map properties, - @NotNull final String key, - @NotNull final String message, - @NotNull final Collection res) { - if (jetbrains.buildServer.util.StringUtil.isEmptyOrSpaces(properties.get(key))) { - res.add(new InvalidProperty(key, message)); - } - } - @Override @NotNull public Collection process(@Nullable final Map p) { final Collection result = new ArrayList(); if (p == null) return result; - final boolean usingConnection = !StringUtil.isEmptyOrSpaces(p.get(c.getConnectionIdKey())); - if (!usingConnection) { - checkNotEmpty(p, c.getApiKey(), "API key must be specified", result); - checkNotEmpty(p, c.getServerKey(), "Server must be specified", result); - } + checkCredentialsUnlessUsingConnection(p, c, result); checkNotEmpty(p, c.getProjectNameKey(), "Project name must be specified", result); checkNotEmpty( p, c.getPromoteFromKey(), "Environment to promote from must be specified", result); diff --git a/octopus-server/src/main/java/octopus/teamcity/server/OctopusPushPackageRunType.java b/octopus-server/src/main/java/octopus/teamcity/server/OctopusPushPackageRunType.java index cd2d35e3..798c69e4 100644 --- a/octopus-server/src/main/java/octopus/teamcity/server/OctopusPushPackageRunType.java +++ b/octopus-server/src/main/java/octopus/teamcity/server/OctopusPushPackageRunType.java @@ -16,6 +16,9 @@ package octopus.teamcity.server; +import static octopus.teamcity.server.PropertiesValidator.checkCredentialsUnlessUsingConnection; +import static octopus.teamcity.server.PropertiesValidator.checkNotEmpty; + import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -25,7 +28,6 @@ import jetbrains.buildServer.serverSide.PropertiesProcessor; import jetbrains.buildServer.serverSide.RunType; import jetbrains.buildServer.serverSide.RunTypeRegistry; -import jetbrains.buildServer.util.StringUtil; import jetbrains.buildServer.web.openapi.PluginDescriptor; import octopus.teamcity.common.OctopusConstants; import octopus.teamcity.server.connection.ConnectionInlineFieldCleaner; @@ -62,27 +64,13 @@ public String getDescription() { public PropertiesProcessor getRunnerPropertiesProcessor() { final OctopusConstants c = new OctopusConstants(); return new PropertiesProcessor() { - private void checkNotEmpty( - @NotNull final Map properties, - @NotNull final String key, - @NotNull final String message, - @NotNull final Collection res) { - if (jetbrains.buildServer.util.StringUtil.isEmptyOrSpaces(properties.get(key))) { - res.add(new InvalidProperty(key, message)); - } - } - @Override @NotNull public Collection process(@Nullable final Map p) { final Collection result = new ArrayList(); if (p == null) return result; - final boolean usingConnection = !StringUtil.isEmptyOrSpaces(p.get(c.getConnectionIdKey())); - if (!usingConnection) { - checkNotEmpty(p, c.getApiKey(), "API key must be specified", result); - checkNotEmpty(p, c.getServerKey(), "Server must be specified", result); - } + checkCredentialsUnlessUsingConnection(p, c, result); checkNotEmpty(p, c.getPackagePathsKey(), "Package paths must be specified", result); if (result.isEmpty()) { diff --git a/octopus-server/src/main/java/octopus/teamcity/server/PropertiesValidator.java b/octopus-server/src/main/java/octopus/teamcity/server/PropertiesValidator.java new file mode 100644 index 00000000..33d78de9 --- /dev/null +++ b/octopus-server/src/main/java/octopus/teamcity/server/PropertiesValidator.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) Octopus Deploy and contributors. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * these files except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package octopus.teamcity.server; + +import java.util.Collection; +import java.util.Map; + +import jetbrains.buildServer.serverSide.InvalidProperty; +import jetbrains.buildServer.util.StringUtil; +import octopus.teamcity.common.OctopusConstants; +import org.jetbrains.annotations.NotNull; + +/** Shared validation helpers for run type {@code PropertiesProcessor}s. */ +public final class PropertiesValidator { + + private PropertiesValidator() {} + + /** + * Adds an {@link InvalidProperty} to {@code result} when the value for {@code key} is missing or + * blank. + */ + public static void checkNotEmpty( + @NotNull final Map properties, + @NotNull final String key, + @NotNull final String message, + @NotNull final Collection result) { + if (StringUtil.isEmptyOrSpaces(properties.get(key))) { + result.add(new InvalidProperty(key, message)); + } + } + + /** + * Requires the inline server URL and API key unless a reusable connection is selected (in which + * case those credentials come from the connection at build start). + */ + public static void checkCredentialsUnlessUsingConnection( + @NotNull final Map properties, + @NotNull final OctopusConstants constants, + @NotNull final Collection result) { + final boolean usingConnection = + !StringUtil.isEmptyOrSpaces(properties.get(constants.getConnectionIdKey())); + if (!usingConnection) { + checkNotEmpty(properties, constants.getServerKey(), "Server must be specified", result); + checkNotEmpty(properties, constants.getApiKey(), "API key must be specified", result); + } + } +}