3 require_once 'MTrack/Interface/CommitListener.php';
4 require_once 'MTrack/Interface/CommitHookBridge.php';
5 require_once 'MTrack/Interface/CommitHookBridge2.php';
7 //require_once 'MTrack/Issue.php';
8 //require_once 'MTrack/Changeset.php';
13 class MTrack_CommitChecker {
15 static $fileChecks = array(
20 static $listeners = array();
22 static function addCheck($name)
24 require_once "MTrack/CommitCheck/$name.php";
25 $cls = "MTrackCommitCheck_$name";
26 self::$listeners[] = new $cls;
35 function __construct($ar) {
36 foreach($ar as $k=>$v) {
45 $args = func_get_args();
46 $method = array_shift($args);
49 foreach (self::$listeners as $l) {
50 $v = call_user_func_array(array($l, $method), $args);
52 if ($v === null || $v === false) {
53 $reasons[] = sprintf("%s:%s() returned %s",
54 get_class($l), $method, $v === null ? 'null' : 'false');
55 } elseif (is_array($v)) {
64 if (count($reasons)) {
65 require_once 'MTrack/Exception/Veto.php';
66 throw new MTrackVetoException($reasons);
70 function parseCommitMessage($msg)
72 // Parse the commit message and look for commands;
73 // returns each recognized command and its args in an array
75 $close = array('resolves', 'resolved', 'close', 'closed',
76 'closes', 'fix', 'fixed', 'fixes');
77 $refs = array('addresses', 'references', 'referenced',
78 'refs', 'ref', 'see', 're');
80 $cmds = join('|', $close) . '|' . join('|', $refs);
83 $timepat = ''; //'(?:\s*\((?:spent|sp)\s*(-?[0-9]*(?:\.[0-9]+)?)\s*(?:hours?|hrs)?\s*\))?';
84 $tktref = "(?:#|(?:(?:ticket|issue|bug):?\s*))([a-z]*[0-9]+)$timepat";
86 $pat = "(?P<action>(?:$cmds))\s*(?P<ticket>$tktref(?:(?:[, &]*|\s+and\s+)$tktref)*)";
92 if (preg_match_all("/$pat/smi", $msg, $M, PREG_SET_ORDER)) {
94 foreach ($M as $match) {
95 if (in_array($match['action'], $close)) {
96 $action = 'ref'; // 'close'; - commits need reviewing before they can close something.
102 if (preg_match_all("/$tktref/smi", $match['ticket'],
103 $T, PREG_SET_ORDER)) {
105 foreach ($T as $tmatch) {
106 if (isset($tmatch[2])) {
107 // [ action, ticket, spent ]
108 $actions[] = array($action, $tmatch[1], $tmatch[2]);
110 // [ action, ticket ]
111 $actions[] = array($action, $tmatch[1]);
121 function preCommit(IMTrackCommitHookBridge $bridge)
123 die("NOT SUPPORTED YET!");
126 $this->bridge = $bridge;
127 MTrackACL::requireAllRights("repo:" . $this->repo->id, 'commit');
130 $files = $bridge->enumChangedOrModifiedFileNames();
132 $changes = $this->_getChanges($bridge);
133 foreach ($changes as $c) {
134 $log = $c->changelog;
135 $actions = $this->parseCommitMessage($log);
137 // check permissions on the tickets
139 foreach ($actions as $act) {
141 $tickets[$tkt] = $tkt;
144 foreach ($tickets as $tkt) {
145 if (strlen($tkt) == 32) {
146 $T = MTrackIssue::loadById($tkt);
148 $T = MTrackIssue::loadByNSIdent($tkt);
152 $reasons[] = "#$tkt is not a valid ticket\n";
157 if ($c->hash !== null) {
158 list($accounted) = MTrackDB::q(
159 'select count(hash) from ticket_changeset_hashes
160 where tid = ? and hash = ?',
161 $T->tid, $c->hash)->fetchAll(PDO::FETCH_COLUMN, 0);
167 if (!MTrackACL::hasAllRights("ticket:$T->tid", "modify")) {
168 $reasons[] = MTrackAuth::whoami() . " does not have permission to modify #$tkt\n";
169 } else if (!$T->isOpen()) {
170 $reasons[] = " ** #$tkt is already closed.\n ** You must either re-open it (if it has not already shipped)\n ** or open a new ticket to track this issue\n";
174 if (count($reasons) > 0) {
175 require_once 'MTrack/Exception/Veto.php';
176 throw new MTrackVetoException($reasons);
178 $this->checkVeto('vetoCommit', $log, $files, $actions, $this);
181 private function _getChanges(IMTrackCommitHookBridge $bridge)
184 if ($bridge instanceof IMTrackCommitHookBridge2) {
185 // this is HG only at present.
186 $changes = $bridge->getChanges();
188 require_once 'MTrack/CommitHookChangeEvent.php';
189 $c = new MTrackCommitHookChangeEvent;
190 $c->rev = $bridge->getChangesetDescriptor();
191 $c->changelog = $bridge->getCommitMessage();
192 $c->changeby = MTrackAuth::whoami();
199 function postCommit(IMTrackCommitHookBridge $bridge)
201 $files = $bridge->enumChangedOrModifiedFileNames();
204 foreach ($files as $filename) {
205 $fqfiles[] = $this->repo->shortname . '/' . $filename;
208 // build up overall picture of what needs to be applied to tickets
209 $changes = $this->_getChanges($bridge);
216 // For correct attribution of spent time
217 $spent_by_tid_by_user = array();
219 // Changes that didn't ref a ticket; we want to show something
221 $no_ticket = array();
223 $me = mtrack_canon_username(MTrackAuth::whoami());
225 foreach ($changes as $c) {
227 $log = $c->changelog;
229 $actions = $this->parseCommitMessage($log);
230 foreach ($actions as $act) {
233 $tickets[$tkt][$what] = $what;
234 if (isset($act[2])) {
235 $tickets[$tkt]['spent'] += $act[2];
238 if (count($tickets) == 0) {
242 // apply changes to tickets
243 foreach ($tickets as $tkt => $act) {
244 if (strlen($tkt) == 32 && isset($T_by_tid[$tkt])) {
245 $T = $T_by_tid[$tkt];
247 if (strlen($tkt) == 32) {
248 $T = MTrackIssue::loadById($tkt);
250 $T = MTrackIssue::loadByNSIdent($tkt);
252 $T_by_tid[$T->tid] = $T;
256 if ($c->hash !== null) {
257 if (isset($hashed[$T->tid][$c->hash])) {
260 list($accounted) = MTrackDB::q(
261 'select count(hash) from ticket_changeset_hashes
262 where tid = ? and hash = ?',
263 $T->tid, $c->hash)->fetchAll(PDO::FETCH_COLUMN, 0);
265 $hashed[$T->tid][$c->hash] = $c->hash;
271 $deferred[$T->tid]['comments'][] =
272 "(In $c->rev) merged to [repo:" .
273 $this->repo->getBrowseRootName() . "]";
276 $log = "(In " . $c->rev . ") ";
277 if ($c->changeby != $me) {
278 $log .= " (on behalf of [user:$c->changeby]) ";
280 $log .= $c->changelog;
281 $deferred[$T->tid]['comments'][] = $log;
282 if (isset($act['spent']) && $c->changeby != $me) {
283 $spent_by_tid_by_user[$T->tid][$c->changeby][] = $act['spent'];
284 unset($act['spent']);
286 $deferred[$T->tid]['act'][] = $act;
289 $this->checkVeto('postCommit', $log, $fqfiles, $actions);
291 // print_r($deferred);
292 foreach ($deferred as $tid => $info) {
293 $T = $T_by_tid[$tid];
295 $log = join("\n\n", $info['comments']);
297 $CS = MTrackChangeset::begin("ticket:" . $T->tid, $log);
299 if (isset($hashed[$T->tid])) {
300 foreach ($hashed[$T->tid] as $hash) {
302 'insert into ticket_changeset_hashes(tid, hash) values (?, ?)',
307 $T->addComment($log);
308 if (isset($info['act'])) foreach ($info['act'] as $act) {
309 if (isset($act['close'])) {
310 $T->resolution = 'fixed';
313 if (isset($act['spent'])) {
314 $T->addEffort($act['spent']);
320 foreach ($spent_by_tid_by_user as $tid => $sdata) {
321 // Load it fresh here, as there seems to be an issue with saving
322 // a second set of changes on a pre-existing object
323 $T = MTrackIssue::loadById($tid);
324 foreach ($sdata as $user => $time) {
325 MTrackAuth::su($user);
326 $CS = MTrackChangeset::begin("ticket:" . $T->tid,
327 "Tracking time from prior push");
329 foreach ($time as $spent) {
330 $T->addEffort($spent);
337 foreach ($no_ticket as $c) {
338 $log .= "(In " . $c->rev . ") ";
339 if ($c->changeby != $me) {
340 $log .= " (on behalf of [user:$c->changeby]) ";
342 $log .= $c->changelog . "\n\n";
344 $CS = MTrackChangeset::begin("repo:" . $this->repo->id, $log);