Skip to content

Commit

Permalink
Remove internal map-only enum
Browse files Browse the repository at this point in the history
We took a change that added an enum that was used purely for mapping from
EventInfo.type to GHEvent. This seemed fine but that enum is used only by EventInfo.

This change removed that enum and adds a map to EventInfo to do the required mapping.
This avoids shoehorning mapping behavior in to the EnumUtils.
  • Loading branch information
bitwiseman committed Jun 1, 2021
1 parent a79971e commit c00d562
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 66 deletions.
3 changes: 1 addition & 2 deletions src/main/java/org/kohsuke/github/GHApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -119,7 +118,7 @@ public void setExternalUrl(String externalUrl) {
*/
public List<GHEvent> getEvents() {
return events.stream()
.map(e -> EnumUtils.getEnumOrDefault(GHEvent.class, e.toUpperCase(Locale.ROOT), GHEvent.UNKNOWN))
.map(e -> EnumUtils.getEnumOrDefault(GHEvent.class, e, GHEvent.UNKNOWN))
.collect(Collectors.toList());
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/kohsuke/github/GHAppInstallation.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -254,7 +253,7 @@ public void setPermissions(Map<String, GHPermissionType> permissions) {
*/
public List<GHEvent> getEvents() {
return events.stream()
.map(e -> EnumUtils.getEnumOrDefault(GHEvent.class, e.toUpperCase(Locale.ROOT), GHEvent.UNKNOWN))
.map(e -> EnumUtils.getEnumOrDefault(GHEvent.class, e, GHEvent.UNKNOWN))
.collect(Collectors.toList());
}

Expand Down
46 changes: 0 additions & 46 deletions src/main/java/org/kohsuke/github/GHEvent.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package org.kohsuke.github;

import org.kohsuke.github.internal.EnumUtils;

import java.util.Locale;

import javax.annotation.Nonnull;

/**
* Hook event type.
*
Expand Down Expand Up @@ -91,46 +87,4 @@ String symbol() {
return "*";
return name().toLowerCase(Locale.ENGLISH);
}

/**
* Representation of GitHub Event Type
*
* @see <a href="https://docs.github.com/en/developers/webhooks-and-events/github-event-types">GitHub event
* types</a>
*/
enum GitHubEventType {
CommitCommentEvent(COMMIT_COMMENT),
CreateEvent(CREATE),
DeleteEvent(DELETE),
ForkEvent(FORK),
GollumEvent(GOLLUM),
IssueCommentEvent(ISSUE_COMMENT),
IssuesEvent(ISSUES),
MemberEvent(MEMBER),
PublicEvent(PUBLIC),
PullRequestEvent(PULL_REQUEST),
PullRequestReviewEvent(PULL_REQUEST_REVIEW),
PullRequestReviewCommentEvent(PULL_REQUEST_REVIEW_COMMENT),
PushEvent(PUSH),
ReleaseEvent(RELEASE),
WatchEvent(WATCH),
UnknownEvent(UNKNOWN);

private final GHEvent event;
GitHubEventType(GHEvent event) {
this.event = event;
}

/**
* Required due to different naming conventions between different GitHub event names for Webhook events and
* GitHub events
*
* @param event
* the github event as a string to convert to Event enum
* @return GHEvent
*/
static GHEvent transformToGHEvent(@Nonnull String event) {
return EnumUtils.getEnumOrDefault(GitHubEventType.class, event, UnknownEvent).event;
}
}
}
47 changes: 44 additions & 3 deletions src/main/java/org/kohsuke/github/GHEventInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.kohsuke.github.GHEvent.GitHubEventType;

import java.io.IOException;
import java.util.Date;
import java.util.*;

/**
* Represents an event.
Expand All @@ -19,6 +18,16 @@ public class GHEventInfo extends GitHubInteractiveObject {

private long id;
private String created_at;

/**
* Representation of GitHub Event API Event Type.
*
* This is not the same as the values used for hook methods such as
* {@link GHRepository#createHook(String, Map, Collection, boolean)}.
*
* @see <a href="https://docs.github.com/en/developers/webhooks-and-events/github-event-types">GitHub event
* types</a>
*/
private String type;

// these are all shallow objects
Expand All @@ -41,13 +50,45 @@ public static class GHEventRepository {
private String name; // owner/repo
}

static final Map<String, GHEvent> mapTypeStringToEvent = createEventMap();

/**
* Map for GitHub Event API Event Type to GHEvent.
*
* @see <a href="https://docs.github.com/en/developers/webhooks-and-events/github-event-types">GitHub event
* types</a>
*/
private static Map<String, GHEvent> createEventMap() {
HashMap<String, GHEvent> map = new HashMap<>();
map.put("CommitCommentEvent", GHEvent.COMMIT_COMMENT);
map.put("CreateEvent", GHEvent.CREATE);
map.put("DeleteEvent", GHEvent.DELETE);
map.put("ForkEvent", GHEvent.FORK);
map.put("GollumEvent", GHEvent.GOLLUM);
map.put("IssueCommentEvent", GHEvent.ISSUE_COMMENT);
map.put("IssuesEvent", GHEvent.ISSUES);
map.put("MemberEvent", GHEvent.MEMBER);
map.put("PublicEvent", GHEvent.PUBLIC);
map.put("PullRequestEvent", GHEvent.PULL_REQUEST);
map.put("PullRequestReviewEvent", GHEvent.PULL_REQUEST_REVIEW);
map.put("PullRequestReviewCommentEvent", GHEvent.PULL_REQUEST_REVIEW_COMMENT);
map.put("PushEvent", GHEvent.PUSH);
map.put("ReleaseEvent", GHEvent.RELEASE);
map.put("WatchEvent", GHEvent.WATCH);
return Collections.unmodifiableMap(map);
}

static GHEvent transformTypeToGHEvent(String type) {
return mapTypeStringToEvent.getOrDefault(type, GHEvent.UNKNOWN);
}

/**
* Gets type.
*
* @return the type
*/
public GHEvent getType() {
return GitHubEventType.transformToGHEvent(type);
return transformTypeToGHEvent(type);
}

GHEventInfo wrapUp(GitHub root) {
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/org/kohsuke/github/GHHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
Expand Down Expand Up @@ -41,10 +40,7 @@ public String getName() {
public EnumSet<GHEvent> getEvents() {
EnumSet<GHEvent> s = EnumSet.noneOf(GHEvent.class);
for (String e : events) {
if (e.equals("*"))
s.add(GHEvent.ALL);
else
s.add(EnumUtils.getEnumOrDefault(GHEvent.class, e.toUpperCase(Locale.ROOT), GHEvent.UNKNOWN));
s.add(e.equals("*") ? GHEvent.ALL : EnumUtils.getEnumOrDefault(GHEvent.class, e, GHEvent.UNKNOWN));
}
return s;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/kohsuke/github/internal/EnumUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static <E extends Enum<E>> E getNullableEnumOrDefault(Class<E> enumClass,
if (value == null) {
return null;
}
return getEnumOrDefault(enumClass, value.toUpperCase(Locale.ROOT), defaultEnum);
return getEnumOrDefault(enumClass, value, defaultEnum);
}

/**
Expand All @@ -49,8 +49,8 @@ public static <E extends Enum<E>> E getNullableEnumOrDefault(Class<E> enumClass,
*/
public static <E extends Enum<E>> E getEnumOrDefault(Class<E> enumClass, String value, E defaultEnum) {
try {
return Enum.valueOf(enumClass, value);
} catch (IllegalArgumentException e) {
return Enum.valueOf(enumClass, value.toUpperCase(Locale.ROOT));
} catch (NullPointerException | IllegalArgumentException e) {
LOGGER.warning("Unknown value " + value + " for enum class " + enumClass.getName() + ", defaulting to "
+ defaultEnum.name());
return defaultEnum;
Expand Down
8 changes: 3 additions & 5 deletions src/test/java/org/kohsuke/github/GHEventTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.kohsuke.github;

import org.junit.Test;
import org.kohsuke.github.GHEvent.GitHubEventType;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
Expand All @@ -26,10 +25,9 @@ private static GHEvent oldTransformationFunction(String t) {

@Test
public void regressionTest() {
assertThat(GitHubEventType.transformToGHEvent("NewlyAddedOrBogusEvent"), is(GHEvent.UNKNOWN));
for (GitHubEventType gitHubEventType : GitHubEventType.values()) {
assertThat(GitHubEventType.transformToGHEvent(gitHubEventType.name()),
is(oldTransformationFunction(gitHubEventType.name())));
assertThat(GHEventInfo.transformTypeToGHEvent("NewlyAddedOrBogusEvent"), is(GHEvent.UNKNOWN));
for (String eventInfoType : GHEventInfo.mapTypeStringToEvent.keySet()) {
assertThat(GHEventInfo.transformTypeToGHEvent(eventInfoType), is(oldTransformationFunction(eventInfoType)));
}
}
}
2 changes: 2 additions & 0 deletions src/test/java/org/kohsuke/github/internal/EnumUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class EnumUtilsTest {

@Test
public void testGetEnum() {
assertThat(EnumUtils.getEnumOrDefault(TestEnum.class, null, TestEnum.UNKNOWN), equalTo(TestEnum.UNKNOWN));

assertThat(EnumUtils.getNullableEnumOrDefault(TestEnum.class, null, TestEnum.UNKNOWN), nullValue());
assertThat(EnumUtils.getNullableEnumOrDefault(TestEnum.class, "foobar", TestEnum.UNKNOWN),
equalTo(TestEnum.UNKNOWN));
Expand Down

0 comments on commit c00d562

Please sign in to comment.