lib/github: better type safety on json accesses
[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 # Repo where this event occured.
45 fun repo: Repo do
46 return new Repo.from_json(api, json["repository"].as(JsonObject))
47 end
48 end
49
50 # Triggered when a commit comment is created.
51 class CommitCommentEvent
52 super GithubEvent
53
54 # The `Comment` itself.
55 fun comment: CommitComment do
56 return new CommitComment.from_json(api, repo, json["comment"].as(JsonObject))
57 end
58 end
59
60 # Triggered when a repository, branch, or tag is created.
61 class CreateEvent
62 super GithubEvent
63
64 # Oject type that was created.
65 #
66 # Can be one of `repository`, `branch`, or `tag`.
67 fun ref_type: String do return json["ref_type"].as(String)
68
69 # Git ref (or null if only a repository was created).
70 fun ref: String do return json["ref"].as(String)
71
72 # Name of the repo's default branch (usually master).
73 fun master_branch: String do return json["master_branch"].as(String)
74
75 # Repo's current description.
76 fun description: String do return json["description"].as(String)
77 end
78
79 # Triggered when a branch or a tag is deleted.
80 class DeleteEvent
81 super GithubEvent
82
83 # Object type that was deleted.
84 #
85 # Can be one of `repository`, `branch`, or `tag`.
86 fun ref_type: String do return json["ref_type"].as(String)
87
88 # Git ref (or null if only a repository was deleted).
89 fun ref: String do return json["ref"].as(String)
90 end
91
92 # Triggered when a new snapshot is deployed.
93 #
94 # Deployement are mainly used with integration testing servers.
95 class DeploymentEvent
96 super GithubEvent
97
98 # Commit SHA for which this deployment was created.
99 fun sha: String do return json["sha"].as(String)
100
101 # Name of repository for this deployment, formatted as :owner/:repo.
102 fun name: String do return json["name"].as(String)
103
104 # Optional extra information for this deployment.
105 fun payload: nullable String do
106 var res = json.get_or_null("payload")
107 if res isa String then return res else return null
108 end
109
110 # Optional environment to deploy to.
111 # Default: "production"
112 fun environment: nullable String do
113 var res = json.get_or_null("environment")
114 if res isa String then return res else return null
115 end
116
117 # Optional human-readable description added to the deployment.
118 fun description: nullable String do
119 var res = json.get_or_null("description")
120 if res isa String then return res else return null
121 end
122 end
123
124 # Triggered when a deployement's status changes.
125 class DeploymentStatusEvent
126 super GithubEvent
127
128 # New deployment state.
129 #
130 # Can be `pending`, `success`, `failure`, or `error`.
131 fun state: String do return json["state"].as(String)
132
133 # Optional link added to the status.
134 fun target_url: nullable String do
135 var res = json.get_or_null("target_url")
136 if res isa String then return res else return null
137 end
138
139 # Deployment hash that this status is associated with.
140 fun deployment: String do return json["deployment"].as(String)
141
142 # Optional human-readable description added to the status.
143 fun description: nullable String do
144 var res = json.get_or_null("description")
145 if res isa String then return res else return null
146 end
147 end
148
149 # Triggered when a user forks a repository.
150 class ForkEvent
151 super GithubEvent
152
153 # Created repository.
154 fun forkee: Repo do return new Repo.from_json(api, json["forkee"].as(JsonObject))
155 end
156
157 # Triggered when an issue comment is created.
158 class IssueCommentEvent
159 super GithubEvent
160
161 # `Issue` the comment belongs to.
162 fun issue: Issue do
163 return new Issue.from_json(api, repo, json["issue"].as(JsonObject))
164 end
165
166 # The `Comment` itself.
167 fun comment: IssueComment do
168 return new IssueComment.from_json(api, repo, json["comment"].as(JsonObject))
169 end
170 end
171
172 # Triggered when an event occurs on an issue.
173 #
174 # Triggered when an issue is assigned, unassigned, labeled, unlabeled,
175 # opened, closed or reopened.
176 class IssuesEvent
177 super GithubEvent
178
179 # The `Issue` itself.
180 fun issue: Issue do return new Issue.from_json(api, repo, json["issue"].as(JsonObject))
181
182 # Optional `Label` that was added or removed from the issue.
183 fun lbl: nullable Label do
184 var res = json.get_or_null("label")
185 if res isa JsonObject then return new Label.from_json(api, repo, res) else return null
186 end
187
188 # Optional `User` that was assigned or unassigned from the issue.
189 fun assignee: nullable User do
190 var res = json.get_or_null("assignee")
191 if res isa JsonObject then return new User.from_json(api, res) else return null
192 end
193 end
194
195 # Triggered when a user is added as a collaborator to a repository.
196 class MemberEvent
197 super GithubEvent
198
199 # `User` that was added.
200 fun member: User do return new User.from_json(api, json["member"].as(JsonObject))
201 end
202
203 # Triggered when an event occurs on a pull request.
204 #
205 # Triggered when a pull request is assigned, unassigned,
206 # labeled, unlabeled, opened, closed, reopened, or synchronized.
207 class PullRequestEvent
208 super GithubEvent
209
210 # The pull request number.
211 fun number: Int do return json["number"].as(Int)
212
213 # The `PullRequest` itself.
214 fun pull: PullRequest do
215 return new PullRequest.from_json(api, repo, json["pull_request"].as(JsonObject))
216 end
217 end
218
219 # Triggered when a comment is created on a pull request diff.
220 class PullRequestReviewCommentEvent
221 super GithubEvent
222
223 # The `Comment` itself.
224 fun comment: ReviewComment do
225 return new ReviewComment.from_json(api, repo, json["comment"].as(JsonObject))
226 end
227
228 # `PullRequest` the `comment` belongs to.
229 fun pull: PullRequest do
230 return new PullRequest.from_json(api, repo, json["pull_request"].as(JsonObject))
231 end
232 end
233
234 # Triggered when a repository branch is pushed to.
235 class PushEvent
236 super GithubEvent
237
238 # SHA of the HEAD commit on the repository.
239 fun head: String do return json["head"].as(String)
240
241 # Full Git ref that was pushed.
242 #
243 # Example: “refs/heads/master”
244 fun ref: String do return json["ref"].as(String)
245
246 # Number of commits in the push.
247 fun size: Int do return json["size"].as(Int)
248
249 # Array of pushed commits.
250 fun commits: Array[Commit] do
251 var res = new Array[Commit]
252 var arr = json["commits"].as(JsonArray)
253 for obj in arr do
254 if not obj isa JsonObject then continue
255 res.add api.load_commit(repo, obj["sha"].as(String)).as(not null)
256 end
257 return res
258 end
259 end
260
261 # Triggered when the status of a Git commit changes.
262 class StatusEvent
263 super GithubEvent
264
265 # The `Commit` itself.
266 fun commit: Commit do
267 return api.load_commit(repo, json["sha"].as(String)).as(not null)
268 end
269
270 # New state.
271 #
272 # Can be `pending`, `success`, `failure`, or `error`.
273 fun state: String do return json["state"].as(String)
274
275 # Optional human-readable description added to the status.
276 fun description: nullable String do
277 var res = json.get_or_null("description")
278 if res isa String then return res else return null
279 end
280
281 # Optional link added to the status.
282 fun target_url: nullable String do
283 var res = json.get_or_null("target_url")
284 if res isa String then return res else return null
285 end
286
287 # Array of branches containing the status' SHA.
288 #
289 # Each branch contains the given SHA,
290 # but the SHA may or may not be the head of the branch.
291 #
292 # The array includes a maximum of 10 branches.
293 fun branches: Array[Branch] do
294 var res = new Array[Branch]
295 var arr = json["branches"].as(JsonArray)
296 for obj in arr do
297 if not obj isa JsonObject then continue
298 res.add api.load_branch(repo, obj["name"].as(String)).as(not null)
299 end
300 return res
301 end
302 end