php8
[web.mtrack] / MTrack / CommitChecker.php
index cf89b39..fc5f940 100644 (file)
@@ -4,17 +4,19 @@ require_once 'MTrack/Interface/CommitListener.php';
 require_once 'MTrack/Interface/CommitHookBridge.php'; 
 require_once 'MTrack/Interface/CommitHookBridge2.php'; 
 
-require_once 'MTrack/Issue.php'; 
-require_once 'MTrack/DB.php'; 
-//require_once 'MTrack/Changeset.php'; 
-require_once 'MTrack/Config.php'; 
+//require_once 'MTrack/Issue.php'; 
+ //require_once 'MTrack/Changeset.php'; 
+  
 
  
 
-class MTrackCommitChecker {
+class MTrack_CommitChecker {
+    
     static $fileChecks = array(
         'php' => 'checkPHP',
     );
+    
+    
     static $listeners = array();
    
     static function addCheck($name)
@@ -27,10 +29,25 @@ class MTrackCommitChecker {
     
     var $repo;
     var $bridge;
+    var $authUser;
     
-    function __construct($repo) {
-        $this->repo = $repo;
+    var $checks = array();
+    var $no_ticket;
+    var $deferred;
+    var $spent_by_tid_by_user;
+    
+    
+    function __construct($ar) {
+        foreach($ar as $k=>$v) {
+            $this->$k = $v;
+        }
+        foreach($this->checks as $chk) {
+            self::addCheck($chk);
+        }
+        
+        
     }
+    
 
     function checkVeto()
     {
@@ -112,6 +129,8 @@ class MTrackCommitChecker {
 
     function preCommit(IMTrackCommitHookBridge $bridge) 
     {
+        die("NOT SUPPORTED YET!");
+       
         //echo "Pre-commit";
         $this->bridge = $bridge;
         MTrackACL::requireAllRights("repo:" . $this->repo->id, 'commit');
@@ -177,10 +196,14 @@ class MTrackCommitChecker {
         } else {
             require_once 'MTrack/CommitHookChangeEvent.php';
             $c = new MTrackCommitHookChangeEvent;
-            $c->rev = $bridge->getChangesetDescriptor();
-            $c->changelog = $bridge->getCommitMessage();
-            $c->changeby = MTrackAuth::whoami();
-            $c->ctime = time();
+            $c->rev         = $bridge->getChangesetDescriptor();
+            $c->changelog   = $bridge->getCommitMessage();
+            $c->changeby    = $this->authUser->email; //???
+            $c->changeby_id = $this->authUser->id; //???
+            $c->branch       = $bridge->branch;
+            //print_r($bridge);exit;
+            $c->ctime       = isset($bridge->props['Date']) ? strtotime($bridge->props['Date']) : time();
+            $c->fileActions = $bridge->fileActions;
             $changes[] = $c;
         }
         return $changes;
@@ -188,15 +211,27 @@ class MTrackCommitChecker {
 
     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();
@@ -209,47 +244,62 @@ class MTrackCommitChecker {
         // Changes that didn't ref a ticket; we want to show something
         // on the timeline
         $no_ticket = array();
+        
+        $me = $this->authUser;
+
+
 
-        $me = mtrack_canon_username(MTrackAuth::whoami());
 
         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;
@@ -263,27 +313,70 @@ class MTrackCommitChecker {
                   $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])) {
@@ -333,6 +426,8 @@ class MTrackCommitChecker {
         }
         $CS = MTrackChangeset::begin("repo:" . $this->repo->id, $log);
         $CS->commit();
+        
+        */
     }