Skip to content

Commit

Permalink
Fixed wording from config pages as per current review status.
Browse files Browse the repository at this point in the history
Refactored SurroundSCM & SurroundStep to use some more shared code for the config pages.
Cleaned up a number of TODO comments & warnings
  • Loading branch information
pvince committed Dec 6, 2016
1 parent 2e6b1a5 commit aeeaf16
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 86 deletions.
127 changes: 126 additions & 1 deletion src/main/java/hudson/scm/SSCMUtils.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
package hudson.scm;

import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import com.cloudbees.plugins.credentials.common.StandardUsernameListBoxModel;
import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.model.Computer;
import hudson.model.Item;
import hudson.model.Job;
import hudson.model.Node;
import hudson.scm.config.RSAKey;
import hudson.util.ListBoxModel;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.plaincredentials.FileCredentials;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.QueryParameter;

import javax.annotation.CheckForNull;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Class of basic utils for working with 'sscm://' urls.
* Class for common, shared methods used to make the Surround SCM integration function.
*/
public class SSCMUtils {
private final static String URI_FORMAT = "sscm://(.*):(.*)//(.*)//(.*)";
Expand Down Expand Up @@ -115,6 +130,12 @@ public static String getRepositoryFromURL(String URL)
return result;
}

/**
* Performs basic sscm:// url validation by attempting to match it to the URI_PATTERN variable.
*
* @param URL URL to test to see if it is valid.
* @return Returns true if the URL is valid, false if not.
*/
public static boolean validateSSCMURL(String URL)
{
if(URL == null)
Expand All @@ -132,6 +153,12 @@ public static boolean validateSSCMURL(String URL)
return result;
}

/**
* Helper function which finds the 'Node' for a provided 'workspace'
*
* @param workspace A 'workspace' that was provided by Jenkins
* @return The 'Node' that the workspace exists on. Defaults to the primary Jenkins instance.
*/
public static Node workspaceToNode(FilePath workspace)
{
Jenkins j = Jenkins.getInstance();
Expand All @@ -150,4 +177,102 @@ public static Node workspaceToNode(FilePath workspace)
return j;
}

/**
* This populates the Username//Password credential dropdown on the config page.
*
* @return Returns a list of credentials to populate the combobox with.
*/
public static ListBoxModel doFillCredentialsIdItems(@AncestorInPath Job<?,?> owner, @QueryParameter String source)
{
if(owner == null || !owner.hasPermission(Item.EXTENDED_READ)) {
return new ListBoxModel();
}
return new StandardUsernameListBoxModel().withEmptySelection().withAll(availableCredentials(owner, new EnvVars().expand(source)));
}
/**
* This populates the rsaKeyFileId dropdown with a list of 'FileCredentials' that could be used.
*
* @return Returns a list of FileCredential objects that have been configured.
*/
public static ListBoxModel doFillRsaKeyFileIdItems(@AncestorInPath Job<?, ?> owner, @QueryParameter String source) {
if(owner == null || !owner.hasPermission(Item.EXTENDED_READ)) {
return new ListBoxModel();
}
return new StandardListBoxModel().withEmptySelection().withAll(availableFileCredentials(owner, new EnvVars().expand(source)));
}

/**
* Uses the {@link CredentialsProvider} to lookup {@link StandardUsernameCredentials} which will be used to populate
* the username // password dropdown box.
*
* @param owner Job that this is being performed for
* @param source A.... source?
* @return Returns a list of {@link StandardUsernameCredentials} which can be thrown into a
* {@link StandardUsernameListBoxModel}
*/
public static List<? extends StandardUsernameCredentials> availableCredentials(Job<?, ?> owner, String source) {
return CredentialsProvider.lookupCredentials(StandardUsernameCredentials.class, owner, null, URIRequirementBuilder.fromUri(source).build());
}

/**
* Uses the {@link CredentialsProvider} to lookup {@link FileCredentials} which will be used to populate
* the username // password dropdown box.
*
* @param owner A.... Jobish object?
* @param source A.... source?
* @return Returns a list of {@link FileCredentials} which can be thrown into a {@link StandardListBoxModel}
*/
public static List<? extends FileCredentials> availableFileCredentials(Job<?, ?> owner, String source) {
return CredentialsProvider.lookupCredentials(FileCredentials.class, owner, null, URIRequirementBuilder.fromUri(source).build());
}


/**
* Looks up a specific credential based on the credential ID.
* @param owner Used during credential lookup from the CredentialProvider
* @param env Used to generate a 'Source' string
* @param server Used to generate the source string
* @param port Used to generate the source string
* @param credentialsId ID string of the credential to lookup.
* @return Returns the {@link StandardUsernameCredentials} matching the specified credentialsID, or null
*/
@CheckForNull
public static StandardUsernameCredentials getCredentials(Job<?,?> owner, EnvVars env,
String server, String port, String credentialsId) {
if(credentialsId != null) {
List<? extends StandardUsernameCredentials> credentials = availableCredentials(owner, env.expand(String.format("sscm://%s:%s", server, port)));

for (StandardUsernameCredentials c : credentials) {
if(c.getId().equals(credentialsId)) {
return c;
}
}
}
return null;
}

