Makefile.am
[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             _RooTicket.loadRepos(); // initalize it..        
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         public RooRepo addRepo(Json.Object t)
124         {
125                 var add = new RooRepo();
126                 add.id = t.get_string_member("id");
127                 add.shortname = t.get_string_member("shortname");
128                 add.description = t.get_string_member("description");           
129                 add.project_id = t.get_string_member("project_id");                                             
130                 this.repos.add(add);
131                 GLib.debug("ADD project  %s : %s : %s", add.id, add.shortname, add.project_id);
132                 return add;
133         }
134         
135  
136         
137         
138         public static RooTicket fakeTicket()
139         {
140                 var t = new RooTicket();
141                 t.id = "-1";
142                 t.summary = "";
143                 t.description = "";
144                 t.project_id_name = "";
145                 RooTicket.singleton().tickets.add(t);
146                 return t;
147         }
148         
149  
150         public RooTicket? getById(string id)
151         {
152                 foreach(var t in this.tickets) {
153                         if (t.id == id) {
154                                 return t;
155                         }
156                 }
157                 if (id == "-1") {
158                         return RooTicket.fakeTicket();
159                 }
160                 
161         return this.loadTicket(id);
162
163  
164         }
165
166
167         public RooTicket? loadTicket(string id)
168         {
169
170                    var table = new GLib.HashTable<string, string>(str_hash, str_equal);
171                    table.insert("_id",id);         
172                    
173                    var params = Soup.Form.encode_hash(table);              
174                    var url = "%s/%s?%s" . printf(roourl, "mtrack_ticket", params);
175                    GLib.debug("request %s", url);
176                    
177                    var session = new Soup.Session ();
178                    session.timeout = 0;
179                    var message = new Soup.Message ("GET", url);
180                    RooTicket.setAuth(message);
181                    session.send_message (message);
182                    
183                    
184                    var data = (string) message.response_body.flatten().data;
185                    GLib.debug("got %s", data);
186                    try {
187                            var parser = new Json.Parser ();
188                            parser.load_from_data (data, -1);
189
190                            var response = parser.get_root().get_object();
191                            var status = response.get_boolean_member("success");
192                    
193                            if(!status){
194                                    GLib.error(response.get_string_member("errorMsg"));
195                                    return null;
196                            }
197                            var rd = response.get_object_member ("data");
198                            
199                            return this.addTicket(rd);
200                            
201                             
202                    
203                    } catch (Error e) {
204                            GLib.error(e.message);
205                            return null;
206                    }
207         }
208
209         public void loadTickets(string project_id, Who who, Status status)
210         {
211                 RooTicket.singleton().tickets = new Gee.ArrayList<RooTicket>();
212          
213                 
214                 var table = new GLib.HashTable<string, string>(str_hash, str_equal);
215
216                 table.insert("_developer", who.to_string().down().substring(15));
217                 
218                 table.insert("query[viewtype]", status.to_string().down().substring(18));
219
220                 table.insert("limit","200");
221                 table.insert("sort","summary");
222                 table.insert("dir","ASC");
223                 
224                 if (project_id != "") {
225                         table.insert("project_id",project_id);
226                 }
227
228
229                 var params = Soup.Form.encode_hash(table);
230                 
231                 var url = "%s/%s?%s" . printf(roourl, "mtrack_ticket", params);
232                 
233                 GLib.debug("request %s", url);
234                 
235                 var session = new Soup.Session ();
236                 session.timeout = 0;
237                 var message = new Soup.Message ("GET", url);
238                 
239                 
240                 RooTicket.setAuth(message);
241                 
242                 session.send_message (message);
243                 
244                 var data = (string) message.response_body.flatten().data;
245                 //GLib.debug("got %s", data);
246                 try {
247                         var parser = new Json.Parser ();
248                         parser.load_from_data (data, -1);
249
250                         var response = parser.get_root().get_object();
251                         var success = response.get_boolean_member("success");
252                 
253                         if(!success){
254                                 GLib.error(response.get_string_member("errorMsg"));
255                                 return;
256                         }
257                         var rd = response.get_array_member ("data");
258                         
259                         // got a valid result...
260                         var _this = RooTicket.singleton();
261                         for(var i = 0; i < rd.get_length(); i++) {
262                                 _this.addTicket(rd.get_object_element(i));
263                         }
264                          
265                 
266                 } catch (Error e) {
267                         GLib.error(e.message);
268                         return;
269                 }
270                 
271         }
272         
273          
274         public void loadRepos()
275         {
276                 RooTicket.singleton().repos = new Gee.ArrayList<RooRepo>();
277          
278                 
279                 var table = new GLib.HashTable<string, string>(str_hash, str_equal);
280          
281
282                 table.insert("limit","200");
283                 table.insert("sort","shortname");
284                 table.insert("dir","ASC");
285
286                 var params = Soup.Form.encode_hash(table);
287                 
288                 var url = "%s/%s?%s" . printf(roourl, "mtrack_repos", 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_array_member ("data");
315                         
316                         // got a valid result...
317                         var _this = RooTicket.singleton();
318                         for(var i = 0; i < rd.get_length(); i++) {
319                                 _this.addRepo(rd.get_object_element(i));
320                         }
321                          
322                 
323                 } catch (Error e) {
324                         GLib.error(e.message);
325                         return;
326                 }
327                 
328         }
329         public void loadProjectOptions(string pid)
330         {
331                 var rt = RooTicket.singleton();
332                 rt.milestones = new Gee.ArrayList<RooOption>();
333                 rt.priorities =  new Gee.ArrayList<RooOption>();
334                 rt.serverities =  new Gee.ArrayList<RooOption>();
335                 rt.classifications  =  new Gee.ArrayList<RooOption>();
336                 rt.developers =  new Gee.ArrayList<RooOption>();
337  
338                 if (pid == "") {
339                         return;
340                 }
341                 
342
343
344                 var table = new GLib.HashTable<string, string>(str_hash, str_equal);
345          
346                 table.insert("_options_for",pid);
347
348                 var params = Soup.Form.encode_hash(table);
349                 
350                 var url = "%s/%s?%s" . printf(roourl, "mtrack_ticket", params);
351                 
352                 GLib.debug("request %s", url);
353                 
354                 var session = new Soup.Session ();
355                 session.timeout = 0;
356                 var message = new Soup.Message ("GET", url);
357                 
358                 
359                 RooTicket.setAuth(message);
360                 
361                 session.send_message (message);
362                 
363                 var data = (string) message.response_body.flatten().data;
364                 //GLib.debug("got %s", data);
365                 try {
366                         var parser = new Json.Parser ();
367                         parser.load_from_data (data, -1);
368
369                         var response = parser.get_root().get_object();
370                         var status = response.get_boolean_member("success");
371                 
372                         if(!status){
373                                 GLib.error(response.get_string_member("errorMsg"));
374                                 return;
375                         }
376                         var rd = response.get_object_member ("data");
377                         
378                         rt.milestones = this.readJsonArray( rd.get_array_member("milestone"));
379                         rt.priorities = this.readJsonArray( rd.get_array_member("priority"));
380                         rt.serverities = this.readJsonArray( rd.get_array_member("severity"));
381                         rt.classifications  = this.readJsonArray( rd.get_array_member("classification"));
382                         rt.developers = this.readJsonArray( rd.get_array_member("developer"));
383  
384                         
385                         
386                         
387                         // got a valid result...
388                          
389                 
390                 } catch (Error e) {
391                         GLib.error(e.message);
392                         return;
393                 }
394                 
395         }
396         
397         public  static void setAuth(Soup.Message message) 
398         {
399                 var rs =  RooTicket.singleton();                
400                 if (rs.username.length < 1) {
401                         string str;
402                         GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
403                         var lines = str.split("\n");
404                         for(var i=0; i< lines.length; i++) {
405                         // assumes one line per entry.. if not we are buggered...
406                                         GLib.debug("got %s" , lines[i]);
407                         
408                                 var bits =  Regex.split_simple ("[ \t]+", lines[i].strip());
409                                 if (bits.length < 6 || bits[0] != "machine" || bits[1] != "git.roojs.com") {
410                                         continue;
411                                 }
412                                         GLib.debug("found password?");
413                                 // we are gussing.... 
414
415                                 RooTicket.singleton().username  = bits[3];
416                                 RooTicket.singleton().password  = bits[5];              
417                         }
418                 }
419
420
421                 var authCode = Base64.encode ("%s:%s".printf(rs.username, rs.password).data);
422                 message.request_headers.append("Authorization", "Basic %s".printf(authCode));
423         
424         
425         }
426
427
428         public void close(string commits)
429     {
430                 if (this.id == "-1") {
431                         return;
432                 }
433                 var table = new GLib.HashTable<string, string>(str_hash, str_equal);
434                 table.insert("id",id);
435                 table.insert("status_name","resolved");
436                 table.insert("reason","fixed by Commits: %s".printf(commits));  
437                 var params = Soup.Form.encode_hash(table);              
438
439                 GLib.debug("request POST %s / %s", roourl, id);
440
441                 var session = new Soup.Session ();
442                 session.timeout = 0;
443                 var message = new Soup.Message ("POST", roourl + "/mtrack_ticket");
444                 RooTicket.setAuth(message);
445
446                 message.set_request ("application/x-www-form-urlencoded", Soup.MemoryUse.STATIC, params.data);
447                 session.send_message (message);
448
449
450                 var data = (string) message.response_body.flatten().data;
451                 GLib.debug("got %s", data);
452                 try {
453                            var parser = new Json.Parser ();
454                            parser.load_from_data (data, -1);
455
456                            var response = parser.get_root().get_object();
457                            var status = response.get_boolean_member("success");
458
459                            if(!status){
460                                            GLib.error(response.get_string_member("errorMsg"));
461                                            return ;
462                            }
463                                 
464                            
465                                 
466
467                 } catch (Error e) {
468                            GLib.error(e.message);
469                            return ;
470                 }
471
472    }
473         public string createTicket(     
474                         string project_id,
475                         string milestone_id,
476                         string priority_id,
477                         string classification_id,
478                         string developer_id,
479                         string summary,
480                         string description
481                 ) 
482         {
483                  
484                 var table = new GLib.HashTable<string, string>(str_hash, str_equal);
485                  
486                 table.insert("project_id", project_id);
487                 table.insert("milestone_id", milestone_id);
488                 table.insert("priority_id", priority_id);
489                 table.insert("classification_id", classification_id);
490                 table.insert("developer_id", developer_id);
491                 table.insert("summary", summary);
492                 table.insert("description", description);
493                  
494                 var params = Soup.Form.encode_hash(table);              
495
496                 GLib.debug("request POST %s / %s", roourl, id);
497
498                 var session = new Soup.Session ();
499                 session.timeout = 0;
500                 var message = new Soup.Message ("POST", roourl + "/mtrack_ticket");
501                 RooTicket.setAuth(message);
502
503                 message.set_request ("application/x-www-form-urlencoded", Soup.MemoryUse.STATIC, params.data);
504                 session.send_message (message);
505
506
507                 var data = (string) message.response_body.flatten().data;
508                 GLib.debug("got %s", data);
509                 try {
510                            var parser = new Json.Parser ();
511                            parser.load_from_data (data, -1);
512
513                            var response = parser.get_root().get_object();
514                            var status = response.get_boolean_member("success");
515
516                            if(!status){
517                                            GLib.error(response.get_string_member("errorMsg"));
518                                            return "" ;
519                            }
520                                 var rd = response.get_object_member ("data");
521                                 return rd.get_string_member("id");
522                            
523                                 
524
525                 } catch (Error e) {
526                            GLib.error(e.message);
527                            return "";
528                 }
529    
530         }
531         
532 }
533