diff --git a/src/main/java/org/kohsuke/github/GHApp.java b/src/main/java/org/kohsuke/github/GHApp.java index 3424a65547..eb1f8c35a1 100644 --- a/src/main/java/org/kohsuke/github/GHApp.java +++ b/src/main/java/org/kohsuke/github/GHApp.java @@ -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; @@ -119,7 +118,7 @@ public void setExternalUrl(String externalUrl) { */ public List 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()); } diff --git a/src/main/java/org/kohsuke/github/GHAppInstallation.java b/src/main/java/org/kohsuke/github/GHAppInstallation.java index 9839180393..ee5c732c25 100644 --- a/src/main/java/org/kohsuke/github/GHAppInstallation.java +++ b/src/main/java/org/kohsuke/github/GHAppInstallation.java @@ -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; @@ -254,7 +253,7 @@ public void setPermissions(Map permissions) { */ public List 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()); } diff --git a/src/main/java/org/kohsuke/github/GHEvent.java b/src/main/java/org/kohsuke/github/GHEvent.java index b77e2e11b0..a8c3b245be 100644 --- a/src/main/java/org/kohsuke/github/GHEvent.java +++ b/src/main/java/org/kohsuke/github/GHEvent.java @@ -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. * @@ -91,46 +87,4 @@ String symbol() { return "*"; return name().toLowerCase(Locale.ENGLISH); } - - /** - * Representation of GitHub Event Type - * - * @see GitHub event - * types - */ - 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; - } - } } diff --git a/src/main/java/org/kohsuke/github/GHEventInfo.java b/src/main/java/org/kohsuke/github/GHEventInfo.java index 43e178f9c8..f550c9bccd 100644 --- a/src/main/java/org/kohsuke/github/GHEventInfo.java +++ b/src/main/java/org/kohsuke/github/GHEventInfo.java @@ -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. @@ -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 GitHub event + * types + */ private String type; // these are all shallow objects @@ -41,13 +50,45 @@ public static class GHEventRepository { private String name; // owner/repo } + static final Map mapTypeStringToEvent = createEventMap(); + + /** + * Map for GitHub Event API Event Type to GHEvent. + * + * @see GitHub event + * types + */ + private static Map createEventMap() { + HashMap 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) { diff --git a/src/main/java/org/kohsuke/github/GHHook.java b/src/main/java/org/kohsuke/github/GHHook.java index be79b17e0b..0d8193213d 100644 --- a/src/main/java/org/kohsuke/github/GHHook.java +++ b/src/main/java/org/kohsuke/github/GHHook.java @@ -8,7 +8,6 @@ import java.util.Collections; import java.util.EnumSet; import java.util.List; -import java.util.Locale; import java.util.Map; /** @@ -41,10 +40,7 @@ public String getName() { public EnumSet getEvents() { EnumSet 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; } diff --git a/src/main/java/org/kohsuke/github/internal/EnumUtils.java b/src/main/java/org/kohsuke/github/internal/EnumUtils.java index 4e81b9f8c5..fb07c7d4df 100644 --- a/src/main/java/org/kohsuke/github/internal/EnumUtils.java +++ b/src/main/java/org/kohsuke/github/internal/EnumUtils.java @@ -30,7 +30,7 @@ public static > E getNullableEnumOrDefault(Class enumClass, if (value == null) { return null; } - return getEnumOrDefault(enumClass, value.toUpperCase(Locale.ROOT), defaultEnum); + return getEnumOrDefault(enumClass, value, defaultEnum); } /** @@ -49,14 +49,11 @@ public static > E getNullableEnumOrDefault(Class enumClass, */ public static > E getEnumOrDefault(Class 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; } } - - private EnumUtils() { - } } diff --git a/src/test/java/org/kohsuke/github/GHEventTest.java b/src/test/java/org/kohsuke/github/GHEventTest.java index 97543a036a..aea0fa14b1 100644 --- a/src/test/java/org/kohsuke/github/GHEventTest.java +++ b/src/test/java/org/kohsuke/github/GHEventTest.java @@ -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; @@ -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))); } } }