Skip to content

Commit

Permalink
Implemented changes to create a 'nice' config page while also maintai…
Browse files Browse the repository at this point in the history
…ning compatibility with the Snippet Generator for the 'checkout' and new 'sscm' command
  • Loading branch information
pvince committed Nov 18, 2016
1 parent dcee58d commit 5026866
Show file tree
Hide file tree
Showing 5 changed files with 334 additions and 93 deletions.
148 changes: 101 additions & 47 deletions src/main/java/hudson/scm/SurroundSCM.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder;
import hudson.*;
import hudson.model.*;
import hudson.scm.config.RSAKey;
import hudson.util.ArgumentListBuilder;
import hudson.util.ListBoxModel;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.jenkinsci.plugins.plaincredentials.FileCredentials;
import org.kohsuke.stapler.*;
import org.kohsuke.stapler.export.Exported;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -84,23 +86,34 @@ public ListBoxModel doFillRsaKeyFileIdItems(@AncestorInPath Job<?, ?> owner, @Qu
}
return new StandardListBoxModel().withEmptySelection().withAll(availableFileCredentials(owner, new EnvVars().expand(source)));
}

private RSAKey getRSAKeyFromRequest(final StaplerRequest req, final JSONObject scmData) {
if(scmData.containsKey("RSAKey")) {
return req.bindJSON(RSAKey.class, scmData.getJSONObject("RSAKey"));
} else {
return null;
}
}
}
/*--------------------- END INNER CLASS ------------------------------------------------------------*/


// if there are > changesThreshold changes, that it's build now incomparable
// if there are < changesThreshold changes, but > 0 changes, then it's significant
private static transient final int changesThreshold = 1;
private static transient final int pluginVersion = 9;

// config options
private String rsaKeyPath;
private String server;
private String serverPort;
private String branch ;
private String repository;

private String credentialsId;
private String rsaKeyFileId;

private RSAKey rsaKey;
@Deprecated
private transient String rsaKeyPath;

