Skip to content

Commit

Permalink
Merge pull request #1319 from gsmet/discussion-payload
Browse files Browse the repository at this point in the history
Add support for the Discussion payload
  • Loading branch information
bitwiseman authored Nov 24, 2021
2 parents 48fe488 + 82cccc7 commit f47c3e4
Show file tree
Hide file tree
Showing 6 changed files with 989 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/main/java/org/kohsuke/github/GHEventPayload.java
Original file line number Diff line number Diff line change
Expand Up @@ -1477,4 +1477,39 @@ public GHLabelChanges getChanges() {
return changes;
}
}

/**
* A discussion was created, edited, deleted, pinned, unpinned, locked, unlocked, transferred, category_changed,
* answered, or unanswered.
*
* @see <a href=
* "https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#discussion">
* discussion event</a>
*/
public static class Discussion extends GHEventPayload {

private GHRepositoryDiscussion discussion;

private GHLabel label;

/**
* Gets discussion.
*
* @return the discussion
*/
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected")
public GHRepositoryDiscussion getDiscussion() {
return discussion;
}

/**
* Gets the added or removed label for labeled/unlabeled events.
*
* @return label the added or removed label
*/
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected")
public GHLabel getLabel() {
return label;
}
}
}
165 changes: 165 additions & 0 deletions src/main/java/org/kohsuke/github/GHRepositoryDiscussion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package org.kohsuke.github;

import org.kohsuke.github.internal.EnumUtils;

import java.io.IOException;
import java.net.URL;
import java.util.Date;

/**
* A discussion in the repository.
* <p>
* This is different from Teams discussions (see {@link GHDiscussion}).
* <p>
* The discussion event exposes the GraphQL object (more or less - the ids are handled differently for instance)
* directly. The new Discussions API is only available through GraphQL so for now you cannot execute any actions on this
* object.
*
* @author Guillaume Smet
* @see <a href="https://docs.github.com/en/graphql/guides/using-the-graphql-api-for-discussions#discussion">The GraphQL
* API for Discussions</a>
*/
public class GHRepositoryDiscussion extends GHObject {

private Category category;

private String answerHtmlUrl;

private String answerChosenAt;
private GHUser answerChosenBy;
private String htmlUrl;

private int number;
private String title;
private GHUser user;
private String state;
private boolean locked;
private int comments;
private GHCommentAuthorAssociation authorAssociation;
private String activeLockReason;
private String body;
private String timelineUrl;

public Category getCategory() {
return category;
}

public URL getAnswerHtmlUrl() {
return GitHubClient.parseURL(answerHtmlUrl);
}

public Date getAnswerChosenAt() {
return GitHubClient.parseDate(answerChosenAt);
}

public GHUser getAnswerChosenBy() throws IOException {
return root().intern(answerChosenBy);
}

public URL getHtmlUrl() {
return GitHubClient.parseURL(htmlUrl);
}

public int getNumber() {
return number;
}

public String getTitle() {
return title;
}

public GHUser getUser() throws IOException {
return root().intern(user);
}

public State getState() {
return EnumUtils.getEnumOrDefault(State.class, state, State.UNKNOWN);
}

public boolean isLocked() {
return locked;
}

public int getComments() {
return comments;
}

public GHCommentAuthorAssociation getAuthorAssociation() {
return authorAssociation;
}

public String getActiveLockReason() {
return activeLockReason;
}

public String getBody() {
return body;
}

public String getTimelineUrl() {
return timelineUrl;
}

/**
* Category of a discussion.
* <p>
* Note that while it is relatively close to the GraphQL objects, some of the fields such as the id are handled
* differently.
*
* @see <a href=
* "https://docs.github.com/en/graphql/guides/using-the-graphql-api-for-discussions#discussioncategory">The
* GraphQL API for Discussions</a>
*/
public static class Category {

private long id;
private long repositoryId;
private String emoji;
private String name;
private String description;
private String createdAt;
private String updatedAt;
private String slug;
private boolean isAnswerable;

public long getId() {
return id;
}

public long getRepositoryId() {
return repositoryId;
}

public String getEmoji() {
return emoji;
}

public String getName() {
return name;
}

public String getDescription() {
return description;
}

public Date getCreatedAt() {
return GitHubClient.parseDate(createdAt);
}

public Date getUpdatedAt() {
return GitHubClient.parseDate(updatedAt);
}

public String getSlug() {
return slug;
}

public boolean isAnswerable() {
return isAnswerable;
}
}

public enum State {
OPEN, LOCKED, UNKNOWN;
}
}
152 changes: 152 additions & 0 deletions src/test/java/org/kohsuke/github/GHEventPayloadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -919,4 +919,156 @@ public void label_deleted() throws Exception {
assertThat(label.isDefault(), is(false));
assertThat(label.getDescription(), is("description"));
}

