X-Git-Url: http://nitlanguage.org diff --git a/lib/github/events.nit b/lib/github/events.nit index b6e5af3..f2f398a 100644 --- a/lib/github/events.nit +++ b/lib/github/events.nit @@ -18,75 +18,63 @@ module events import api +intrude import json # Github event stub. class GithubEvent + serialize - # Github API client. - var api: GithubAPI - - # Json representation of `self`. - var json: JsonObject is noinit - - init do - json = new JsonObject - end - - # Init `self` from a `json` object. - init from_json(api: GithubAPI, json: JsonObject) do - self.api = api - self.json = json - end + # Event ID from Github. + var id: nullable String is writable # Action performed by the event. - fun action: String do return json["action"].to_s + var action: nullable String is writable # Repo where this event occured. - fun repo: Repo do - return new Repo.from_json(api, json["repository"].as(JsonObject)) - end + var repo: Repo is writable end # Triggered when a commit comment is created. class CommitCommentEvent super GithubEvent + serialize # The `Comment` itself. - fun comment: CommitComment do - return new CommitComment.from_json(api, repo, json["comment"].as(JsonObject)) - end + var comment: CommitComment is writable end # Triggered when a repository, branch, or tag is created. class CreateEvent super GithubEvent + serialize # Oject type that was created. # # Can be one of `repository`, `branch`, or `tag`. - fun ref_type: String do return json["ref_type"].to_s + var ref_type: String is writable # Git ref (or null if only a repository was created). - fun ref: String do return json["ref"].to_s + var ref: String is writable # Name of the repo's default branch (usually master). - fun master_branch: String do return json["master_branch"].to_s + var master_branch: String is writable # Repo's current description. - fun description: String do return json["description"].to_s + var description: nullable String is writable end # Triggered when a branch or a tag is deleted. class DeleteEvent super GithubEvent + serialize # Object type that was deleted. # # Can be one of `repository`, `branch`, or `tag`. - fun ref_type: String do return json["ref_type"].to_s + var ref_type: String is writable # Git ref (or null if only a repository was deleted). - fun ref: String do return json["ref"].to_s + var ref: String is writable end # Triggered when a new snapshot is deployed. @@ -94,79 +82,64 @@ end # Deployement are mainly used with integration testing servers. class DeploymentEvent super GithubEvent + serialize # Commit SHA for which this deployment was created. - fun sha: String do return json["sha"].to_s + var sha: String is writable # Name of repository for this deployment, formatted as :owner/:repo. - fun name: String do return json["name"].to_s + var name: String is writable # Optional extra information for this deployment. - fun payload: nullable String do - if not json.has_key("payload") then return null - return json["payload"].to_s - end + var payload: nullable String is writable # Optional environment to deploy to. # Default: "production" - fun environment: nullable String do - if not json.has_key("environment") then return null - return json["environment"].to_s - end + var environment: nullable String is writable # Optional human-readable description added to the deployment. - fun description: nullable String do - if not json.has_key("description") then return null - return json["description"].to_s - end + var description: nullable String is writable end # Triggered when a deployement's status changes. class DeploymentStatusEvent super GithubEvent + serialize # New deployment state. # # Can be `pending`, `success`, `failure`, or `error`. - fun state: String do return json["state"].to_s + var state: String is writable # Optional link added to the status. - fun target_url: nullable String do - if not json.has_key("target_url") then return null - return json["target_url"].to_s - end + var target_url: nullable String is writable # Deployment hash that this status is associated with. - fun deployment: String do return json["deployment"].to_s + var deployment: String is writable # Optional human-readable description added to the status. - fun description: nullable String do - if not json.has_key("description") then return null - return json["description"].to_s - end + var description: nullable String is writable end # Triggered when a user forks a repository. class ForkEvent super GithubEvent + serialize # Created repository. - fun forkee: Repo do return new Repo.from_json(api, json["forkee"].as(JsonObject)) + var forkee: Repo is writable end # Triggered when an issue comment is created. class IssueCommentEvent super GithubEvent + serialize # `Issue` the comment belongs to. - fun issue: Issue do - return new Issue.from_json(api, repo, json["issue"].as(JsonObject)) - end + var issue: Issue is writable # The `Comment` itself. - fun comment: IssueComment do - return new IssueComment.from_json(api, repo, json["comment"].as(JsonObject)) - end + var comment: IssueComment is writable end # Triggered when an event occurs on an issue. @@ -175,29 +148,25 @@ end # opened, closed or reopened. class IssuesEvent super GithubEvent + serialize # The `Issue` itself. - fun issue: Issue do return new Issue.from_json(api, repo, json["issue"].as(JsonObject)) + var issue: Issue is writable # Optional `Label` that was added or removed from the issue. - fun lbl: nullable Label do - if not json.has_key("label") then return null - return new Label.from_json(api, repo, json["label"].as(JsonObject)) - end + var lbl: nullable Label is writable, serialize_as("label") # Optional `User` that was assigned or unassigned from the issue. - fun assignee: nullable User do - if not json.has_key("assignee") then return null - return new User.from_json(api, json["assignee"].as(JsonObject)) - end + var assignee: nullable User is writable end # Triggered when a user is added as a collaborator to a repository. class MemberEvent super GithubEvent + serialize # `User` that was added. - fun member: User do return new User.from_json(api, json["member"].as(JsonObject)) + var member: User is writable end # Triggered when an event occurs on a pull request. @@ -206,83 +175,65 @@ end # labeled, unlabeled, opened, closed, reopened, or synchronized. class PullRequestEvent super GithubEvent + serialize # The pull request number. - fun number: Int do return json["number"].as(Int) + var number: Int is writable # The `PullRequest` itself. - fun pull: PullRequest do - return new PullRequest.from_json(api, repo, json["pull_request"].as(JsonObject)) - end + var pull: PullRequest is writable end # Triggered when a comment is created on a pull request diff. class PullRequestReviewCommentEvent super GithubEvent + serialize # The `Comment` itself. - fun comment: ReviewComment do - return new ReviewComment.from_json(api, repo, json["comment"].as(JsonObject)) - end + var comment: ReviewComment is writable # `PullRequest` the `comment` belongs to. - fun pull: PullRequest do - return new PullRequest.from_json(api, repo, json["pull_request"].as(JsonObject)) - end + var pull: PullRequest is writable end # Triggered when a repository branch is pushed to. class PushEvent super GithubEvent + serialize # SHA of the HEAD commit on the repository. - fun head: String do return json["head"].to_s + var head_commit: Commit is writable # Full Git ref that was pushed. # # Example: “refs/heads/master” - fun ref: String do return json["ref"].to_s + var ref: String is writable # Number of commits in the push. - fun size: Int do return json["size"].as(Int) + var size: nullable Int is writable # Array of pushed commits. - fun commits: Array[Commit] do - var res = new Array[Commit] - var arr = json["commits"].as(JsonArray) - for obj in arr do - if not obj isa JsonObject then continue - res.add api.load_commit(repo, obj["sha"].to_s).as(not null) - end - return res - end + var commits = new Array[Commit] is writable, optional end # Triggered when the status of a Git commit changes. class StatusEvent super GithubEvent + serialize # The `Commit` itself. - fun commit: Commit do - return api.load_commit(repo, json["sha"].to_s).as(not null) - end + var sha: String is writable # New state. # # Can be `pending`, `success`, `failure`, or `error`. - fun state: String do return json["state"].to_s + var state: String is writable # Optional human-readable description added to the status. - fun description: nullable String do - if not json.has_key("description") then return null - return json["description"].to_s - end + var description: nullable String is writable # Optional link added to the status. - fun target_url: nullable String do - if not json.has_key("target_url") then return null - return json["target_url"].to_s - end + var target_url: nullable String is writable # Array of branches containing the status' SHA. # @@ -290,13 +241,41 @@ class StatusEvent # but the SHA may or may not be the head of the branch. # # The array includes a maximum of 10 branches. - fun branches: Array[Branch] do - var res = new Array[Branch] - var arr = json["branches"].as(JsonArray) - for obj in arr do - if not obj isa JsonObject then continue - res.add api.load_branch(repo, obj["name"].to_s).as(not null) + var branches = new Array[Branch] is writable, optional +end + +redef class GithubDeserializer + + redef fun class_name_heuristic(json_object) do + if json_object.has_key("action") and json_object.has_key("commit") and json_object.has_key("comment") then + return "CommitCommentEvent" + else if json_object.has_key("ref") and json_object.has_key("master_branch") then + return "CreateEvent" + else if json_object.has_key("ref") and json_object.has_key("ref_type") then + return "DeleteEvent" + else if json_object.has_key("action") and json_object.has_key("sha") then + return "DeploymentEvent" + else if json_object.has_key("action") and json_object.has_key("state") then + return "DeploymentStatusEvent" + else if json_object.has_key("action") and json_object.has_key("forkee") then + return "ForkEvent" + else if json_object.has_key("action") and json_object.has_key("issue") and json_object.has_key("comment") then + return "IssueCommentEvent" + else if json_object.has_key("action") and json_object.has_key("issue") then + return "IssuesEvent" + else if json_object.has_key("action") and json_object.has_key("member") then + return "MemberEvent" + else if json_object.has_key("action") and json_object.has_key("number") then + return "PullRequestEvent" + else if json_object.has_key("action") and json_object.has_key("pull") and json_object.has_key("comment") then + return "PullRequestReviewCommentEvent" + else if json_object.has_key("head_commit") and json_object.has_key("commits") then + return "PushEvent" + else if json_object.has_key("action") and json_object.has_key("branches") then + return "StatusEvent" + else if json_object.has_key("action") and json_object.has_key("issue") then + return "GithubEvent" end - return res + return super end end