Skip to content
Permalink
Browse files
JENKINS-40707
leaving tokens alone when we are in a pipeline.
Some resulting ugliness should be a candidate for refactoring
  • Loading branch information
prospero238 committed Dec 30, 2016
1 parent d77241c commit ae1ff9045f7396c8b78cec9db3502225cdc10dce
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 25 deletions.
@@ -2,6 +2,7 @@

import hudson.EnvVars;
import hudson.FilePath;
import hudson.model.AbstractBuild;
import hudson.model.Run;

import java.io.IOException;
@@ -48,7 +49,13 @@ public static Properties createLiquibaseProperties(AbstractLiquibaseBuilder liqu
throws IOException, InterruptedException {
Properties properties = new Properties();
assembleDefaults(properties);
String propertiesPath = resolvePropertiesPath(liquibaseBuilder, environment);

String propertiesPath = null;
if (build instanceof AbstractBuild) {
propertiesPath = hudson.Util.replaceMacro(liquibaseBuilder.getLiquibasePropertiesPath(), environment);
} else {
propertiesPath = liquibaseBuilder.getLiquibasePropertiesPath();
}
assembleFromPropertiesFile(properties, propertiesPath, build, workspace);

assembleFromProjectConfiguration(liquibaseBuilder, properties, environment, build);
@@ -68,38 +75,40 @@ protected static void assembleFromProjectConfiguration(AbstractLiquibaseBuilder
Lists.<DomainRequirement>newArrayList());

if (credentialById!=null) {
addPropertyIfDefined(properties, LiquibaseProperty.USERNAME, credentialById.getUsername(), environment);
addPropertyIfDefined(properties, LiquibaseProperty.PASSWORD, credentialById.getPassword().getPlainText(), environment);
addPropertyIfDefined(properties, LiquibaseProperty.USERNAME, credentialById.getUsername(), environment, build);
addPropertyIfDefined(properties, LiquibaseProperty.PASSWORD, credentialById.getPassword().getPlainText(), environment,
build);
}
}



addPropertyIfDefined(properties, LiquibaseProperty.CHANGELOG_FILE, liquibaseBuilder.getChangeLogFile(),
environment);
environment, build);

addPropertyIfDefined(properties, LiquibaseProperty.CLASSPATH, liquibaseBuilder.getClasspath(), environment);
addPropertyIfDefined(properties, LiquibaseProperty.CLASSPATH, liquibaseBuilder.getClasspath(), environment,
build);
addPropertyIfDefined(properties, LiquibaseProperty.DEFAULT_SCHEMA_NAME, liquibaseBuilder.getDefaultSchemaName(),
environment);
addPropertyIfDefined(properties, LiquibaseProperty.URL, liquibaseBuilder.getUrl(), environment);
environment, build);
addPropertyIfDefined(properties, LiquibaseProperty.URL, liquibaseBuilder.getUrl(), environment, build);
addPropertyIfDefined(properties, LiquibaseProperty.CHANGELOG_FILE, liquibaseBuilder.getChangeLogFile(),
environment);
addPropertyIfDefined(properties, LiquibaseProperty.LABELS, liquibaseBuilder.getLabels(), environment);
addPropertyIfDefined(properties, LiquibaseProperty.CONTEXTS, liquibaseBuilder.getContexts(), environment);
resolveDatabaseDriver(liquibaseBuilder, properties, environment);
environment, build);
addPropertyIfDefined(properties, LiquibaseProperty.LABELS, liquibaseBuilder.getLabels(), environment, build);
addPropertyIfDefined(properties, LiquibaseProperty.CONTEXTS, liquibaseBuilder.getContexts(), environment, build);
resolveDatabaseDriver(liquibaseBuilder, properties, environment, build);
}

