forked from spotify/github-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIssueClient.java
More file actions
319 lines (292 loc) · 11.1 KB
/
IssueClient.java
File metadata and controls
319 lines (292 loc) · 11.1 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*-
* -\-\-
* github-api
* --
* Copyright (C) 2016 - 2020 Spotify AB
* --
* 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 com.spotify.github.v3.clients;
import static com.spotify.github.v3.clients.GitHubClient.*;
import com.google.common.collect.ImmutableMap;
import com.spotify.github.async.AsyncPage;
import com.spotify.github.http.HttpResponse;
import com.spotify.github.v3.comment.Comment;
import com.spotify.github.v3.comment.CommentReaction;
import com.spotify.github.v3.comment.CommentReactionContent;
import com.spotify.github.v3.issues.Issue;
import java.lang.invoke.MethodHandles;
import java.util.Iterator;
import java.util.concurrent.CompletableFuture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** Issue API client */
public class IssueClient {
// URI templates for various API endpoints
static final String COMMENTS_URI_NUMBER_TEMPLATE = "/repos/%s/%s/issues/%s/comments";
static final String COMMENTS_URI_TEMPLATE = "/repos/%s/%s/issues/comments";
static final String COMMENTS_URI_ID_TEMPLATE = "/repos/%s/%s/issues/comments/%s";
static final String COMMENTS_REACTION_TEMPLATE = "/repos/%s/%s/issues/comments/%s/reactions";
static final String COMMENTS_REACTION_ID_TEMPLATE = "/repos/%s/%s/issues/comments/%s/reactions/%s";
static final String ISSUES_REACTION_TEMPLATE = "/repos/%s/%s/issues/%s/reactions";
static final String ISSUES_REACTION_ID_TEMPLATE = "/repos/%s/%s/issues/%s/reactions/%s";
static final String ISSUES_URI_ID_TEMPLATE = "/repos/%s/%s/issues/%s";
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private final GitHubClient github;
private final String owner;
private final String repo;
/**
* Constructs an IssueClient.
* @param github the GitHub client
* @param owner the repository owner
* @param repo the repository name
*/
IssueClient(final GitHubClient github, final String owner, final String repo) {
this.github = github;
this.owner = owner;
this.repo = repo;
}
/**
* Creates an IssueClient.
*
* @param github the GitHub client
* @param owner the repository owner
* @param repo the repository name
* @return a new IssueClient instance
*/
static IssueClient create(final GitHubClient github, final String owner, final String repo) {
return new IssueClient(github, owner, repo);
}
/**
* Lists repository comments.
*
* @return an iterator of asynchronous pages of comments
*/
public Iterator<AsyncPage<Comment>> listComments() {
return listComments(String.format(COMMENTS_URI_TEMPLATE, owner, repo));
}
/**
* Lists comments for a given issue number.
*
* @param issueNumber the issue number
* @return an iterator of asynchronous pages of comments
*/
public Iterator<AsyncPage<Comment>> listComments(final long issueNumber) {
return listComments(String.format(COMMENTS_URI_NUMBER_TEMPLATE, owner, repo, issueNumber));
}
/**
* Lists comments for a given issue number.
*
* @deprecated Use {@link #listComments(long)} instead
* @param issueNumber the issue number
* @return an iterator of asynchronous pages of comments
*/
@Deprecated
public Iterator<AsyncPage<Comment>> listComments(final int issueNumber) {
return listComments((long) issueNumber);
}
/**
* Gets a specific comment.
*
* @param commentId the comment id
* @return a CompletableFuture containing the comment
*/
public CompletableFuture<Comment> getComment(final long commentId) {
final String path = String.format(COMMENTS_URI_ID_TEMPLATE, owner, repo, commentId);
log.info("Fetching issue comments from " + path);
return github.request(path, Comment.class);
}
/**
* Gets a specific comment.
*
* @deprecated Use {@link #getComment(long)} instead
* @param commentId the comment id
* @return a CompletableFuture containing the comment
*/
@Deprecated
public CompletableFuture<Comment> getComment(final int commentId) {
return getComment((long) commentId);
}
/**
* Creates a comment for a given issue number.
*
* @param issueNumber the issue number
* @param body the comment content
* @return a CompletableFuture containing the created comment
*/
public CompletableFuture<Comment> createComment(final long issueNumber, final String body) {
final String path = String.format(COMMENTS_URI_NUMBER_TEMPLATE, owner, repo, issueNumber);
final String requestBody = github.json().toJsonUnchecked(ImmutableMap.of("body", body));
return github.post(path, requestBody, Comment.class);
}
/**
* Creates a comment for a given issue number.
*
* @deprecated Use {@link #createComment(long, String)} instead
* @param issueNumber the issue number
* @param body the comment content
* @return a CompletableFuture containing the created comment
*/
@Deprecated
public CompletableFuture<Comment> createComment(final int issueNumber, final String body) {
return createComment((long) issueNumber, body);
}
/**
* Edits a specific comment.
*
* @param commentId the comment id
* @param body the new comment content
* @return a CompletableFuture representing the completion of the operation
*/
public CompletableFuture<Void> editComment(final long commentId, final String body) {
final String path = String.format(COMMENTS_URI_ID_TEMPLATE, owner, repo, commentId);
return github
.patch(path, github.json().toJsonUnchecked(ImmutableMap.of("body", body)))
.thenAccept(IGNORE_RESPONSE_CONSUMER);
}
/**
* Edits a specific comment.
*
* @deprecated Use {@link #editComment(long, String)} instead
* @param commentId the comment id
* @param body the new comment content
* @return a CompletableFuture representing the completion of the operation
*/
@Deprecated
public CompletableFuture<Void> editComment(final int commentId, final String body) {
return editComment((long) commentId, body);
}
/**
* Deletes a comment.
*
* @param commentId the comment id
* @return a CompletableFuture representing the completion of the operation
*/
public CompletableFuture<Void> deleteComment(final long commentId) {
return github
.delete(String.format(COMMENTS_URI_ID_TEMPLATE, owner, repo, commentId))
.thenAccept(IGNORE_RESPONSE_CONSUMER);
}
/**
* Deletes a comment.
*
* @deprecated Use {@link #deleteComment(long)} instead
* @param commentId the comment id
* @return a CompletableFuture representing the completion of the operation
*/
@Deprecated
public CompletableFuture<Void> deleteComment(final int commentId) {
return deleteComment((long) commentId);
}
/**
* Lists comments for a given path.
*
* @param path the API endpoint path
* @return an iterator of asynchronous pages of comments
*/
private Iterator<AsyncPage<Comment>> listComments(final String path) {
return new GithubPageIterator<>(new GithubPage<>(github, path, LIST_COMMENT_TYPE_REFERENCE));
}
/**
* Gets an issue by id.
*
* @param issueId the issue id
* @return a CompletableFuture containing the issue
*/
public CompletableFuture<Issue> getIssue(final long issueId) {
return github.request(String.format(ISSUES_URI_ID_TEMPLATE, owner, repo, issueId), Issue.class);
}
/**
* Gets an issue by id.
*
* @deprecated Use {@link #getIssue(long)} instead
* @param issueId the issue id
* @return a CompletableFuture containing the issue
*/
@Deprecated
public CompletableFuture<Issue> getIssue(final int issueId) {
return getIssue((long) issueId);
}
/**
* Creates a reaction on a comment.
*
* @param commentId the comment id
* @param reaction the reaction content
* @return a CompletableFuture containing the created reaction
*/
public CompletableFuture<CommentReaction> createCommentReaction(
final long commentId, final CommentReactionContent reaction) {
final String path = String.format(COMMENTS_REACTION_TEMPLATE, owner, repo, commentId);
final String requestBody =
github.json().toJsonUnchecked(ImmutableMap.of("content", reaction.toString()));
return github.post(path, requestBody, CommentReaction.class);
}
/**
* Deletes a reaction on a comment. See <a
* href="https://docs.github.com/en/rest/reactions/reactions?apiVersion=2022-11-28#delete-an-issue-comment-reaction">List
* reactions for an issue comment</a>
*
* @param commentId the comment id
* @param reactionId the reaction id
* @return a CompletableFuture containing the HTTP response
*/
public CompletableFuture<HttpResponse> deleteCommentReaction(
final long commentId, final long reactionId) {
final String path =
String.format(COMMENTS_REACTION_ID_TEMPLATE, owner, repo, commentId, reactionId);
return github.delete(path);
}
/**
* Lists reactions on a comment. See <a
* href="https://docs.github.com/en/rest/reactions/reactions?apiVersion=2022-11-28#list-reactions-for-an-issue-comment">List
* reactions for an issue comment</a>
*
* @param commentId the comment id
* @return an iterator of asynchronous pages of comment reactions
*/
public GithubPageIterator<CommentReaction> listCommentReaction(final long commentId) {
final String path = String.format(COMMENTS_REACTION_TEMPLATE, owner, repo, commentId);
return new GithubPageIterator<>(
new GithubPage<>(github, path, LIST_COMMENT_REACTION_TYPE_REFERENCE));
}
/**
* Creates a reaction on an issue.
*
* @param issueNumber the issue number
* @param reaction the reaction content
* @return a CompletableFuture containing the created reaction
*/
public CompletableFuture<CommentReaction> createIssueReaction(
final long issueNumber, final CommentReactionContent reaction) {
final String path = String.format(ISSUES_REACTION_TEMPLATE, owner, repo, issueNumber);
final String requestBody =
github.json().toJsonUnchecked(ImmutableMap.of("content", reaction.toString()));
return github.post(path, requestBody, CommentReaction.class);
}
/**
* Deletes a reaction on an issue. See <a
* href="https://docs.github.com/en/rest/reactions/reactions?apiVersion=2022-11-28#delete-an-issue-reaction">Delete
* an issue reaction</a>
*
* @param issueNumber the issue number
* @param reactionId the reaction id
* @return a CompletableFuture containing the HTTP response
*/
public CompletableFuture<HttpResponse> deleteIssueReaction(
final long issueNumber, final long reactionId) {
final String path =
String.format(ISSUES_REACTION_ID_TEMPLATE, owner, repo, issueNumber, reactionId);
return github.delete(path);
}
}