forked from nmusaelian-rally/rally-java-rest-apps
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCreateMilestones.java
More file actions
132 lines (117 loc) · 5 KB
/
CreateMilestones.java
File metadata and controls
132 lines (117 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.JsonObject;
import com.rallydev.rest.RallyRestApi;
import com.rallydev.rest.request.QueryRequest;
import com.rallydev.rest.response.QueryResponse;
import com.rallydev.rest.util.Fetch;
import com.rallydev.rest.util.Ref;
import com.rallydev.rest.request.CreateRequest;
import com.rallydev.rest.response.CreateResponse;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class CreateMilestones {
public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException {
//start main thread
String host = "https://rally1.rallydev.com";
String apiKey = "_abc123";
String applicationName = "Nick:createMilestones";
RallyRestApi restApi = new RallyRestApi(new URI(host), apiKey);
restApi.setApplicationName(applicationName);
String workspaceRef = "/workspace/1234";
List<String> projects = new ArrayList<String>();
QueryRequest projectsRequest = new QueryRequest("Project");
projectsRequest.setWorkspace(workspaceRef);
//Workspace cannot be used in queries, this will not work:
//projectsRequest.setQueryFilter(new QueryFilter("Workspace", "=", workspaceRef));
projectsRequest.setFetch(new Fetch("ObjectID", "Children"));
QueryResponse projectsResponse = restApi.query(projectsRequest);
int numberOfProjects = projectsResponse.getTotalResultCount();
System.out.println(numberOfProjects);
if (numberOfProjects > 0) {
for (int i = 0; i < numberOfProjects; i++) {
JsonObject projectObject = projectsResponse.getResults().get(i).getAsJsonObject();
if(projectObject.getAsJsonObject("Children").getAsJsonObject().get("Count").getAsInt() > 0){
String projectRef = projectObject.get("ObjectID").getAsString();
projects.add(projectRef);
}
}
}
System.out.println("projects " + projects);
int numberOfParnetProjects = projects.size();
class MyRunnable implements Runnable {
private String oid;
public MyRunnable(String oid) {
this.oid = oid;
}
public void run() {
try{
createReleases(this.oid);
}
catch (Exception e){
e.printStackTrace();
}
}
}
Thread [] threads = new Thread[numberOfParnetProjects];
for (int i = 0; i < threads.length; i++)
{
threads[i] = new Thread(new MyRunnable(projects.get(i)));
threads[i].start();
threads[i].join();
}
if (restApi != null) {
restApi.close();
}
}
private static void createReleases(String oid) throws Exception
{
try{
//start sub threads
String host = "https://rally1.rallydev.com";
String apiKey = "_abc123";
String applicationName = "Nick:createMilestones " + oid;
RallyRestApi restApi = new RallyRestApi(new URI(host), apiKey);
restApi.setApplicationName(applicationName);
String workspaceRef = "/workspace/1234";
String projectRef = "/project/" + oid;
String name = "test milestone " + oid;
JsonObject milestone = new JsonObject();
milestone.addProperty("Name", name);
milestone.addProperty("TargetProject", projectRef);
milestone.addProperty("TargetDate", getDate());
CreateRequest createRequest = new CreateRequest("milestone", milestone);
createRequest.addParam("workspace", workspaceRef);
CreateResponse createResponse = restApi.create(createRequest);
if (createResponse.wasSuccessful()) {
String milestoneRef = Ref.getRelativeRef(createResponse.getObject().get("_ref").getAsString());
System.out.println("Milestone..." + milestoneRef);
}
else{
String[] createErrors;
createErrors = createResponse.getErrors();
System.out.println("Error occurred creating a milestone: ");
for (int i=0; i<createErrors.length;i++) {
System.out.println(createErrors[i]);
}
}
}
finally {
System.out.println("done with thread " + oid + "\n--------------------------------------------------------");
}
}
private static String getDate()
{
int x = 30;
Calendar cal = GregorianCalendar.getInstance();
cal.add( Calendar.DAY_OF_YEAR, x);
Date fewDaysForward = cal.getTime();
SimpleDateFormat iso = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");
return iso.format(fewDaysForward);
}
}