Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 72dc5c5

Browse filesBrowse files
committed
(refactor) Replace complex parsing logic from GHEvent.type to GHEvent with static mapping
[hub4j#1099]
1 parent f6e8a2c commit 72dc5c5
Copy full SHA for 72dc5c5

File tree

Expand file treeCollapse file tree

3 files changed

+84
-8
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+84
-8
lines changed

‎src/main/java/org/kohsuke/github/GHEvent.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHEvent.java
+48Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.Locale;
44

5+
import javax.annotation.Nonnull;
6+
57
/**
68
* Hook event type.
79
*
@@ -85,4 +87,50 @@ String symbol() {
8587
return "*";
8688
return name().toLowerCase(Locale.ENGLISH);
8789
}
90+
91+
/**
92+
* Representation of GitHub Event Type
93+
*
94+
* @see <a href="https://docs.github.com/en/developers/webhooks-and-events/github-event-types">GitHub event
95+
* types</a>
96+
*/
97+
public enum GitHubEventType {
98+
CommitCommentEvent(COMMIT_COMMENT),
99+
CreateEvent(CREATE),
100+
DeleteEvent(DELETE),
101+
ForkEvent(FORK),
102+
GollumEvent(GOLLUM),
103+
IssueCommentEvent(ISSUE_COMMENT),
104+
IssuesEvent(ISSUES),
105+
MemberEvent(MEMBER),
106+
PublicEvent(PUBLIC),
107+
PullRequestEvent(PULL_REQUEST),
108+
PullRequestReviewEvent(PULL_REQUEST_REVIEW),
109+
PullRequestReviewCommentEvent(PULL_REQUEST_REVIEW_COMMENT),
110+
PushEvent(PUSH),
111+
ReleaseEvent(RELEASE),
112+
WatchEvent(WATCH);
113+
114+
private final GHEvent event;
115+
GitHubEventType(GHEvent event) {
116+
this.event = event;
117+
}
118+
119+
/**
120+
* Required due to different naming conventions between different GitHub event names for Webhook events and
121+
* GitHub events
122+
*
123+
* @param event
124+
* the github event as a string to convert to Event enum
125+
* @return GHEvent
126+
*/
127+
public static GHEvent transformToGHEvent(@Nonnull String event) {
128+
try {
129+
GitHubEventType gitHubEventType = GitHubEventType.valueOf(event);
130+
return gitHubEventType.event;
131+
} catch (IllegalArgumentException ignored) {
132+
return UNKNOWN;
133+
}
134+
}
135+
}
88136
}

‎src/main/java/org/kohsuke/github/GHEventInfo.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHEventInfo.java
+2-8Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.databind.node.ObjectNode;
44
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
5+
import org.kohsuke.github.GHEvent.GitHubEventType;
56

67
import java.io.IOException;
78
import java.util.Date;
@@ -46,14 +47,7 @@ public static class GHEventRepository {
4647
* @return the type
4748
*/
4849
public GHEvent getType() {
49-
String t = type;
50-
if (t.endsWith("Event"))
51-
t = t.substring(0, t.length() - 5);
52-
for (GHEvent e : GHEvent.values()) {
53-
if (e.name().replace("_", "").equalsIgnoreCase(t))
54-
return e;
55-
}
56-
return GHEvent.UNKNOWN;
50+
return GitHubEventType.transformToGHEvent(type);
5751
}
5852

5953
GHEventInfo wrapUp(GitHub root) {
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.kohsuke.github;
2+
3+
import org.junit.Test;
4+
import org.kohsuke.github.GHEvent.GitHubEventType;
5+
6+
import static org.hamcrest.MatcherAssert.assertThat;
7+
import static org.hamcrest.Matchers.is;
8+
9+
public class GHEventTest {
10+
11+
/**
12+
* Function from GHEventInfo to transform string event to GHEvent which has been replaced by static mapping due to
13+
* complex parsing logic below
14+
*/
15+
private static GHEvent oldTransformationFunction(String t) {
16+
if (t.endsWith("Event")) {
17+
t = t.substring(0, t.length() - 5);
18+
}
19+
for (GHEvent e : GHEvent.values()) {
20+
if (e.name().replace("_", "").equalsIgnoreCase(t)) {
21+
return e;
22+
}
23+
}
24+
return GHEvent.UNKNOWN;
25+
}
26+
27+
@Test
28+
public void regressionTest() {
29+
for (GitHubEventType gitHubEventType : GitHubEventType.values()) {
30+
assertThat(GitHubEventType.transformToGHEvent(gitHubEventType.name()),
31+
is(oldTransformationFunction(gitHubEventType.name())));
32+
}
33+
}
34+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.