mirror of
https://github.com/jlengrand/github-api.git
synced 2026-03-10 08:21:21 +00:00
(refactor) Replace complex parsing logic from GHEvent.type to GHEvent with static mapping
[https://github.com/hub4j/github-api/issues/1099]
This commit is contained in:
@@ -2,6 +2,8 @@ package org.kohsuke.github;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* Hook event type.
|
||||
*
|
||||
@@ -85,4 +87,50 @@ 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>
|
||||
*/
|
||||
public 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);
|
||||
|
||||
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
|
||||
*/
|
||||
public static GHEvent transformToGHEvent(@Nonnull String event) {
|
||||
try {
|
||||
GitHubEventType gitHubEventType = GitHubEventType.valueOf(event);
|
||||
return gitHubEventType.event;
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
return UNKNOWN;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
34
src/test/java/org/kohsuke/github/GHEventTest.java
Normal file
34
src/test/java/org/kohsuke/github/GHEventTest.java
Normal file
@@ -0,0 +1,34 @@
|
||||
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() {
|
||||
for (GitHubEventType gitHubEventType : GitHubEventType.values()) {
|
||||
assertThat(GitHubEventType.transformToGHEvent(gitHubEventType.name()),
|
||||
is(oldTransformationFunction(gitHubEventType.name())));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user