Merge branch 'master' of /home/git/private/web.mtrack
[web.mtrack] / MTrack / CommitChecker.php
index 0898f14..1924c50 100644 (file)
@@ -31,6 +31,7 @@ class MTrack_CommitChecker {
     var $bridge;
     var $authUser;
     
+    var $checks = array();
     
     function __construct($ar) {
         foreach($ar as $k=>$v) {
@@ -38,11 +39,11 @@ class MTrack_CommitChecker {
         }
         foreach($this->checks as $chk) {
             self::addCheck($chk);
-            
         }
         
         
     }
+    
 
     function checkVeto()
     {
@@ -191,10 +192,13 @@ class MTrack_CommitChecker {
         } else {
             require_once 'MTrack/CommitHookChangeEvent.php';
             $c = new MTrackCommitHookChangeEvent;
-            $c->rev = $bridge->getChangesetDescriptor();
-            $c->changelog = $bridge->getCommitMessage();
-            $c->changeby = $this->authUser->email; //???
-            $c->ctime = time();
+            $c->rev         = $bridge->getChangesetDescriptor();
+            $c->changelog   = $bridge->getCommitMessage();
+            $c->changeby    = $this->authUser->email; //???
+            $c->changeby_id = $this->authUser->id; //???
+            //print_r($bridge);exit;
+            $c->ctime       = isset($bridge->props['Date']) ? strtotime($bridge->props['Date']) : time();
+            $c->fileActions = $bridge->fileActions;
             $changes[] = $c;
         }
         return $changes;
@@ -202,15 +206,27 @@ class MTrack_CommitChecker {
 
     function postCommit(IMTrackCommitHookBridge $bridge)
     {
+       
+        // this might be run on multiple commits (big push...)
+        
+        
+        // in our system, we not only log commits that are against a
+        // ticket, but also ones that are not..
+        
+        
         $files = $bridge->enumChangedOrModifiedFileNames();
         
         $fqfiles = array();
         foreach ($files as $filename) {
-          $fqfiles[] = $this->repo->shortname . '/' . $filename;
+            $fqfiles[] = $this->repo->shortname . '/' . $filename;
         }
 
         // build up overall picture of what needs to be applied to tickets
         $changes = $this->_getChanges($bridge);
+        
+        
+        //print_R($changes);
+        
 
         // Deferred by tid
         $deferred = array();
@@ -226,44 +242,59 @@ class MTrack_CommitChecker {
         
         $me = $this->authUser;
 
+
+
+
         foreach ($changes as $c) {
-          $tickets = array();
-          $log = $c->changelog;
-
-          $actions = $this->parseCommitMessage($log);
-          foreach ($actions as $act) {
-            $what = $act[0];
-            $tkt = $act[1];
-            $tickets[$tkt][$what] = $what;
-            if (isset($act[2])) {
-              $tickets[$tkt]['spent'] += $act[2];
+            $tickets = array();
+            $log = $c->changelog;
+
+            $actions = $this->parseCommitMessage($log);
+            foreach ($actions as $act) {
+                $what = $act[0];
+                $tkt = $act[1];
+                $tickets[$tkt][$what] = $what;
+                if (isset($act[2])) {
+                  $tickets[$tkt]['spent'] += $act[2];
+                }
             }
-          }
-          if (count($tickets) == 0) {
-            $no_ticket[] = $c;
-            continue;
-          }
-          // apply changes to tickets
-          foreach ($tickets as $tkt => $act) {
-            if (strlen($tkt) == 32 && isset($T_by_tid[$tkt])) {
-              $T = $T_by_tid[$tkt];
-            } else {
-              if (strlen($tkt) == 32) {
-                $T = MTrackIssue::loadById($tkt);
-              } else {
-                $T = MTrackIssue::loadByNSIdent($tkt);
-              }
-              $T_by_tid[$T->tid] = $T;
+            if (count($tickets) == 0) {
+                $no_ticket[] = $c;
+                
+                continue;
             }
-
+            
+            // apply changes to tickets
+            $T = false;
+            foreach ($tickets as $tkt => $act) {
+                // removed all the code that handles hashed ticked ids...
+                //DB_DataObject::DebugLevel(1);
+                $T = DB_DataObject::Factory('mtrack_ticket');
+                $T->project_id = $this->repo->project_id;
+                if (!$T->get($tkt)) {
+                    continue;
+                }
+                break;
+                
+                $T_by_tid[$T->id] = $T;
+            }
+            
+            if (!$T) {
+                continue;
+            }
+            /*
             $accounted = false;
+            
             if ($c->hash !== null) {
-              if (isset($hashed[$T->tid][$c->hash])) {
-                $accounted = true;
-              } else {
-                list($accounted) = MTrackDB::q(
-                  'select count(hash) from ticket_changeset_hashes
-                  where tid = ? and hash = ?',
+                if (isset($hashed[$T->tid][$c->hash])) {
+                    $accounted = true;
+                } else {
+                    // see if we already have a reference
+                    
+                    
+                    list($accounted) = MTrackDB::q(
+                          'select count(hash) from ticket_changeset_hashes
+                      where tid = ? and hash = ?',
                   $T->tid, $c->hash)->fetchAll(PDO::FETCH_COLUMN, 0);
                 if (!$accounted) {
                   $hashed[$T->tid][$c->hash] = $c->hash;
@@ -277,27 +308,70 @@ class MTrack_CommitChecker {
                   $this->repo->getBrowseRootName() . "]";
               continue;
             }
+            
+            */
+            
             $log = "(In " . $c->rev . ") ";
-            if ($c->changeby != $me) {
-              $log .= " (on behalf of [user:$c->changeby]) ";
-            }
+                /*
+                 .. we do not support commits on behalf of yet..
+                 .. to fix this we need to change the auth code to
+                 .. really pick up auth data..
+                 
+                if ($c->changeby != $me) {
+                  $log .= " (on behalf of [user:$c->changeby]) ";
+                }
+                */
+                
+            // for the stuff below we do not currently support multiple tickets..
+            
             $log .= $c->changelog;
-            $deferred[$T->tid]['comments'][] = $log;
+            if (!isset($deferred[$T->id])) {
+                $deferred[$T->id] = array(
+                    'comments' => array(),
+                    'changes' => array(),
+                    'act' => array(),
+                    'ticket' => $T
+                    
+                );
+            }
+            
+            
+            $deferred[$T->id]['comments'][] = $log;
+            $deferred[$T->id]['changes'][] = $c;
+            
+            
+            
             if (isset($act['spent']) && $c->changeby != $me) {
-              $spent_by_tid_by_user[$T->tid][$c->changeby][] = $act['spent'];
-              unset($act['spent']);
+                $spent_by_tid_by_user[$T->id][$c->changeby_id][] = $act['spent'];
+                unset($act['spent']);
             }
-            $deferred[$T->tid]['act'][] = $act;
-
-          }
-          $this->checkVeto('postCommit', $log, $fqfiles, $actions);
+            $deferred[$T->id]['act'][] = $act;
+            //??? 
+            $this->checkVeto('postCommit', $log, $fqfiles, $actions);
         }
+        
+        
+        // defered is a list of actions...
+        $this->no_ticket = $no_ticket;
+        $this->deferred = $deferred;
+        $this->spent_by_tid_by_user = $spent_by_tid_by_user;
+        
+        
+        return  true;
+    
+    /*    
+        $ret = array();
        // print_r($deferred);
         foreach ($deferred as $tid => $info) {
-          $T = $T_by_tid[$tid];
-
-          $log = join("\n\n", $info['comments']);
+            $T = $T_by_tid[$tid];
 
+            $log = join("\n\n", $info['comments']);
+            
+            
+          
+          
+          
           $CS = MTrackChangeset::begin("ticket:" . $T->tid, $log);
 
           if (isset($hashed[$T->tid])) {
@@ -347,6 +421,8 @@ class MTrack_CommitChecker {
         }
         $CS = MTrackChangeset::begin("repo:" . $this->repo->id, $log);
         $CS->commit();
+        
+        */
     }