Repository: hub4j/github-api
Pull request #1096 (2021-04-14 14:30:49)

Add SCHEDULE to GHEvent and add UNKNOWN handling to GHEvent
`SCHEDULE` is a valid event for scheduled workflow runs.

Also make `GHEvent` parsing more permissive as per our discussion from the previous weeks.


Patch:
diff --git a/src/main/java/org/kohsuke/github/GHEvent.java b/src/main/java/org/kohsuke/github/GHEvent.java
index 481dfa27b..ff61f057a 100644
--- a/src/main/java/org/kohsuke/github/GHEvent.java
+++ b/src/main/java/org/kohsuke/github/GHEvent.java
@@ -57,6 +57,7 @@ public enum GHEvent {
     REPOSITORY,
     REPOSITORY_IMPORT,
     REPOSITORY_VULNERABILITY_ALERT,
+    SCHEDULE,
     SECURITY_ADVISORY,
     STAR,
     STATUS,
@@ -66,6 +67,11 @@ public enum GHEvent {
     WORKFLOW_DISPATCH,
     WORKFLOW_RUN,
 
+    /**
+     * Special event type that means we haven't found an enum value corresponding to the event.
+     */
+    UNKNOWN,
+
     /**
      * Special event type that means "every possible event"
      */
diff --git a/src/main/java/org/kohsuke/github/GHEventInfo.java b/src/main/java/org/kohsuke/github/GHEventInfo.java
index c1ce2f024..e3214baa1 100644
--- a/src/main/java/org/kohsuke/github/GHEventInfo.java
+++ b/src/main/java/org/kohsuke/github/GHEventInfo.java
@@ -53,7 +53,7 @@ public class GHEventInfo extends GitHubInteractiveObject {
             if (e.name().replace("_", "").equalsIgnoreCase(t))
                 return e;
         }
-        return null; // unknown event type
+        return GHEvent.UNKNOWN;
     }
 
     GHEventInfo wrapUp(GitHub root) {
diff --git a/src/main/java/org/kohsuke/github/GHWorkflowRun.java b/src/main/java/org/kohsuke/github/GHWorkflowRun.java
index 235f935cf..b66e5a01f 100644
--- a/src/main/java/org/kohsuke/github/GHWorkflowRun.java
+++ b/src/main/java/org/kohsuke/github/GHWorkflowRun.java
@@ -188,7 +188,7 @@ public class GHWorkflowRun extends GHObject {
      * @return type of event
      */
     public GHEvent getEvent() {
-        return Enum.valueOf(GHEvent.class, event.toUpperCase(Locale.ROOT));
+        return EnumUtils.getNullableEnumOrDefault(GHEvent.class, event, GHEvent.UNKNOWN);
     }
 
     /**
diff --git a/src/main/java/org/kohsuke/github/internal/EnumUtils.java b/src/main/java/org/kohsuke/github/internal/EnumUtils.java
index b717cf03f..80a23ca23 100644
--- a/src/main/java/org/kohsuke/github/internal/EnumUtils.java
+++ b/src/main/java/org/kohsuke/github/internal/EnumUtils.java
@@ -1,12 +1,15 @@
 package org.kohsuke.github.internal;
 
 import java.util.Locale;
+import java.util.logging.Logger;
 
 /**
  * Utils for Enums.
  */
 public final class EnumUtils {
 
+    private static final Logger LOGGER = Logger.getLogger(EnumUtils.class.getName());
+
     /**
      * Returns an enum value matching the value if found, null if the value is null and {@code defaultEnum} if the value
      * cannot be matched to a value of the enum.
@@ -30,6 +33,8 @@ public final class EnumUtils {
         try {
             return Enum.valueOf(enumClass, value.toUpperCase(Locale.ROOT));
         } catch (IllegalArgumentException e) {
+            LOGGER.warning("Unknown value " + value + " for enum class " + enumClass.getName() + ", defaulting to "
+                    + defaultEnum.name());
             return defaultEnum;
         }
     }


Test patch:
diff --git a/src/test/java/org/kohsuke/github/EnumTest.java b/src/test/java/org/kohsuke/github/EnumTest.java
index b3c63e0dd..a83a6267c 100644
--- a/src/test/java/org/kohsuke/github/EnumTest.java
+++ b/src/test/java/org/kohsuke/github/EnumTest.java
@@ -27,7 +27,7 @@ public class EnumTest extends AbstractGitHubWireMockTest {
 
         assertThat(GHDirection.values().length, equalTo(2));
 
-        assertThat(GHEvent.values().length, equalTo(56));
+        assertThat(GHEvent.values().length, equalTo(58));
         assertThat(GHEvent.ALL.symbol(), equalTo("*"));
         assertThat(GHEvent.PULL_REQUEST.symbol(), equalTo(GHEvent.PULL_REQUEST.toString().toLowerCase()));