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