d32ad47802fb93d3148a7abb989f65ddf103e591
[gitlive] / RooTicket.vala
1 /** 
2
3 code to fetch ticket info...
4
5 */
6
7
8 public class RooOption : Object 
9 {
10         
11         public string id; // not really important that they are numbers..
12         public string name;     
13         public string display_name;
14  
15         public RooOption (string id, string name, string display_name)
16         {
17                 this.id = id;
18                 this.name = name;
19                 this.display_name = name;
20         }
21
22 }
23
24
25 static RooTicket  _RooTicket;
26
27
28
29 public class RooTicket : Object
30 {
31         
32          public enum Who {
33                 ANYBODY,
34                 ME
35         }
36         public enum Status {
37                 ALL,
38                 ACTIVE
39         }
40         
41    //const string baseurl = "https://roojs.com/admin.php/Ro/mtrack_ticket"; 
42         const string roourl = "https://roojs.com/admin.php/Roo";        
43         public static RooTicket singleton()
44     {
45         if (_RooTicket == null) {
46             _RooTicket = new RooTicket();
47             _RooTicket.tickets = new Gee.ArrayList<RooTicket>();
48  
49  
50  
51         }
52         return _RooTicket;
53     }
54         public Gee.ArrayList<RooTicket> tickets; // only available for singletonn.
55  
56
57         
58         public Gee.ArrayList<RooOption> milestones;
59         public Gee.ArrayList<RooOption> priorities;
60         public Gee.ArrayList<RooOption> serverities;
61         public Gee.ArrayList<RooOption> classifications;
62         public Gee.ArrayList<RooOption> developers;
63                  
64         
65         public string username = ""; // only available for singletonn.
66         public string password = ""; // only available for singletonn.
67
68
69         public string  id; // not really important that they are numbers..
70         public string summary;
71         public string description;
72         public string project_id_name;
73
74
75         public string summaryToBranchName()
76         {
77                 // first 5 words of summary..
78                 var  regex = new Regex ("[^A-Za-z0-9 ]+");
79                 var str = regex.replace(this.summary, this.summary.length,0, "");
80                 string[] words = Regex.split_simple ("[ \t]+", str);
81                 var ret = "";
82                 for (var i =0; i< (words.length > 5 ? 5 : words.length); i++) {
83                         ret += ret.length > 0 ? "_" : "";
84                         ret += words[i];
85                 }
86                 return ret;
87         }
88         public string usernameLocal()
89         {
90                 // git username is an email addres... - so this reutrns the local part..
91                 //?? assumes that all members are under the same domain... normally the case......
92                 return RooTicket.singleton().username.split("@")[0];
93         
94         }
95
96         public Gee.ArrayList<RooOption> readJsonArray(Json.Array a)
97         {
98                 var ret = new Gee.ArrayList<RooOption>();
99                 for(var i = 0; i < a.get_length(); i++) {
100                         var t = a.get_object_element(i);
101                         ret.add(new RooOption(
102                                 t.get_string_member("id"),
103                                 t.get_string_member("name"),                            
104                                 t.get_string_member("display_name")
105                         ));
106                 }
107                 return ret;
108                          
109         }
110
111         public RooTicket addTicket(Json.Object t)
112         {
113                 var add = new RooTicket();
114                 add.id = t.get_string_member("id");
115                 add.summary = t.get_string_member("summary");
116                 add.description = t.get_string_member("description");
117                 add.project_id_name = t.get_string_member("project_id_name");                                           
118                 this.tickets.add(add);
119                 GLib.debug("ADD ticket  %s : %s : %s", add.id, add.summary, add.project_id_name);
120                 return add;
121         }
122          
123  
124  
125         
126         
127         public static RooTicket fakeTicket()
128         {
129                 var t = new RooTicket();
130                 t.id = "-1";
131                 t.summary = "";
132                 t.description = "";
133                 t.project_id_name = "";
134                 RooTicket.singleton().tickets.add(t);
135                 return t;
136         }
137         
138  
139         public RooTicket? getById(string id)
140         {
141                 foreach(var t in this.tickets) {
142                         if (t.id == id) {
143                                 return t;
144                         }
145                 }
146                 if (id == "-1") {
147                         return RooTicket.fakeTicket();
148                 }
149                 
150         return this.loadTicket(id);
151
152  
153         }
154
155
156         public RooTicket? loadTicket(string id)
157         {
158
159                    var table = new GLib.HashTable<string, string>(str_hash, str_equal);
160                    table.insert("_id",id);         
161                    
162                    var params = Soup.Form.encode_hash(table);              
163                    var url = "%s/%s?%s" . printf(roourl, "mtrack_ticket", params);
164                    GLib.debug("request %s", url);
165                    
166                    var session = new Soup.Session ();
167                    session.timeout = 0;
168                    var message = new Soup.Message ("GET", url);
169                    RooTicket.setAuth(message);
170                    session.send_message (message);
171                    
172                    
173                    var data = (string) message.response_body.flatten().data;
174                    GLib.debug("got %s", data);
175                    try {
176                            var parser = new Json.Parser ();
177                            parser.load_from_data (data, -1);
178
179                            var response = parser.get_root().get_object();
180                            var status = response.get_boolean_member("success");
181                    
182                            if(!status){
183                                    GLib.error(response.get_string_member("errorMsg"));
184                                    return null;
185                            }
186                            var rd = response.get_object_member ("data");
187                            
188                            return this.addTicket(rd);
189                            
190                             
191                    
192                    } catch (Error e) {
193                            GLib.error(e.message);
194                            return null;
195                    }
196         }
197
198         public void loadTickets(string project_id, Who who, Status status)
199         {
200                 RooTicket.singleton().tickets = new Gee.ArrayList<RooTicket>();
201          
202                 
203                 var table = new GLib.HashTable<string, string>(str_hash, str_equal);
204
205                 table.insert("_developer", who.to_string().down().substring(15));
206                 
207                 table.insert("query[viewtype]", status.to_string().down().substring(18));
208
209                 table.insert("limit","200");
210                 table.insert("sort","summary");
211                 table.insert("dir","ASC");
212                 
213                 if (project_id != "") {
214                         table.insert("project_id",project_id);
215                 }
216
217
218                 var params = Soup.Form.encode_hash(table);
219                 
220                 var url = "%s/%s?%s" . printf(roourl, "mtrack_ticket", params);
221                 
222                 GLib.debug("request %s", url);
223                 
224                 var session = new Soup.Session ();
225                 session.timeout = 0;
226                 var message = new Soup.Message ("GET", url);
227                 
228                 
229                 RooTicket.setAuth(message);
230                 
231                 session.send_message (message);
232                 
233                 var data = (string) message.response_body.flatten().data;
234                 //GLib.debug("got %s", data);
235                 try {
236                         var parser = new Json.Parser ();
237                         parser.load_from_data (data, -1);
238
239                         var response = parser.get_root().get_object();
240                         var success = response.get_boolean_member("success");
241                 
242                         if(!success){
243                                 GLib.error(response.get_string_member("errorMsg"));
244                                 return;
245                         }
246                         var rd = response.get_array_member ("data");
247                         
248                         // got a valid result...
249                         var _this = RooTicket.singleton();
250                         for(var i = 0; i < rd.get_length(); i++) {
251                                 _this.addTicket(rd.get_object_element(i));
252                         }
253                          
254                 
255                 } catch (Error e) {
256                         GLib.error(e.message);
257                         return;
258                 }
259                 
260         }
261         
262  
263         public void loadProjectOptions(string pid)
264         {
265                 var rt = RooTicket.singleton();
266                 rt.milestones = new Gee.ArrayList<RooOption>();
267                 rt.priorities =  new Gee.ArrayList<RooOption>();
268                 rt.serverities =  new Gee.ArrayList<RooOption>();
269                 rt.classifications  =  new Gee.ArrayList<RooOption>();
270                 rt.developers =  new Gee.ArrayList<RooOption>();
271  
272                 if (pid == "") {
273                         return;
274                 }
275                 
276
277
278                 var table = new GLib.HashTable<string, string>(str_hash, str_equal);
279          
280                 table.insert("_options_for",pid);
281
282                 var params = Soup.Form.encode_hash(table);
283                 
284                 var url = "%s/%s?%s" . printf(roourl, "mtrack_ticket", params);
285                 
286                 GLib.debug("request %s", url);
287                 
288                 var session = new Soup.Session ();
289                 session.timeout = 0;
290                 var message = new Soup.Message ("GET", url);
291                 
292                 
293                 RooTicket.setAuth(message);
294                 
295                 session.send_message (message);
296                 
297                 var data = (string) message.response_body.flatten().data;
298                 //GLib.debug("got %s", data);
299                 try {
300                         var parser = new Json.Parser ();
301                         parser.load_from_data (data, -1);
302
303                         var response = parser.get_root().get_object();
304                         var status = response.get_boolean_member("success");
305                 
306                         if(!status){
307                                 GLib.error(response.get_string_member("errorMsg"));
308                                 return;
309                         }
310                         var rd = response.get_object_member ("data");
311                         
312                         rt.milestones = this.readJsonArray( rd.get_array_member("milestone"));
313                         rt.priorities = this.readJsonArray( rd.get_array_member("priority"));
314                         rt.serverities = this.readJsonArray( rd.get_array_member("severity"));
315                         rt.classifications  = this.readJsonArray( rd.get_array_member("classification"));
316                         rt.developers = this.readJsonArray( rd.get_array_member("developer"));
317  
318                         
319                         
320                         
321                         // got a valid result...
322                          
323                 
324                 } catch (Error e) {
325                         GLib.error(e.message);
326                         return;
327                 }
328                 
329         }
330         
331         public  static void setAuth(Soup.Message message) 
332         {
333                 var rs =  RooTicket.singleton();                
334                 if (rs.username.length < 1) {
335                         string str;
336                         GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
337                         var lines = str.split("\n");
338                         for(var i=0; i< lines.length; i++) {
339                         // assumes one line per entry.. if not we are buggered...
340                                         GLib.debug("got %s" , lines[i]);
341                         
342                                 var bits =  Regex.split_simple ("[ \t]+", lines[i].strip());
343                                 if (bits.length < 6 || bits[0] != "machine" || bits[1] != "git.roojs.com") {
344                                         continue;
345                                 }
346                                         GLib.debug("found password?");
347                                 // we are gussing.... 
348
349                                 RooTicket.singleton().username  = bits[3];
350                                 RooTicket.singleton().password  = bits[5];              
351                         }
352                 }
353
354
355                 var authCode = Base64.encode ("%s:%s".printf(rs.username, rs.password).data);
356                 message.request_headers.append("Authorization", "Basic %s".printf(authCode));
357         
358         
359         }
360
361
362         public void close(string commits)
363     {
364                 if (this.id == "-1") {
365                         return;
366                 }
367                 var table = new GLib.HashTable<string, string>(str_hash, str_equal);
368                 table.insert("id",id);
369                 table.insert("status_name","resolved");
370                 table.insert("reason","fixed by Commits: %s".printf(commits));  
371                 var params = Soup.Form.encode_hash(table);              
372
373                 GLib.debug("request POST %s / %s", roourl, id);
374
375                 var session = new Soup.Session ();
376                 session.timeout = 0;
377                 var message = new Soup.Message ("POST", roourl + "/mtrack_ticket");
378                 RooTicket.setAuth(message);
379
380                 message.set_request ("application/x-www-form-urlencoded", Soup.MemoryUse.STATIC, params.data);
381                 session.send_message (message);
382
383
384                 var data = (string) message.response_body.flatten().data;
385                 GLib.debug("got %s", data);
386                 try {
387                            var parser = new Json.Parser ();
388                            parser.load_from_data (data, -1);
389
390                            var response = parser.get_root().get_object();
391                            var status = response.get_boolean_member("success");
392
393                            if(!status){
394                                            GLib.error(response.get_string_member("errorMsg"));
395                                            return ;
396                            }
397                                 
398                            
399                                 
400
401                 } catch (Error e) {
402                            GLib.error(e.message);
403                            return ;
404                 }
405
406    }
407         public string createTicket(     
408                         string project_id,
409                         string milestone_id,
410                         string priority_id,
411                         string severity_id,                     
412                         string classification_id,
413                         string developer_id,
414                         string summary,
415                         string description
416                 ) 
417         {
418                  
419                 var table = new GLib.HashTable<string, string>(str_hash, str_equal);
420                  
421                 table.insert("project_id", project_id);
422                 table.insert("milestone_id", milestone_id);
423                 table.insert("priority_id", priority_id);
424                 table.insert("severity_id", classification_id);         
425                 table.insert("classification_id", classification_id);
426                 table.insert("developer_id", developer_id);
427                 table.insert("summary", summary);
428                 table.insert("description", description);
429                  
430                 var params = Soup.Form.encode_hash(table);              
431
432                 GLib.debug("request POST %s / %s", roourl, id);
433
434                 var session = new Soup.Session ();
435                 session.timeout = 0;
436                 var message = new Soup.Message ("POST", roourl + "/mtrack_ticket");
437                 RooTicket.setAuth(message);
438
439                 message.set_request ("application/x-www-form-urlencoded", Soup.MemoryUse.STATIC, params.data);
440                 session.send_message (message);
441
442
443                 var data = (string) message.response_body.flatten().data;
444                 GLib.debug("got %s", data);
445                 try {
446                            var parser = new Json.Parser ();
447                            parser.load_from_data (data, -1);
448
449                            var response = parser.get_root().get_object();
450                            var status = response.get_boolean_member("success");
451
452                            if(!status){
453                                            GLib.error(response.get_string_member("errorMsg"));
454                                            return "" ;
455                            }
456                                 var rd = response.get_object_member ("data");
457                                 return rd.get_string_member("id");
458                            
459                                 
460
461                 } catch (Error e) {
462                            GLib.error(e.message);
463                            return "";
464                 }
465    
466         }
467         
468 }
469