forked from hub4j/github-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGHEventInfo.java
More file actions
188 lines (170 loc) · 5.92 KB
/
Copy pathGHEventInfo.java
File metadata and controls
188 lines (170 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package org.kohsuke.github;
import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.util.*;
// TODO: Auto-generated Javadoc
/**
* Represents an event.
*
* @author Kohsuke Kawaguchi
*/
@SuppressFBWarnings(value = "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", justification = "JSON API")
public class GHEventInfo extends GitHubInteractiveObject {
// we don't want to expose Jackson dependency to the user. This needs databinding
private ObjectNode payload;
private long id;
private String created_at;
/**
* Representation of GitHub Event API Event Type.
*
* This is not the same as the values used for hook methods such as
* {@link GHRepository#createHook(String, Map, Collection, boolean)}.
*
* @see <a href="https://docs.github.com/en/developers/webhooks-and-events/github-event-types">GitHub event
* types</a>
*/
private String type;
// these are all shallow objects
private GHEventRepository repo;
private GHUser actor;
private GHOrganization org;
/**
* Inside the event JSON model, GitHub uses a slightly different format.
*/
@SuppressFBWarnings(
value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD",
"UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR" },
justification = "JSON API")
public static class GHEventRepository {
@SuppressFBWarnings(value = "UUF_UNUSED_FIELD", justification = "We don't provide it in API now")
private long id;
@SuppressFBWarnings(value = "UUF_UNUSED_FIELD", justification = "We don't provide it in API now")
private String url; // repository API URL
private String name; // owner/repo
}
/** The Constant mapTypeStringToEvent. */
static final Map<String, GHEvent> mapTypeStringToEvent = createEventMap();
/**
* Map for GitHub Event API Event Type to GHEvent.
*
* @see <a href="https://docs.github.com/en/developers/webhooks-and-events/github-event-types">GitHub event
* types</a>
*/
private static Map<String, GHEvent> createEventMap() {
HashMap<String, GHEvent> map = new HashMap<>();
map.put("CommitCommentEvent", GHEvent.COMMIT_COMMENT);
map.put("CreateEvent", GHEvent.CREATE);
map.put("DeleteEvent", GHEvent.DELETE);
map.put("ForkEvent", GHEvent.FORK);
map.put("GollumEvent", GHEvent.GOLLUM);
map.put("IssueCommentEvent", GHEvent.ISSUE_COMMENT);
map.put("IssuesEvent", GHEvent.ISSUES);
map.put("MemberEvent", GHEvent.MEMBER);
map.put("PublicEvent", GHEvent.PUBLIC);
map.put("PullRequestEvent", GHEvent.PULL_REQUEST);
map.put("PullRequestReviewEvent", GHEvent.PULL_REQUEST_REVIEW);
map.put("PullRequestReviewCommentEvent", GHEvent.PULL_REQUEST_REVIEW_COMMENT);
map.put("PushEvent", GHEvent.PUSH);
map.put("ReleaseEvent", GHEvent.RELEASE);
map.put("WatchEvent", GHEvent.WATCH);
return Collections.unmodifiableMap(map);
}
/**
* Transform type to GH event.
*
* @param type
* the type
* @return the GH event
*/
static GHEvent transformTypeToGHEvent(String type) {
return mapTypeStringToEvent.getOrDefault(type, GHEvent.UNKNOWN);
}
/**
* Gets type.
*
* @return the type
*/
public GHEvent getType() {
return transformTypeToGHEvent(type);
}
/**
* Gets id.
*
* @return the id
*/
public long getId() {
return id;
}
/**
* Gets created at.
*
* @return the created at
*/
public Date getCreatedAt() {
return GitHubClient.parseDate(created_at);
}
/**
* Gets repository.
*
* @return Repository where the change was made.
* @throws IOException
* on error
*/
@SuppressFBWarnings(value = { "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR" },
justification = "The field comes from JSON deserialization")
public GHRepository getRepository() throws IOException {
return root().getRepository(repo.name);
}
/**
* Gets actor.
*
* @return the {@link GHUser} actor for this event.
* @throws IOException
* on error
*/
@SuppressFBWarnings(value = { "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR" },
justification = "The field comes from JSON deserialization")
public GHUser getActor() throws IOException {
return root().getUser(actor.getLogin());
}
/**
* Gets actor login.
*
* @return the login of the actor.
* @throws IOException
* on error
*/
public String getActorLogin() throws IOException {
return actor.getLogin();
}
/**
* Gets organization.
*
* @return the organization
* @throws IOException
* the io exception
*/
@SuppressFBWarnings(value = { "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR" },
justification = "The field comes from JSON deserialization")
public GHOrganization getOrganization() throws IOException {
return (org == null || org.getLogin() == null) ? null : root().getOrganization(org.getLogin());
}
/**
* Retrieves the payload.
*
* @param <T>
* the type parameter
* @param type
* Specify one of the {@link GHEventPayload} subtype that defines a type-safe access to the payload. This
* must match the {@linkplain #getType() event type}.
* @return parsed event payload
* @throws IOException
* if payload cannot be parsed
*/
public <T extends GHEventPayload> T getPayload(Class<T> type) throws IOException {
T v = GitHubClient.getMappingObjectReader(root()).readValue(payload.traverse(), type);
v.lateBind();
return v;
}
}