private static void resolveDatabaseDriver(AbstractLiquibaseBuilder liquibaseBuilder,
Properties properties,
EnvVars environment) {
EnvVars environment, Run<?, ?> build) {


boolean useIncludedDriver = useIncludedDriver(liquibaseBuilder);
if (useIncludedDriver) {
PropertiesAssembler.setDriverFromDBEngine(liquibaseBuilder, properties);
} else {
addPropertyIfDefined(properties, LiquibaseProperty.DRIVER, liquibaseBuilder.getDriverClassname(),
environment);
environment, build);
}
}

@@ -150,17 +159,18 @@ private static void setProperty(Properties properties, LiquibaseProperty liquiba

protected static void addPropertyIfDefined(Properties properties,
LiquibaseProperty liquibaseProperty,
String value, EnvVars environment) {
String value, EnvVars environment, Run<?, ?> build) {
if (!Strings.isNullOrEmpty(value)) {
String resolvedValue = hudson.Util.replaceMacro(value, environment);
String resolvedValue;
if (build instanceof AbstractBuild) {
resolvedValue = hudson.Util.replaceMacro(value, environment);
} else {
resolvedValue = value;
}
properties.setProperty(liquibaseProperty.propertyName(), resolvedValue);
}
}

private static String resolvePropertiesPath(AbstractLiquibaseBuilder liquibaseBuilder, EnvVars environment) {
return hudson.Util.replaceMacro(liquibaseBuilder.getLiquibasePropertiesPath(), environment);
}

public static void setDriverFromDBEngine(AbstractLiquibaseBuilder liquibaseBuilder, Properties properties) {
if (!Strings.isNullOrEmpty(liquibaseBuilder.getDatabaseEngine())) {
for (IncludedDatabaseDriver includedDatabaseDriver : liquibaseBuilder.getDrivers()) {
@@ -3,10 +3,12 @@
import hudson.EnvVars;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Run;

import java.io.IOException;
import java.util.Properties;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@@ -21,39 +23,53 @@ public class PropertiesAssemblerTest {

@Mock
private AbstractBuild build;

@Mock
private Run jobRun;

@Mock
private BuildListener listener;
protected Properties properties = new Properties();
protected LiquibaseProperty changelogFile = LiquibaseProperty.CHANGELOG_FILE;
protected EnvVars envVars = new EnvVars();
protected String environmentProperty = "token";
protected String environmentValue = "resolvedValue";
protected String expression;

@Before
public void setup() throws IOException {
expression = "${" + environmentProperty + "}";
}

@Test
public void should_resolve_env_variables() throws IOException, InterruptedException {
String environmentProperty = "token";
String environmentValue = "resolvedValue";
envVars.put(environmentProperty, environmentValue);

when(build.getEnvironment(listener)).thenReturn(envVars);

String expression = "${" + environmentProperty + "}";
BuilderStub liquibaseBuilder = new BuilderStub();
liquibaseBuilder.setChangeLogFile(expression);
PropertiesAssembler.assembleFromProjectConfiguration(liquibaseBuilder, properties,
build.getEnvironment(listener), build);

assertThat(properties.getProperty(changelogFile.propertyName()), is(environmentValue));

}

@Test
public void should_not_resolve_expression() {
Properties properties = new Properties();
String value = "${not_in_env}";
PropertiesAssembler.addPropertyIfDefined(properties, changelogFile, value, envVars);

PropertiesAssembler.addPropertyIfDefined(properties, changelogFile, value, envVars, build);
assertThat(properties.getProperty(changelogFile.propertyName()), is(value));
}

@Test
public void should_leave_tokens_alone_when_pipeline_step() throws IOException, InterruptedException {
envVars.put(environmentProperty, environmentValue);

when(jobRun.getEnvironment(listener)).thenReturn(envVars);
PropertiesAssembler.assembleFromProjectConfiguration(new BuilderStub(), properties,
build.getEnvironment(listener), jobRun);
assertThat(properties.getProperty(changelogFile.propertyName()), is(expression));
}
}

0 comments on commit ae1ff90

Please sign in to comment.