File tree Expand file tree Collapse file tree 3 files changed +84
-8
lines changed
Filter options
main/java/org/kohsuke/github
test/java/org/kohsuke/github Expand file tree Collapse file tree 3 files changed +84
-8
lines changed
Original file line number Diff line number Diff line change 2
2
3
3
import java .util .Locale ;
4
4
5
+ import javax .annotation .Nonnull ;
6
+
5
7
/**
6
8
* Hook event type.
7
9
*
@@ -85,4 +87,50 @@ String symbol() {
85
87
return "*" ;
86
88
return name ().toLowerCase (Locale .ENGLISH );
87
89
}
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
+ }
88
136
}
Original file line number Diff line number Diff line change 2
2
3
3
import com .fasterxml .jackson .databind .node .ObjectNode ;
4
4
import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
5
+ import org .kohsuke .github .GHEvent .GitHubEventType ;
5
6
6
7
import java .io .IOException ;
7
8
import java .util .Date ;
@@ -46,14 +47,7 @@ public static class GHEventRepository {
46
47
* @return the type
47
48
*/
48
49
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 );
57
51
}
58
52
59
53
GHEventInfo wrapUp (GitHub root ) {
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments