6 * This script stand for receiving a HTTP Post from a hook in a
7 * svn script. Arguments are passed and new entries in DB are
10 * Copyright Daniel A. Perez <danielperez.arg@gmail.com>
14 * This script takes some POST variables.It makes a check and it
15 * store in DB the commit info attached to the tracker or task.
19 require_once dirname(__FILE__)."/../../env.inc.php";
20 require_once $gfwww.'include/pre.php';
21 require_once $gfconfig.'plugins/svntracker/config.php';
23 if ($svn_tracker_debug) {
24 $file = fopen($svn_tracker_debug_file,"a+");
28 * Getting POST variables
29 * UserName, Repository, Path, FileName, PrevVersion, ActualVersion, Log,
30 * ArtifactNumbers and TaskNumbers
33 $SubmittedVars = array();
34 $SubmittedVars = unserialize(str_replace('\\\\', '\\', str_replace('\"','"',$_POST['data'])));
37 foreach ($SubmittedVars as $SubmittedVar) {
38 $Configs[$i] = array();
39 $Configs[$i]['UserName'] = $SubmittedVar['UserName'];
40 //$Configs[$i]['UserName'] = 'def_admin'; use this to make tests, just replace with a gforge user
41 $Configs[$i]['Repository'] = $SubmittedVar['Repository'];
42 $Configs[$i]['FileName'] = $SubmittedVar['FileName'];
43 $Configs[$i]['PrevVersion'] = $SubmittedVar['PrevVersion'];
44 $Configs[$i]['ActualVersion'] = $SubmittedVar['ActualVersion'];
45 $Configs[$i]['ArtifactNumbers'] = $SubmittedVar['ArtifactNumbers'];
46 $Configs[$i]['TaskNumbers'] = $SubmittedVar['TaskNumbers'];
47 $Configs[$i]['Log'] = $SubmittedVar['Log'];
48 $Configs[$i]['SvnDate'] = $SubmittedVar['SvnDate'];
50 if($svn_tracker_debug) {
51 echo "Variables received by newcommit.php:\n";
52 print_r($Configs[$i]);
59 * Checks if the commit it's possible and parse arguments
60 * Checks if repository, group and user_name are right.
61 * It extract group from svnroot, and check if the plugin
62 * is availabe. It checks if the user exists.
64 * @param array $Config Config
66 * @return array Returns 'check'=true if check passed, group, group_id
68 function parseConfig(&$Config)
70 global $sys_svnroot_path, $svn_tracker_debug,$file;
73 $Result['check'] = true;
74 $Repository = $Config['Repository'];
75 $UserName = $Config['UserName'];
77 if($sys_svnroot_path[strlen($sys_svnroot_path)-1]!='/') {
78 $sys_svnroot_path.='/';
80 $repo_root = substr($Repository,0,strrpos($Repository,"/") + 1); //we get the directory of the repository root (with trailing slash)
82 if(fileinode($sys_svnroot_path) == fileinode($repo_root)) { // since the $sys_svnroot_path is usually $sys_svnroot, and that one is a symlink, we check that the inode is the same for both
83 $GroupName = substr($Repository, strrpos($Repository,"/") + 1);
84 $Config['FileName'] = substr($Config['FileName'],strlen($Repository)); //get only the filename relative to the repo
86 $GroupName = $Repository;
87 $Config['FileName'] = $Config['FileName'];
90 if($svn_tracker_debug) {
91 echo "GroupName = ".$GroupName."\n";
92 echo "SVNRootPath = ".$sys_svnroot_path."\n";
95 if ($svn_tracker_debug) {
96 fwrite($file,$GroupName."\n");
99 $Result['group'] = group_get_object_by_name($GroupName);
100 $Result['user'] = user_get_object_by_name($UserName);
101 if (!$Result['group'] || !is_object($Result['group']) ||
102 $Result['group']->isError() || !$Result['group']->isActive()) {
103 $Result['check'] = false;
104 $Result['error'] = 'Group Not Found';
106 $Result['group_id'] = $Result['group']->getID();
107 if (!$Result['group']->usesPlugin('svntracker')) {
108 $Result['check'] = false;
109 $Result['error'] = 'Plugin not enabled for this Group';
113 if (!$Result['user'] || !is_object($Result['user']) ||
114 $Result['user']->isError() || !$Result['user']->isActive()) {
115 $Result['check'] = false;
116 $Result['error'] = 'Invalid User';
122 * Add a entry in the DataBase for a Artifact associated to a commit
124 * @param array $Config Config
125 * @param string $GroupId The GroupId to insert it into
126 * @param string $Num The artifact_id
128 * @return array Returns 'check'=true if check passed, group, group_id
130 function addArtifactLog($Config, $GroupId, $Num)
132 global $file, $svn_tracker_debug;
134 $Result = db_query_params ('SELECT * FROM artifact,artifact_group_list WHERE
135 artifact.group_artifact_id=artifact_group_list.group_artifact_id
136 AND artifact_group_list.group_id=$1 AND artifact.artifact_id=$2',
139 $Rows = db_numrows($Result);
140 if ($svn_tracker_debug) {
141 fwrite($file,"query : " . $Query ."\n");
142 fwrite($file,"rows : " . $Rows ."\n");
145 $return['Error'] = "Artifact ".$Num." Not Found.";
150 $DBRes = db_query_params ('INSERT INTO plugin_svntracker_data_artifact
151 (kind, group_artifact_id) VALUES
154 $HolderID= db_insertid($DBRes,'plugin_svntracker_data_artifact','id');
155 if (!$DBRes || !$HolderID) {
156 $return['Error']='Problems with Artifact $Num: '.db_error($DBRes);
159 $DBRes = db_query_params ('INSERT INTO plugin_svntracker_data_master (holder_id, svn_date, log_text, file, prev_version, actual_version, author) VALUES ($1, $2, $3, $4, $5, $6, $7)',
164 $Config['PrevVersion'],
165 $Config['ActualVersion'],
166 $Config['UserName'])) ;
168 $return['Error']="Problems with Artifact $Num: ".db_error($DBRes)."\n";
177 $return['Error'] .= "Unknown problem adding Tracker: $Num.";
183 * Add a entry in the DataBase for a Tracker associated to a commit
185 * @param array $Config Config
186 * @param string $GroupId The GroupId to insert it into
187 * @param string $Num The tracker_id
189 * @return array Returns 'check'=true if check passed, group, group_id
191 function addTaskLog($Config, $GroupId, $Num)
194 $Result = db_query_params ('SELECT * FROM project_task,project_group_list
195 WHERE project_task.group_project_id=project_group_list.group_project_id
196 AND project_task.project_task_id=$1
197 AND project_group_list.group_id=$2',
200 $Rows = db_numrows($Result);
202 $return['Error'] .= "Task:$Num Not Found.";
206 $DBRes = db_query_params ('INSERT INTO plugin_svntracker_data_artifact
207 (kind, project_task_id) VALUES
210 $HolderID= db_insertid($DBRes,'plugin_svntracker_data_artifact','id');
211 if (!$DBRes || !$HolderID) {
212 $return['Error']='Problems with Task $Num: '.db_error($DBRes);
215 $DBRes = db_query_params ('INSERT INTO plugin_svntracker_data_master
216 (holder_id, svn_date, log_text, file, prev_version,
217 actual_version, author)
218 VALUES ($1, $2, $3, $4, $5, $6, $7)',
225 $Config['PrevVersion'],
226 $Config['ActualVersion'],
227 $Config['UserName'])) ;
236 $return['Error'] .= "Unknown problem adding Task:$Num.";
241 foreach ($Configs as $Config) {
242 $Result = parseConfig($Config);
243 if ($Result['check'] == false) {
244 exit_error('Check_error', $Result['error']);
247 if (!is_null($Config['ArtifactNumbers'])) {
248 foreach ($Config['ArtifactNumbers'] as $Num)
250 $AddResult = addArtifactLog($Config, $Result['group_id'], $Num);
251 if (isset($AddResult['Error'])) {
252 exit_error('Adding ArtifactNumber',$AddResult['Error']);
257 if (!is_null($Config['TaskNumbers'])) {
258 foreach ($Config['TaskNumbers'] as $Num)
260 $AddResult = addTaskLog($Config, $Result['group_id'], $Num);
261 if (isset($AddResult['Error'])) {
262 exit_error('Adding TaskNumber',$AddResult['Error']);
274 // c-file-style: "bsd"