diff --git a/src/main/java/hudson/scm/IntegritySCMChkptNotifierStep.java b/src/main/java/hudson/scm/IntegritySCMChkptNotifierStep.java new file mode 100644 index 0000000..03709cd --- /dev/null +++ b/src/main/java/hudson/scm/IntegritySCMChkptNotifierStep.java @@ -0,0 +1,86 @@ +package hudson.scm; + +import java.io.IOException; +import java.util.logging.Logger; + +import com.mks.api.Command; +import com.mks.api.Option; +import com.mks.api.response.APIException; + +import hudson.FilePath; +import hudson.Launcher; +import hudson.model.TaskListener; +import hudson.model.Run; +import hudson.tasks.BuildStepMonitor; +import hudson.tasks.Notifier; +import jenkins.tasks.SimpleBuildStep; + +@SuppressWarnings("unchecked") +public class IntegritySCMChkptNotifierStep extends Notifier implements SimpleBuildStep +{ + private static final Logger LOGGER = Logger.getLogger("IntegritySCM"); + private final IntegrityConfigurable ciSettings; + private final String configPath; + private final String checkpointLabel; + private final String checkpointDesc; + + public IntegritySCMChkptNotifierStep(IntegrityConfigurable ciSettings, String configPath, String checkpointLabel, String checkpointDesc) + { + this.ciSettings = ciSettings; + this.configPath = configPath; + this.checkpointLabel = checkpointLabel; + this.checkpointDesc = checkpointDesc; + } + + public BuildStepMonitor getRequiredMonitorService() + { + return BuildStepMonitor.NONE; + } + + public void perform(Run run, FilePath workspace, Launcher launcher, TaskListener listener) throws InterruptedException, IOException + { + APISession api = APISession.create(ciSettings); + if( null != api ) + { + listener.getLogger().println("Preparing to execute si checkpoint for project " + configPath); + try + { + // Construct the checkpoint command + Command siCheckpoint = new Command(Command.SI, "checkpoint"); + // Set the project name + siCheckpoint.addOption(new Option("project", configPath)); + // Set the label and description if applicable + if( null != checkpointLabel && checkpointLabel.length() > 0 ) + { + // Set the label + siCheckpoint.addOption(new Option("label", checkpointLabel)); + } + + if( null != checkpointDesc && checkpointDesc.length() > 0 ) + { + // Set the description + siCheckpoint.addOption(new Option("description", checkpointDesc)); + } + + api.runCommand(siCheckpoint); + listener.getLogger().println("Successfully checkpointed project " + configPath); + } + catch (APIException aex) + { + LOGGER.severe("API Exception caught..."); + ExceptionHandler eh = new ExceptionHandler(aex); + aex.printStackTrace(listener.fatalError(eh.getMessage())); + LOGGER.severe(eh.getMessage()); + LOGGER.fine(eh.getCommand() + " returned exit code " + eh.getExitCode()); + } + finally + { + api.Terminate(); + } + } + else + { + listener.getLogger().println("Failed to establish connection with Integrity!"); + } + } +} diff --git a/src/main/java/hudson/scm/IntegritySCMChkptStep.java b/src/main/java/hudson/scm/IntegritySCMChkptStep.java new file mode 100644 index 0000000..e930edc --- /dev/null +++ b/src/main/java/hudson/scm/IntegritySCMChkptStep.java @@ -0,0 +1,183 @@ + +package hudson.scm; + +import hudson.Extension; +import hudson.FilePath; +import hudson.Launcher; +import hudson.model.TaskListener; +import hudson.model.Run; +import hudson.scm.IntegritySCM.DescriptorImpl; +import hudson.util.ListBoxModel; +import hudson.util.Secret; + +import java.util.logging.Logger; + +import javax.inject.Inject; + +import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl; +import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl; +import org.jenkinsci.plugins.workflow.steps.AbstractSynchronousStepExecution; +import org.jenkinsci.plugins.workflow.steps.StepContextParameter; +import org.kohsuke.stapler.DataBoundConstructor; +import org.kohsuke.stapler.DataBoundSetter; +import org.kohsuke.stapler.QueryParameter; + + +public class IntegritySCMChkptStep extends AbstractStepImpl +{ + private static final Logger LOGGER = Logger.getLogger("IntegritySCM"); + + private String serverConfig; + private String userName; + private Secret password; + private String configPath; + private String checkpointLabel; + private String checkpointDesc; + private IntegrityConfigurable connectionSettings; + + public String getServerConfig() + { + return this.serverConfig; + } + + @DataBoundSetter + public void setUserName(String userName) + { + this.userName = userName; + } + + public String getUserName() + { + return this.userName; + } + + @DataBoundSetter + public void setPassword(String password) + { + this.password = Secret.fromString(password); + } + + /** + * Returns the project specific encrypted password of the user connecting to the Integrity Server + * @return + */ + public String getPassword() + { + return this.password.getEncryptedValue(); + } + + @DataBoundSetter + public void setConfigPath(String configPath) + { + this.configPath = configPath; + } + + public String getConfigPath() + { + return this.configPath; + } + + @DataBoundSetter + public void setCheckpointLabel(String checkpointLabel) + { + this.checkpointLabel = checkpointLabel; + } + + public String getCheckpointLabel() + { + return this.checkpointLabel; + } + + @DataBoundSetter + public void setCheckpointDesc(String checkpointDesc) + { + this.checkpointDesc = checkpointDesc; + } + + public String getCheckpointDesc() + { + return this.checkpointDesc; + } + + public IntegrityConfigurable getConnectionSettings() + { + IntegrityConfigurable desSettings = DescriptorImpl.INTEGRITY_DESCRIPTOR.getConfiguration(serverConfig); + this.connectionSettings = new IntegrityConfigurable("TEMP_ID", desSettings.getIpHostName(), desSettings.getIpPort(), + desSettings.getHostName(), desSettings.getPort(), desSettings.getSecure(), + null == userName ? desSettings.getUserName() : userName, + null == password ? desSettings.getPasswordInPlainText() : password.getPlainText()); + + return this.connectionSettings; + } + + @DataBoundConstructor + public IntegritySCMChkptStep(String serverConfig) + { + this.serverConfig = serverConfig; + IntegrityConfigurable config = getConnectionSettings(); + this.userName = config.getUserName(); + this.password = Secret.fromString(config.getPassword()); + this.configPath = ""; + this.checkpointLabel = ""; + this.checkpointDesc = ""; + + LOGGER.fine("IntegritySCMChkptStep() constructed!"); + } + + @Extension(optional = true) + public static final class IntegritySCMChkptDescriptorImpl extends AbstractStepDescriptorImpl + { + + public IntegritySCMChkptDescriptorImpl() + { + super(IntegritySCMChkptStepExecution.class); + + LOGGER.fine("IntegritySCMChkptDescriptorImpl() invoked!"); + } + + @Override + public String getFunctionName() + { + return "sicheckpoint"; + } + + @Override + public String getDisplayName() + { + return "Integrity SCM Checkpoint"; + } + + /** + * Provides a list box for users to choose from a list of Integrity Server configurations + * @param configuration Simple configuration name + * @return + */ + public ListBoxModel doFillServerConfigItems(@QueryParameter String serverConfig) + { + return DescriptorImpl.INTEGRITY_DESCRIPTOR.doFillServerConfigItems(serverConfig); + } + } + + public static class IntegritySCMChkptStepExecution extends AbstractSynchronousStepExecution + { + private static final long serialVersionUID = 7564942554899422192L; + @Inject + private transient IntegritySCMChkptStep step; + @StepContextParameter + private transient Run run; + @StepContextParameter + private transient FilePath workspace; + @StepContextParameter + private transient TaskListener listener; + @StepContextParameter + private transient Launcher launcher; + + @Override + protected Void run() throws Exception + { + IntegritySCMChkptNotifierStep notifier = new IntegritySCMChkptNotifierStep(step.getConnectionSettings(), step.getConfigPath(), step.getCheckpointLabel(), step.getCheckpointDesc()); + notifier.perform(run, workspace, launcher, listener); + return null; + } + } +} \ No newline at end of file diff --git a/src/main/resources/hudson/scm/IntegritySCMChkptStep/config.jelly b/src/main/resources/hudson/scm/IntegritySCMChkptStep/config.jelly new file mode 100644 index 0000000..dd99a3b --- /dev/null +++ b/src/main/resources/hudson/scm/IntegritySCMChkptStep/config.jelly @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/hudson/scm/IntegritySCMChkptStep/help-checkpointLabel.html b/src/main/resources/hudson/scm/IntegritySCMChkptStep/help-checkpointLabel.html new file mode 100644 index 0000000..b8d1d7a --- /dev/null +++ b/src/main/resources/hudson/scm/IntegritySCMChkptStep/help-checkpointLabel.html @@ -0,0 +1,3 @@ +
+ A valid Integrity Checkpoint label string. No groovy pre-processing is applied. +
\ No newline at end of file diff --git a/src/main/resources/hudson/scm/IntegritySCMLabelStep/config.jelly b/src/main/resources/hudson/scm/IntegritySCMLabelStep/config.jelly index 54d4e9a..131724f 100644 --- a/src/main/resources/hudson/scm/IntegritySCMLabelStep/config.jelly +++ b/src/main/resources/hudson/scm/IntegritySCMLabelStep/config.jelly @@ -16,7 +16,7 @@ - + diff --git a/src/main/resources/hudson/scm/IntegritySCMLabelStep/help-checkpointLabel.html b/src/main/resources/hudson/scm/IntegritySCMLabelStep/help-checkpointLabel.html new file mode 100644 index 0000000..b8d1d7a --- /dev/null +++ b/src/main/resources/hudson/scm/IntegritySCMLabelStep/help-checkpointLabel.html @@ -0,0 +1,3 @@ +
+ A valid Integrity Checkpoint label string. No groovy pre-processing is applied. +
\ No newline at end of file diff --git a/src/main/webapp/help-checkpointDesc.html b/src/main/webapp/help-checkpointDesc.html new file mode 100644 index 0000000..1a9490e --- /dev/null +++ b/src/main/webapp/help-checkpointDesc.html @@ -0,0 +1,3 @@ +
+ Integrity Checkpoint description string +
\ No newline at end of file