Skip to content

Commit ae912d7

Browse files
committed
feat: Add Issue Comment Reaction functionality
1 parent c4a4e64 commit ae912d7

File tree

9 files changed

+346
-13
lines changed

9 files changed

+346
-13
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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
public class CommentReactionContentDeserializer extends JsonDeserializer<CommentReactionContent> {
30+
@Override
31+
public CommentReactionContent deserialize(final JsonParser p, final DeserializationContext ctxt)
32+
throws IOException {
33+
String value = p.getText();
34+
for (CommentReactionContent content : CommentReactionContent.values()) {
35+
if (content.toString().equals(value)) {
36+
return content;
37+
}
38+
}
39+
return null;
40+
}
41+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
public class CommentReactionContentSerializer extends JsonSerializer<CommentReactionContent> {
29+
@Override
30+
public void serialize(final CommentReactionContent value, final JsonGenerator gen, final SerializerProvider serializers) throws IOException {
31+
gen.writeString(value.toString());
32+
}
33+
}

β€Ž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: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,30 @@
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;
25-
2623
import com.google.common.collect.ImmutableMap;
2724
import com.spotify.github.async.AsyncPage;
2825
import com.spotify.github.v3.comment.Comment;
26+
import com.spotify.github.v3.comment.CommentReaction;
27+
import com.spotify.github.v3.comment.CommentReactionContent;
2928
import com.spotify.github.v3.issues.Issue;
3029
import java.lang.invoke.MethodHandles;
3130
import java.util.Iterator;
3231
import java.util.concurrent.CompletableFuture;
32+
33+
import okhttp3.Response;
3334
import org.slf4j.Logger;
3435
import org.slf4j.LoggerFactory;
3536

37+
import static com.spotify.github.v3.clients.GitHubClient.*;
38+
3639
/** Issue API client */
3740
public class IssueClient {
3841

3942
static final String COMMENTS_URI_NUMBER_TEMPLATE = "/repos/%s/%s/issues/%s/comments";
4043
static final String COMMENTS_URI_TEMPLATE = "/repos/%s/%s/issues/comments";
4144
static final String COMMENTS_URI_ID_TEMPLATE = "/repos/%s/%s/issues/comments/%s";
45+
static final String COMMENTS_REACTION_TEMPLATE = "/repos/%s/%s/issues/comments/%s/reactions";
46+
static final String COMMENTS_REACTION_ID_TEMPLATE = "/repos/%s/%s/issues/%s/reactions/%s";
4247
static final String ISSUES_URI_ID_TEMPLATE = "/repos/%s/%s/issues/%s";
4348
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
4449

@@ -137,4 +142,28 @@ private Iterator<AsyncPage<Comment>> listComments(final String path) {
137142
public CompletableFuture<Issue> getIssue(final int id) {
138143
return github.request(String.format(ISSUES_URI_ID_TEMPLATE, owner, repo, id), Issue.class);
139144
}
145+
146+
/**
147+
* Create a reaction on a comment.
148+
*
149+
* @param commentId comment id
150+
* @param reaction reaction content
151+
* @return the Comment that was just created
152+
*/
153+
public CompletableFuture<CommentReaction> createCommentReaction(final long commentId, final CommentReactionContent reaction) {
154+
final String path = String.format(COMMENTS_REACTION_TEMPLATE, owner, repo, commentId);
155+
final String requestBody = github.json().toJsonUnchecked(ImmutableMap.of("content", reaction.toString()));
156+
return github.post(path, requestBody, CommentReaction.class);
157+
}
158+
159+
public CompletableFuture<Response> deleteCommentReaction(final long issueNumber, final long reactionId) {
160+
final String path = String.format(COMMENTS_REACTION_ID_TEMPLATE, owner, repo, issueNumber, reactionId);
161+
final String requestBody = github.json().toJsonUnchecked("");
162+
return github.delete(path, requestBody);
163+
}
164+
165+
public GithubPageIterator<CommentReaction> listCommentReaction(final long commentId) {
166+
final String path = String.format(COMMENTS_REACTION_TEMPLATE, owner, repo, commentId);
167+
return new GithubPageIterator<>(new GithubPage<>(github, path, LIST_COMMENT_REACTION_TYPE_REFERENCE));
168+
}
140169
}
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.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+
@Value.Immutable
30+
@GithubStyle
31+
@JsonSerialize(as = ImmutableCommentReaction.class)
32+
@JsonDeserialize(as = ImmutableCommentReaction.class)
33+
public interface CommentReaction extends UpdateTracking {
34+
35+
/** Reaction ID. */
36+
long id();
37+
38+
/** Reaction user. */
39+
User user();
40+
41+
/** Reaction content. */
42+
CommentReactionContent content();
43+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
@JsonDeserialize(using = CommentReactionContentDeserializer.class)
28+
@JsonSerialize(using = CommentReactionContentSerializer.class)
29+
public enum CommentReactionContent {
30+
THUMBS_UP("+1"), // πŸ‘
31+
THUMBS_DOWN("-1"), // πŸ‘Ž
32+
LAUGH("laugh"), // πŸ˜„
33+
HOORAY("hooray"), // πŸŽ‰
34+
CONFUSED("confused"), // πŸ˜•
35+
HEART("heart"), // ❀️
36+
ROCKET("rocket"), // πŸš€
37+
EYES("eyes"); // πŸ‘€
38+
39+
private final String reaction;
40+
41+
CommentReactionContent(final String reaction) {
42+
this.reaction = reaction;
43+
}
44+
45+
@Override
46+
public String toString() {
47+
return reaction;
48+
}
49+
}

0 commit comments

Comments
Β (0)