Skip to content

Commit b214c29

Browse files
committed
* refactored request class names
* added domain class for comment * adde service for adding comments
1 parent 91e8742 commit b214c29

7 files changed

Lines changed: 213 additions & 49 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2009 David Linsin
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package de.linsin.github.rest.domain;
17+
18+
/**
19+
* Domain class representing a comment on an Issue
20+
* Note: this is preliminary, since the github API only support adding a comment
21+
*
22+
* @author David Linsin - dlinsin@gmail.com
23+
*/
24+
public class Comment {
25+
private String comment;
26+
private Status status;
27+
28+
public Comment() {
29+
status = Status.saved;
30+
}
31+
32+
// TODO find out other statuses
33+
public enum Status {
34+
saved;
35+
}
36+
37+
public String getComment() {
38+
return comment;
39+
}
40+
41+
public void setComment(String argComment) {
42+
comment = argComment;
43+
}
44+
45+
public String getStatus() {
46+
return status.name();
47+
}
48+
49+
public void setStatus(String argStatus) {
50+
status = Status.valueOf(argStatus);
51+
}
52+
53+
@Override
54+
public String toString() {
55+
return "Comment{" +
56+
"comment='" + comment + '\'' +
57+
", status=" + status +
58+
'}';
59+
}
60+
}

