lib/github: handles milestones
authorAlexandre Terrasa <alexandre@moz-code.org>
Sat, 13 Dec 2014 02:02:02 +0000 (21:02 -0500)
committerAlexandre Terrasa <alexandre@moz-code.org>
Thu, 8 Jan 2015 12:53:34 +0000 (13:53 +0100)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

lib/github/api.nit

index cbf7b82..138b079 100644 (file)
@@ -218,6 +218,22 @@ class GithubAPI
                if was_error then return null
                return labl
        end
+
+       # Get the Github milestone with `id`.
+       #
+       # Returns `null` if the milestone cannot be found.
+       #
+       #     var api = new GithubAPI(get_github_oauth)
+       #     var repo = api.load_repo("privat/nit")
+       #     assert repo isa Repo
+       #     var stone = api.load_milestone(repo, 4)
+       #     assert stone.title == "v1.0prealpha"
+       fun load_milestone(repo: Repo, id: Int): nullable Milestone do
+               var milestone = new Milestone(self, repo, id)
+               milestone.load_from_github
+               if was_error then return null
+               return milestone
+       end
 end
 
 # Something returned by the Github API.
@@ -329,6 +345,22 @@ class Repo
                end
                return res
        end
+
+       # List of milestones associated with their ids.
+       fun milestones: Map[Int, Milestone] do
+               api.message(1, "Get milestones for {full_name}")
+               var array = api.get("repos/{full_name}/milestones")
+               var res = new HashMap[Int, Milestone]
+               if array isa JsonArray then
+                       for obj in array do
+                               if not obj isa JsonObject then continue
+                               var number = obj["number"].as(Int)
+                               res[number] = new Milestone.from_json(api, self, obj)
+                       end
+               end
+               return res
+       end
+
        # Repo default branch.
        fun default_branch: Branch do
                var name = json["default_branch"].to_s
@@ -483,3 +515,68 @@ class Label
        # Label color code.
        fun color: String do return json["color"].to_s
 end
+
+# A Github milestone.
+#
+# Should be accessed from `GithubAPI::load_milestone`.
+#
+# See <https://developer.github.com/v3/issues/milestones/>.
+class Milestone
+       super RepoEntity
+
+       redef var key is lazy do return "{repo.key}/milestones/{number}"
+
+       # The milestone id on Github.
+       var number: Int
+
+       redef init from_json(api, repo, json) do
+               super
+               self.number = json["number"].as(Int)
+       end
+
+       # Milestone title.
+       fun title: String do return json["title"].to_s
+
+       # Milestone long description.
+       fun description: String do return json["description"].to_s
+
+       # Count of opened issues linked to this milestone.
+       fun open_issues: Int do return json["open_issues"].to_s.to_i
+
+       # Count of closed issues linked to this milestone.
+       fun closed_issues: Int do return json["closed_issues"].to_s.to_i
+
+       # Milestone state.
+       fun state: String do return json["state"].to_s
+
+       # Creation time in ISODate format.
+       fun created_at: ISODate do
+               return new ISODate.from_string(json["created_at"].to_s)
+       end
+
+       # User that created this milestone.
+       fun creator: User do
+               return new User.from_json(api, json["creator"].as(JsonObject))
+       end
+
+       # Due time in ISODate format (if any).
+       fun due_on: nullable ISODate do
+               var res = json["updated_at"]
+               if res == null then return null
+               return new ISODate.from_string(res.to_s)
+       end
+
+       # Update time in ISODate format (if any).
+       fun updated_at: nullable ISODate do
+               var res = json["updated_at"]
+               if res == null then return null
+               return new ISODate.from_string(res.to_s)
+       end
+
+       # Close time in ISODate format (if any).
+       fun closed_at: nullable ISODate do
+               var res = json["closed_at"]
+               if res == null then return null
+               return new ISODate.from_string(res.to_s)
+       end
+end