Merge branch 'main' into upgrade-commons-io

This commit is contained in:
Liam Newman
2021-04-27 02:33:44 -07:00
committed by GitHub
5 changed files with 103 additions and 20 deletions

12
pom.xml
View File

@@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.kohsuke</groupId>
<artifactId>github-api</artifactId>
<version>1.128-SNAPSHOT</version>
<version>1.129-SNAPSHOT</version>
<name>GitHub API for Java</name>
<url>https://github-api.kohsuke.org/</url>
<description>GitHub API for Java</description>
@@ -596,16 +596,6 @@
</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>

View File

@@ -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;
}
}
}

View File

@@ -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) {

View File

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

View File

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