X-Git-Url: http://git.roojs.org/?p=gitlive;a=blobdiff_plain;f=RooTicket.vala;h=3d854010c3ced913a35c3f4444a85ead8a0355e6;hp=ca3c101198672543258b1584ee276b15f983ae56;hb=35449c3a6ed4c08cf09abae2ab8a582acf7080c3;hpb=5cc0ef576bd6fa9e1369dce5d58e5944cd96401c diff --git a/RooTicket.vala b/RooTicket.vala index ca3c1011..3d854010 100644 --- a/RooTicket.vala +++ b/RooTicket.vala @@ -1,25 +1,92 @@ -/** +/** code to fetch ticket info... */ + +public class RooRepo : Object +{ + + + + public string id; // not really important that they are numbers.. + public string project_id; + public string description; + public string shortname; + + +} + +public class RooOption : Object +{ + + public string id; // not really important that they are numbers.. + public string name; + public string display_name; + + public RooOption (string id, string name, string display_name) + { + this.id = id; + this.name = name; + this.display_name = name; + } + +} + + +public class RooProject : Object +{ + public string id; // not really important that they are numbers.. + public string code; + public string name; + public string type; + +} + + static RooTicket _RooTicket; -class RooTicket : Object +public class RooTicket : Object { - - + + public enum Who { + ANYBODY, + ME + } + public enum Status { + ALL, + ACTIVE + } + + //const string baseurl = "https://roojs.com/admin.php/Ro/mtrack_ticket"; + const string roourl = "https://roojs.com/admin.php/Roo"; public static RooTicket singleton() { if (_RooTicket == null) { _RooTicket = new RooTicket(); _RooTicket.tickets = new Gee.ArrayList(); + _RooTicket.projects = new Gee.ArrayList(); + _RooTicket.repos = new Gee.ArrayList(); + _RooTicket.loadRepos(); // initalize it.. } return _RooTicket; } public Gee.ArrayList tickets; // only available for singletonn. + public Gee.ArrayList projects; // only available for singletonn. + public Gee.ArrayList repos; // only available for singletonn. + + public Gee.ArrayList milestones; + public Gee.ArrayList priorities; + public Gee.ArrayList serverities; + public Gee.ArrayList classifications; + public Gee.ArrayList developers; + + + public string username = ""; // only available for singletonn. + public string password = ""; // only available for singletonn. + public string id; // not really important that they are numbers.. public string summary; @@ -27,7 +94,43 @@ class RooTicket : Object public string project_id_name; - public void addTicket(Json.Object t) + public string summaryToBranchName() + { + // first 5 words of summary.. + var regex = new Regex ("[^A-Za-z0-9 ]+"); + var str = regex.replace(this.summary, this.summary.length,0, ""); + string[] words = Regex.split_simple ("[ \t]+", str); + var ret = ""; + for (var i =0; i< (words.length > 5 ? 5 : words.length); i++) { + ret += ret.length > 0 ? "_" : ""; + ret += words[i]; + } + return ret; + } + public string usernameLocal() + { + // git username is an email addres... - so this reutrns the local part.. + //?? assumes that all members are under the same domain... normally the case...... + return RooTicket.singleton().username.split("@")[0]; + + } + + public Gee.ArrayList readJsonArray(Json.Array a) + { + var ret = new Gee.ArrayList(); + for(var i = 0; i < a.get_length(); i++) { + var t = a.get_object_element(i); + ret.add(new RooOption( + t.get_string_member("id"), + t.get_string_member("name"), + t.get_string_member("display_name") + )); + } + return ret; + + } + + public RooTicket addTicket(Json.Object t) { var add = new RooTicket(); add.id = t.get_string_member("id"); @@ -36,7 +139,78 @@ class RooTicket : Object add.project_id_name = t.get_string_member("project_id_name"); this.tickets.add(add); GLib.debug("ADD ticket %s : %s : %s", add.id, add.summary, add.project_id_name); + return add; + } + public RooProject addProject(Json.Object t) + { + var add = new RooProject(); + add.id = t.get_string_member("id"); + add.name = t.get_string_member("name"); + add.type = t.get_string_member("type"); + add.code = t.get_string_member("code"); + this.projects.add(add); + GLib.debug("ADD project %s : %s : %s", add.id, add.code, add.name); + return add; + } + + public RooRepo addRepo(Json.Object t) + { + var add = new RooRepo(); + add.id = t.get_string_member("id"); + add.shortname = t.get_string_member("shortname"); + add.description = t.get_string_member("description"); + add.project_id = t.get_string_member("project_id"); + this.repos.add(add); + GLib.debug("ADD project %s : %s : %s", add.id, add.shortname, add.project_id); + return add; } + + public RooProject? getProjectByRepo(GitRepo repo) + { + var rt = RooTicket.singleton(); + + if (rt.repos.size < 1) { + rt.loadRepos(); + } + if (rt.projects.size < 1) { + rt.loadProjects(); + } + + var pid = ""; + foreach(var roo_repo in rt.repos) { + if (roo_repo.shortname == repo.name) { + pid = roo_repo.project_id; + break; + } + } + if (pid == "") { + GLib.debug("getProjectByRepo: repo has no project"); + return null; + } + // get project by id... + foreach(var roo_project in rt.projects) { + if (roo_project.id == pid) { + GLib.debug("getProjectByRepo: project_id = %s", pid); + return roo_project; + } + } + GLib.debug("getProjectByRepo: can not find project"); + return null; + + } + + + public static RooTicket fakeTicket() + { + var t = new RooTicket(); + t.id = "-1"; + t.summary = ""; + t.description = ""; + t.project_id_name = ""; + RooTicket.singleton().tickets.add(t); + return t; + } + public RooTicket? getById(string id) { @@ -45,27 +219,81 @@ class RooTicket : Object return t; } } - return null; + if (id == "-1") { + return RooTicket.fakeTicket(); + } + + return this.loadTicket(id); + - } + } - public void loadTickets() + + public RooTicket? loadTicket(string id) + { + + var table = new GLib.HashTable(str_hash, str_equal); + table.insert("_id",id); + + var params = Soup.Form.encode_hash(table); + var url = "%s/%s?%s" . printf(roourl, "mtrack_ticket", params); + GLib.debug("request %s", url); + + var session = new Soup.Session (); + session.timeout = 0; + var message = new Soup.Message ("GET", url); + RooTicket.setAuth(message); + session.send_message (message); + + + var data = (string) message.response_body.flatten().data; + GLib.debug("got %s", data); + try { + var parser = new Json.Parser (); + parser.load_from_data (data, -1); + + var response = parser.get_root().get_object(); + var status = response.get_boolean_member("success"); + + if(!status){ + GLib.error(response.get_string_member("errorMsg")); + return null; + } + var rd = response.get_object_member ("data"); + + return this.addTicket(rd); + + + + } catch (Error e) { + GLib.error(e.message); + return null; + } + } + + public void loadTickets(string project_id, Who who, Status status) { RooTicket.singleton().tickets = new Gee.ArrayList(); - - var url = "https://roojs.com/admin.php/Roo/mtrack_ticket"; + var table = new GLib.HashTable(str_hash, str_equal); - - table.insert("query[viewtype]","active"); - table.insert("developer_id","494"); - table.insert("limit","999"); + + table.insert("_developer", who.to_string().down().substring(15)); + + table.insert("query[viewtype]", status.to_string().down().substring(18)); + + table.insert("limit","200"); table.insert("sort","summary"); table.insert("dir","ASC"); + + if (project_id != "") { + table.insert("project_id",project_id); + } + var params = Soup.Form.encode_hash(table); - url = "%s?%s" . printf(url, params); + var url = "%s/%s?%s" . printf(roourl, "mtrack_ticket", params); GLib.debug("request %s", url); @@ -79,15 +307,15 @@ class RooTicket : Object session.send_message (message); var data = (string) message.response_body.flatten().data; - GLib.debug("got %s", data); + //GLib.debug("got %s", data); try { var parser = new Json.Parser (); parser.load_from_data (data, -1); var response = parser.get_root().get_object(); - var status = response.get_boolean_member("success"); + var success = response.get_boolean_member("success"); - if(!status){ + if(!success){ GLib.error(response.get_string_member("errorMsg")); return; } @@ -107,34 +335,319 @@ class RooTicket : Object } - public static void setAuth(Soup.Message message) { - string str; - var username = ""; - var password = ""; - GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str); - var lines = str.split("\n"); - for(var i=0; i< lines.length; i++) { - // assumes one line per entry.. if not we are buggered... - GLib.debug("got %s" , lines[i]); + public void loadProjects() + { + RooTicket.singleton().projects = new Gee.ArrayList(); + + + var table = new GLib.HashTable(str_hash, str_equal); + + table.insert("query[project_filter]","P,N,U"); + table.insert("limit","200"); + table.insert("sort","name"); + table.insert("dir","ASC"); + + var params = Soup.Form.encode_hash(table); + + var url = "%s/%s?%s" . printf(roourl, "core_project", params); + + GLib.debug("request %s", url); + + var session = new Soup.Session (); + session.timeout = 0; + var message = new Soup.Message ("GET", url); + + + RooTicket.setAuth(message); + + session.send_message (message); + + var data = (string) message.response_body.flatten().data; + //GLib.debug("got %s", data); + try { + var parser = new Json.Parser (); + parser.load_from_data (data, -1); + + var response = parser.get_root().get_object(); + var status = response.get_boolean_member("success"); + + if(!status){ + GLib.error(response.get_string_member("errorMsg")); + return; + } + var rd = response.get_array_member ("data"); + + // got a valid result... + var _this = RooTicket.singleton(); + for(var i = 0; i < rd.get_length(); i++) { + _this.addProject(rd.get_object_element(i)); + } + + + } catch (Error e) { + GLib.error(e.message); + return; + } + + } + public void loadRepos() + { + RooTicket.singleton().repos = new Gee.ArrayList(); + + + var table = new GLib.HashTable(str_hash, str_equal); + + + table.insert("limit","200"); + table.insert("sort","shortname"); + table.insert("dir","ASC"); + + var params = Soup.Form.encode_hash(table); + + var url = "%s/%s?%s" . printf(roourl, "mtrack_repos", params); + + GLib.debug("request %s", url); + + var session = new Soup.Session (); + session.timeout = 0; + var message = new Soup.Message ("GET", url); + + + RooTicket.setAuth(message); + + session.send_message (message); + + var data = (string) message.response_body.flatten().data; + //GLib.debug("got %s", data); + try { + var parser = new Json.Parser (); + parser.load_from_data (data, -1); + + var response = parser.get_root().get_object(); + var status = response.get_boolean_member("success"); + + if(!status){ + GLib.error(response.get_string_member("errorMsg")); + return; + } + var rd = response.get_array_member ("data"); + + // got a valid result... + var _this = RooTicket.singleton(); + for(var i = 0; i < rd.get_length(); i++) { + _this.addRepo(rd.get_object_element(i)); + } + + + } catch (Error e) { + GLib.error(e.message); + return; + } + + } + public void loadProjectOptions(string pid) + { + var rt = RooTicket.singleton(); + rt.milestones = new Gee.ArrayList(); + rt.priorities = new Gee.ArrayList(); + rt.serverities = new Gee.ArrayList(); + rt.classifications = new Gee.ArrayList(); + rt.developers = new Gee.ArrayList(); + + if (pid == "") { + return; + } + + + + var table = new GLib.HashTable(str_hash, str_equal); + + table.insert("_options_for",pid); + + var params = Soup.Form.encode_hash(table); + + var url = "%s/%s?%s" . printf(roourl, "mtrack_ticket", params); + + GLib.debug("request %s", url); + + var session = new Soup.Session (); + session.timeout = 0; + var message = new Soup.Message ("GET", url); + + + RooTicket.setAuth(message); + + session.send_message (message); + + var data = (string) message.response_body.flatten().data; + //GLib.debug("got %s", data); + try { + var parser = new Json.Parser (); + parser.load_from_data (data, -1); + + var response = parser.get_root().get_object(); + var status = response.get_boolean_member("success"); - var bits = Regex.split_simple ("[ \t]+", lines[i].strip()); - if (bits.length < 6 || bits[0] != "machine" || bits[1] != "git.roojs.com") { - continue; + if(!status){ + GLib.error(response.get_string_member("errorMsg")); + return; } - GLib.debug("found password?"); - // we are gussing.... - username = bits[3]; - password = bits[5]; + var rd = response.get_object_member ("data"); + rt.milestones = this.readJsonArray( rd.get_array_member("milestone")); + rt.priorities = this.readJsonArray( rd.get_array_member("priority")); + rt.serverities = this.readJsonArray( rd.get_array_member("severity")); + rt.classifications = this.readJsonArray( rd.get_array_member("classification")); + rt.developers = this.readJsonArray( rd.get_array_member("developer")); + + + + + // got a valid result... + + + } catch (Error e) { + GLib.error(e.message); + return; } + + } + + public static void setAuth(Soup.Message message) + { + var rs = RooTicket.singleton(); + if (rs.username.length < 1) { + string str; + GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str); + var lines = str.split("\n"); + for(var i=0; i< lines.length; i++) { + // assumes one line per entry.. if not we are buggered... + GLib.debug("got %s" , lines[i]); + + var bits = Regex.split_simple ("[ \t]+", lines[i].strip()); + if (bits.length < 6 || bits[0] != "machine" || bits[1] != "git.roojs.com") { + continue; + } + GLib.debug("found password?"); + // we are gussing.... + + RooTicket.singleton().username = bits[3]; + RooTicket.singleton().password = bits[5]; + } + } + - var authCode = Base64.encode ("%s:%s".printf(username, password).data); + var authCode = Base64.encode ("%s:%s".printf(rs.username, rs.password).data); message.request_headers.append("Authorization", "Basic %s".printf(authCode)); } + + + public void close(string commits) + { + if (this.id == "-1") { + return; + } + var table = new GLib.HashTable(str_hash, str_equal); + table.insert("id",id); + table.insert("status_name","resolved"); + table.insert("reason","fixed by Commits: %s".printf(commits)); + var params = Soup.Form.encode_hash(table); + + GLib.debug("request POST %s / %s", roourl, id); + + var session = new Soup.Session (); + session.timeout = 0; + var message = new Soup.Message ("POST", roourl + "/mtrack_ticket"); + RooTicket.setAuth(message); + + message.set_request ("application/x-www-form-urlencoded", Soup.MemoryUse.STATIC, params.data); + session.send_message (message); + + + var data = (string) message.response_body.flatten().data; + GLib.debug("got %s", data); + try { + var parser = new Json.Parser (); + parser.load_from_data (data, -1); + + var response = parser.get_root().get_object(); + var status = response.get_boolean_member("success"); + + if(!status){ + GLib.error(response.get_string_member("errorMsg")); + return ; + } + + + + + } catch (Error e) { + GLib.error(e.message); + return ; + } + + } + public string createTicket( + string project_id, + string milestone_id, + string priority_id, + string classification_id, + string developer_id, + string summary, + string description + ) + { + + var table = new GLib.HashTable(str_hash, str_equal); + + table.insert("project_id", project_id); + table.insert("milestone_id", milestone_id); + table.insert("priority_id", priority_id); + table.insert("classification_id", classification_id); + table.insert("developer_id", developer_id); + table.insert("summary", summary); + table.insert("description", description); + + var params = Soup.Form.encode_hash(table); + + GLib.debug("request POST %s / %s", roourl, id); + + var session = new Soup.Session (); + session.timeout = 0; + var message = new Soup.Message ("POST", roourl + "/mtrack_ticket"); + RooTicket.setAuth(message); + + message.set_request ("application/x-www-form-urlencoded", Soup.MemoryUse.STATIC, params.data); + session.send_message (message); + + + var data = (string) message.response_body.flatten().data; + GLib.debug("got %s", data); + try { + var parser = new Json.Parser (); + parser.load_from_data (data, -1); + + var response = parser.get_root().get_object(); + var status = response.get_boolean_member("success"); + + if(!status){ + GLib.error(response.get_string_member("errorMsg")); + return "" ; + } + var rd = response.get_object_member ("data"); + return rd.get_string_member("id"); + + + + } catch (Error e) { + GLib.error(e.message); + return ""; + } + + } - - - } \ No newline at end of file +} +