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