/**
* Looks up a specific file credential based on its ID.
* @param owner Used during credential lookup from the CredentialProvider
* @param env Used to generate a 'Source' string
* @param server Used to generate the source string
* @param port Used to generate the source string
* @param rsaKey This will only work if this is an rsaKey with a {@link RSAKey.Type} of "ID"
* @return Returns the fileCredential specified in the {@link RSAKey} 'value', or null
*/
@CheckForNull
public static FileCredentials getFileCredentials(Job<?,?> owner, EnvVars env,
String server, String port, RSAKey rsaKey) {

if(rsaKey != null && rsaKey.getRsaKeyType() == RSAKey.Type.ID) {
List<? extends FileCredentials> credentials = availableFileCredentials(owner, env.expand(String.format("sscm://%s:%s", server, port)));

for(FileCredentials fc : credentials) {
if(fc.getId().equals(rsaKey.getRsaKeyValue())) {
return fc;
}
}
}
return null;
}
}
43 changes: 4 additions & 39 deletions src/main/java/hudson/scm/SurroundSCM.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package hudson.scm;

import com.cloudbees.plugins.credentials.CredentialsNameProvider;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import com.cloudbees.plugins.credentials.common.StandardUsernameListBoxModel;
import com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder;
import hudson.*;
import hudson.model.*;
import hudson.scm.config.RSAKey;
Expand All @@ -24,7 +20,6 @@
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -154,13 +149,6 @@ public SurroundSCM(String rsaKeyPath, String server, String serverPort, String u
@Deprecated
public SurroundSCM() { }

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

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

/**
* @deprecated Use getCredentialsId instead of this function. This was left in place to support legacy
Expand Down Expand Up @@ -640,9 +628,6 @@ public SurroundTool resolveSscmTool(TaskListener listener) {
sscm = SurroundTool.getDefaultInstallation();
}
}
// TODO_PTV: Is it safe to save off the tool name so we don't need to perform a lookup again?
//if(sscm != null)
// sscm_tool_name = sscm.getName();

return sscm;
}
Expand Down Expand Up @@ -730,26 +715,12 @@ private String getServerConnectionArgument(Job<?, ?> owner, EnvVars env, FilePat

@CheckForNull
private StandardUsernameCredentials getCredentials(Job<?, ?> owner, EnvVars env) {
if (credentialsId != null) {
for (StandardUsernameCredentials c : availableCredentials(owner, env.expand("sscm://" + server + ":" + serverPort))) { // TODO: This seems like a royal hack.
if (c.getId().equals(credentialsId)) {
return c;
}
}
}
return null;
return SSCMUtils.getCredentials(owner, env, server, serverPort, credentialsId);
}

@CheckForNull
private FileCredentials getFileCredentials(Job<?, ?> owner, EnvVars env) {
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(rsaKey.getRsaKeyValue())) {
return fc;
}
}
}
return null;
return SSCMUtils.getFileCredentials(owner, env, server, serverPort, rsaKey);
}

/**
Expand Down Expand Up @@ -849,10 +820,7 @@ public SCM newInstance(StaplerRequest req, JSONObject formData)
*/
@SuppressWarnings("unused") // This is called via Stapler
public ListBoxModel doFillCredentialsIdItems(@AncestorInPath Job<?, ?> owner, @QueryParameter String source) {
if (owner == null || !owner.hasPermission(Item.EXTENDED_READ)) {
return new ListBoxModel();
}
return new StandardUsernameListBoxModel().withEmptySelection().withAll(availableCredentials(owner, new EnvVars().expand(source)));
return SSCMUtils.doFillCredentialsIdItems(owner, source);
}

/**
Expand All @@ -862,10 +830,7 @@ public ListBoxModel doFillCredentialsIdItems(@AncestorInPath Job<?, ?> owner, @Q
*/
@SuppressWarnings("unused") // This is called via Stapler
public ListBoxModel doFillRsaKeyFileIdItems(@AncestorInPath Job<?, ?> owner, @QueryParameter String source) {
if (owner == null || !owner.hasPermission(Item.EXTENDED_READ)) {
return new ListBoxModel();
}
return new StandardListBoxModel().withEmptySelection().withAll(availableFileCredentials(owner, new EnvVars().expand(source)));
return SSCMUtils.doFillRsaKeyFileIdItems(owner, source);
}

/**
Expand Down
43 changes: 8 additions & 35 deletions src/main/java/hudson/scm/SurroundStep.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package hudson.scm;

import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import com.cloudbees.plugins.credentials.common.StandardUsernameListBoxModel;
import hudson.EnvVars;
import hudson.Extension;
import hudson.Util;
import hudson.model.Item;
import hudson.model.Job;
import hudson.scm.config.RSAKey;
import hudson.util.ListBoxModel;
Expand All @@ -22,8 +19,6 @@
import javax.annotation.Nonnull;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Runs Surround SCM using {@link SurroundSCM}
Expand Down Expand Up @@ -115,7 +110,6 @@ public String getRsaKeyFilePath()
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;
}

Expand All @@ -125,40 +119,25 @@ public String getRsaKeyFileId() {
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;
}

// TODO: Somehow make this a shared function between SurroundSCM and SurroundStep
@Exported
@CheckForNull
public StandardUsernameCredentials getCredentials(Job<?,?> owner, EnvVars env) {
String server = SSCMUtils.getServerFromURL(url);
String port = SSCMUtils.getPortFromURL(url);

if(credentialsId != null) {
for (StandardUsernameCredentials c : SurroundSCM.availableCredentials(owner, env.expand("sscm://" + server + ":" + port))) { // TODO: This seems like a royal hack.
if(c.getId().equals(credentialsId)) {
return c;
}
}
}
return null;
return SSCMUtils.getCredentials(owner, env, server, port, credentialsId);
}

// TODO: Somehow make this a shared function between SurroundSCM and SurroundStep
@Exported
@CheckForNull
public FileCredentials getFileCredentials(Job<?,?> owner, EnvVars env) {
String server = SSCMUtils.getServerFromURL(url);
String port = SSCMUtils.getPortFromURL(url);

if(rsaKey != null && rsaKey.getRsaKeyType() == RSAKey.Type.ID) {
for(FileCredentials fc : SurroundSCM.availableFileCredentials(owner, env.expand(String.format("sscm://%s:%s", server, port)))) { // TODO: This seems like a royal hack
if(fc.getId().equals(rsaKey.getRsaKeyValue())) {
return fc;
}
}
}
return null;
return SSCMUtils.getFileCredentials(owner, env, server ,port, rsaKey);
}

@Extension
Expand All @@ -174,31 +153,25 @@ public String getDisplayName() {
return "Surround SCM";
}

// TODO: Somehow make this a shared function between SurroundSCM and SurroundStep
/**
* This populates the Username//Password credential dropdown on the config page.
*
* @return Returns a list of credentials to populate the combobox with.
*/
@Exported
public ListBoxModel doFillCredentialsIdItems(@AncestorInPath Job<?,?> owner, @QueryParameter String source)
{
if(owner == null || !owner.hasPermission(Item.EXTENDED_READ)) {
return new ListBoxModel();
}
return new StandardUsernameListBoxModel().withEmptySelection().withAll(SurroundSCM.availableCredentials(owner, new EnvVars().expand(source)));
return SSCMUtils.doFillCredentialsIdItems(owner, source);
}

// TODO: Somehow make this a shared function between SurroundSCM and SurroundStep
/**
* This populates the rsaKeyFileId dropdown with a list of 'FileCredentials' that could be used.
*
* @return Returns a list of FileCredential objects that have been configured.
*/
@Exported
public ListBoxModel doFillRsaKeyFileIdItems(@AncestorInPath Job<?, ?> owner, @QueryParameter String source) {
if(owner == null || !owner.hasPermission(Item.EXTENDED_READ)) {
return new ListBoxModel();
}
return new StandardListBoxModel().withEmptySelection().withAll(SurroundSCM.availableFileCredentials(owner, new EnvVars().expand(source)));
return SSCMUtils.doFillRsaKeyFileIdItems(owner, source);
}
}
}
6 changes: 3 additions & 3 deletions src/main/resources/hudson/scm/SurroundSCM/config.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
<c:select />
</f:entry>

<f:dropdownList name="rsaKey" title="${%Use RSA key file}">
<f:dropdownListBlock title="${%- None -}" value="0" selected="${!instance.hasRsaKeyConfigured()}" />
<f:dropdownList name="rsaKey" title="${%RSA key file}">
<f:dropdownListBlock title="${%- none -}" value="0" selected="${!instance.hasRsaKeyConfigured()}" />
<f:dropdownListBlock title="${%Path to RSA key file}" value="2" selected="${instance.isUsingRsaKeyPath()}">
<f:entry field="rsaKeyFilePath">
<f:entry title="${%Path}" field="rsaKeyFilePath">
<f:textbox />
</f:entry>
</f:dropdownListBlock>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div>
Enter the full path the Surround SCM connection RSA key file. Ex: C:\SurroundRSAKeyFile.xml
Enter the full path to the RSA key file for the Surround SCM connection. Example: C:\SurroundRSAKeyFile.xml
</div>

0 comments on commit aeeaf16

Please sign in to comment.