Skip to content

Commit 56337cf

Browse files
authored
feat: Add Issue Comment Reaction functionality (#215)
1 parent c4a4e64 commit 56337cf

File tree

9 files changed

+386
-16
lines changed

9 files changed

+386
-16
lines changed

β€Žpom.xmlβ€Ž

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44

55
<artifactId>github-client</artifactId>
6-
<version>0.4.1-SNAPSHOT</version>
6+
<version>0.3.7-SNAPSHOT</version>
77

88
<parent>
99
<groupId>com.spotify</groupId>
@@ -221,6 +221,12 @@
221221
<version>${junit.version}</version>
222222
<scope>test</scope>
223223
</dependency>
224+
<dependency>
225+
<groupId>org.junit.jupiter</groupId>
226+
<artifactId>junit-jupiter-params</artifactId>
227+
<version>${junit.version}</version>
228+
<scope>test</scope>
229+
</dependency>
224230

225231
<dependency>
226232
<groupId>org.mockito</groupId>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2016 - 2020 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
package com.spotify.github.jackson;
21+
22+
import com.fasterxml.jackson.core.JsonParser;
23+
import com.fasterxml.jackson.databind.DeserializationContext;
24+
import com.fasterxml.jackson.databind.JsonDeserializer;
25+
import com.spotify.github.v3.comment.CommentReactionContent;
26+
27+
import java.io.IOException;
28+
/**
29+
* Custom deserializer for {@link CommentReactionContent}.
30+
*/
31+
public class CommentReactionContentDeserializer extends JsonDeserializer<CommentReactionContent> {
32+
@Override
33+
public CommentReactionContent deserialize(final JsonParser p, final DeserializationContext ctxt)
34+
throws IOException {
35+
String value = p.getText();
36+
for (CommentReactionContent content : CommentReactionContent.values()) {
37+
if (content.toString().equals(value)) {
38+
return content;
39+
}
40+
}
41+
return null;
42+
}
43+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2016 - 2020 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
package com.spotify.github.jackson;
21+
22+
import com.fasterxml.jackson.core.JsonGenerator;
23+
import com.fasterxml.jackson.databind.JsonSerializer;
24+
import com.fasterxml.jackson.databind.SerializerProvider;
25+
import com.spotify.github.v3.comment.CommentReactionContent;
26+
import java.io.IOException;
27+
/**
28+
* Custom serializer for {@link CommentReactionContent}.
29+
*/
30+
public class CommentReactionContentSerializer extends JsonSerializer<CommentReactionContent> {
31+
@Override
32+
public void serialize(final CommentReactionContent value, final JsonGenerator gen, final SerializerProvider serializers) throws IOException {
33+
gen.writeString(value.toString());
34+
}
35+
}

β€Žsrc/main/java/com/spotify/github/v3/clients/GitHubClient.javaβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.spotify.github.v3.checks.AccessToken;
3333
import com.spotify.github.v3.checks.Installation;
3434
import com.spotify.github.v3.comment.Comment;
35+
import com.spotify.github.v3.comment.CommentReaction;
3536
import com.spotify.github.v3.exceptions.ReadOnlyRepositoryException;
3637
import com.spotify.github.v3.exceptions.RequestNotOkException;
3738
import com.spotify.github.v3.git.Reference;
@@ -85,6 +86,8 @@ public class GitHubClient {
8586
};
8687
static final TypeReference<List<Comment>> LIST_COMMENT_TYPE_REFERENCE =
8788
new TypeReference<>() {};
89+
static final TypeReference<List<CommentReaction>> LIST_COMMENT_REACTION_TYPE_REFERENCE =
90+
new TypeReference<>() {};
8891
static final TypeReference<List<Repository>> LIST_REPOSITORY =
8992
new TypeReference<>() {};
9093
static final TypeReference<List<CommitItem>> LIST_COMMIT_TYPE_REFERENCE =

β€Žsrc/main/java/com/spotify/github/v3/clients/IssueClient.javaβ€Ž

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -20,16 +20,18 @@
2020

2121
package com.spotify.github.v3.clients;
2222

23-
import static com.spotify.github.v3.clients.GitHubClient.IGNORE_RESPONSE_CONSUMER;
24-
import static com.spotify.github.v3.clients.GitHubClient.LIST_COMMENT_TYPE_REFERENCE;
23+
import static com.spotify.github.v3.clients.GitHubClient.*;
2524

2625
import com.google.common.collect.ImmutableMap;
2726
import com.spotify.github.async.AsyncPage;
2827
import com.spotify.github.v3.comment.Comment;
28+
import com.spotify.github.v3.comment.CommentReaction;
29+
import com.spotify.github.v3.comment.CommentReactionContent;
2930
import com.spotify.github.v3.issues.Issue;
3031
import java.lang.invoke.MethodHandles;
3132
import java.util.Iterator;
3233
import java.util.concurrent.CompletableFuture;
34+
import okhttp3.Response;
3335
import org.slf4j.Logger;
3436
import org.slf4j.LoggerFactory;
3537

@@ -39,6 +41,8 @@ public class IssueClient {
3941
static final String COMMENTS_URI_NUMBER_TEMPLATE = "/repos/%s/%s/issues/%s/comments";
4042
static final String COMMENTS_URI_TEMPLATE = "/repos/%s/%s/issues/comments";
4143
static final String COMMENTS_URI_ID_TEMPLATE = "/repos/%s/%s/issues/comments/%s";
44+
static final String COMMENTS_REACTION_TEMPLATE = "/repos/%s/%s/issues/comments/%s/reactions";
45+
static final String COMMENTS_REACTION_ID_TEMPLATE = "/repos/%s/%s/issues/%s/reactions/%s";
4246
static final String ISSUES_URI_ID_TEMPLATE = "/repos/%s/%s/issues/%s";
4347
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
4448

@@ -128,13 +132,59 @@ private Iterator<AsyncPage<Comment>> listComments(final String path) {
128132
return new GithubPageIterator<>(new GithubPage<>(github, path, LIST_COMMENT_TYPE_REFERENCE));
129133
}
130134

131-
/***
135+
/**
132136
* Get issue by id
133137
*
134-
* @param id
138+
* @param id issue id
135139
* @return the Issue for the given id if exists.
136140
*/
137141
public CompletableFuture<Issue> getIssue(final int id) {
138142
return github.request(String.format(ISSUES_URI_ID_TEMPLATE, owner, repo, id), Issue.class);
139143
}
144+
145+
/**
146+
* Create a reaction on a comment. See <a *
147+
* href="https://docs.github.com/en/rest/reactions/reactions?apiVersion=2022-11-28#create-reaction-for-an-issue-comment">Create
148+
* reaction for an issue comment</a>
149+
*
150+
* @param commentId comment id
151+
* @param reaction reaction content
152+
* @return the Comment that was just created
153+
*/
154+
public CompletableFuture<CommentReaction> createCommentReaction(
155+
final long commentId, final CommentReactionContent reaction) {
156+
final String path = String.format(COMMENTS_REACTION_TEMPLATE, owner, repo, commentId);
157+
final String requestBody =
158+
github.json().toJsonUnchecked(ImmutableMap.of("content", reaction.toString()));
159+
return github.post(path, requestBody, CommentReaction.class);
160+
}
161+
162+
/**
163+
* Delete a reaction on a comment. See <a
164+
* href="https://docs.github.com/en/rest/reactions/reactions?apiVersion=2022-11-28#delete-an-issue-comment-reaction">List
165+
* reactions for an issue comment</a>
166+
*
167+
* @param issueNumber issue number
168+
* @param reactionId reaction id
169+
*/
170+
public CompletableFuture<Response> deleteCommentReaction(
171+
final long issueNumber, final long reactionId) {
172+
final String path =
173+
String.format(COMMENTS_REACTION_ID_TEMPLATE, owner, repo, issueNumber, reactionId);
174+
return github.delete(path);
175+
}
176+
177+
/**
178+
* List reactions on a comment. See <a
179+
* href="https://docs.github.com/en/rest/reactions/reactions?apiVersion=2022-11-28#list-reactions-for-an-issue-comment">List
180+
* reactions for an issue comment</a>
181+
*
182+
* @param commentId comment id
183+
* @return reactions
184+
*/
185+
public GithubPageIterator<CommentReaction> listCommentReaction(final long commentId) {
186+
final String path = String.format(COMMENTS_REACTION_TEMPLATE, owner, repo, commentId);
187+
return new GithubPageIterator<>(
188+
new GithubPage<>(github, path, LIST_COMMENT_REACTION_TYPE_REFERENCE));
189+
}
140190
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2016 - 2020 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
package com.spotify.github.v3.comment;
21+
22+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
23+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
24+
import com.spotify.github.GithubStyle;
25+
import com.spotify.github.UpdateTracking;
26+
import com.spotify.github.v3.User;
27+
import org.immutables.value.Value;
28+
29+
/**
30+
* Comment reaction object.
31+
*
32+
* <p>See <a
33+
* href="https://docs.github.com/en/rest/reactions/reactions?apiVersion=2022-11-28#about-reactions">About
34+
* GitHub Issue Comment reactions</a>
35+
*/
36+
@Value.Immutable
37+
@GithubStyle
38+
@JsonSerialize(as = ImmutableCommentReaction.class)
39+
@JsonDeserialize(as = ImmutableCommentReaction.class)
40+
public interface CommentReaction extends UpdateTracking {
41+
42+
/** Reaction ID. */
43+
long id();
44+
45+
/** Reaction user. */
46+
User user();
47+
48+
/** Reaction content. */
49+
CommentReactionContent content();
50+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2016 - 2020 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
package com.spotify.github.v3.comment;
21+
22+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
23+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
24+
import com.spotify.github.jackson.CommentReactionContentDeserializer;
25+
import com.spotify.github.jackson.CommentReactionContentSerializer;
26+
27+
/**
28+
* Comment reaction content.
29+
*
30+
* <p>See <a
31+
* href="https://docs.github.com/en/rest/reactions/reactions?apiVersion=2022-11-28#about-reactions">About
32+
* GitHub Issue Comment reactions</a>
33+
*/
34+
@JsonDeserialize(using = CommentReactionContentDeserializer.class)
35+
@JsonSerialize(using = CommentReactionContentSerializer.class)
36+
public enum CommentReactionContent {
37+
THUMBS_UP("+1"), // πŸ‘
38+
THUMBS_DOWN("-1"), // πŸ‘Ž
39+
LAUGH("laugh"), // πŸ˜„
40+
HOORAY("hooray"), // πŸŽ‰
41+
CONFUSED("confused"), // πŸ˜•
42+
HEART("heart"), // ❀️
43+
ROCKET("rocket"), // πŸš€
44+
EYES("eyes"); // πŸ‘€
45+
46+
private final String reaction;
47+
48+
CommentReactionContent(final String reaction) {
49+
this.reaction = reaction;
50+
}
51+
52+
@Override
53+
public String toString() {
54+
return reaction;
55+
}
56+
}

0 commit comments

Comments
Β (0)