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