@Test
public void discussion_created() throws Exception {
final GHEventPayload.Discussion discussionPayload = GitHub.offline()
.parseEventPayload(payload.asReader(), GHEventPayload.Discussion.class);

assertThat(discussionPayload.getAction(), is("created"));
assertThat(discussionPayload.getRepository().getFullName(), is("gsmet/quarkus-bot-java-playground"));
assertThat(discussionPayload.getSender().getLogin(), is("gsmet"));

GHRepositoryDiscussion discussion = discussionPayload.getDiscussion();

GHRepositoryDiscussion.Category category = discussion.getCategory();

assertThat(category.getId(), is(33522033L));
assertThat(category.getEmoji(), is(":pray:"));
assertThat(category.getName(), is("Q&A"));
assertThat(category.getDescription(), is("Ask the community for help"));
assertThat(category.getCreatedAt().getTime(), is(1636991431000L));
assertThat(category.getUpdatedAt().getTime(), is(1636991431000L));
assertThat(category.getSlug(), is("q-a"));
assertThat(category.isAnswerable(), is(true));

assertThat(discussion.getAnswerHtmlUrl(), is(nullValue()));
assertThat(discussion.getAnswerChosenAt(), is(nullValue()));
assertThat(discussion.getAnswerChosenBy(), is(nullValue()));

assertThat(discussion.getHtmlUrl().toString(),
is("https://github.com/gsmet/quarkus-bot-java-playground/discussions/78"));
assertThat(discussion.getId(), is(3698909L));
assertThat(discussion.getNodeId(), is("D_kwDOEq3cwc4AOHDd"));
assertThat(discussion.getNumber(), is(78));
assertThat(discussion.getTitle(), is("Title of discussion"));

assertThat(discussion.getUser().getLogin(), is("gsmet"));
assertThat(discussion.getUser().getId(), is(1279749L));
assertThat(discussion.getUser().getNodeId(), is("MDQ6VXNlcjEyNzk3NDk="));

assertThat(discussion.getState(), is(GHRepositoryDiscussion.State.OPEN));
assertThat(discussion.isLocked(), is(false));
assertThat(discussion.getComments(), is(0));
assertThat(discussion.getCreatedAt().getTime(), is(1637584949000L));
assertThat(discussion.getUpdatedAt().getTime(), is(1637584949000L));
assertThat(discussion.getAuthorAssociation(), is(GHCommentAuthorAssociation.OWNER));
assertThat(discussion.getActiveLockReason(), is(nullValue()));
assertThat(discussion.getBody(), is("Body of discussion."));
}

@Test
public void discussion_answered() throws Exception {
final GHEventPayload.Discussion discussionPayload = GitHub.offline()
.parseEventPayload(payload.asReader(), GHEventPayload.Discussion.class);

assertThat(discussionPayload.getAction(), is("answered"));
assertThat(discussionPayload.getRepository().getFullName(), is("gsmet/quarkus-bot-java-playground"));
assertThat(discussionPayload.getSender().getLogin(), is("gsmet"));

GHRepositoryDiscussion discussion = discussionPayload.getDiscussion();

GHRepositoryDiscussion.Category category = discussion.getCategory();

assertThat(category.getId(), is(33522033L));
assertThat(category.getEmoji(), is(":pray:"));
assertThat(category.getName(), is("Q&A"));
assertThat(category.getDescription(), is("Ask the community for help"));
assertThat(category.getCreatedAt().getTime(), is(1636991431000L));
assertThat(category.getUpdatedAt().getTime(), is(1636991431000L));
assertThat(category.getSlug(), is("q-a"));
assertThat(category.isAnswerable(), is(true));

assertThat(discussion.getAnswerHtmlUrl().toString(),
is("https://github.com/gsmet/quarkus-bot-java-playground/discussions/78#discussioncomment-1681242"));
assertThat(discussion.getAnswerChosenAt().getTime(), is(1637585047000L));
assertThat(discussion.getAnswerChosenBy().getLogin(), is("gsmet"));

assertThat(discussion.getHtmlUrl().toString(),
is("https://github.com/gsmet/quarkus-bot-java-playground/discussions/78"));
assertThat(discussion.getId(), is(3698909L));
assertThat(discussion.getNodeId(), is("D_kwDOEq3cwc4AOHDd"));
assertThat(discussion.getNumber(), is(78));
assertThat(discussion.getTitle(), is("Title of discussion"));

assertThat(discussion.getUser().getLogin(), is("gsmet"));
assertThat(discussion.getUser().getId(), is(1279749L));
assertThat(discussion.getUser().getNodeId(), is("MDQ6VXNlcjEyNzk3NDk="));

assertThat(discussion.getState(), is(GHRepositoryDiscussion.State.OPEN));
assertThat(discussion.isLocked(), is(false));
assertThat(discussion.getComments(), is(1));
assertThat(discussion.getCreatedAt().getTime(), is(1637584949000L));
assertThat(discussion.getUpdatedAt().getTime(), is(1637585047000L));
assertThat(discussion.getAuthorAssociation(), is(GHCommentAuthorAssociation.OWNER));
assertThat(discussion.getActiveLockReason(), is(nullValue()));
assertThat(discussion.getBody(), is("Body of discussion."));
}

