Skip to content

Commit

Permalink
Merge pull request #4 from vistaprint/master
Browse files Browse the repository at this point in the history
Issue 5: Allow passing in of variables to octopus deployment
  • Loading branch information
badriance authored Jul 21, 2016
2 parents a98f993 + 43d01c3 commit 9820ed7
Show file tree
Hide file tree
Showing 16 changed files with 288 additions and 94 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/octopusdeploy/api/DeploymentProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ public DeploymentProcess(String id, String projectId, Set<DeploymentProcessStep>
this.projectId = projectId;
this.steps = steps;
}

@Override
public String toString() {
return "DeploymentProcess [id=" + id + ", projectId=" + projectId + ", steps=" + steps + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ public DeploymentProcessStep(String id, String name, Set<DeploymentProcessStepAc
this.name = name;
this.actions = actions;
}

@Override
public String toString() {
return "DeploymentProcessStep [id=" + id + ", name=" + name + ", actions=" + actions + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ public DeploymentProcessStepAction(String id, String name, String actionType, Ma
this.actionType = actionType;
this.properties = properties;
}

@Override
public String toString() {
return "DeploymentProcessStepAction [id=" + id + ", name=" + name + ", actionType=" + actionType + ", properties=" + properties + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ public DeploymentProcessTemplate(String id, String projectId, Set<SelectedPackag
this.projectId = projectId;
this.packages = packages;
}

@Override
public String toString() {
return "DeploymentProcessTemplate [id=" + id + ", projectId=" + projectId + ", packages=" + packages + "]";
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/octopusdeploy/api/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ public Environment(String id, String name, String description)
this.name = name;
this.description = description;
}

@Override
public String toString() {
return "Environment [name=" + name + ", id=" + id + ", description=" + description + "]";
}
}
181 changes: 92 additions & 89 deletions src/main/java/com/octopusdeploy/api/ErrorParser.java
Original file line number Diff line number Diff line change
@@ -1,89 +1,92 @@
package com.octopusdeploy.api;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Parses errors from Octopus html/javascript responses
* @author jlabroad
*/
public class ErrorParser {

/** Find a group of messages in the format: "Errors":["error message 1", "error message 2", "error message 3"] */
protected static final String errDetailsOutsideString = "(?:\\\"Errors\\\")(?:[^\\[]\\[)(?<fullDetailString>[^\\]]+)";
protected static Pattern errDetailsOutsidePattern = Pattern.compile(errDetailsOutsideString);

/** Parse each individual message from "error message 1", "error message 2", "error message 3" */
protected static final String errDetailsInsideString = "(?:\\\")(?<singleError>[^\\\"]+)*(?:\\\")";
protected static Pattern errDetailsInsidePattern = Pattern.compile(errDetailsInsideString);

/**
* Parse any errors from the returned HTML/javascript from Octopus
* @param response The Octopus html response that may include error data
* @return A list of error strings
*/
public static String getErrorsFromResponse(String response) {
List<String> errorStrings = new ArrayList<String>();

//Get the error title and main message
String errorTitle = getErrorDataByFieldName("title", response);
if (!errorTitle.isEmpty()) {
errorStrings.add(String.format("%s", errorTitle));
}

//Get the error details
String errorDetailMessage = getErrorDataByFieldName("ErrorMessage", response);
if (!errorDetailMessage.isEmpty()) {
errorStrings.add("\t" + errorDetailMessage);
}
errorStrings.addAll(getErrorDetails(response));

String errorMsg = "";
for (String err : errorStrings) {
errorMsg += String.format("%s\n", err);
}

return errorMsg;
}

/**
* Grabs a single error data field from an Octopus html response
* @param fieldName The field name of the error string
* @param response The field data
* @return The error data
*/
protected static String getErrorDataByFieldName(String fieldName, String response) {
//Get the next string in script parameter list: "var errorData = {<fieldName>:"Field value", ...
final String patternString = String.format("(?:errorData.+)(?:\"%s\")(?:[:\\[\"]+)(?<fieldValue>[^\"]+)", fieldName);

Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(response);
String errData = "";
if (matcher.find() && matcher.groupCount() > 0) {
errData = matcher.group("fieldValue");
}
return errData;
}

/**
* Returns a list of "Errors" values from Octopus html response
* @param response The full Octopus html response
* @return
*/
protected static List<String> getErrorDetails(String response) {
List<String> errorList = new ArrayList<String>();

Matcher m = errDetailsOutsidePattern.matcher(response);
if (m.find() && m.groupCount() > 0) {
//Split up the list of error messages into individual messages
String errors = m.group("fullDetailString");
m = errDetailsInsidePattern.matcher(errors);
while (m.find() && m.groupCount() > 0) {
errorList.add("\t" + m.group("singleError"));
}
}
return errorList;
}
}
package com.octopusdeploy.api;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringEscapeUtils;

/**
* Parses errors from Octopus html/javascript responses
* @author jlabroad
*/
public class ErrorParser {

/** Find a group of messages in the format: "Errors":["error message 1", "error message 2", "error message 3"] */
protected static final String errDetailsOutsideString = "(?:\\\"Errors\\\")(?:[^\\[]\\[)(?<fullDetailString>[^\\]]+)";
protected static Pattern errDetailsOutsidePattern = Pattern.compile(errDetailsOutsideString);

/** Parse each individual message from "error message 1", "error message 2", "error message 3" */
protected static final String errDetailsInsideString = "(?:\\\")(?<singleError>[^\\\"]+)*(?:\\\")";
protected static Pattern errDetailsInsidePattern = Pattern.compile(errDetailsInsideString);

/**
* Parse any errors from the returned HTML/javascript from Octopus
* @param response The Octopus html response that may include error data
* @return A list of error strings
*/
public static String getErrorsFromResponse(String response) {
List<String> errorStrings = new ArrayList<String>();

//Get the error title and main message
String errorTitle = getErrorDataByFieldName("title", response);
if (!errorTitle.isEmpty()) {
errorStrings.add(String.format("%s", errorTitle));
}

//Get the error details
String errorDetailMessage = getErrorDataByFieldName("ErrorMessage", response);
if (!errorDetailMessage.isEmpty()) {
errorStrings.add("\t" + errorDetailMessage);
}
errorStrings.addAll(getErrorDetails(response));

String errorMsg = "";
for (String err : errorStrings) {
errorMsg += String.format("%s\n", err);
}

return errorMsg;
}

/**
* Grabs a single error data field from an Octopus html response
* @param fieldName The field name of the error string
* @param response The field data
* @return The error data
*/
protected static String getErrorDataByFieldName(String fieldName, String response) {
//Get the next string in script parameter list: "var errorData = {<fieldName>:"Field value", ...
final String patternString = String.format("(?:errorData.+)(?:\"%s\")(?:[:\\[\"]+)(?<fieldValue>[^\"]+)", fieldName);

Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(response);
String errData = "";
if (matcher.find() && matcher.groupCount() > 0) {
errData = matcher.group("fieldValue");
}
return errData;
}

/**
* Returns a list of "Errors" values from Octopus html response
* @param response The full Octopus html response
* @return
*/
protected static List<String> getErrorDetails(String response) {
List<String> errorList = new ArrayList<String>();

Matcher m = errDetailsOutsidePattern.matcher(response);
if (m.find() && m.groupCount() > 0) {
//Split up the list of error messages into individual messages
String errors = m.group("fullDetailString");
m = errDetailsInsidePattern.matcher(errors);
while (m.find() && m.groupCount() > 0) {
String singleError = StringEscapeUtils.unescapeJava(m.group("singleError"));
errorList.add("\t" + singleError);
}
}
return errorList;
}
}
66 changes: 65 additions & 1 deletion src/main/java/com/octopusdeploy/api/OctopusApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,34 @@ public String createRelease(String project, String releaseVersion, String releas
* @throws IOException
*/
public String executeDeployment(String releaseId, String environmentId) throws IOException {
String json = String.format("{EnvironmentId:\"%s\",ReleaseId:\"%s\"}", environmentId, releaseId);
return executeDeployment( releaseId, environmentId, null);
}

/**
* Deploys a given release to provided environment.
* @param releaseId Release Id from Octopus to deploy.
* @param environmentId Environment Id from Octopus to deploy to.
* @param variables Variables used during deployment.
* @return the content of the web response.
* @throws IOException
*/
public String executeDeployment(String releaseId, String environmentId, Set<Variable> variables) throws IOException {

StringBuilder jsonBuilder = new StringBuilder();
jsonBuilder.append(String.format("{EnvironmentId:\"%s\",ReleaseId:\"%s\"", environmentId, releaseId));

if (variables != null && !variables.isEmpty()) {
jsonBuilder.append(",FormValues:{");
Set<String> variablesStrings = new HashSet<String>();
for (Variable v : variables) {
variablesStrings.add(String.format("\"%s\":\"%s\"", v.getId(), v.getValue()));
}
jsonBuilder.append(StringUtils.join(variablesStrings, ","));
jsonBuilder.append("}");
}
jsonBuilder.append("}");
String json = jsonBuilder.toString();

byte[] data = json.getBytes(Charset.forName(UTF8));
AuthenticatedWebClient.WebResponse response = webClient.post("api/deployments", data);
if (response.isErrorCode()) {
Expand Down Expand Up @@ -198,6 +225,43 @@ public Environment getEnvironmentByName(String name, boolean ignoreCase) throws
return null;
}

/**
* Get the variables for a combination of release and environment, return null otherwise.
* @param releaseId The id of the Release.
* @param environmentId The id of the Environment.
* @return A set of all variables for a given Release and Environment combination.
* @throws IllegalArgumentException
* @throws IOException
*/
public Set<Variable> getVariablesByReleaseAndEnvironment(String releaseId, String environmentId, Properties entryProperties) throws IllegalArgumentException, IOException {
Set<Variable> variables = new HashSet<Variable>();

AuthenticatedWebClient.WebResponse response = webClient.get("api/releases/" + releaseId + "/deployments/preview/" + environmentId);
if (response.isErrorCode()) {
throw new IOException(String.format("Code %s - %n%s", response.getCode(), response.getContent()));
}
JSONObject json = (JSONObject)JSONSerializer.toJSON(response.getContent());
JSONObject form = json.getJSONObject("Form");
if (form != null){
JSONObject formValues = form.getJSONObject("Values");
for (Object obj : form.getJSONArray("Elements")) {
JSONObject jsonObj = (JSONObject) obj;
String id = jsonObj.getString("Name");
String name = jsonObj.getJSONObject("Control").getString("Name");
String value = formValues.getString(id);

String entryValue = entryProperties.getProperty(name);
if (StringUtils.isNotEmpty(entryValue)) {
value = entryValue;
}
String description = jsonObj.getJSONObject("Control").getString("Description");
variables.add(new Variable(id, name, value, description));
}
}

return variables;
}

/**
* Get all releases for a given project from the Octopus server;
* @param projectId
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/octopusdeploy/api/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ public Project(String id, String name) {
this.id = id;
this.name = name;
}


@Override
public String toString() {
return "Project [name=" + name + ", id=" + id + "]";
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/octopusdeploy/api/Release.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ public Release(String id, String projectId, String releaseNotes, String version)
this.releaseNotes = releaseNotes;
this.version = version;
}

@Override
public String toString() {
return "Release [id=" + id + ", projectId=" + projectId + ", releaseNotes=" + releaseNotes + ", version=" + version + "]";
}

}
6 changes: 6 additions & 0 deletions src/main/java/com/octopusdeploy/api/SelectedPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ public SelectedPackage(String stepName, String version) {
this.stepName = stepName;
this.version = version;
}

@Override
public String toString() {
return "SelectedPackage [stepName=" + stepName + ", version=" + version + "]";
}

}
6 changes: 6 additions & 0 deletions src/main/java/com/octopusdeploy/api/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ public Task(String id, String name, String description, String state, boolean is
this.state = state;
this.isCompleted = isCompleted;
}

@Override
public String toString() {
return "Task [id=" + id + ", name=" + name + ", description=" + description + ", state=" + state + ", isCompleted=" + isCompleted + "]";
}

}
40 changes: 40 additions & 0 deletions src/main/java/com/octopusdeploy/api/Variable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.octopusdeploy.api;

/**
* Represents a variable that will be passed to a deployment.
*/
public class Variable {
private final String name;
public String getName() {
return name;
}

private final String value;
public String getValue() {
return value;
}

private final String id;
public String getId() {
return id;
}

private final String description;
public String getDescription() {
return description;
}

public Variable(String id, String name, String value, String description)
{
this.id = id;
this.name = name;
this.value = value;
this.description = description;
}

@Override
public String toString() {
return "Variable [name=" + name + ", value=" + value + ", id=" + id + ", description=" + description + "]";
}

}
Loading

0 comments on commit 9820ed7

Please sign in to comment.