Skip to content

Commit

Permalink
JENKINS-40707
Browse files Browse the repository at this point in the history
updated to use newer APIs, including:
  - using Run insead of AbstractBuild)
  - using TaskListener, not BuildListener
added what pipeline script should look like
  • Loading branch information
prospero238 committed Dec 28, 2016
1 parent 7c865b8 commit 9b66382
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 144 deletions.
Expand Up @@ -2,7 +2,7 @@

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

import java.io.IOException;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -40,23 +40,24 @@ public class PropertiesAssembler {
* @param liquibaseBuilder
* @param build
* @param environment
* @param workspace
* @return
*/
public static Properties createLiquibaseProperties(AbstractLiquibaseBuilder liquibaseBuilder,
AbstractBuild<?, ?> build, EnvVars environment)
Run<?, ?> build, EnvVars environment, FilePath workspace)
throws IOException, InterruptedException {
Properties properties = new Properties();
assembleDefaults(properties);
String propertiesPath = resolvePropertiesPath(liquibaseBuilder, environment);
assembleFromPropertiesFile(properties, propertiesPath, build);
assembleFromPropertiesFile(properties, propertiesPath, build, workspace);

assembleFromProjectConfiguration(liquibaseBuilder, properties, environment, build);
return properties;
}

protected static void assembleFromProjectConfiguration(AbstractLiquibaseBuilder liquibaseBuilder,
Properties properties,
EnvVars environment, AbstractBuild<?, ?> build)
EnvVars environment, Run<?, ?> build)
throws IOException, InterruptedException {


Expand Down Expand Up @@ -111,9 +112,9 @@ private static boolean useIncludedDriver(AbstractLiquibaseBuilder liquibaseBuild

private static void assembleFromPropertiesFile(Properties properties,
String liquibasePropertiesPath,
AbstractBuild<?, ?> build) {
Run<?, ?> build, FilePath workspace) {

if (!Strings.isNullOrEmpty(liquibasePropertiesPath)) {
FilePath workspace = build.getWorkspace();
if (workspace != null) {
InputStreamReader streamReader = null;
try {
Expand Down
Expand Up @@ -3,11 +3,12 @@
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Descriptor;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.tasks.Builder;
import jenkins.tasks.SimpleBuildStep;
import liquibase.Contexts;
import liquibase.Liquibase;
import liquibase.database.Database;
Expand All @@ -28,6 +29,8 @@
import java.util.Map;
import java.util.Properties;

import javax.annotation.Nonnull;

import org.jenkinsci.plugins.liquibase.common.LiquibaseProperty;
import org.jenkinsci.plugins.liquibase.common.PropertiesAssembler;
import org.jenkinsci.plugins.liquibase.common.Util;
Expand All @@ -38,7 +41,7 @@
import com.google.common.base.Splitter;
import com.google.common.base.Strings;

public abstract class AbstractLiquibaseBuilder extends Builder {
public abstract class AbstractLiquibaseBuilder extends Builder implements SimpleBuildStep {
private static final Logger LOG = LoggerFactory.getLogger(AbstractLiquibaseBuilder.class);

protected String databaseEngine;
Expand All @@ -55,9 +58,6 @@ public abstract class AbstractLiquibaseBuilder extends Builder {
private Boolean useIncludedDriver;
private String credentialsId;




public AbstractLiquibaseBuilder(String databaseEngine,
String changeLogFile,
String url,
Expand All @@ -82,7 +82,6 @@ public AbstractLiquibaseBuilder(String databaseEngine,
this.labels = labels;
this.basePath = basePath;
this.useIncludedDriver = useIncludedDriver;

this.credentialsId = credentialsId;
}

Expand All @@ -97,28 +96,29 @@ protected Object readResolve() {
return this;
}

public abstract void doPerform(AbstractBuild<?, ?> build,
BuildListener listener,
Liquibase liquibase,
Contexts contexts,
ExecutedChangesetAction executedChangesetAction, Properties configProperties)
throws InterruptedException, IOException, LiquibaseException;

abstract public Descriptor<Builder> getDescriptor();
public abstract void runPerform(Run<?, ?> build,
TaskListener listener,
Liquibase liquibase,
Contexts contexts,
ExecutedChangesetAction executedChangesetAction,
Properties configProperties)
throws InterruptedException, IOException, LiquibaseException;

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException {

public void perform(@Nonnull Run<?, ?> build,
@Nonnull FilePath workspace,
@Nonnull Launcher launcher,
@Nonnull TaskListener listener) throws InterruptedException, IOException {

Properties configProperties = PropertiesAssembler.createLiquibaseProperties(this, build,
build.getEnvironment(listener));
build.getEnvironment(listener), workspace);
ExecutedChangesetAction executedChangesetAction = new ExecutedChangesetAction(build);
Liquibase liquibase = createLiquibase(build, listener, executedChangesetAction, configProperties, launcher);
Liquibase liquibase =
createLiquibase(build, listener, executedChangesetAction, configProperties, launcher, workspace);
String liqContexts = getProperty(configProperties, LiquibaseProperty.CONTEXTS);
Contexts contexts = new Contexts(liqContexts);
try {
doPerform(build, listener, liquibase, contexts, executedChangesetAction, configProperties);
runPerform(build, listener, liquibase, contexts, executedChangesetAction, configProperties);
} catch (LiquibaseException e) {
e.printStackTrace(listener.getLogger());
build.setResult(Result.UNSTABLE);
Expand All @@ -128,20 +128,20 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
if (!executedChangesetAction.isRollbackOnly()) {
build.addAction(executedChangesetAction);
}
return true;
}

public Liquibase createLiquibase(AbstractBuild<?, ?> build,
BuildListener listener,
abstract public Descriptor<Builder> getDescriptor();

public Liquibase createLiquibase(Run<?, ?> build,
TaskListener listener,
ExecutedChangesetAction action,
Properties configProperties,
Launcher launcher) throws IOException, InterruptedException {
Launcher launcher, FilePath workspace) throws IOException, InterruptedException {
Liquibase liquibase;
String driverName = getProperty(configProperties, LiquibaseProperty.DRIVER);
String resolvedClasspath = getProperty(configProperties, LiquibaseProperty.CLASSPATH);

try {
FilePath workspace = build.getWorkspace();
if (!Strings.isNullOrEmpty(resolvedClasspath)) {
Util.addClassloader(launcher.isUnix(), workspace, resolvedClasspath);
}
Expand Down Expand Up @@ -194,7 +194,7 @@ protected static void populateChangeLogParameters(Liquibase liquibase,
}
}

private JdbcConnection createJdbcConnection(Properties configProperties, String driverName) {
private static JdbcConnection createJdbcConnection(Properties configProperties, String driverName) {
Connection connection;
String dbUrl = getProperty(configProperties, LiquibaseProperty.URL);
try {
Expand Down Expand Up @@ -340,17 +340,17 @@ public String getBasePath() {
public void setBasePath(String basePath) {
this.basePath = basePath;
}

public void clearDriverClassname() {
driverClassname = null;
}

public void clearDatabaseEngine() {
databaseEngine=null;
}

public boolean hasUseIncludedDriverBeenSet() {
return useIncludedDriver!=null;
}

public boolean isUseIncludedDriver() {
return useIncludedDriver;
}
Expand Down
@@ -1,6 +1,6 @@
package org.jenkinsci.plugins.liquibase.evaluator;

import hudson.model.BuildListener;
import hudson.model.TaskListener;
import liquibase.change.Change;
import liquibase.changelog.ChangeSet;
import liquibase.changelog.DatabaseChangeLog;
Expand Down Expand Up @@ -28,16 +28,16 @@
public class BuildChangeExecListener implements ChangeExecListener {
private final ExecutedChangesetAction action;
private static final Logger LOG = LoggerFactory.getLogger(BuildChangeExecListener.class);
private BuildListener buildListener;
private TaskListener taskListener;
private static final String RAN_CHANGESET_MSG = "Ran changeset: ";

public BuildChangeExecListener(ExecutedChangesetAction action) {
this.action = action;
}

public BuildChangeExecListener(ExecutedChangesetAction action, BuildListener buildListener) {
public BuildChangeExecListener(ExecutedChangesetAction action, TaskListener taskListener) {
this.action = action;
this.buildListener = buildListener;
this.taskListener = taskListener;
}

public void willRun(ChangeSet changeSet,
Expand All @@ -58,7 +58,7 @@ public void rolledBack(ChangeSet changeSet, DatabaseChangeLog databaseChangeLog,
}

String logMessage = formatChangesetForLog(changeSet, "Rolled back");
buildListener.getLogger().println(logMessage);
taskListener.getLogger().println(logMessage);
ChangeSetDetail changeSetDetail = ChangeSetDetail.fromChangeSet(changeSet);
action.addRolledBackChangesetDetail(changeSetDetail);
if (action.hasChangesetWithId(changeSetDetail.getId())) {
Expand Down Expand Up @@ -102,7 +102,7 @@ protected static ChangeSetDetail createChangeSetDetail(Change change, ChangeSet

protected void printConsoleLogMessage(ChangeSet changeSet) {
String logMessage = Util.formatChangeset(changeSet);
buildListener.getLogger().println(RAN_CHANGESET_MSG + logMessage);
taskListener.getLogger().println(RAN_CHANGESET_MSG + logMessage);
}

public void runFailed(ChangeSet changeSet, DatabaseChangeLog databaseChangeLog, Database database, Exception e) {
Expand Down
@@ -1,19 +1,18 @@
package org.jenkinsci.plugins.liquibase.evaluator;

import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Descriptor;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.tasks.Builder;
import liquibase.Contexts;
import liquibase.LabelExpression;
import liquibase.Liquibase;
import liquibase.exception.LiquibaseException;
import liquibase.exception.MigrationFailedException;

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

import org.jenkinsci.plugins.liquibase.common.LiquibaseCommand;
Expand Down Expand Up @@ -41,38 +40,13 @@ public ChangesetEvaluator() {
super();
}

@DataBoundConstructor
public ChangesetEvaluator(String databaseEngine,
String changeLogFile,
String url,
String defaultSchemaName,
String contexts,
String liquibasePropertiesPath,
String classpath,
String driverClassname,
String changeLogParameters,
boolean testRollbacks,
boolean dropAll,
String labels,
String basePath,
boolean tagOnSuccessfulBuild,
boolean useIncludedDriver,
String credentialsId) {
super(databaseEngine, changeLogFile, url, defaultSchemaName, contexts,
liquibasePropertiesPath,
classpath, driverClassname, changeLogParameters, labels, basePath, useIncludedDriver, credentialsId);
this.testRollbacks = testRollbacks;
this.dropAll = dropAll;
this.tagOnSuccessfulBuild = tagOnSuccessfulBuild;
}

@Override
public void doPerform(AbstractBuild<?, ?> build,
BuildListener listener,
Liquibase liquibase,
Contexts contexts,
ExecutedChangesetAction executedChangesetAction, Properties configProperties)
throws InterruptedException, IOException {
public void runPerform(Run<?, ?> build,
TaskListener listener,
Liquibase liquibase,
Contexts contexts,
ExecutedChangesetAction executedChangesetAction,
Properties configProperties) {

executedChangesetAction.setRollbacksTested(testRollbacks);

Expand Down Expand Up @@ -100,7 +74,7 @@ public void doPerform(AbstractBuild<?, ?> build,
}

if (tagOnSuccessfulBuild) {
String tagString = build.getProject().getName() + "-" + build.getNumber();
String tagString = build.getParent().getName() + "-" + build.getNumber();
listener.getLogger().println("Applying tag '" + tagString + "' to schema");
liquibase.tag(tagString);
executedChangesetAction.setAppliedTag(tagString);
Expand All @@ -112,6 +86,32 @@ public void doPerform(AbstractBuild<?, ?> build,
e.printStackTrace(listener.getLogger());
build.setResult(Result.FAILURE);
}

}

@DataBoundConstructor
public ChangesetEvaluator(String databaseEngine,
String changeLogFile,
String url,
String defaultSchemaName,
String contexts,
String liquibasePropertiesPath,
String classpath,
String driverClassname,
String changeLogParameters,
boolean testRollbacks,
boolean dropAll,
String labels,
String basePath,
boolean tagOnSuccessfulBuild,
boolean useIncludedDriver,
String credentialsId) {
super(databaseEngine, changeLogFile, url, defaultSchemaName, contexts,
liquibasePropertiesPath,
classpath, driverClassname, changeLogParameters, labels, basePath, useIncludedDriver, credentialsId);
this.testRollbacks = testRollbacks;
this.dropAll = dropAll;
this.tagOnSuccessfulBuild = tagOnSuccessfulBuild;
}

@Override
Expand Down
Expand Up @@ -2,6 +2,7 @@

import hudson.model.AbstractBuild;
import hudson.model.Action;
import hudson.model.Run;

import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -19,7 +20,7 @@
*/
public class ExecutedChangesetAction implements Action {

private AbstractBuild<?, ?> build;
private Run<?, ?> build;

private List<ChangeSetDetail> changeSetDetails = Lists.newArrayList();

Expand All @@ -34,7 +35,7 @@ public class ExecutedChangesetAction implements Action {
public ExecutedChangesetAction() {
}

public ExecutedChangesetAction(AbstractBuild<?, ?> build) {
public ExecutedChangesetAction(Run<?, ?> build) {
this.build = build;
}

Expand All @@ -50,7 +51,7 @@ public String getUrlName() {
return "executedChangeSets";
}

public AbstractBuild<?, ?> getBuild() {
public Run<?, ?> getBuild() {
return build;
}

Expand Down

0 comments on commit 9b66382

Please sign in to comment.