Skip to content

Commit 85aa2ad

Browse files
committed
Added reaction API
1 parent 818f6dc commit 85aa2ad

9 files changed

Lines changed: 239 additions & 5 deletions

File tree

src/main/java/org/kohsuke/github/GHCommitComment.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.net.URL;
66
import java.util.Date;
77

8+
import static org.kohsuke.github.Previews.SQUIRREL_GIRL;
9+
810
/**
911
* A comment attached to a commit (or a specific line in a specific file of a commit.)
1012
*
@@ -15,7 +17,7 @@
1517
*/
1618
@SuppressFBWarnings(value = {"UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD",
1719
"NP_UNWRITTEN_FIELD"}, justification = "JSON API")
18-
public class GHCommitComment extends GHObject {
20+
public class GHCommitComment extends GHObject implements Reactable {
1921
private GHRepository owner;
2022

2123
String body, html_url, commit_id;
@@ -86,6 +88,29 @@ public void update(String body) throws IOException {
8688
this.body = body;
8789
}
8890

91+
@Preview @Deprecated
92+
public GHReaction createReaction(ReactionContent content) throws IOException {
93+
return new Requester(owner.root)
94+
.withPreview(SQUIRREL_GIRL)
95+
.with("content", content.getContent())
96+
.to(getApiTail()+"/reactions", GHReaction.class).wrap(owner.root);
97+
}
98+
99+
@Preview @Deprecated
100+
public PagedIterable<GHReaction> listReactions() {
101+
return new PagedIterable<GHReaction>() {
102+
public PagedIterator<GHReaction> _iterator(int pageSize) {
103+
return new PagedIterator<GHReaction>(owner.root.retrieve().withPreview(SQUIRREL_GIRL).asIterator(getApiTail()+"/reactions", GHReaction[].class, pageSize)) {
104+
@Override
105+
protected void wrapUp(GHReaction[] page) {
106+
for (GHReaction c : page)
107+
c.wrap(owner.root);
108+
}
109+
};
110+
}
111+
};
112+
}
113+
89114
/**
90115
* Deletes this comment.
91116
*/

src/main/java/org/kohsuke/github/GHIssue.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import java.util.List;
3636
import java.util.Locale;
3737

38+
import static org.kohsuke.github.Previews.SQUIRREL_GIRL;
39+
3840
/**
3941
* Represents an issue on GitHub.
4042
*
@@ -44,7 +46,7 @@
4446
* @see GitHub#searchIssues()
4547
* @see GHIssueSearchBuilder
4648
*/
47-
public class GHIssue extends GHObject {
49+
public class GHIssue extends GHObject implements Reactable{
4850
GitHub root;
4951
GHRepository owner;
5052

@@ -217,6 +219,29 @@ protected void wrapUp(GHIssueComment[] page) {
217219
};
218220
}
219221

222+
@Preview @Deprecated
223+
public GHReaction createReaction(ReactionContent content) throws IOException {
224+
return new Requester(owner.root)
225+
.withPreview(SQUIRREL_GIRL)
226+
.with("content", content.getContent())
227+
.to(getApiRoute()+"/reactions", GHReaction.class).wrap(root);
228+
}
229+
230+
@Preview @Deprecated
231+
public PagedIterable<GHReaction> listReactions() {
232+
return new PagedIterable<GHReaction>() {
233+
public PagedIterator<GHReaction> _iterator(int pageSize) {
234+
return new PagedIterator<GHReaction>(owner.root.retrieve().withPreview(SQUIRREL_GIRL).asIterator(getApiRoute()+"/reactions", GHReaction[].class, pageSize)) {
235+
@Override
236+
protected void wrapUp(GHReaction[] page) {
237+
for (GHReaction c : page)
238+
c.wrap(owner.root);
239+
}
240+
};
241+
}
242+
};
243+
}
244+
220245
protected String getApiRoute() {
221246
return getIssuesApiRoute();
222247
}

src/main/java/org/kohsuke/github/GHIssueComment.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626
import java.io.IOException;
2727
import java.net.URL;
2828

29+
import static org.kohsuke.github.Previews.SQUIRREL_GIRL;
30+
2931
/**
3032
* Comment to the issue
3133
*
3234
* @author Kohsuke Kawaguchi
3335
*/
34-
public class GHIssueComment extends GHObject {
36+
public class GHIssueComment extends GHObject implements Reactable {
3537
GHIssue owner;
3638

3739
private String body, gravatar_id;
@@ -93,7 +95,30 @@ public void update(String body) throws IOException {
9395
public void delete() throws IOException {
9496
new Requester(owner.root).method("DELETE").to(getApiRoute());
9597
}
96-
98+
99+
@Preview @Deprecated
100+
public GHReaction createReaction(ReactionContent content) throws IOException {
101+
return new Requester(owner.root)
102+
.withPreview(SQUIRREL_GIRL)
103+
.with("content", content.getContent())
104+
.to(getApiRoute()+"/reactions", GHReaction.class).wrap(owner.root);
105+
}
106+
107+
@Preview @Deprecated
108+
public PagedIterable<GHReaction> listReactions() {
109+
return new PagedIterable<GHReaction>() {
110+
public PagedIterator<GHReaction> _iterator(int pageSize) {
111+
return new PagedIterator<GHReaction>(owner.root.retrieve().withPreview(SQUIRREL_GIRL).asIterator(getApiRoute()+"/reactions", GHReaction[].class, pageSize)) {
112+
@Override
113+
protected void wrapUp(GHReaction[] page) {
114+
for (GHReaction c : page)
115+
c.wrap(owner.root);
116+
}
117+
};
118+
}
119+
};
120+
}
121+
97122
private String getApiRoute() {
98123
return "/repos/"+owner.getRepository().getOwnerName()+"/"+owner.getRepository().getName()+"/issues/comments/" + id;
99124
}

src/main/java/org/kohsuke/github/GHPullRequestReviewComment.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@
2626
import java.io.IOException;
2727
import java.net.URL;
2828

29+
import static org.kohsuke.github.Previews.*;
30+
2931
/**
3032
* Review comment to the pull request
3133
*
3234
* @author Julien Henry
3335
* @see GHPullRequest#listReviewComments()
3436
* @see GHPullRequest#createReviewComment(String, String, String, int)
3537
*/
36-
public class GHPullRequestReviewComment extends GHObject {
38+
public class GHPullRequestReviewComment extends GHObject implements Reactable {
3739
GHPullRequest owner;
3840

3941
private String body;
@@ -103,4 +105,27 @@ public void update(String body) throws IOException {
103105
public void delete() throws IOException {
104106
new Requester(owner.root).method("DELETE").to(getApiRoute());
105107
}
108+
109+
@Preview @Deprecated
110+
public GHReaction createReaction(ReactionContent content) throws IOException {
111+
return new Requester(owner.root)
112+
.withPreview(SQUIRREL_GIRL)
113+
.with("content", content.getContent())
114+
.to(getApiRoute()+"/reactions", GHReaction.class).wrap(owner.root);
115+
}
116+
117+
@Preview @Deprecated
118+
public PagedIterable<GHReaction> listReactions() {
119+
return new PagedIterable<GHReaction>() {
120+
public PagedIterator<GHReaction> _iterator(int pageSize) {
121+
return new PagedIterator<GHReaction>(owner.root.retrieve().withPreview(SQUIRREL_GIRL).asIterator(getApiRoute()+"/reactions", GHReaction[].class, pageSize)) {
122+
@Override
123+
protected void wrapUp(GHReaction[] page) {
124+
for (GHReaction c : page)
125+
c.wrap(owner.root);
126+
}
127+
};
128+
}
129+
};
130+
}
106131
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.kohsuke.github;
2+
3+
import java.io.IOException;
4+
import java.net.URL;
5+
6+
import static org.kohsuke.github.Previews.SQUIRREL_GIRL;
7+
8+
/**
9+
* Reaction to issue, comment, PR, and so on.
10+
*
11+
* @author Kohsuke Kawaguchi
12+
* @see Reactable
13+
*/
14+
@Preview @Deprecated
15+
public class GHReaction extends GHObject {
16+
private GitHub root;
17+
18+
private GHUser user;
19+
private ReactionContent content;
20+
21+
/*package*/ GHReaction wrap(GitHub root) {
22+
this.root = root;
23+
user.wrapUp(root);
24+
return this;
25+
}
26+
27+
/**
28+
* The kind of reaction left.
29+
*/
30+
public ReactionContent getContent() {
31+
return content;
32+
}
33+
34+
/**
35+
* User who left the reaction.
36+
*/
37+
public GHUser getUser() {
38+
return user;
39+
}
40+
41+
/**
42+
* Reaction has no HTML URL. Don't call this method.
43+
*/
44+
@Deprecated
45+
public URL getHtmlUrl() {
46+
return null;
47+
}
48+
49+
/**
50+
* Removes this reaction.
51+
*/
52+
public void delete() throws IOException {
53+
new Requester(root).method("DELETE").withPreview(SQUIRREL_GIRL).to("/reactions/"+id);
54+
}
55+
}

src/main/java/org/kohsuke/github/Previews.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
/*package*/ class Previews {
77
static final String LOKI = "application/vnd.github.loki-preview+json";
88
static final String DRAX = "application/vnd.github.drax-preview+json";
9+
static final String SQUIRREL_GIRL = "application/vnd.github.squirrel-girl-preview";
910
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.kohsuke.github;
2+
3+
import java.io.IOException;
4+
5+
/**
6+
* Those {@link GHObject}s that can have {@linkplain GHReaction reactions}.
7+
*
8+
* @author Kohsuke Kawaguchi
9+
*/
10+
@Preview @Deprecated
11+
public interface Reactable {
12+
/**
13+
* List all the reactions left to this object.
14+
*/
15+
@Preview @Deprecated
16+
PagedIterable<GHReaction> listReactions();
17+
18+
/**
19+
* Leaves a reaction to this object.
20+
*/
21+
@Preview @Deprecated
22+
GHReaction createReaction(ReactionContent content) throws IOException;
23+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.kohsuke.github;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonValue;
5+
6+
/**
7+
* Content of reactions.
8+
*
9+
* @author Kohsuke Kawaguchi
10+
* @see <a href="https://developer.github.com/v3/reactions/">API documentation</a>
11+
* @see GHReaction
12+
*/
13+
public enum ReactionContent {
14+
PLUS_ONE("+1"),
15+
MINUS_ONE("-1"),
16+
LAUGH("laugh"),
17+
CONFUSED("confused"),
18+
HEART("heart"),
19+
HOORAY("hooray");
20+
21+
private final String content;
22+
23+
ReactionContent(String content) {
24+
this.content = content;
25+
}
26+
27+
@JsonValue
28+
public String getContent() {
29+
return content;
30+
}
31+
32+
@JsonCreator
33+
public static ReactionContent forContent(String content) {
34+
for (ReactionContent c : ReactionContent.values()) {
35+
if (c.getContent().equals(content))
36+
return c;
37+
}
38+
return null;
39+
}
40+
}

src/test/java/org/kohsuke/github/AppTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,21 @@ public void checkToString() throws Exception {
870870
System.out.println(r.getIssue(1));
871871
}
872872

873+
@Test
874+
public void reactions() throws Exception {
875+
GHIssue i = gitHub.getRepository("kohsuke/github-api").getIssue(311);
876+
877+
// retrieval
878+
GHReaction r = i.listReactions().iterator().next();
879+
assert r.getUser().getName().equals("kohsuke");
880+
assert r.getContent()==ReactionContent.HEART;
881+
882+
// CRUD
883+
GHReaction a = i.createReaction(ReactionContent.HOORAY);
884+
assert a.getUser().equals(gitHub.getMyself());
885+
a.delete();
886+
}
887+
873888
private void kohsuke() {
874889
String login = getUser().getLogin();
875890
Assume.assumeTrue(login.equals("kohsuke") || login.equals("kohsuke2"));

0 commit comments

Comments
 (0)