code: explicitly call init in some named constructors
[nit.git] / lib / github / events.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Events are emitted by Github Hooks.
16 #
17 # See <https://developer.github.com/v3/activity/events/types/>
18 module events
19
20 import api
21
22 # Github event stub.
23 class GithubEvent
24
25 # Github API client.
26 var api: GithubAPI
27
28 # Json representation of `self`.
29 var json: JsonObject is noinit
30
31 init do
32 json = new JsonObject
33 end
34
35 # Init `self` from a `json` object.
36 init from_json(api: GithubAPI, json: JsonObject) do
37 init(api)
38 self.json = json
39 end
40
41 # Event ID from Github.
42 fun id: String do return json["id"].as(String)
43
44 # Set id.
45 fun id=(id: String) do json["id"] = id
46
47 # Action performed by the event.
48 fun action: String do return json["action"].as(String)
49
50 # Set action.
51 fun action=(action: String) do json["action"] = action
52
53 # Repo where this event occured.
54 fun repo: Repo do
55 return new Repo.from_json(api, json["repository"].as(JsonObject))
56 end
57
58 # Set repo.
59 fun repo=(repo: Repo) do json["repository"] = repo.json
60 end
61
62 # Triggered when a commit comment is created.
63 class CommitCommentEvent
64 super GithubEvent
65
66 # The `Comment` itself.
67 fun comment: CommitComment do
68 return new CommitComment.from_json(api, repo, json["comment"].as(JsonObject))
69 end
70
71 # Set comment.
72 fun comment=(comment: CommitComment) do json["comment"] = comment.json
73 end
74
75 # Triggered when a repository, branch, or tag is created.
76 class CreateEvent
77 super GithubEvent
78
79 # Oject type that was created.
80 #
81 # Can be one of `repository`, `branch`, or `tag`.
82 fun ref_type: String do return json["ref_type"].as(String)
83
84 # Set ref_type.
85 fun ref_type=(ref_type: String) do json["ref_type"] = ref_type
86
87 # Git ref (or null if only a repository was created).
88 fun ref: String do return json["ref"].as(String)
89
90 # Set ref.
91 fun ref=(ref: String) do json["ref"] = ref
92
93 # Name of the repo's default branch (usually master).
94 fun master_branch: String do return json["master_branch"].as(String)
95
96 # Set master_branch.
97 fun master_branch=(master_branch: String) do json["master_branch"] = master_branch
98
99 # Repo's current description.
100 fun description: String do return json["description"].as(String)
101
102 # Set description.
103 fun description=(description: String) do json["description"] = description
104 end
105
106 # Triggered when a branch or a tag is deleted.
107 class DeleteEvent
108 super GithubEvent
109
110 # Object type that was deleted.
111 #
112 # Can be one of `repository`, `branch`, or `tag`.
113 fun ref_type: String do return json["ref_type"].as(String)
114
115 # Set ref_type.
116 fun ref_type=(ref_type: String) do json["ref_type"] = ref_type
117
118 # Git ref (or null if only a repository was deleted).
119 fun ref: String do return json["ref"].as(String)
120
121 # Set ref.
122 fun ref=(ref: String) do json["ref"] = ref
123 end
124
125 # Triggered when a new snapshot is deployed.
126 #
127 # Deployement are mainly used with integration testing servers.
128 class DeploymentEvent
129 super GithubEvent
130
131 # Commit SHA for which this deployment was created.
132 fun sha: String do return json["sha"].as(String)
133
134 # Set sha.
135 fun sha=(sha: String) do json["sha"] = sha
136
137 # Name of repository for this deployment, formatted as :owner/:repo.
138 fun name: String do return json["name"].as(String)
139
140 # Set name.
141 fun name=(name: String) do json["name"] = name
142
143 # Optional extra information for this deployment.
144 fun payload: nullable String do
145 var res = json.get_or_null("payload")
146 if res isa String then return res else return null
147 end
148
149 # Set payload.
150 fun payload=(payload: nullable String) do json["payload"] = payload
151
152 # Optional environment to deploy to.
153 # Default: "production"
154 fun environment: nullable String do
155 var res = json.get_or_null("environment")
156 if res isa String then return res else return null
157 end
158
159 # Set environment.
160 fun environment=(environment: nullable String) do json["environment"] = environment
161
162 # Optional human-readable description added to the deployment.
163 fun description: nullable String do
164 var res = json.get_or_null("description")
165 if res isa String then return res else return null
166 end
167
168 # Set description.
169 fun description=(description: nullable String) do json["description"] = description
170 end
171
172 # Triggered when a deployement's status changes.
173 class DeploymentStatusEvent
174 super GithubEvent
175
176 # New deployment state.
177 #
178 # Can be `pending`, `success`, `failure`, or `error`.
179 fun state: String do return json["state"].as(String)
180
181 # Optional link added to the status.
182 fun target_url: nullable String do
183 var res = json.get_or_null("target_url")
184 if res isa String then return res else return null
185 end
186
187 # Set target_url.
188 fun target_url=(target_url: nullable String) do json["target_url"] = target_url
189
190 # Deployment hash that this status is associated with.
191 fun deployment: String do return json["deployment"].as(String)
192
193 # Set deployment.
194 fun deployment=(deployment: String) do json["deployment"] = deployment
195
196 # Optional human-readable description added to the status.
197 fun description: nullable String do
198 var res = json.get_or_null("description")
199 if res isa String then return res else return null
200 end
201
202 # Set description.
203 fun description=(description: nullable String) do json["description"] = description
204 end
205
206 # Triggered when a user forks a repository.
207 class ForkEvent
208 super GithubEvent
209
210 # Created repository.
211 fun forkee: Repo do return new Repo.from_json(api, json["forkee"].as(JsonObject))
212
213 # Set forkee.
214 fun forkee=(forkee: Repo) do json["forkee"] = forkee.json
215 end
216
217 # Triggered when an issue comment is created.
218 class IssueCommentEvent
219 super GithubEvent
220
221 # `Issue` the comment belongs to.
222 fun issue: Issue do
223 return new Issue.from_json(api, repo, json["issue"].as(JsonObject))
224 end
225
226 # Set issue.
227 fun issue=(issue: Issue) do json["issue"] = issue.json
228
229 # The `Comment` itself.
230 fun comment: IssueComment do
231 return new IssueComment.from_json(api, repo, json["comment"].as(JsonObject))
232 end
233
234 # Set comment.
235 fun comment=(comment: IssueComment) do json["comment"] = comment.json
236 end
237
238 # Triggered when an event occurs on an issue.
239 #
240 # Triggered when an issue is assigned, unassigned, labeled, unlabeled,
241 # opened, closed or reopened.
242 class IssuesEvent
243 super GithubEvent
244
245 # The `Issue` itself.
246 fun issue: Issue do return new Issue.from_json(api, repo, json["issue"].as(JsonObject))
247
248 # Set issue.
249 fun issue=(issue: Issue) do json["issue"] = issue.json
250
251 # Optional `Label` that was added or removed from the issue.
252 fun lbl: nullable Label do
253 var res = json.get_or_null("label")
254 if res isa JsonObject then return new Label.from_json(api, repo, res) else return null
255 end
256
257 # Set lbl.
258 fun lbl=(lbl: nullable Label) do
259 if lbl == null then
260 json["lbl"] = null
261 else
262 json["lbl"] = lbl.json
263 end
264 end
265
266 # Optional `User` that was assigned or unassigned from the issue.
267 fun assignee: nullable User do
268 var res = json.get_or_null("assignee")
269 if res isa JsonObject then return new User.from_json(api, res) else return null
270 end
271
272 # Set assignee.
273 fun assignee=(assignee: nullable User) do
274 if assignee == null then
275 json["assignee"] = null
276 else
277 json["assignee"] = assignee.json
278 end
279 end
280 end
281
282 # Triggered when a user is added as a collaborator to a repository.
283 class MemberEvent
284 super GithubEvent
285
286 # `User` that was added.
287 fun member: User do return new User.from_json(api, json["member"].as(JsonObject))
288
289 # Set member.
290 fun member=(member: User) do json["member"] = member.json
291 end
292
293 # Triggered when an event occurs on a pull request.
294 #
295 # Triggered when a pull request is assigned, unassigned,
296 # labeled, unlabeled, opened, closed, reopened, or synchronized.
297 class PullRequestEvent
298 super GithubEvent
299
300 # The pull request number.
301 fun number: Int do return json["number"].as(Int)
302
303 # Set number.
304 fun number=(number: Int) do json["number"] = number
305
306 # The `PullRequest` itself.
307 fun pull: PullRequest do
308 return new PullRequest.from_json(api, repo, json["pull_request"].as(JsonObject))
309 end
310
311 # Set pull.
312 fun pull=(pull: PullRequest) do json["pull_request"] = pull.json
313 end
314
315 # Triggered when a comment is created on a pull request diff.
316 class PullRequestReviewCommentEvent
317 super GithubEvent
318
319 # The `Comment` itself.
320 fun comment: ReviewComment do
321 return new ReviewComment.from_json(api, repo, json["comment"].as(JsonObject))
322 end
323
324 # Set comment.
325 fun comment=(comment: ReviewComment) do json["comment"] = comment.json
326
327 # `PullRequest` the `comment` belongs to.
328 fun pull: PullRequest do
329 return new PullRequest.from_json(api, repo, json["pull_request"].as(JsonObject))
330 end
331
332 # Set pull.
333 fun pull=(pull: PullRequest) do json["pull_request"] = pull.json
334 end
335
336 # Triggered when a repository branch is pushed to.
337 class PushEvent
338 super GithubEvent
339
340 # SHA of the HEAD commit on the repository.
341 fun head: String do return json["head"].as(String)
342
343 # Set head.
344 fun head=(head: String) do json["head"] = head
345
346 # Full Git ref that was pushed.
347 #
348 # Example: “refs/heads/master”
349 fun ref: String do return json["ref"].as(String)
350
351 # Set ref.
352 fun ref=(ref: String) do json["ref"] = ref
353
354 # Number of commits in the push.
355 fun size: Int do return json["size"].as(Int)
356
357 # Set size.
358 fun size=(size: Int) do json["size"] = size
359
360 # Array of pushed commits.
361 fun commits: Array[Commit] do
362 var res = new Array[Commit]
363 var arr = json["commits"].as(JsonArray)
364 for obj in arr do
365 if not obj isa JsonObject then continue
366 res.add api.load_commit(repo, obj["sha"].as(String)).as(not null)
367 end
368 return res
369 end
370
371 # Set commits.
372 fun commits=(commits: Array[Commit]) do
373 var arr = new JsonArray
374 for commit in commits do arr.add commit.json
375 json["commits"] = arr
376 end
377 end
378
379 # Triggered when the status of a Git commit changes.
380 class StatusEvent
381 super GithubEvent
382
383 # The `Commit` itself.
384 fun commit: Commit do
385 return api.load_commit(repo, json["sha"].as(String)).as(not null)
386 end
387
388 # Set commit.
389 fun commit=(commit: Commit) do json["sha"] = commit.sha
390
391 # New state.
392 #
393 # Can be `pending`, `success`, `failure`, or `error`.
394 fun state: String do return json["state"].as(String)
395
396 # Set state.
397 fun state=(state: String) do json["state"] = state
398
399 # Optional human-readable description added to the status.
400 fun description: nullable String do
401 var res = json.get_or_null("description")
402 if res isa String then return res else return null
403 end
404
405 # Set description.
406 fun description=(description: nullable String) do json["description"] = description
407
408 # Optional link added to the status.
409 fun target_url: nullable String do
410 var res = json.get_or_null("target_url")
411 if res isa String then return res else return null
412 end
413
414 # Set target_url.
415 fun target_url=(target_url: nullable String) do json["target_url"] = target_url
416
417 # Array of branches containing the status' SHA.
418 #
419 # Each branch contains the given SHA,
420 # but the SHA may or may not be the head of the branch.
421 #
422 # The array includes a maximum of 10 branches.
423 fun branches: Array[Branch] do
424 var res = new Array[Branch]
425 var arr = json["branches"].as(JsonArray)
426 for obj in arr do
427 if not obj isa JsonObject then continue
428 res.add api.load_branch(repo, obj["name"].as(String)).as(not null)
429 end
430 return res
431 end
432
433 # Set branches.
434 fun branches=(branches: Array[Commit]) do
435 var arr = new JsonArray
436 for branch in branches do arr.add branch.json
437 json["branches"] = arr
438 end
439 end