src/main/java/de/linsin/github/rest/domain/Issue.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ public void setLabels(String[] argLabels) {
5959

6060
public enum State {
6161
open, closed;
62-
63-
State() {
64-
}
6562
}
6663

6764
public int getNumber() {

src/main/java/de/linsin/github/rest/resource/ManipulateIssueRequest.java renamed to src/main/java/de/linsin/github/rest/resource/IssueCommentRequest.java

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,23 @@
1616
package de.linsin.github.rest.resource;
1717

1818
/**
19-
* Represents a request to open an issue
19+
* Represents a request to add a comment to an issue
2020
*
2121
* @author David Linsin - dlinsin@gmail.com
2222
*/
23-
public class ManipulateIssueRequest extends IssueRequest {
24-
private String title;
25-
private String body;
23+
public class IssueCommentRequest extends Request {
24+
private String comment;
2625

27-
public ManipulateIssueRequest(String argLogin, String argToken, String argTitle, String argBody) {
26+
public IssueCommentRequest(String argLogin, String argToken, String argComment) {
2827
super(argLogin, argToken);
29-
title = argTitle;
30-
body = argBody;
28+
comment = argComment;
3129
}
3230

33-
public ManipulateIssueRequest() {
31+
public String getComment() {
32+
return comment;
3433
}
3534

36-
public String getTitle() {
37-
return title;
38-
}
39-
40-
public void setTitle(String argTitle) {
41-
title = argTitle;
42-
}
43-
44-
public String getBody() {
45-
return body;
46-
}
47-
48-
public void setBody(String argBody) {
49-
body = argBody;
35+
public void setComment(String argComment) {
36+
comment = argComment;
5037
}
5138
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2009 David Linsin
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package de.linsin.github.rest.resource;
17+
18+
import de.linsin.github.rest.domain.Comment;
19+
20+
/**
21+
* Represents a response containing a {@link Comment}
22+
*
23+
* @author David Linsin - dlinsin@gmail.com
24+
*/
25+
public class IssueCommentResponse {
26+
private Comment comment;
27+
28+
public Comment getComment() {
29+
return comment;
30+
}
31+
32+
public void setComment(Comment argComment) {
33+
comment = argComment;
34+
}
35+
36+
@Override
37+
public String toString() {
38+
return "IssueCommentResponse{" +
39+
"comment=" + comment +
40+
'}';
41+
}
42+
}

src/main/java/de/linsin/github/rest/resource/IssueRequest.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,36 @@
1616
package de.linsin.github.rest.resource;
1717

1818
/**
19-
* Represents a request for an issue
19+
* Represents a request to open or edit an issue
2020
*
2121
* @author David Linsin - dlinsin@gmail.com
2222
*/
23-
public class IssueRequest {
24-
private String login;
25-
private String token;
26-
27-
public IssueRequest(String argLogin, String argToken) {
28-
login = argLogin;
29-
token = argToken;
23+
public class IssueRequest extends Request {
24+
private String title;
25+
private String body;
26+
27+
public IssueRequest(String argLogin, String argToken, String argTitle, String argBody) {
28+
super(argLogin, argToken);
29+
title = argTitle;
30+
body = argBody;
3031
}
3132

3233
public IssueRequest() {
3334
}
3435

35-
public String getLogin() {
36-
return login;
36+
public String getTitle() {
37+
return title;
3738
}
3839

39-
public void setLogin(String argLogin) {
40-
login = argLogin;
40+
public void setTitle(String argTitle) {
41+
title = argTitle;
4142
}
4243

43-
public String getToken() {
44-
return token;
44+
public String getBody() {
45+
return body;
4546
}
4647

47-
public void setToken(String argToken) {
48-
token = argToken;
48+
public void setBody(String argBody) {
49+
body = argBody;
4950
}
50-
51-
}
51+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2009 David Linsin
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package de.linsin.github.rest.resource;
17+
18+
/**
19+
* Represents a POST request which contains auth information of github API
20+
*
21+
* @author David Linsin - dlinsin@gmail.com
22+
*/
23+
public class Request {
24+
private String login;
25+
private String token;
26+
27+
public Request(String argLogin, String argToken) {
28+
login = argLogin;
29+
token = argToken;
30+
}
31+
32+
public Request() {
33+
}
34+
35+
public String getLogin() {
36+
return login;
37+
}
38+
39+
public void setLogin(String argLogin) {
40+
login = argLogin;
41+
}
42+
43+
public String getToken() {
44+
return token;
45+
}
46+
47+
public void setToken(String argToken) {
48+
token = argToken;
49+
}
50+
51+
}

src/main/java/de/linsin/github/rest/service/IssueBrowser.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@
1919
import java.util.Collections;
2020
import java.util.List;
2121

22+
import de.linsin.github.rest.domain.Comment;
2223
import de.linsin.github.rest.domain.Issue;
2324
import de.linsin.github.rest.domain.Repository;
25+
import de.linsin.github.rest.resource.IssueCommentRequest;
2426
import de.linsin.github.rest.resource.IssueRequest;
2527
import de.linsin.github.rest.resource.IssueResponse;
2628
import de.linsin.github.rest.resource.IssuesResponse;
27-
import de.linsin.github.rest.resource.ManipulateIssueRequest;
29+
import de.linsin.github.rest.resource.Request;
30+
import de.linsin.github.rest.resource.IssueCommentResponse;
2831
import org.springframework.util.Assert;
2932
import org.springframework.web.client.RestTemplate;
3033

@@ -126,7 +129,7 @@ public Issue open(Repository argRepository, Issue argIssue) {
126129
Assert.hasText(argIssue.getTitle());
127130
Assert.hasText(argIssue.getBody());
128131

129-
ManipulateIssueRequest req = new ManipulateIssueRequest(username, apiToken, argIssue.getTitle(), argIssue.getBody());
132+
IssueRequest req = new IssueRequest(username, apiToken, argIssue.getTitle(), argIssue.getBody());
130133
IssueResponse resp = template.postForObject(OPEN_ISSUE_URL, req, IssueResponse.class, argRepository.getOwner(), argRepository.getName());
131134

132135
return resp.getIssue();
@@ -147,7 +150,7 @@ public Issue reopen(Repository argRepository, Issue argIssue) {
147150

148151
Assert.isTrue(argIssue.getNumber() > 0);
149152

150-
IssueRequest req = new IssueRequest(username, apiToken);
153+
Request req = new Request(username, apiToken);
151154
IssueResponse resp = template.postForObject(REOPEN_ISSUE_URL, req, IssueResponse.class, argRepository.getOwner(), argRepository.getName(), String.valueOf(argIssue.getNumber()));
152155

153156
return resp.getIssue();
@@ -167,7 +170,7 @@ public void close(Repository argRepository, Issue argIssue) {
167170

168171
Assert.isTrue(argIssue.getNumber() > 0);
169172

170-
IssueRequest req = new IssueRequest(username, apiToken);
173+
Request req = new Request(username, apiToken);
171174
template.postForObject(CLOSE_ISSUE_URL, req, IssueResponse.class, argRepository.getOwner(), argRepository.getName(), String.valueOf(argIssue.getNumber()));
172175
}
173176

@@ -176,7 +179,7 @@ public void close(Repository argRepository, Issue argIssue) {
176179
* Edits the existing {@link Issue} passed, which resides in the provided {@link Repository}
177180
* Note: so far only id, title and body are used from passed instance
178181
*
179-
* @param argRepository {@link Repository} instance used to open issue
182+
* @param argRepository {@link Repository} instance used to edit issue
180183
* @param argIssue {@link Issue} instance containing id, title and body of the issue
181184
* @return the {@link Issue} instance which was edited
182185
* @throws IllegalArgumentException in case passed Issue doesn't contain an id, body or title
@@ -190,12 +193,36 @@ public Issue edit(Repository argRepository, Issue argIssue) {
190193
Assert.hasText(argIssue.getTitle());
191194
Assert.hasText(argIssue.getBody());
192195

193-
ManipulateIssueRequest req = new ManipulateIssueRequest(username, apiToken, argIssue.getTitle(), argIssue.getBody());
196+
IssueRequest req = new IssueRequest(username, apiToken, argIssue.getTitle(), argIssue.getBody());
194197
IssueResponse resp = template.postForObject(EDIT_ISSUE_URL, req, IssueResponse.class, argRepository.getOwner(), argRepository.getName(), String.valueOf(argIssue.getNumber()));
195198

196199
return resp.getIssue();
197200
}
198201

202+
/**
203+
* Adds a comment to an exisiting {@link Issue}, which resides in the provided {@link Repository}
204+
* Note: only adding comments is supported by the github API http://support.github.com/discussions/repos/1112-retreiving-comments-for-an-issue
205+
*
206+
* @param argRepository {@link Repository} instance used to retrieve issue
207+
* @param argIssue {@link Issue} instance which exists in passed Repository
208+
* @param argComment {@link Comment} which must contain a comment
209+
* @return the {@link Comment} which was added
210+
* @throws IllegalArgumentException in case passed Issue doesn't contain an id
211+
* @throws NullPointerException in case passed repository, issue or comment is null
212+
* @throws HttpClientErrorException in case passed user, repository or issue doesn't exist
213+
*/
214+
public Comment comment(Repository argRepository, Issue argIssue, Comment argComment) {
215+
RestTemplate template = initTemplate();
216+
217+
Assert.isTrue(argIssue.getNumber() > 0);
218+
Assert.hasText(argComment.getComment());
219+
220+
IssueCommentRequest req = new IssueCommentRequest(username, apiToken, argComment.getComment());
221+
IssueCommentResponse resp = template.postForObject(COMMENT_ISSUE_URL, req, IssueCommentResponse.class, argRepository.getOwner(), argRepository.getName(), String.valueOf(argIssue.getNumber()));
222+
223+
return resp.getComment();
224+
}
225+
199226
// TODO implement comment
200227

201228
protected List<Issue> doBrowse(Repository argRepository, String argUrl) {

0 commit comments

Comments
 (0)