<?php
/**
* Perforce Swarm
*
* @copyright 2013 Perforce Software. All rights reserved.
* @license Please see LICENSE.txt in top-level readme folder of this distribution.
* @version 2013.2/706770
*/
namespace ChuckNorris;
use Activity\Model\Activity;
use Application\Filter\Linkify;
use Comments\Model\Comment;
use P4\Spec\Change;
use P4\File\File;
use P4\File\Diff;
use Projects\Model\Project;
use Users\Model\User;
use Zend\Mvc\MvcEvent;
class Module
{
/**
* Connect to queue event manager to handle changes.
*
* @param MvcEvent $event the bootstrap event
* @return void
*/
public function onBootstrap(MvcEvent $event)
{
$application = $event->getApplication();
$services = $application->getServiceManager();
$events = $services->get('queue')->getEventManager();
// when a change is committed, fetch the change object
// we do this early (high-priority), so that later event
// handlers can use this data and implement special rules
$events->attach(
'task.commit',
function ($event) use ($services) {
$patterns = array
(
array
(
"type" => "add",
"pattern" => "/.*TEST.*DISABLED.*/",
"user" => "ChuckNorris",
"message" => "Why you keep disabling tests?",
),
array
(
"type" => "delete",
"pattern" => "/Norris/",
"user" => "ChuckNorris",
"message" => "How you dare remove my name from the code",
),
array
(
"type" => "add",
"pattern" => "/^[+]\s*(\S+\s*)+[{]\s*$/",
"user" => "ChuckNorris",
"message" => "You know I hate when you put the opening brace at the end of the line",
),
array
(
"type" => "add",
"pattern" => "/\t/",
"user" => "ChuckNorris",
"message" => "Not all white spaces are equally white. Get rid of these tabs.",
),
array
(
"type" => "add",
"pattern" => "/.{120}/",
"user" => "ChuckNorris",
"message" => "Wow - this line is longer than my legs.",
),
array
(
"type" => "add",
"pattern" => "/TODO:/",
"user" => "ChuckNorris",
"message" => "Why do not you just stop being lazy and do it.",
),
array
(
"type" => "add",
"pattern" => "/temporary/",
"user" => "ChuckNorris",
"message" => "Are you serious, once submitted it is forever in perforce.",
),
array
(
"type" => "add",
"pattern" => "/#if 0/",
"user" => "ChuckNorris",
"message" => "Do not comment out in the code - this is SCM.",
),
array
(
"type" => "add",
"pattern" => "/\s$/",
"user" => "ChuckNorris",
"message" => "Are you blind. Do not you see the white space at the end of the line.",
),
);
$p4Admin = $services->get('p4_admin');
$p4 = $services->get('p4');
$id = $event->getParam('id');
try {
// fetch change record, ignore pending changes.
$change = Change::fetch($id, $p4Admin);
$event->setParam('change', $change);
if ($change->getStatus() !== 'submitted') {
return;
}
$changeData = $change->get();
$files = $changeData['Files'];
file_put_contents("/tmp/mylog/log.txt", time() . "\xA");
//file_put_contents("/tmp/mylog/log.txt", print_r($changeData, true) . "\xA", FILE_APPEND);
$differ = new Diff($p4);
$options = array(
$differ::UTF8_CONVERT => true
);
foreach ($files as $file)
{
$sp = preg_split('/#/', $file);
$name = $sp[0];
$prev = $name . '#' . strval(intval($sp[1]) - 1);
$ffile = File::fetch($file, $p4);
$fprev = File::fetch($prev, $p4);
$diff = $differ->diff($ffile, $fprev, $options);
//file_put_contents("/tmp/mylog/log.txt", "diff: " . print_r($diff, true) . "\xA", FILE_APPEND);
//$review = Review::createFromChange($changeId, $p4Admin);
// add participant and save
//$review->addParticipant($user->getId())->save();
foreach ($diff['lines'] as $line)
{
//file_put_contents("/tmp/mylog/log.txt", "every line: " . print_r($line, true) . "\xA", FILE_APPEND);
foreach ($patterns as $pattern)
{
if ($line['type'] != $pattern['type'])
continue;
//file_put_contents("/tmp/mylog/log.txt", "pattern type line: " . print_r($line, true) . "\xA", FILE_APPEND);
if (!preg_match($pattern['pattern'], $line['value']))
continue;
$message = $pattern['message'];
// file_put_contents("/tmp/mylog/log.txt", "commented line: " . $line['value'] . ", pattern: " . $pattern['pattern'] . "\xA", FILE_APPEND);
$comment = new Comment($p4Admin);
$comment->set(array(
"user" => $pattern['user'],
"topic" => "changes/" . $change->getId(),
"context" => array (
//"content" => $line['value'],
"file" => $name,
"leftLine" => $line['leftLine'],
"rightLine" => $line['rightLine'],
),
"body" => $message,
))
->save();
}
//file_put_contents("/tmp/mylog/log.txt", "pattern type line: " . print_r($line, true), FILE_APPEND);
}
}
} catch (\Exception $e) {
$services->get('logger')->err($e);
}
},
100
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
} | # | Change | User | Description | Committed | |
|---|---|---|---|---|---|
| #1 | 8441 | Matt Attaway |
Add George Georgiev's ChcuckNorris module from the Swarm hackathon. See the readme for installation instructions. |