-
-
Save markwilliams970/0ee60296d012757db2f1 to your computer and use it in GitHub Desktop.
RestExample_AddBuild
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.gson.JsonArray; | |
import com.google.gson.JsonElement; | |
import com.google.gson.JsonObject; | |
import com.rallydev.rest.RallyRestApi; | |
import com.rallydev.rest.request.CreateRequest; | |
import com.rallydev.rest.request.DeleteRequest; | |
import com.rallydev.rest.request.GetRequest; | |
import com.rallydev.rest.request.QueryRequest; | |
import com.rallydev.rest.request.UpdateRequest; | |
import com.rallydev.rest.response.CreateResponse; | |
import com.rallydev.rest.response.DeleteResponse; | |
import com.rallydev.rest.response.GetResponse; | |
import com.rallydev.rest.response.QueryResponse; | |
import com.rallydev.rest.response.UpdateResponse; | |
import com.rallydev.rest.util.Fetch; | |
import com.rallydev.rest.util.QueryFilter; | |
import com.rallydev.rest.util.Ref; | |
import java.io.IOException; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import java.text.DateFormat; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
public class RestExample_AddBuild { | |
public static void main(String[] args) throws URISyntaxException, IOException { | |
// Rally parameters | |
String rallyURL = new String("https://rally1.rallydev.com"); | |
String wsapiVersion = new String("1.42"); | |
String userName = new String("user@company.com"); | |
String userPassword = new String("topsecret"); | |
String applicationName = new String("RestExample_AddBuild"); | |
// Workspace and Project refs | |
// Your Workspace ObjectID HERE in place of 12345678910: | |
String workspaceRef = new String("/workspace/12345678910"); | |
// Your Project ObjectID HERE in place of 12345678911: | |
String projectRef = new String("/project/12345678911"); | |
// Create and configure a new instance of RallyRestApi | |
RallyRestApi restApi = new RallyRestApi( | |
new URI(rallyURL), | |
userName, | |
userPassword | |
); | |
restApi.setWsapiVersion(wsapiVersion); | |
restApi.setApplicationName(applicationName); | |
// Query for Build Definition which pertains to this Build | |
// Your BuildDefinition Name HERE in place of "Jenkins1" | |
String buildDefinitionName = "Jenkins1"; | |
QueryRequest buildDefinitionRequest = new QueryRequest("BuildDefinition"); | |
buildDefinitionRequest.setFetch(new Fetch("ObjectID","Name","Description","Project","Builds")); | |
buildDefinitionRequest.setWorkspace(workspaceRef); | |
buildDefinitionRequest.setProject(projectRef); | |
buildDefinitionRequest.setQueryFilter(new QueryFilter("Name", "=", buildDefinitionName)); | |
QueryResponse buildDefinitionQueryResponse = restApi.query(buildDefinitionRequest); | |
JsonObject buildDefinitionJsonObject = buildDefinitionQueryResponse.getResults().get(0).getAsJsonObject(); | |
JsonArray buildsJsonArray = buildDefinitionJsonObject.getAsJsonArray("Builds"); | |
String buildDefinitionRef = buildDefinitionJsonObject.get("_ref").toString(); | |
System.out.println("buildDefinitionRef: " + buildDefinitionRef); | |
System.out.println("Number of existing Builds: " + buildsJsonArray.size()); | |
// Start date/time of build | |
Date myDate = new Date(); | |
DateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ"); | |
// Setup JsonObject for new Build | |
JsonObject newBuild = new JsonObject(); | |
newBuild.addProperty("Workspace", workspaceRef); | |
newBuild.addProperty("Duration", 0.75); | |
newBuild.addProperty("Message", "Master 4683 Success"); | |
newBuild.addProperty("Start", isoFormat.format(myDate)); | |
newBuild.addProperty("Status", "SUCCESS"); | |
newBuild.addProperty("Number", "4683"); | |
newBuild.addProperty("Uri", "http://jenkins-build:8080/hudson/view/master/job/master-deploy/4683/"); | |
newBuild.addProperty("BuildDefinition", buildDefinitionRef); | |
try { | |
//Create the Build | |
System.out.println("Creating Build..."); | |
CreateRequest createRequest = new CreateRequest("Build", newBuild); | |
System.out.println(createRequest.getBody()); | |
CreateResponse createResponse = restApi.create(createRequest); | |
if (createResponse.wasSuccessful()) { | |
System.out.println(String.format("Created %s", createResponse.getObject().get("_ref").getAsString())); | |
String[] warningList; | |
warningList = createResponse.getWarnings(); | |
for (int i=0;i<warningList.length;i++) { | |
System.out.println(warningList[i]); | |
} | |
} else { | |
String[] errorList; | |
errorList = createResponse.getErrors(); | |
for (int i=0;i<errorList.length;i++) { | |
System.out.println(errorList[i]); | |
} | |
} | |
} finally { | |
//Release all resources | |
restApi.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment