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