forked from google/gdata-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectHostingWriteDemo.java
More file actions
256 lines (218 loc) · 8.71 KB
/
Copy pathProjectHostingWriteDemo.java
File metadata and controls
256 lines (218 loc) · 8.71 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* Copyright (c) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.projecthosting;
import com.google.gdata.client.projecthosting.ProjectHostingService;
import sample.util.SimpleCommandLineParser;
import com.google.gdata.data.HtmlTextConstruct;
import com.google.gdata.data.Person;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.projecthosting.Cc;
import com.google.gdata.data.projecthosting.CcUpdate;
import com.google.gdata.data.projecthosting.IssueCommentsEntry;
import com.google.gdata.data.projecthosting.IssuesEntry;
import com.google.gdata.data.projecthosting.Label;
import com.google.gdata.data.projecthosting.Owner;
import com.google.gdata.data.projecthosting.OwnerUpdate;
import com.google.gdata.data.projecthosting.SendEmail;
import com.google.gdata.data.projecthosting.Status;
import com.google.gdata.data.projecthosting.Summary;
import com.google.gdata.data.projecthosting.Updates;
import com.google.gdata.data.projecthosting.Username;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.MalformedURLException;
/**
* Demonstrates how to use the Google Data API's Java client library to
* interface with the Google Code Issue Tracker Data API.
* There are examples for the following operations:
*
* <ol>
* <li>Creating a new issue</li>
* <li>Updating the issue by adding a comment with updates</li>
* <li>Closing the issue by adding a comment with status "Fixed"</li>
* </ol>
*
*
*/
public class ProjectHostingWriteDemo {
/** Disable default public constructor */
private ProjectHostingWriteDemo() {
}
/** Client that provides high level operations of the API */
private ProjectHostingClient client;
private String username;
/**
* Constructs the command line application.
*
* @throws AuthenticationException if authentication fails
* @throws MalformedURLException if there's a problem with URL
*/
public ProjectHostingWriteDemo(
ProjectHostingService service, String project, String username,
String password) throws AuthenticationException, MalformedURLException {
this.username = username;
client = new ProjectHostingClient(service, project, username, password);
}
/**
* Main entry point. Parses arguments and creates and invokes the demo.
*/
public static void main(String[] arg) throws Exception {
SimpleCommandLineParser parser = new SimpleCommandLineParser(arg);
// Parse command-line flags
String project = parser.getValue("project");
String username = parser.getValue("username");
String password = parser.getValue("password");
boolean help = parser.containsKey("help");
if (help || (project == null) || (username == null) || (password == null)) {
usage();
System.exit(help ? 0 : 1);
}
ProjectHostingService service =
new ProjectHostingService("projecthosting-write-demo");
try {
new ProjectHostingWriteDemo(service, project, username, password).run();
} catch (AuthenticationException e) {
System.out.println("The username/password entered is invalid.");
System.exit(1);
}
}
/**
* Creates a new issue and adds two comments to it, first to update the
* issue and second to close it.
*
* @throws IOException if there is a problem communicating with the server
* @throws ServiceException if the service is unable to handle the request
*/
private void run() throws IOException, ServiceException {
// Create an issue
IssuesEntry issueInserted = client.insertIssue(makeNewIssue());
String issueId = client.getIssueId(issueInserted.getId());
System.out.println("Issue #" + issueId + " created");
// Add comments and updates to the issue created
addComment(issueId, makeUpdatingComment());
addComment(issueId, makePlainComment());
addComment(issueId, makeClosingComment());
// Print the issue and comments added
System.out.println("-----------------------------------------------------");
System.out.println("Issue created and comments added:");
client.printIssueAndComments(issueInserted);
}
/**
* Adds the given comment to the given issue.
*
* @throws IOException if there is a problem communicating with the server
* @throws ServiceException if the service is unable to handle the request
*/
private void addComment(String issueId, IssueCommentsEntry issueComment)
throws IOException, ServiceException {
IssueCommentsEntry commentInserted = client.insertComment(
issueId, issueComment);
String commentId = client.getCommentId(commentInserted.getId());
System.out.println("Comment #" + commentId + " added in issue #" + issueId);
}
/**
* Creates a new issue that can be inserted to the issues feed.
*/
protected IssuesEntry makeNewIssue() {
Person author = new Person();
author.setName(username);
Owner owner = new Owner();
owner.setUsername(new Username(username));
Cc cc = new Cc();
cc.setUsername(new Username(username));
IssuesEntry entry = new IssuesEntry();
entry.getAuthors().add(author);
// Uncomment the following line to set the owner along with issue creation.
// It's intentionally commented out so we can demonstrate setting the owner
// field using setOwnerUpdate() as shown in makeUpdatingComment() below.
// entry.setOwner(owner);
entry.setContent(new HtmlTextConstruct("issue description"));
entry.setTitle(new PlainTextConstruct("issue summary"));
entry.setStatus(new Status("New"));
entry.addLabel(new Label("Priority-High"));
entry.addLabel(new Label("Milestone-2009"));
entry.addCc(cc);
entry.setSendEmail(new SendEmail("False"));
return entry;
}
/**
* Creates a comment that updates an existing issue.
*/
protected IssueCommentsEntry makeUpdatingComment() {
Person author = new Person();
author.setName(username);
// Create issue updates
Updates updates = new Updates();
updates.setSummary(new Summary("New issue summary"));
updates.setStatus(new Status("Accepted"));
updates.setOwnerUpdate(new OwnerUpdate(username));
updates.addLabel(new Label("-Priority-High"));
updates.addLabel(new Label("Priority-Low"));
updates.addLabel(new Label("-Milestone-2009"));
updates.addLabel(new Label("Milestone-2010"));
updates.addLabel(new Label("Type-Enhancement"));
updates.addCcUpdate(new CcUpdate("-" + username));
// Create issue comment entry
IssueCommentsEntry entry = new IssueCommentsEntry();
entry.getAuthors().add(author);
entry.setContent(new HtmlTextConstruct("some comment"));
entry.setUpdates(updates);
entry.setSendEmail(new SendEmail("False"));
return entry;
}
/**
* Creates a comment without any updates.
*/
protected IssueCommentsEntry makePlainComment() {
Person author = new Person();
author.setName(username);
// Create issue comment entry
IssueCommentsEntry entry = new IssueCommentsEntry();
entry.getAuthors().add(author);
entry.setContent(new HtmlTextConstruct("Some comment"));
entry.setSendEmail(new SendEmail("False"));
return entry;
}
/**
* Creates a comment that closes an issue by setting Status to "Fixed".
*/
protected IssueCommentsEntry makeClosingComment() {
Person author = new Person();
author.setName(username);
// Set the Status as Fixed
Updates updates = new Updates();
updates.setStatus(new Status("Fixed"));
// Create issue comment entry
IssueCommentsEntry entry = new IssueCommentsEntry();
entry.getAuthors().add(author);
entry.setContent(new HtmlTextConstruct("This was fixed last week."));
entry.setUpdates(updates);
entry.setSendEmail(new SendEmail("False"));
return entry;
}
/**
* Prints usage of this application.
*/
private static void usage() {
System.out.println(
"Syntax: ProjectHostingWriteDemo --project <project> "
+ "--username <username> --password <password>\n"
+ "\t<project>\tProject on which the demo will run. This demo will "
+ "create a new issue in the given project and add comments to it.\n"
+ "\t<username>\tGoogle Account username\n"
+ "\t<password>\tGoogle Account password\n");
}
}