@Test
public void discussion_labeled() throws Exception {
final GHEventPayload.Discussion discussionPayload = GitHub.offline()
.parseEventPayload(payload.asReader(), GHEventPayload.Discussion.class);

assertThat(discussionPayload.getAction(), is("labeled"));
assertThat(discussionPayload.getRepository().getFullName(), is("gsmet/quarkus-bot-java-playground"));
assertThat(discussionPayload.getSender().getLogin(), is("gsmet"));

GHRepositoryDiscussion discussion = discussionPayload.getDiscussion();

GHRepositoryDiscussion.Category category = discussion.getCategory();

assertThat(category.getId(), is(33522033L));
assertThat(category.getEmoji(), is(":pray:"));
assertThat(category.getName(), is("Q&A"));
assertThat(category.getDescription(), is("Ask the community for help"));
assertThat(category.getCreatedAt().getTime(), is(1636991431000L));
assertThat(category.getUpdatedAt().getTime(), is(1636991431000L));
assertThat(category.getSlug(), is("q-a"));
assertThat(category.isAnswerable(), is(true));

assertThat(discussion.getAnswerHtmlUrl(), is(nullValue()));
assertThat(discussion.getAnswerChosenAt(), is(nullValue()));
assertThat(discussion.getAnswerChosenBy(), is(nullValue()));

assertThat(discussion.getHtmlUrl().toString(),
is("https://github.com/gsmet/quarkus-bot-java-playground/discussions/78"));
assertThat(discussion.getId(), is(3698909L));
assertThat(discussion.getNodeId(), is("D_kwDOEq3cwc4AOHDd"));
assertThat(discussion.getNumber(), is(78));
assertThat(discussion.getTitle(), is("Title of discussion"));

assertThat(discussion.getUser().getLogin(), is("gsmet"));
assertThat(discussion.getUser().getId(), is(1279749L));
assertThat(discussion.getUser().getNodeId(), is("MDQ6VXNlcjEyNzk3NDk="));

assertThat(discussion.getState(), is(GHRepositoryDiscussion.State.OPEN));
assertThat(discussion.isLocked(), is(false));
assertThat(discussion.getComments(), is(0));
assertThat(discussion.getCreatedAt().getTime(), is(1637584949000L));
assertThat(discussion.getUpdatedAt().getTime(), is(1637584961000L));
assertThat(discussion.getAuthorAssociation(), is(GHCommentAuthorAssociation.OWNER));
assertThat(discussion.getActiveLockReason(), is(nullValue()));
assertThat(discussion.getBody(), is("Body of discussion."));

GHLabel label = discussionPayload.getLabel();
assertThat(label.getId(), is(2543373314L));
assertThat(label.getNodeId(), is("MDU6TGFiZWwyNTQzMzczMzE0"));
assertThat(label.getUrl().toString(),
is("https://api.github.com/repos/gsmet/quarkus-bot-java-playground/labels/area/hibernate-validator"));
assertThat(label.getName(), is("area/hibernate-validator"));
assertThat(label.getColor(), is("ededed"));
assertThat(label.isDefault(), is(false));
assertThat(label.getDescription(), is(nullValue()));
}
}
Loading

0 comments on commit f47c3e4

Please sign in to comment.