Repository: hub4j/github-api
Pull request #1126 (2021-04-21 12:15:51)

(refactor) Replace complex parsing logic from GHEvent.type to GHEvent with static mapping
https://github.com/hub4j/github-api/issues/1099

# Description

Replace complex parsing logic from GHEvent.type to GHEvent with static mapping 



Patch:
diff --git a/pom.xml b/pom.xml
index dcfc61e0b..d9506a031 100644
--- a/pom.xml
+++ b/pom.xml
@@ -2,7 +2,7 @@
   <modelVersion>4.0.0</modelVersion>
   <groupId>org.kohsuke</groupId>
   <artifactId>github-api</artifactId>
-  <version>1.129-SNAPSHOT</version>
+  <version>1.128-SNAPSHOT</version>
   <name>GitHub API for Java</name>
   <url>https://github-api.kohsuke.org/</url>
   <description>GitHub API for Java</description>
@@ -596,6 +596,16 @@
         </plugins>
       </build>
     </profile>
+    <profile>
+      <id>jdk11+</id>
+      <activation>
+        <jdk>[11,)</jdk>
+      </activation>
+      <properties>
+        <!-- this is required for GithubHttpUrlConnectionClient#setRequestMethod() to work with JDK 16+ -->
+        <surefire.argLine>--add-opens java.base/java.net=ALL-UNNAMED</surefire.argLine>
+      </properties>
+    </profile>
     <profile>
       <id>ci-non-windows</id>
       <activation>
diff --git a/src/main/java/org/kohsuke/github/GHEvent.java b/src/main/java/org/kohsuke/github/GHEvent.java
index ff61f057a..046d1ddf2 100644
--- a/src/main/java/org/kohsuke/github/GHEvent.java
+++ b/src/main/java/org/kohsuke/github/GHEvent.java
@@ -1,7 +1,11 @@
 package org.kohsuke.github;
 
+import org.kohsuke.github.internal.EnumUtils;
+
 import java.util.Locale;
 
+import javax.annotation.Nonnull;
+
 /**
  * Hook event type.
  *
@@ -85,4 +89,46 @@ public enum GHEvent {
             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;
+        }
+    }
 }
diff --git a/src/main/java/org/kohsuke/github/GHEventInfo.java b/src/main/java/org/kohsuke/github/GHEventInfo.java
index e3214baa1..43e178f9c 100644
--- a/src/main/java/org/kohsuke/github/GHEventInfo.java
+++ b/src/main/java/org/kohsuke/github/GHEventInfo.java
@@ -2,6 +2,7 @@ package org.kohsuke.github;
 
 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;
@@ -46,14 +47,7 @@ public class GHEventInfo extends GitHubInteractiveObject {
      * @return the type
      */
     public GHEvent getType() {
-        String t = type;
-        if (t.endsWith("Event"))
-            t = t.substring(0, t.length() - 5);
-        for (GHEvent e : GHEvent.values()) {
-            if (e.name().replace("_", "").equalsIgnoreCase(t))
-                return e;
-        }
-        return GHEvent.UNKNOWN;
+        return GitHubEventType.transformToGHEvent(type);
     }
 
     GHEventInfo wrapUp(GitHub root) {
diff --git a/src/main/java/org/kohsuke/github/internal/EnumUtils.java b/src/main/java/org/kohsuke/github/internal/EnumUtils.java
index 80a23ca23..4e81b9f8c 100644
--- a/src/main/java/org/kohsuke/github/internal/EnumUtils.java
+++ b/src/main/java/org/kohsuke/github/internal/EnumUtils.java
@@ -30,8 +30,26 @@ public final class EnumUtils {
         if (value == null) {
             return null;
         }
+        return getEnumOrDefault(enumClass, value.toUpperCase(Locale.ROOT), defaultEnum);
+    }
+
+    /**
+     * Returns an enum value matching the value if found, {@code defaultEnum} if the value is null or cannot be matched
+     * to a value of the enum.
+     *
+     * @param <E>
+     *            the type of the enum
+     * @param enumClass
+     *            the type of the enum
+     * @param value
+     *            the value to interpret
+     * @param defaultEnum
+     *            the default enum value if the value doesn't match one of the enum value
+     * @return an enum value
+     */
+    public static <E extends Enum<E>> E getEnumOrDefault(Class<E> enumClass, String value, E defaultEnum) {
         try {
-            return Enum.valueOf(enumClass, value.toUpperCase(Locale.ROOT));
+            return Enum.valueOf(enumClass, value);
         } catch (IllegalArgumentException e) {
             LOGGER.warning("Unknown value " + value + " for enum class " + enumClass.getName() + ", defaulting to "
                     + defaultEnum.name());


Test patch:
diff --git a/src/test/java/org/kohsuke/github/GHEventTest.java b/src/test/java/org/kohsuke/github/GHEventTest.java
new file mode 100644
index 000000000..97543a036
--- /dev/null
+++ b/src/test/java/org/kohsuke/github/GHEventTest.java
@@ -0,0 +1,35 @@
+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;
+
+public class GHEventTest {
+
+    /**
+     * Function from GHEventInfo to transform string event to GHEvent which has been replaced by static mapping due to
+     * complex parsing logic below
+     */
+    private static GHEvent oldTransformationFunction(String t) {
+        if (t.endsWith("Event")) {
+            t = t.substring(0, t.length() - 5);
+        }
+        for (GHEvent e : GHEvent.values()) {
+            if (e.name().replace("_", "").equalsIgnoreCase(t)) {
+                return e;
+            }
+        }
+        return GHEvent.UNKNOWN;
+    }
+
+    @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())));
+        }
+    }
+}