(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:
Akash Rindhe
2021-04-21 20:07:10 +08:00
parent f6e8a2c7c6
commit 72dc5c5d18
3 changed files with 84 additions and 8 deletions

View File

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