# For most use-cases you need to use the `GithubAPI` client.
module api
-# TODO to remove
intrude import json::serialization_read
import json::static
return get("/repos/{repo_slug}/commits/{sha}").as(nullable Commit)
end
+ # Get the status of a commit
+ #
+ # The status holds the result of each check ran on a commit like CI, reviews etc.
+ fun get_commit_status(repo_slug: String, sha: String): nullable CommitStatus do
+ return get("/repos/{repo_slug}/commits/{sha}/status").as(nullable CommitStatus)
+ end
+
# Get the Github issue #`number`.
#
# Returns `null` if the issue cannot be found.
var owner: User is writable
# Repo default branch name.
- var default_branch: String is writable
+ var default_branch: nullable String = null is optional, writable
end
# A Github branch.
var path: nullable String is writable
end
+# Status of a commit
+#
+# Can contain sub-status for reviews, CI etc.
+class CommitStatus
+ serialize
+
+ # Global state of this commit
+ var state: nullable String = null is optional, writable
+
+ # Sha of the commit this status is for
+ var sha: nullable String = null is optional, writable
+
+ # Repository the commit belongs to
+ var repository: nullable Repo = null is optional, writable
+
+ # All sub statuses (one for each check)
+ var statuses = new Array[RepoStatus] is optional, writable
+
+ # Total count of sub statuses
+ var total_count: nullable Int = null is optional, writable
+end
+
+# Sub status of a CommitStatus
+#
+# Represents a check applied to a commit (reviews, CI, ...).
+class RepoStatus
+ serialize
+
+ # State of this check
+ var state: nullable String = null is optional, writable
+
+ # Description of this check
+ var description: nullable String = null is optional, writable
+
+ # External URL
+ var target_url: nullable String = null is optional, writable
+
+ # Context this status is related to
+ #
+ # Used to hold the name of the check applied.
+ var context: nullable String = null is optional, writable
+
+ # Date when this status was created
+ var created_at: nullable String = null is optional, writable
+
+ # Last date this status was updated
+ var updated_at: nullable String = null is optional, writable
+end
+
# Comments made on Github issue and pull request pages.
#
# Should be accessed from `GithubAPI::get_issue_comment`.
class GithubDeserializer
super JsonDeserializer
- redef fun class_name_heuristic(json_object) do
- if json_object.has_key("login") then
- return "User"
- else if json_object.has_key("full_name") then
- return "Repo"
- else if json_object.has_key("name") and json_object.has_key("commit") then
+ private var pattern_base = "https://api.github.com"
+
+ # Url patterns to class names
+ var url_patterns: Map[Regex, String] is lazy do
+ var map = new HashMap[Regex, String]
+ map["{pattern_base}/users/[^/]*$".to_re] = "User"
+ map["{pattern_base}/repos/[^/]*/[^/]*$".to_re] = "Repo"
+ map["{pattern_base}/repos/[^/]*/[^/]*/labels/[^/]+$".to_re] = "Label"
+ map["{pattern_base}/repos/[^/]*/[^/]*/milestones/[0-9]+$".to_re] = "Milestone"
+ map["{pattern_base}/repos/[^/]*/[^/]*/issues/[0-9]+$".to_re] = "Issue"
+ map["{pattern_base}/repos/[^/]*/[^/]*/issues/comments/[0-9]+$".to_re] = "IssueComment"
+ map["{pattern_base}/repos/[^/]*/[^/]*/issues/events/[0-9]+$".to_re] = "IssueEvent"
+ map["{pattern_base}/repos/[^/]*/[^/]*/pulls/[0-9]+$".to_re] = "PullRequest"
+ map["{pattern_base}/repos/[^/]*/[^/]*/pulls/comments/[0-9]+$".to_re] = "ReviewComment"
+ map["{pattern_base}/repos/[^/]*/[^/]*/comments/[0-9]+$".to_re] = "CommitComment"
+ map["{pattern_base}/repos/[^/]*/[^/]*/commits/[a-f0-9]+$".to_re] = "Commit"
+ map["{pattern_base}/repos/[^/]*/[^/]*/commits/[a-f0-9]+/status$".to_re] = "CommitStatus"
+ map["{pattern_base}/repos/[^/]*/[^/]*/statuses/[a-f0-9]+$".to_re] = "RepoStatus"
+ return map
+ end
+
+ # Match `url` property in object to a class name
+ fun url_heuristic(raw: Map[String, nullable Object]): nullable String do
+ if not raw.has_key("url") then return null
+
+ var url = raw["url"].as(String)
+ for re, class_name in url_patterns do
+ if url.has(re) then return class_name
+ end
+ return null
+ end
+
+ redef fun class_name_heuristic(raw) do
+ # Try with url
+ var class_name = url_heuristic(raw)
+ if class_name != null then return class_name
+
+ # print raw.serialize_to_json(true, true) # debug
+
+ # Use properties heuristics
+ if raw.has_key("name") and raw.has_key("commit") then
return "Branch"
- else if json_object.has_key("sha") and json_object.has_key("ref") then
- return "PullRef"
- else if (json_object.has_key("sha") and json_object.has_key("commit")) or (json_object.has_key("id") and json_object.has_key("tree_id")) then
- return "Commit"
- else if json_object.has_key("sha") and json_object.has_key("tree") then
- return "GitCommit"
- else if json_object.has_key("name") and json_object.has_key("date") then
- return "GitUser"
- else if json_object.has_key("number") and json_object.has_key("patch_url") then
- return "PullRequest"
- else if json_object.has_key("open_issues") and json_object.has_key("closed_issues") then
- return "Milestone"
- else if json_object.has_key("number") and json_object.has_key("title") then
- return "Issue"
- else if json_object.has_key("color") then
- return "Label"
- else if json_object.has_key("event") then
- return "IssueEvent"
- else if json_object.has_key("original_commit_id") then
- return "ReviewComment"
- else if json_object.has_key("commit_id") then
- return "CommitComment"
- else if json_object.has_key("issue_url") then
- return "IssueComment"
- else if json_object.has_key("total_count") then
+ else if raw.has_key("total_count") and raw.has_key("items") then
return "SearchResults"
end
return null