/**
*
* This is the script called by svn. It takes some params, and prepare some
- * HTTP POSTs to svntracker/newcommit.php.
+ * HTTP POSTs to scmhook/www/newcommitgit.php.
*
*/
$files = array();
-if (count($argv) != 3) {
+if (count($argv) != 5) {
echo <<<USAGE
Usage: $0 <repository> <revision>
This program should be automatically called by SVN
exit;
}
-$repository = $argv[1];
-$revision = $argv[2];
-$git_tracker_debug = 0;
-
-$UserName = trim(`svnlook author -r $revision $repository`); //username of author
-$date = trim(`svnlook date -r $revision $repository`); //date
-$log = trim(`svnlook log -r $revision $repository`); // the log
-$changed = trim(`svnlook changed -r $revision $repository | sed 's/[A-Z]* //'`); // the filenames
+$oldrev = $argv[1];
+$newrev = $argv[2];
+$refname = $argv[3];
+$hook_path=$argv[4];
+//$repository = $argv[1];
+//$revision = $argv[2];
+$git_tracker_debug = 1;
+//cd /var/opt/fusionforge/chroot/scmrepos/git/projet02/projet02.git
+$repos_path=forge_get_config('repos_path','scmgit');
+//var_dump($repos_path);
+//var_dump($refname);
+//echo "hook path ::::: ";
+//var_dump($hook_path);
+chdir($hook_path.'/..');
+//git show --pretty=short bed85a02ce30d35e44bd2f6b1020aabe53b16ce5
+$UserName = trim(`git log -n 1 --format=%an $newrev`);
+
+//$UserName = trim(`svnlook author -r $revision $repository`); //username of author
+//$date = trim(`svnlook date -r $revision $repository`); //date
+$date = trim(`git log -n 1 --format=%ai $newrev`); //date
+$log = trim(`git log -n 1 --format=%s $newrev`); // the log
+//echo $log;
+//var_dump($log);
+$changed = trim(`git log -n 1 --format=%b --name-only -p $newrev`); // the filenames
if (isset($git_tracker_debug) && $git_tracker_debug == 1) {
+$file=fopen("/tmp/debug.000","r+");
fwrite($file,"Vars filled:\n");
+ //fwrite($file,"arg : " . var_dump($argv) . " \n");
+ fwrite($file,"rev : " . $newrev . " \n");
fwrite($file,"username : " . $UserName . " \n");
fwrite($file,"date : " . $date . " \n");
fwrite($file,"log : " . $log . " \n");
fwrite($file,"changed : " . $changed . " \n");
+fclose($file);
}
$changed = explode("\n", $changed);
-
foreach ($changed as $onefile) {
//we must see when it was last changed, and that's previous revision
$exit=0;
- $actrev = $revision - 1;
- if ($revision==0) {
+ $actrev = $newrev - 1;
+ if ($newrev==0) {
$exit = 1;
$prev = 1;
}
while ( (!$exit) && ($actrev != 0 ) ) {
- $changed2 = trim(`svnlook changed -r $actrev $repository | sed 's/[A-Z]* //'`);
+// $changed2 = trim(`svnlook changed -r $actrev $repository | sed 's/[A-Z]* //'`);
+ $changed2 = trim(`git log -n 1 --format=%b --name-only -p $newrev`);
$changed2 = explode("\n", $changed2);
if ( in_array($onefile,$changed2) ) {
$prev = $actrev;
}
$files[] = array(
- 'name' => $repository . "/" . $onefile,
- 'previous' => $prev,
- 'actual' => $revision
+ //'name' => $repository . "/" . $onefile,
+ 'name' => $hook_path . "/" . $onefile,
+ //'previous' => $prev,
+ 'previous' => $oldrev,
+ //'actual' => $revision
+ 'actual' => $newrev
);
}
// Our POSTer in Fusionforge
$snoopy = new Snoopy;
-$SubmitUrl = util_make_url('/plugins/scmhook/committracker/newcommit.php');
+$SubmitUrl = util_make_url('/plugins/scmhook/committracker/newcommitgit.php');
$tasks_involved= getInvolvedTasks($log);
$artifacts_involved= getInvolvedArtifacts($log);
foreach ( $files as $onefile )
{
$SubmitVars[$i]["UserName"] = $UserName;
- $SubmitVars[$i]["Repository"] = $repository;
+ $SubmitVars[$i]["Repository"] = $hook_path;
$SubmitVars[$i]["FileName"] = $onefile['name'];
$SubmitVars[$i]["PrevVersion"] = $onefile['previous'];
$SubmitVars[$i]["ActualVersion"] = $onefile['actual'];
$SubmitVars[$i]["Log"] = $log;
$SubmitVars[$i]["TaskNumbers"] = getInvolvedTasks($log);
$SubmitVars[$i]["ArtifactNumbers"] = getInvolvedArtifacts($log);
- $SubmitVars[$i]["SvnDate"] = time();
+ $SubmitVars[$i]["GitDate"] = time();
$i++;
}
-
$vars['data'] = urlencode(serialize($SubmitVars));
+//var_dump($SubmitUrl);
+//var_dump($vars['data']);
$snoopy->submit($SubmitUrl, $vars);
?>
--- /dev/null
+<?php
+/**
+ * scmhook plugin: git committracker hook
+ *
+ * Copyright Daniel A. Perez <danielperez.arg@gmail.com>
+ * Copyright 2013, Franck Villaume - TrivialDev
+ * Copyright 2013, Benoit Debaenst - TrivialDev
+ *
+ * This file is part of FusionForge. FusionForge is free software;
+ * you can redistribute it and/or modify it under the terms of the
+ * GNU General Public License as published by the Free Software
+ * Foundation; either version 2 of the Licence, or (at your option)
+ * any later version.
+ *
+ * FusionForge 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with FusionForge; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+/**
+ * This script takes some POST variables. It makes a check and it
+ * store in DB the commit info attached to the tracker or task.
+ *
+ */
+
+require_once dirname(__FILE__)."/../../../env.inc.php";
+require_once $gfcommon.'include/pre.php';
+
+/**
+ * Getting POST variables
+ * UserName, Repository, Path, FileName, PrevVersion, ActualVersion, Log,
+ * ArtifactNumbers and TaskNumbers
+ */
+
+$Config = array();
+$SubmittedVars = array();
+$SubmittedVars = unserialize(urldecode($_POST['data']));
+
+$data = urldecode($_POST['data']);
+$i = 0;
+if(is_array($SubmittedVars)) {
+ foreach ($SubmittedVars as $SubmittedVar) {
+ $Configs[$i] = array();
+ $Configs[$i]['UserName'] = $SubmittedVar['UserName'];
+ //$Configs[$i]['UserName'] = 'def_admin'; use this to make tests, just replace with a gforge user
+ $Configs[$i]['Repository'] = $SubmittedVar['Repository'];
+ $Configs[$i]['FileName'] = $SubmittedVar['FileName'];
+ $Configs[$i]['PrevVersion'] = $SubmittedVar['PrevVersion'];
+ $Configs[$i]['ActualVersion'] = $SubmittedVar['ActualVersion'];
+ $Configs[$i]['ArtifactNumbers'] = $SubmittedVar['ArtifactNumbers'];
+ $Configs[$i]['TaskNumbers'] = $SubmittedVar['TaskNumbers'];
+ $Configs[$i]['Log'] = $SubmittedVar['Log'];
+ $Configs[$i]['GitDate'] = $SubmittedVar['GitDate'];
+ $i++;
+ }
+}
+var_dump($SubmittedVar);
+echo "inside newcommit ";
+/**
+ * Checks if the commit it's possible and parse arguments
+ * Checks if repository, group and user_name are right.
+ * It extract group from gitroot, and check if the plugin
+ * is availabe. It checks if the user exists.
+ *
+ * @param array $Config Config
+ *
+ * @return array Returns 'check'=true if check passed, group, group_id
+ */
+function parseConfig(&$Config)
+{
+ $repos_path = forge_get_config ('repos_path', 'scmgit') ;
+
+ $Result = array();
+ $Result['check'] = true;
+ $Repository = $Config['Repository'];
+ $UserName = $Config['UserName'];
+
+ if($repos_path[strlen($repos_path)-1]!='/') {
+ $repos_path.='/';
+ }
+ $repo_root = substr($Repository,0,strrpos($Repository,"/") + 1); //we get the directory of the repository root (with trailing slash)
+
+ if(fileinode($repos_path) == fileinode($repo_root)) { // since the $repos_path is usually $sys_gitroot, and that one is a symlink, we check that the inode is the same for both
+ $GroupName = substr($Repository, strrpos($Repository,"/") + 1);
+ $Config['FileName'] = substr($Config['FileName'],strlen($Repository)); //get only the filename relative to the repo
+ } else {
+ $GroupName = $Repository;
+ $Config['FileName'] = $Config['FileName'];
+ }
+
+ $Result['group'] = group_get_object_by_name($GroupName);
+ $Result['user'] = user_get_object_by_name($UserName);
+ if (!$Result['group'] || !is_object($Result['group']) ||
+ $Result['group']->isError() || !$Result['group']->isActive()) {
+ $Result['check'] = false;
+ $Result['error'] = 'Group Not Found';
+ } else {
+ $Result['group_id'] = $Result['group']->getID();
+ if (!$Result['group']->usesPlugin('scmhook')) {
+ $Result['check'] = false;
+ $Result['error'] = 'Plugin not enabled for this Group';
+ }
+ }
+
+ if (!$Result['user'] || !is_object($Result['user']) ||
+ $Result['user']->isError() || !$Result['user']->isActive()) {
+ $Result['check'] = false;
+ $Result['error'] = 'Invalid User';
+ }
+ return $Result;
+}
+
+/**
+ * Add a entry in the DataBase for a Artifact associated to a commit
+ *
+ * @param array $Config Config
+ * @param string $GroupId The GroupId to insert it into
+ * @param string $Num The artifact_id
+ *
+ * @return array Returns 'check'=true if check passed, group, group_id
+ */
+function addArtifactLog($Config, $GroupId, $Num)
+{
+ $return = array();
+ $Result = db_query_params ('SELECT * FROM artifact,artifact_group_list WHERE
+artifact.group_artifact_id=artifact_group_list.group_artifact_id
+AND artifact_group_list.group_id=$1 AND artifact.artifact_id=$2',
+ array ($GroupId,
+ $Num));
+ $Rows = db_numrows($Result);
+ if ($Rows == 0) {
+ $return['Error'] = "Artifact ".$Num." Not Found.";
+ }
+
+ if ($Rows == 1) {
+ db_begin();
+ $DBRes = db_query_params ('INSERT INTO plugin_scmhook_scmgit_committracker_data_artifact
+(kind, group_artifact_id) VALUES
+(0, $1)',
+ array ($Num));
+ $HolderID= db_insertid($DBRes,'plugin_scmhook_scmgit_committracker_data_artifact','id');
+ if (!$DBRes || !$HolderID) {
+ $return['Error']='Problems with Artifact $Num: '.db_error();
+ db_rollback();
+ } else {
+ $DBRes = db_query_params ('INSERT INTO plugin_scmhook_scmgit_committracker_data_master (holder_id, git_date, log_text, file, prev_version, actual_version, author) VALUES ($1, $2, $3, $4, $5, $6, $7)',
+ array ($HolderID,
+ $Config['GitDate'],
+ $Config['Log'],
+ $Config['FileName'],
+ $Config['PrevVersion'],
+ $Config['ActualVersion'],
+ $Config['UserName'])) ;
+ if(!$DBRes) {
+ $return['Error']="Problems with Artifact $Num: ".db_error()."\n";
+ db_rollback();
+ } else {
+ db_commit();
+ }
+ }
+
+ }
+ if ($Rows > 1) {
+ $return['Error'] .= "Unknown problem adding Tracker: $Num.";
+ }
+ return $return;
+}
+
+/**
+ * Add a entry in the DataBase for a Tracker associated to a commit
+ *
+ * @param array $Config Config
+ * @param string $GroupId The GroupId to insert it into
+ * @param string $Num The tracker_id
+ *
+ * @return array Returns 'check'=true if check passed, group, group_id
+ */
+function addTaskLog($Config, $GroupId, $Num)
+{
+ $return = array();
+ $Result = db_query_params ('SELECT * FROM project_task,project_group_list
+WHERE project_task.group_project_id=project_group_list.group_project_id
+AND project_task.project_task_id=$1
+AND project_group_list.group_id=$2',
+ array ($Num,
+ $GroupId));
+ $Rows = db_numrows($Result);
+ if ($Rows == 0) {
+ $return['Error'] .= "Task:$Num Not Found.";
+ }
+ if ($Rows == 1) {
+ db_begin();
+ $DBRes = db_query_params ('INSERT INTO plugin_scmhook_scmgit_committracker_data_artifact
+ (kind, project_task_id) VALUES
+ (1, $1)',
+ array ($Num));
+ $HolderID= db_insertid($DBRes,'plugin_scmhook_scmgit_committracker_data_artifact','id');
+ if (!$DBRes || !$HolderID) {
+ $return['Error']='Problems with Task $Num: '.db_error();
+ db_rollback();
+ } else {
+ $DBRes = db_query_params ('INSERT INTO plugin_scmhook_scmgit_committracker_data_master
+ (holder_id, git_date, log_text, file, prev_version,
+ actual_version, author)
+ VALUES ($1, $2, $3, $4, $5, $6, $7)',
+ array ($HolderID,
+ $Config['GitDate'],
+ $Config['Log'],
+ $Config['FileName'],
+ $Config['PrevVersion'],
+ $Config['ActualVersion'],
+ $Config['UserName'])) ;
+ if(!$DBRes) {
+ db_rollback();
+ } else {
+ db_commit();
+ }
+ }
+ }
+ if ($Rows > 1) {
+ $return['Error'] .= "Unknown problem adding Task:$Num.";
+ }
+ return $return;
+}
+
+if (isset($Configs) && is_array($Configs)) {
+ foreach ($Configs as $Config) {
+ $Result = parseConfig($Config);
+ if ($Result['check'] == false) {
+ exit_error('Check_error', $Result['error']);
+ }
+
+ if (!is_null($Config['ArtifactNumbers'])) {
+ foreach ($Config['ArtifactNumbers'] as $Num)
+ {
+ $AddResult = addArtifactLog($Config, $Result['group_id'], $Num);
+ if (isset($AddResult['Error'])) {
+ exit_error('Adding ArtifactNumber',$AddResult['Error']);
+ }
+ }
+ }
+
+ if (!is_null($Config['TaskNumbers'])) {
+ foreach ($Config['TaskNumbers'] as $Num)
+ {
+ $AddResult = addTaskLog($Config, $Result['group_id'], $Num);
+ if (isset($AddResult['Error'])) {
+ exit_error('Adding TaskNumber',$AddResult['Error']);
+ }
+ }
+ }
+ }
+}
+
+exit (0);
+
+// Local Variables:
+// mode: php
+// c-file-style: "bsd"
+// End: