forked from jcabi/jcabi-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvent.java
More file actions
281 lines (245 loc) · 7.43 KB
/
Event.java
File metadata and controls
281 lines (245 loc) · 7.43 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* SPDX-FileCopyrightText: Copyright (c) 2013-2026 Yegor Bugayenko
* SPDX-License-Identifier: MIT
*/
package com.jcabi.github;
import com.google.common.base.Optional;
import com.jcabi.aspects.Immutable;
import com.jcabi.aspects.Loggable;
import jakarta.json.JsonObject;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.text.ParseException;
import java.util.Date;
import lombok.EqualsAndHashCode;
import lombok.ToString;
/**
* GitHub event.
* @see <a href="https://developer.github.com/v3/issues/events/">Issue Events API</a>
* @since 0.4
* @checkstyle MultipleStringLiterals (500 lines)
*/
@Immutable
@SuppressWarnings("PMD.TooManyMethods")
public interface Event extends Comparable<Event>, JsonReadable {
/**
* Event type.
* @see <a href="https://developer.github.com/v3/issues/events/">Event Types</a>
*/
String CLOSED = "closed";
/**
* Event type.
* @see <a href="https://developer.github.com/v3/issues/events/">Event Types</a>
*/
String REOPENED = "reopened";
/**
* Event type.
* @see <a href="https://developer.github.com/v3/issues/events/">Event Types</a>
*/
String SUBSCRIBED = "subscribed";
/**
* Event type.
* @see <a href="https://developer.github.com/v3/issues/events/">Event Types</a>
*/
String MERGED = "merged";
/**
* Event type.
* @see <a href="https://developer.github.com/v3/issues/events/">Event Types</a>
*/
String REFERENCED = "referenced";
/**
* Event type.
* @see <a href="https://developer.github.com/v3/issues/events/">Event Types</a>
*/
String MENTIONED = "mentioned";
/**
* Event type.
* @see <a href="https://developer.github.com/v3/issues/events/">Event Types</a>
*/
String ASSIGNED = "assigned";
/**
* Event type.
* @see <a href="https://developer.github.com/v3/issues/events/">Event Types</a>
*/
String UNASSIGNED = "unassigned";
/**
* Event type.
* @see <a href="https://developer.github.com/v3/issues/events/">Event Types</a>
*/
String LABELED = "labeled";
/**
* Event type.
* @see <a href="https://developer.github.com/v3/issues/events/">Event Types</a>
*/
String UNLABELED = "unlabeled";
/**
* Event type.
* @see <a href="https://developer.github.com/v3/issues/events/">Event Types</a>
*/
String MILESTONED = "milestoned";
/**
* Event type.
* @see <a href="https://developer.github.com/v3/issues/events/">Event Types</a>
*/
String DEMILESTONED = "demilestoned";
/**
* Event type.
* @see <a href="https://developer.github.com/v3/issues/events/">Event Types</a>
*/
String RENAMED = "renamed";
/**
* Event type.
* @see <a href="https://developer.github.com/v3/issues/events/">Event Types</a>
*/
String LOCKED = "locked";
/**
* Event type.
* @see <a href="https://developer.github.com/v3/issues/events/">Event Types</a>
*/
String UNLOCKED = "unlocked";
/**
* Event type.
* @see <a href="https://developer.github.com/v3/issues/events/">Event Types</a>
*/
String HEAD_REF_DELETED = "head_ref_deleted";
/**
* Event type.
* @see <a href="https://developer.github.com/v3/issues/events/">Event Types</a>
*/
String HEAD_REF_RESTORED = "head_ref_restored";
/**
* Repository we're in.
* @return Repo
*/
Repo repo();
/**
* Get its number.
* @return Issue number
*/
int number();
/**
* Smart event with extra features.
* @since 0.6.1
*/
@Immutable
@ToString
@Loggable(Loggable.DEBUG)
@EqualsAndHashCode(of = { "event", "jsn" })
final class Smart implements Event {
/**
* Encapsulated event.
*/
private final transient Event event;
/**
* SmartJson object for convenient JSON parsing.
*/
private final transient SmartJson jsn;
/**
* Public ctor.
* @param evt Event
*/
public Smart(final Event evt) {
this.event = evt;
this.jsn = new SmartJson(evt);
}
/**
* Does it have an author?
* @return TRUE if the author exists
* @throws IOException If there is any I/O problem
*/
public boolean hasAuthor() throws IOException {
return !this.event.json().isNull("actor");
}
/**
* Get its author.
* @return Author of comment
* @throws IOException If there is any I/O problem
*/
public User author() throws IOException {
return this.event.repo().github().users().get(
this.event.json().getJsonObject("actor").getString("login")
);
}
/**
* Get its type.
* @return State of issue
* @throws IOException If there is any I/O problem
*/
public String type() throws IOException {
return this.jsn.text("event");
}
/**
* Get its URL.
* @return URL of issue
* @throws IOException If there is any I/O problem
*/
public URL url() throws IOException {
try {
return new URI(this.jsn.text("url")).toURL();
} catch (final URISyntaxException ex) {
throw new IllegalArgumentException(ex);
}
}
/**
* When this issue was created.
* @return Date of creation
* @throws IOException If there is any I/O problem
*/
public Date createdAt() throws IOException {
try {
return new GitHub.Time(
this.jsn.text("created_at")
).date();
} catch (final ParseException ex) {
throw new IllegalStateException(ex);
}
}
/**
* SHA of the commit referenced by this event (if any).
* @return SHA of the commit, or absent if not associated with one
* @throws IOException If there is any I/O problem
* @since 1.7.0
*/
public Optional<String> commitId() throws IOException {
return Optional.fromNullable(
this.event.json().getString("commit_id", null)
);
}
/**
* Label that was added or removed in this event (if any).
* @return Label that was added or removed
* @throws IOException If there is any I/O problem
* @since 0.24
*/
public Optional<Label> label() throws IOException {
Optional<Label> lab = Optional.absent();
final JsonObject lbl = this.jsn.json().getJsonObject("label");
if (lbl != null) {
lab = Optional.of(
this.event.repo()
.labels()
.get(lbl.getString("name"))
);
}
return lab;
}
@Override
public Repo repo() {
return this.event.repo();
}
@Override
public int number() {
return this.event.number();
}
@Override
public JsonObject json() throws IOException {
return this.event.json();
}
@Override
public int compareTo(final Event obj) {
return this.event.compareTo(obj);
}
}
}