forked from nmusaelian-rally/rally-java-rest-apps
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathGetTotalCountOfStoriesInSub.java
More file actions
132 lines (110 loc) · 5.45 KB
/
GetTotalCountOfStoriesInSub.java
File metadata and controls
132 lines (110 loc) · 5.45 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 com.google.gson.JsonArray;
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.request.GetRequest;
import com.rallydev.rest.response.GetResponse;
import com.rallydev.rest.util.Fetch;
public class GetTotalCountOfStoriesInSub {
public static int totalNumOfStories = 0;
public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException {
String host = "https://rally1.rallydev.com";
String apiKey = "_abc123";
String applicationName = "Nick:get count of all stories in a sub";
RallyRestApi restApi = new RallyRestApi(new URI(host), apiKey);
restApi.setApplicationName(applicationName);
try {
GetRequest getRequest = new GetRequest("/user");
GetResponse getResponse = restApi.get(getRequest);
JsonObject currentUser = getResponse.getObject();
String currentUserRef = currentUser.get("_ref").getAsString();
String currentUserName = currentUser.get("_refObjectName").getAsString();
System.out.println("current user " + " " + currentUserName + " " + currentUserRef);
List<String> workspaces = new ArrayList<String>();
GetRequest getSubscriptionRequest = new GetRequest("/subscription");
getSubscriptionRequest.setFetch(new Fetch("Workspaces"));
GetResponse getSubscriptionResponse = restApi.get(getSubscriptionRequest);
JsonObject subJsonObject = getSubscriptionResponse.getObject();
String subRef = subJsonObject.get("_ref").getAsString();
System.out.println("subscription " + " " + subRef);
int numberOfWorkspaces = subJsonObject.getAsJsonObject("Workspaces").get("Count").getAsInt();
System.out.println("number of workspaces in the sub " + " " + numberOfWorkspaces);
if(numberOfWorkspaces > 0) {
QueryRequest workspaceRequest = new QueryRequest(subJsonObject.getAsJsonObject("Workspaces"));
workspaceRequest.setFetch(new Fetch("Name","ObjectID","State"));
workspaceRequest.setLimit(1000000);
JsonArray workspacesInSub = restApi.query(workspaceRequest).getResults();
for (int i=0;i<numberOfWorkspaces;i++){
System.out.println("Name: " + workspacesInSub.get(i).getAsJsonObject().get("Name")
+ " OID: " + workspacesInSub.get(i).getAsJsonObject().get("ObjectID").getAsString()
+ " State:" + workspacesInSub.get(i).getAsJsonObject().get("State").getAsString());
if(workspacesInSub.get(i).getAsJsonObject().get("State").getAsString().equals("Open") ){
workspaces.add(workspacesInSub.get(i).getAsJsonObject().get("ObjectID").getAsString());
}
}
}
System.out.println("workspaces: " + workspaces);
class MyRunnable implements Runnable {
private String oid;
private String state;
public MyRunnable(String oid) {
this.oid = oid;
}
public void run() {
try{
int count = getRally(this.oid);
totalNumOfStories = totalNumOfStories + count;
System.out.println("Running total number of stories in Subscription: " + totalNumOfStories);
}
catch (Exception e){
e.printStackTrace();
}
}
}
Thread [] threads = new Thread[numberOfWorkspaces];
for (int i = 0; i < threads.length; i++)
{
threads[i] = new Thread(new MyRunnable(workspaces.get(i)));
threads[i].start();
threads[i].join();
}
if (restApi != null) {
restApi.close();
}
} catch (Exception e) {
System.out.println(e);
} finally {
restApi.close();
}
}
private static int getRally(String oid) throws Exception
{
int numberOfStories = 0;
String host = "https://rally1.rallydev.com";
String apiKey = "_abc123";
String applicationName = "Nick:get count of all stories and defects in a sub";
RallyRestApi restApi = new RallyRestApi(new URI(host), apiKey);
restApi.setApplicationName(applicationName);
try {
String workspaceRef = "/workspace/" + oid;
QueryRequest storyRequest = new QueryRequest("HierarchicalRequirement");
storyRequest.setWorkspace(workspaceRef);
storyRequest.setLimit(1000000);
QueryResponse storyResponse = restApi.query(storyRequest);
numberOfStories = storyResponse.getTotalResultCount();
System.out.println("\n########## Workspace : " + oid + " ###########");
System.out.println("number of stories in workspace : " + oid + " : "+ numberOfStories);
} catch (Exception e) {
System.out.println(e);
} finally {
restApi.close();
}
return numberOfStories;
}
}