Skip to content

Commit

Permalink
feat: Add Issue Comment Reaction functionality (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhi347 authored Feb 25, 2025
1 parent c4a4e64 commit 56337cf
Show file tree
Hide file tree
Showing 9 changed files with 386 additions and 16 deletions.
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>github-client</artifactId>
<version>0.4.1-SNAPSHOT</version>
<version>0.3.7-SNAPSHOT</version>

<parent>
<groupId>com.spotify</groupId>
Expand Down Expand Up @@ -221,6 +221,12 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*-
* -\-\-
* 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.jackson;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.spotify.github.v3.comment.CommentReactionContent;

import java.io.IOException;
/**
* Custom deserializer for {@link CommentReactionContent}.
*/
public class CommentReactionContentDeserializer extends JsonDeserializer<CommentReactionContent> {
@Override
public CommentReactionContent deserialize(final JsonParser p, final DeserializationContext ctxt)
throws IOException {
String value = p.getText();
for (CommentReactionContent content : CommentReactionContent.values()) {
if (content.toString().equals(value)) {
return content;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*-
* -\-\-
* 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.jackson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.spotify.github.v3.comment.CommentReactionContent;
import java.io.IOException;
/**
* Custom serializer for {@link CommentReactionContent}.
*/
public class CommentReactionContentSerializer extends JsonSerializer<CommentReactionContent> {
@Override
public void serialize(final CommentReactionContent value, final JsonGenerator gen, final SerializerProvider serializers) throws IOException {
gen.writeString(value.toString());
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/spotify/github/v3/clients/GitHubClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.spotify.github.v3.checks.AccessToken;
import com.spotify.github.v3.checks.Installation;
import com.spotify.github.v3.comment.Comment;
import com.spotify.github.v3.comment.CommentReaction;
import com.spotify.github.v3.exceptions.ReadOnlyRepositoryException;
import com.spotify.github.v3.exceptions.RequestNotOkException;
import com.spotify.github.v3.git.Reference;
Expand Down Expand Up @@ -85,6 +86,8 @@ public class GitHubClient {
};
static final TypeReference<List<Comment>> LIST_COMMENT_TYPE_REFERENCE =
new TypeReference<>() {};
static final TypeReference<List<CommentReaction>> LIST_COMMENT_REACTION_TYPE_REFERENCE =
new TypeReference<>() {};
static final TypeReference<List<Repository>> LIST_REPOSITORY =
new TypeReference<>() {};
static final TypeReference<List<CommitItem>> LIST_COMMIT_TYPE_REFERENCE =
Expand Down
62 changes: 56 additions & 6 deletions src/main/java/com/spotify/github/v3/clients/IssueClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* 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.
Expand All @@ -20,16 +20,18 @@

package com.spotify.github.v3.clients;

import static com.spotify.github.v3.clients.GitHubClient.IGNORE_RESPONSE_CONSUMER;
import static com.spotify.github.v3.clients.GitHubClient.LIST_COMMENT_TYPE_REFERENCE;
import static com.spotify.github.v3.clients.GitHubClient.*;

import com.google.common.collect.ImmutableMap;
import com.spotify.github.async.AsyncPage;
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 okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -39,6 +41,8 @@ public class IssueClient {
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/%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());

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

/***
/**
* Get issue by id
*
* @param id
* @param id issue id
* @return the Issue for the given id if exists.
*/
public CompletableFuture<Issue> getIssue(final int id) {
return github.request(String.format(ISSUES_URI_ID_TEMPLATE, owner, repo, id), Issue.class);
}

/**
* Create a reaction on a comment. See <a *
* href="https://docs.github.com/en/rest/reactions/reactions?apiVersion=2022-11-28#create-reaction-for-an-issue-comment">Create
* reaction for an issue comment</a>
*
* @param commentId comment id
* @param reaction reaction content
* @return the Comment that was just created
*/
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);
}

/**
* Delete 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 issueNumber issue number
* @param reactionId reaction id
*/
public CompletableFuture<Response> deleteCommentReaction(
final long issueNumber, final long reactionId) {
final String path =
String.format(COMMENTS_REACTION_ID_TEMPLATE, owner, repo, issueNumber, reactionId);
return github.delete(path);
}

/**
* List 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 comment id
* @return 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));
}
}
50 changes: 50 additions & 0 deletions src/main/java/com/spotify/github/v3/comment/CommentReaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*-
* -\-\-
* 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.comment;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.spotify.github.GithubStyle;
import com.spotify.github.UpdateTracking;
import com.spotify.github.v3.User;
import org.immutables.value.Value;

/**
* Comment reaction object.
*
* <p>See <a
* href="https://docs.github.com/en/rest/reactions/reactions?apiVersion=2022-11-28#about-reactions">About
* GitHub Issue Comment reactions</a>
*/
@Value.Immutable
@GithubStyle
@JsonSerialize(as = ImmutableCommentReaction.class)
@JsonDeserialize(as = ImmutableCommentReaction.class)
public interface CommentReaction extends UpdateTracking {

/** Reaction ID. */
long id();

/** Reaction user. */
User user();

/** Reaction content. */
CommentReactionContent content();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*-
* -\-\-
* 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.comment;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.spotify.github.jackson.CommentReactionContentDeserializer;
import com.spotify.github.jackson.CommentReactionContentSerializer;

/**
* Comment reaction content.
*
* <p>See <a
* href="https://docs.github.com/en/rest/reactions/reactions?apiVersion=2022-11-28#about-reactions">About
* GitHub Issue Comment reactions</a>
*/
@JsonDeserialize(using = CommentReactionContentDeserializer.class)
@JsonSerialize(using = CommentReactionContentSerializer.class)
public enum CommentReactionContent {
THUMBS_UP("+1"), // πŸ‘
THUMBS_DOWN("-1"), // πŸ‘Ž
LAUGH("laugh"), // πŸ˜„
HOORAY("hooray"), // πŸŽ‰
CONFUSED("confused"), // πŸ˜•
HEART("heart"), // ❀️
ROCKET("rocket"), // πŸš€
EYES("eyes"); // πŸ‘€

private final String reaction;

CommentReactionContent(final String reaction) {
this.reaction = reaction;
}

@Override
public String toString() {
return reaction;
}
}
Loading

0 comments on commit 56337cf

Please sign in to comment.