websiteWebsite
codingteam CodingTeam
A free forge, lightweight and extensible.

 

Browse the code

Revision log Information on the revision
Revision: 332 (differences)
Author: xbright
Log message: * Welcome 2010 :)
Change revision:
#!/usr/bin/php
<?php
#    This file is a part of CodingTeam. See <http://www.codingteam.net>.
#    Copyright (C) 2007-2010 CodingTeam (See AUTHORS and THANKS for details)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, version 3 only.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
// Handle args
if (empty($argv[1]) || empty($argv[2]))
    exit('Error.'); 
 
// Find were we are
$base = dirname(__FILE__);
$whereis = '/scripts/miscellaneous';
$length = -mb_strlen($whereis);
 
if (mb_substr($base, $length, mb_strlen($base)) == $whereis)
{
    // Set the CodingTeam basedir
    $basedir = mb_substr($base, 0, $length);
    define('CT_BASEDIR', $basedir);
    require($basedir.'/inc/globalFunctions.php');
 
    // Treatment for args
    $var = explode('/', $argv[1]);
    $project = $var[ count($var) - 1 ];
    $rev = $argv[2];
    $prev_rev = $rev - 1;
 
    // Check if the configuration file exist
    if (!file_exists($basedir.'/inc/codingteam.cfg'))
        die ('There are no configuration file. CodingTeam cannot start.');
 
    // Check if the configuration file is well formed
    $xml = new DomDocument();
    $xml->load($basedir.'/inc/codingteam.cfg');
    if (!$xml->schemaValidate($basedir.'/inc/codingteam-conf.xsd'))
        exit('POST-COMMIT HOOK FAILED');
 
    // Import configuration
    $xml = simplexml_load_file($basedir.'/inc/codingteam.cfg');
    $db_type = $xml->db->type;
    $db_hostname = $xml->db->hostname;
    $db_database = $xml->db->database;
    $db_username = $xml->db->username;
    $db_password = $xml->db->password;
 
    // Database connection
    require($basedir.'/inc/classes/db.php');
    $ct_db = new Database($db_type, $db_hostname, $db_database,
                          $db_username, $db_password);
 
    // Project identifier
    $prj_class = getClass('projects.projects', $ct_db);
    $prj_obj = $prj_class->load($project, 'dbname');
    if ($prj_obj)
        $prj_id = $prj_class->getId();
    else
        exit('POST-COMMIT HOOK FAILED');
 
    // Construct URL
    $dbname = htmlspecialchars($prj_class->getDbname());
    $url = 'project/'.$dbname.'/browse/diff/'.$prev_rev.'/'.$rev;
 
    // Get commits information
    $author = exec(escapeshellcmd('svnlook author '.$argv[1].' -r'.$rev));
    $date = exec(escapeshellcmd('svnlook date '.$argv[1].' -r'.$rev));
    $datetime = mb_substr($date, 0, 19);
 
    $cmd = escapeshellcmd('svnlook log '.$argv[1].' -r'.$rev);
    ob_start();
    passthru($cmd);
    $log = ob_get_clean();
 
    // Get user identifier
    $user = getUser($author, $ct_db, 'nickname');
    if ($user)
    {
        $user_id = $user->getId();
 
        // Advanced hook
        $lines = explode("\n", $log);
        foreach ($lines as $line)
            if (eregi('.*fixe[d|s] #([0-9]+).*', $line, $ret))
                if (isset($ret[1]))
                {
                    $bugid = $ret[1];
 
                    // Get the bug
                    $bug_class = getClass('project.bugs', $ct_db);
                    $bug_obj = $bug_class->load($bugid);
                    if ($bug_obj && $bug_class->getProjectid() == $prj_id)
                    {
                        // Add a comment to the bug
                        $title = 'Fixed by r'.$rev;
                        $text = 'This bug was fixed by the commit '.
                        '<a href="'.$url.'">r'.$rev.'</a>. This is an '.
                        'automatic fix.';
                        $date = date('Y-m-d H:i:s');
 
                        $lastid = $bug_class->addAnswer(array(
                                               'bugid'             => $bugid,
                                               'name'              => $title,
                                               'text'              => $text,
                                               'allow_nl2br'       => TRUE,
                                               'unregistered_nick' => '',
                                               'registered_id'     => $user_id,
                                               'author_ip'         => '',
                                               'datetime'          => $date));
 
                        // Update bug's log
                        $values = array();
                        array_push($values, array('bugid' => $bugid));
                        array_push($values, array('projectid' => $prj_id));
                        array_push($values, array('unregistered_nick' => ''));
                        array_push($values, array('registered_id' => $user_id));
                        array_push($values, array('author_ip' => ''));
                        array_push($values, array('datetime' => $date));
 
                        array_push($values, array('milestone' => '!nochange!'));
                        array_push($values, array('version' => '!nochange!'));
                        array_push($values, array('priority' => '!nochange!'));
                        array_push($values, array('type' => '!nochange!'));
                        array_push($values, array('status' => 'resolved'));
                        array_push($values, array('assignedid' => 0));
                        array_push($values, array('attachement' => 0));
 
                        array_push($values, array('answer' => $lastid));
 
                        foreach ($values as $key)
                            foreach ($key as $key => $value)
                                $logentry[$key] = $value;
 
                        $bug_class->addLog($logentry);
 
                        $bu = '/project/'.$dbname;
                        deleteCacheVersion($bu.'/bugs/show/'.$bugid);
                        deleteCacheVersion($bu.'/timeline', TRUE);
 
                        // Close the bug (with status: resolved)
                        $bug_class->setStatus('resolved');
                    }
                }
    }
 
    $cmt_class = getClass('projects.commits', $ct_db);
    $cmt_class->push($prj_id, $rev, $prev_rev, $log, $author, $datetime);
 
    // Delete cache for subversion browser
    deleteCacheVersion('/project/'.$project.'/browse', TRUE);
    deleteCacheVersion('/project/'.$project.'/timeline', TRUE);
    deleteCacheVersion('/index');
}
?>