File: newcommit.php

package info (click to toggle)
gforge 4.5.14-22etch13
  • links: PTS
  • area: main
  • in suites: etch
  • size: 13,004 kB
  • ctags: 11,918
  • sloc: php: 36,047; sql: 29,050; sh: 10,538; perl: 6,496; xml: 3,810; makefile: 341; python: 263; ansic: 256
file content (252 lines) | stat: -rw-r--r-- 7,545 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/**
  *
  * cvstracker plugin
  *
  * This script stand for receiving a HTTP Post from a hook in a
  *  CVS script. Arguments are passed and new entries in DB are
  *  generated.
  *
  * Copyright Francisco Gimeno <kikov@kikov.org>
  *
  * @version $Id: newcommit.php 5331 2006-02-17 14:30:41Z marcelo $
  */
/**
 * 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('squal_pre.php');
require_once('plugins/cvstracker/config.php');

/**
 * Getting POST variables
 * UserName, Repository, Path, FileName, PrevVersion, ActualVersion, Log,
 * ArtifactNumbers and TaskNumbers
 */
$Config = array();
$SubmittedVars = array();
$SubmittedVars = unserialize(str_replace('\"','"',$_POST['data']));
$i = 0;
foreach ($SubmittedVars as $SubmittedVar) {
        $Configs[$i]['UserName']        = $SubmittedVar['UserName'];
        $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]['CvsDate']         = $SubmittedVar['CvsDate'];
        $i++;
        if($cvs_tracker_debug) {
                echo "Variables received by newcommit.php:\n";
                print_r($Configs[$i]);
        }
}

/**
 * Checks if the commit it's possible and parse arguments
 * Checks if repository, group and user_name are right.
 *  It extract group from cvsroot, 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)
{
	global $sys_cvsroot_path, $cvs_tracker_debug;
	
	$Result = array();
	$Result['check'] = true;
	$Repository = $Config['Repository'];
	$UserName = $Config['UserName'];

	// add a trailing / if needed
	if($sys_cvsroot_path[strlen($sys_cvsroot_path)-1]!='/') {
		$sys_cvsroot_path.='/';
	}
	if(strncmp($Repository,$sys_cvsroot_path, strlen($sys_cvsroot_path)) == 0) {
		$GroupName = substr($Repository, strlen($sys_cvsroot_path));
	} else {
		$GroupName = $Repository;
	}
	if($cvs_tracker_debug) {
		echo "GroupName = ".$GroupName."\n";
		echo "CVSRootPath = ".$sys_cvsroot_path."\n";
	}

	
	$Result['user']     = user_get_object_by_name($UserName);
	if (!$Result['user'] || !is_object($Result['user']) ||
		$Result['user']->isError() || !$Result['user']->isActive()) {
		$Result['check'] = false;
		$Result['error'] = 'Invalid User';
		return $Result;
	}
	session_set_new($Result['user']->getID());

	$Result['group']    = group_get_object_by_name($GroupName);
	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('cvstracker')) {
			$Result['check'] = false;
			$Result['error'] = 'Plugin not enabled for this Group';
		}
	}

	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();
	$Query = "SELECT * from artifact,artifact_group_list WHERE ".
		"artifact.group_artifact_id=artifact_group_list.group_artifact_id ".
		"AND artifact_group_list.group_id=".
		"'".$GroupId."' AND artifact.artifact_id='".$Num."'";
	$Result = db_query($Query);
	$Rows = db_numrows($Result);
	if ($Rows == 0) {
		$return['Error'] .= "Artifact ".$Num." Not Found.";
	}

	if ($Rows == 1) {
		db_begin();
		$Query = "INSERT INTO plugin_cvstracker_data_artifact ".
		"(kind, group_artifact_id) VALUES ".
		"('0', '".$Num."')";
		$DBRes = db_query($Query);
		$HolderID= db_insertid($DBRes,'plugin_cvstracker_data_artifact','id');
		if (!$DBRes || !$HolderID) {
			$return['Error']='Problems with Artifact $Num: '.db_error($DBRes);
			db_rollback();
		} else {
			$Query = "INSERT INTO plugin_cvstracker_data_master ".
				"(holder_id, cvs_date, log_text, file, prev_version, ".
				"actual_version, author)".
				" VALUES ('".$HolderID."','".$Config['CvsDate']."','".$Config['Log'].
				"','".$Config['FileName']."','".
				$Config['PrevVersion']."','".
				$Config['ActualVersion']."','".$Config['UserName']."')";
			$DBRes = db_query($Query);
			if(!$DBRes) {
				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();
	$Query = "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='".$Num."' AND ".
		" project_group_list.group_id='".$GroupId."'";
	var_dump($Query);
	$Result = db_query($Query);
	$Rows = db_numrows($Result);
	if ($Rows == 0) {
		$return['Error'] .= "Task:$Num Not Found.";
	}
	if ($Rows == 1) {
		db_begin();
		$Query = "INSERT INTO plugin_cvstracker_data_artifact ".
		"(kind, project_task_id) VALUES ".
		"('1', '".$Num."')";
		$DBRes = db_query($Query);
		$HolderID= db_insertid($DBRes,'plugin_cvstracker_data_artifact','id');
		if (!$DBRes || !$HolderID) {
			$return['Error']='Problems with Task $Num: '.db_error($DBRes);
			db_rollback();
		} else {
			$Query = "INSERT INTO plugin_cvstracker_data_master ".
				"(holder_id, cvs_date, log_text, file, prev_version, ".
				"actual_version, author)".
				" VALUES ('".$HolderID."','".$Config['CvsDate']."','".$Config['Log'].
				"','".$Config['FileName'].
				"','".$Config['PrevVersion']."','".
				$Config['ActualVersion']."','".$Config['UserName']."')";
				$DBRes = db_query($Query);
			if(!$DBRes) {
				db_rollback();
			} else {
				db_commit();
			}
		}
	}
	if ($Rows > 1) {
		$return['Error'] .= "Unknown problem adding Task:$Num.";
	}
	return $return;
}

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:

?>