Skip to content

Commit

Permalink
JENKINS-37700 allow base path to be defined to located changelog file.
Browse files Browse the repository at this point in the history
  • Loading branch information
prospero238 committed Aug 25, 2016
1 parent e4bfd70 commit 8cbb493
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 23 deletions.
@@ -1,6 +1,7 @@
package org.jenkinsci.plugins.liquibase.evaluator;

import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
Expand Down Expand Up @@ -52,6 +53,8 @@ public abstract class AbstractLiquibaseBuilder extends Builder {
protected String driverClassname;
protected String labels;
private String changeLogParameters;
private String basePath;



public AbstractLiquibaseBuilder(String databaseEngine,
Expand All @@ -64,7 +67,7 @@ public AbstractLiquibaseBuilder(String databaseEngine,
String liquibasePropertiesPath,
String classpath,
String driverClassname,
String changeLogParameters, String labels) {
String changeLogParameters, String labels, String basePath) {
this.databaseEngine = databaseEngine;
this.changeLogFile = changeLogFile;
this.username = username;
Expand All @@ -77,6 +80,7 @@ public AbstractLiquibaseBuilder(String databaseEngine,
this.driverClassname = driverClassname;
this.changeLogParameters = changeLogParameters;
this.labels = labels;
this.basePath = basePath;
}

public AbstractLiquibaseBuilder() {
Expand Down Expand Up @@ -133,7 +137,16 @@ public Liquibase createLiquibase(AbstractBuild<?, ?> build,
JdbcConnection jdbcConnection = createJdbcConnection(configProperties, driverName);
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(jdbcConnection);

ResourceAccessor filePathAccessor = new FilePathAccessor(build);

FilePath filePath;
if (Strings.isNullOrEmpty(basePath)) {
filePath = build.getWorkspace();
} else {
filePath = build.getWorkspace().child(hudson.Util.replaceMacro(basePath, build.getEnvironment(listener)));

}

ResourceAccessor filePathAccessor = new FilePathAccessor(filePath);
CompositeResourceAccessor resourceAccessor =
new CompositeResourceAccessor(filePathAccessor,
new ClassLoaderResourceAccessor(Thread.currentThread().getContextClassLoader()),
Expand Down Expand Up @@ -324,4 +337,13 @@ public String getLabels() {
public void setLabels(String labels) {
this.labels = labels;
}

public String getBasePath() {
return basePath;
}

@DataBoundSetter
public void setBasePath(String basePath) {
this.basePath = basePath;
}
}
Expand Up @@ -56,10 +56,11 @@ public ChangesetEvaluator(String databaseEngine,
boolean testRollbacks,
boolean dropAll,
String labels,
String basePath,
boolean tagOnSuccessfulBuild) {
super(databaseEngine, changeLogFile, username, password, url, defaultSchemaName, contexts,
liquibasePropertiesPath,
classpath, driverClassname, changeLogParameters, labels);
classpath, driverClassname, changeLogParameters, labels, basePath);
this.testRollbacks = testRollbacks;
this.dropAll = dropAll;
this.tagOnSuccessfulBuild = tagOnSuccessfulBuild;
Expand Down
@@ -1,7 +1,6 @@
package org.jenkinsci.plugins.liquibase.evaluator;

import hudson.FilePath;
import hudson.model.AbstractBuild;
import liquibase.resource.ResourceAccessor;

import java.io.IOException;
Expand All @@ -24,19 +23,19 @@
* Provides Jenkin's file abstraction as a liquibase resource accessor.
*/
public class FilePathAccessor implements ResourceAccessor {
private final AbstractBuild<?, ?> build;
private final FilePath filePath;

private static final Logger LOG = LoggerFactory.getLogger(FilePathAccessor.class);

public FilePathAccessor(AbstractBuild<?, ?> build) {
this.build = build;
public FilePathAccessor(FilePath filePath) {
this.filePath = filePath;
}

public InputStream getResourceAsStream(String s) throws IOException {
InputStream inputStream = null;
final FilePath workspace = build.getWorkspace();
if (workspace != null) {
FilePath child = workspace.child(s);

if (filePath != null) {
FilePath child = filePath.child(s);
try {
if (child.exists()) {
inputStream = child.read();
Expand Down Expand Up @@ -69,10 +68,10 @@ public Set<String> list(String relativeTo,
boolean includeDirectories,
boolean recursive) throws IOException {

final FilePath workspace = build.getWorkspace();
return list(workspace, relativeTo, path, includeFiles, includeDirectories, recursive);
return list(filePath, relativeTo, path, includeFiles, includeDirectories, recursive);
}

@SuppressWarnings("ReturnOfNull")
protected Set<String> list(FilePath workspace,
String relativeTo,
String path,
Expand Down Expand Up @@ -146,11 +145,10 @@ public ClassLoader toClassLoader() {
@Override
public URLClassLoader run() {
URLClassLoader urlClassLoader = null;
final FilePath workspace = build.getWorkspace();
if (workspace != null) {
if (filePath != null) {
try {
urlClassLoader =
new URLClassLoader(new URL[]{new URL("file://" + workspace.getBaseName())});
new URLClassLoader(new URL[]{new URL("file://" + filePath.getBaseName())});
} catch (MalformedURLException e) {
throw new RuntimeException("Unable to construct classloader.",e);
}
Expand Down
Expand Up @@ -61,13 +61,14 @@ public RollbackBuildStep(String databaseEngine,
String driverClassname,
String changeLogParameters,
String labels,
String basePath,
String rollbackType,
int numberOfChangesetsToRollback,
String rollbackLastHours,
String rollbackToTag, String rollbackToDate) {
super(databaseEngine, changeLogFile, username, password, url, defaultSchemaName, contexts,
liquibasePropertiesPath,
classpath, driverClassname, changeLogParameters, labels);
classpath, driverClassname, changeLogParameters, labels, basePath);

this.rollbackType = rollbackType;
this.numberOfChangesetsToRollback = numberOfChangesetsToRollback;
Expand Down
Expand Up @@ -3,6 +3,10 @@
<j:jelly xmlns:j="jelly:core"
xmlns:f="/lib/form">

<f:entry help="help-basePath.html" title="Directory containing change log file" field="basePath"
description="Leave blank for workspace root. May be relative to workspace or an absolute path.">
<f:textbox value="${instance.basePath}"/>
</f:entry>
<f:entry title="Database Engine">
<select class="setting-input" name="LiquibaseBuilder.databaseEngine">
<option value="">Select included driver</option>
Expand Down
3 changes: 3 additions & 0 deletions src/main/webapp/help-basePath.html
@@ -0,0 +1,3 @@
<div><p>The directory in which the change log file resides. Leave blank if the changelog file resides at the root of the project's workspace.</p>
<p>Otherwise, you can use this if your changelog file resides in "src/main/resources", for example,
but still wish to have generated DATABASECHANGELOG records without that path reference.</p></div>
Expand Up @@ -50,7 +50,7 @@ public void should_list_files() throws IOException, InterruptedException {

FilePath filePath = new FilePath(temporaryFolder.getRoot());

FilePathAccessor filePathAccessor = new FilePathAccessor(build);
FilePathAccessor filePathAccessor = new FilePathAccessor(build.getWorkspace());

Set<String> result =
filePathAccessor.list( filePath, null,
Expand All @@ -62,7 +62,7 @@ public void should_list_files() throws IOException, InterruptedException {

@Test
public void should_return_null_if_not_found() throws IOException {
FilePathAccessor accessor = new FilePathAccessor(build);
FilePathAccessor accessor = new FilePathAccessor(build.getWorkspace());

InputStream inputStream = accessor.getResourceAsStream("i_dont_exist");
assertThat(inputStream, is(nullValue()));
Expand Down
Expand Up @@ -22,6 +22,7 @@
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.hamcrest.CoreMatchers;
import org.hamcrest.Matcher;
import org.jenkinsci.plugins.liquibase.evaluator.ChangeSetDetail;
import org.jenkinsci.plugins.liquibase.evaluator.ChangesetEvaluator;
Expand Down Expand Up @@ -102,6 +103,37 @@ public void should_use_liquibase_defaults_file() throws InterruptedException, Ex
}


@Test
public void should_locate_change_log_using_basepath()
throws IOException, ExecutionException, InterruptedException, SQLException, LiquibaseException {
File changesetDir = temporaryFolder.newFolder("changesetDir");
ChangesetEvaluator evaluator = new ChangesetEvaluator();
File changeLog =
LiquibaseTestUtil.createFileFromResource(changesetDir, "/example-changesets/single-changeset.xml");
evaluator.setChangeLogFile(
changeLog.getAbsolutePath());
evaluator.setUrl(LiquibaseTestUtil.IN_MEMORY_JDBC_URL);
evaluator.setDatabaseEngine(H2);

evaluator.setBasePath("changesetDir");


FreeStyleProject project = jenkinsRule.createFreeStyleProject();
project.getBuildersList().add(evaluator);
project.setCustomWorkspace(changesetDir.getParent());

FreeStyleBuild build = launchBuildForProject(project);

assertThat(build.getResult(), is(Result.SUCCESS));

ExecutedChangesetAction action = build.getAction(ExecutedChangesetAction.class);

assertThat(build.getAction(ExecutedChangesetAction.class), CoreMatchers.notNullValue());
assertThat(action.getSuccessfulChangeSets(), contains(IsChangeSetDetail.hasId("create-table")));


}

@Test
public void should_handle_json_changesets_successfully()
throws InterruptedException, ExecutionException, IOException {
Expand Down Expand Up @@ -238,11 +270,6 @@ public void should_handle_include_all_relative() throws IOException, ExecutionEx
assertThat(changeSetDetails, containsSunnyDayChangesetDetails());
}

@Test
public void should_resolve_expressions_in_configuration() {

}


protected FreeStyleBuild createAndBuildLiquibaseProject(String changesetResourcePath)
throws IOException, InterruptedException, ExecutionException {
Expand Down
2 changes: 2 additions & 0 deletions src/wiki/confluence.txt
Expand Up @@ -53,6 +53,8 @@ h3. Version History
h4. Version 1.1.0 (Aug 24, 2017)

* [JENKINS-37591|https://issues.jenkins-ci.org/browse/JENKINS-37591] Upgraded to use Liquibase 3.5.1
* [JENKINS-37700|https://issues.jenkins-ci.org/browse/JENKINS-37700] Allow base path to be set.
* [JENKINS-37699|https://issues.jenkins-ci.org/browse/JENKINS-37699] Allow expressions in configuration fields
* [JENKINS-37420|https://issues.jenkins-ci.org/browse/JENKINS-37420] Added roll back build step
* [JENKINS-37592|https://issues.jenkins-ci.org/browse/JENKINS-37592] Tag on successful update.
* [JENKINS-37637|https://issues.jenkins-ci.org/browse/JENKINS-37637] Allow changesets to be loaded from classpath.
Expand Down

0 comments on commit 8cbb493

Please sign in to comment.