// TODO: Review if this is needed.
private String sscm_tool_name;
Expand Down Expand Up @@ -128,8 +141,23 @@ public ListBoxModel doFillRsaKeyFileIdItems(@AncestorInPath Job<?, ?> owner, @Qu
private String password ;

//getters
public String getRsaKeyPath() {
return rsaKeyPath;
public String getRsaKeyFilePath()
{
String result = null;
if(rsaKey != null && rsaKey.getRsaKeyType() == RSAKey.Type.Path) {
result = rsaKey.getRsaKeyValue();
}
Logger.getLogger(SurroundSCM.class.toString()).log(Level.SEVERE, String.format("getRsaKeyPath - Value: [%s]", result));
return result;
}

public String getRsaKeyFileId() {
String result = null;
if(rsaKey != null && rsaKey.getRsaKeyType() == RSAKey.Type.ID) {
result = rsaKey.getRsaKeyValue();
}
Logger.getLogger(SurroundSCM.class.toString()).log(Level.SEVERE, String.format("getRsaKeyFileId - Value: [%s]", result));
return result;
}

public String getServer() {
Expand All @@ -140,10 +168,12 @@ public String getServerPort() {
return serverPort;
}

@Deprecated
public String getUserName() {
return userName;
}

@Deprecated
public String getPassword() {
return password;
}
Expand All @@ -162,7 +192,17 @@ public boolean getIncludeOutput() {

public String getCredentialsId() { return credentialsId; }

public String getRsaKeyFileId() { return rsaKeyFileId; }
public boolean hasRsaKeyConfigured() {
return rsaKey == null || rsaKey.getRsaKeyType() != RSAKey.Type.NoKey;
}

public boolean isUsingRsaKeyPath() {
return rsaKey != null && rsaKey.getRsaKeyType() == RSAKey.Type.Path;
}

public boolean isUsingRsaKeyFileId() {
return rsaKey != null && rsaKey.getRsaKeyType() == RSAKey.Type.ID;
}

//DataBoundSetters

Expand All @@ -172,12 +212,35 @@ public void setIncludeOutput(boolean includeOutput) {
}

@DataBoundSetter
public void setRsaKeyPath(String rsaKeyPath) {
this.rsaKeyPath = Util.fixEmptyAndTrim(rsaKeyPath);
public void setRsaKey(RSAKey rsaKey) {
this.rsaKey = rsaKey;
}

@Exported
public RSAKey getRsaKey() { return null; }

@Deprecated
@DataBoundSetter
public void setRsaKeyFileId(String rsaKeyFileId) { this.rsaKeyFileId = Util.fixEmptyAndTrim(rsaKeyFileId); }
public void setRsaKeyPath(String rsaKeyPath)
{
this.rsaKey = new RSAKey(RSAKey.Type.Path, rsaKeyPath);
}

@Deprecated
@Exported
public String getRsaKeyPath() { return null; }

@DataBoundSetter
public void setRsaKeyFilePath(String rsaKeyFilePath)
{
setRsaKeyPath(rsaKeyFilePath);
}

@DataBoundSetter
public void setRsaKeyFileId(String rsaKeyFileId)
{
this.rsaKey = new RSAKey(RSAKey.Type.ID, rsaKeyFileId);
}

/**
* Singleton descriptor.
Expand All @@ -189,50 +252,35 @@ public void setRsaKeyPath(String rsaKeyPath) {
private static final String SURROUND_DATETIME_FORMAT_STR_2 = "yyyyMMddHH:mm:ss";

@DataBoundConstructor
public SurroundSCM(String server, String serverPort, String branch, String repository, String credentialsId)
{
public SurroundSCM(String server, String serverPort, String branch, String repository, String credentialsId) {
this.rsaKeyPath = null;
this.rsaKeyFileId = null;
this.rsaKey = null;

this.server = Util.fixEmptyAndTrim(server);
this.serverPort = Util.fixEmptyAndTrim(serverPort);
this.branch = Util.fixEmptyAndTrim(branch);
this.repository = Util.fixEmptyAndTrim(repository);
this.credentialsId = Util.fixEmptyAndTrim(credentialsId);

this.bIncludeOutput = true; // Leaving this here for future functionality.

this.userName = null;
this.password = null;
this.surroundSCMExecutable = null;
}

@Deprecated
public SurroundSCM(String server, String serverPort, String userName,
String password, String branch, String repository)
{
this(server, serverPort, branch, repository, null);

this.userName = Util.fixEmptyAndTrim(userName);
this.password = Util.fixEmptyAndTrim(password);
}

@Deprecated
public SurroundSCM(String rsaKeyPath, String server, String serverPort, String userName,
String password, String branch, String repository)
{
this(server, serverPort, branch, repository, null);
this.rsaKeyPath = Util.fixEmptyAndTrim(rsaKeyPath);
this.userName = Util.fixEmptyAndTrim(userName);
this.password = Util.fixEmptyAndTrim(password);
}

@Deprecated
/**
* @deprecated as of release v10, Significant updates to the Jenkins integration, including:
* - Switched to @DataBoundSetter's for optional parameters
* - Switched to Credential storage for usernames & rsakeys
* - Updated the config page, which necessitated data structure changes.
*/
public SurroundSCM(String rsaKeyPath, String server, String serverPort, String userName,
String password, String branch, String repository, String surroundSCMExecutable,
boolean includeOutput)
{
this(server, serverPort, branch, repository, null);
this.rsaKeyPath = Util.fixEmptyAndTrim(rsaKeyPath);
this.rsaKey = new RSAKey(RSAKey.Type.Path, rsaKeyPath);
this.userName = Util.fixEmptyAndTrim(userName);
this.password = Util.fixEmptyAndTrim(password);
this.surroundSCMExecutable = Util.fixEmptyAndTrim(surroundSCMExecutable);
Expand Down Expand Up @@ -648,7 +696,7 @@ else if(userName != null && !userName.isEmpty())
*/
private String getServerConnectionArgument(Job<?, ?> owner, EnvVars env, FilePath workspace) {
String result = null;
String rsaKeyPath = getRSAKeyFilePath(owner, env, workspace);
String rsaKeyPath = getRemotePathForRSAKeyFile(owner, env, workspace);
if(rsaKeyPath != null && !rsaKeyPath.isEmpty())
{
result = String.format("-z%s", rsaKeyPath);
Expand All @@ -661,11 +709,11 @@ private String getServerConnectionArgument(Job<?, ?> owner, EnvVars env, FilePat
return result;
}

private static List<? extends StandardUsernameCredentials> availableCredentials(Job<?,?> owner, String source) {
/*package*/ static List<? extends StandardUsernameCredentials> availableCredentials(Job<?,?> owner, String source) {
return CredentialsProvider.lookupCredentials(StandardUsernameCredentials.class, owner, null, URIRequirementBuilder.fromUri(source).build());
}

private static List<? extends FileCredentials> availableFileCredentials(Job<?,?> owner, String source) {
/*package*/ static List<? extends FileCredentials> availableFileCredentials(Job<?,?> owner, String source) {
return CredentialsProvider.lookupCredentials(FileCredentials.class, owner, null, URIRequirementBuilder.fromUri(source).build());
}

Expand All @@ -683,9 +731,9 @@ public StandardUsernameCredentials getCredentials(Job<?,?> owner, EnvVars env) {

@CheckForNull
public FileCredentials getFileCredentials(Job<?,?> owner, EnvVars env) {
if(rsaKeyFileId != null) {
if(rsaKey != null && rsaKey.getRsaKeyType() == RSAKey.Type.ID) {
for(FileCredentials fc : availableFileCredentials(owner, env.expand(String.format("sscm://%s:%s", server, serverPort)))) { // TODO: This seems like a royal hack
if(fc.getId().equals(rsaKeyFileId)) {
if(fc.getId().equals(rsaKey.getRsaKeyValue())) {
return fc;
}
}
Expand Down Expand Up @@ -714,7 +762,7 @@ private String populateRSAKeyFile(Job<?, ?> owner, EnvVars env, @Nullable FilePa
} catch (IOException e) {
Logger.getLogger(SurroundSCM.class.toString()).log(Level.SEVERE,
String.format("Found RSA Key File by ID [%s], however failed to retrieve file to destination machine.\n" +
"Error Message: %s", rsaKeyFileId, e.toString()));
"Error Message: %s", rsaKey != null ? rsaKey.getRsaKeyValue() : "rsaKey object was null?", e.toString()));
} catch (InterruptedException e) {
Logger.getLogger(SurroundSCM.class.toString()).log(Level.SEVERE,
String.format("Exception while attempting to retrieve RSA Key File to destination machine. Error message: %s", e.toString()));
Expand All @@ -734,17 +782,23 @@ private String populateRSAKeyFile(Job<?, ?> owner, EnvVars env, @Nullable FilePa
* @param workspace Used as a destination for any RSA Key File retrieved from fileCredentials
* @return Returns either the path to an RSA Key File, or null indicating no RSA Key File.
*/
private String getRSAKeyFilePath(Job<?, ?> owner, EnvVars env, FilePath workspace)
private String getRemotePathForRSAKeyFile(Job<?, ?> owner, EnvVars env, FilePath workspace)
{
String result = null;
if(rsaKeyFileId != null && !rsaKeyFileId.isEmpty())
if(rsaKey != null)
{
result = populateRSAKeyFile(owner, env, workspace);
}

if(result == null && rsaKeyPath != null && !rsaKeyPath.isEmpty())
{
result = rsaKeyPath;
switch (rsaKey.getRsaKeyType())
{
case ID:
result = populateRSAKeyFile(owner, env, workspace);
break;
case Path:
result = rsaKey.getRsaKeyValue();
break;
case NoKey:
default:
result = null;
}
}

return result;
Expand Down

0 comments on commit 5026866

Please sign in to comment.