Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SCHEDULE to GHEvent and add UNKNOWN handling to GHEvent #1096

Merged
merged 4 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/java/org/kohsuke/github/GHEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public enum GHEvent {
REPOSITORY,
REPOSITORY_IMPORT,
REPOSITORY_VULNERABILITY_ALERT,
SCHEDULE,
SECURITY_ADVISORY,
STAR,
STATUS,
Expand All @@ -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"
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/kohsuke/github/GHEventInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public GHEvent getType() {
if (e.name().replace("_", "").equalsIgnoreCase(t))
return e;
}
Comment on lines 53 to 55
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh, I see the problem now. All this is to handle the completely different strings used in the REST API and actual event calls.
https://github.com/hub4j/github-api/blob/master/src/test/resources/org/kohsuke/github/AppTest/wiremock/testEventApi/__files/events-10.json#L283

There's got to be a better way to do this. I'll file an issue to come back to this.

return null; // unknown event type
return GHEvent.UNKNOWN;
}

GHEventInfo wrapUp(GitHub root) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/kohsuke/github/GHWorkflowRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public GHRepository getHeadRepository() {
* @return type of event
*/
public GHEvent getEvent() {
return Enum.valueOf(GHEvent.class, event.toUpperCase(Locale.ROOT));
return EnumUtils.getNullableEnumOrDefault(GHEvent.class, event, GHEvent.UNKNOWN);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/kohsuke/github/internal/EnumUtils.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -30,6 +33,8 @@ public static <E extends Enum<E>> E getNullableEnumOrDefault(Class<E> enumClass,
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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/kohsuke/github/EnumTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void touchEnums() {

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()));

Expand Down