3 * scmhook plugin: git committracker hook
5 * Copyright Daniel A. Perez <danielperez.arg@gmail.com>
6 * Copyright 2013, Franck Villaume - TrivialDev
7 * Copyright 2014, Benoit Debaenst - TrivialDev
9 * This file is part of FusionForge. FusionForge is free software;
10 * you can redistribute it and/or modify it under the terms of the
11 * GNU General Public License as published by the Free Software
12 * Foundation; either version 2 of the Licence, or (at your option)
15 * FusionForge is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * This script takes some POST variables. It makes a check and it
27 * store in DB the commit info attached to the tracker or task.
31 require_once dirname(__FILE__)."/../../../env.inc.php";
32 require_once $gfcommon.'include/pre.php';
35 * Getting POST variables
36 * UserName, Repository, Path, FileName, PrevVersion, ActualVersion, Log,
37 * ArtifactNumbers and TaskNumbers
41 $SubmittedVars = array();
42 $SubmittedVars = unserialize(urldecode($_POST['data']));
44 $data = urldecode($_POST['data']);
46 if(is_array($SubmittedVars)) {
47 foreach ($SubmittedVars as $SubmittedVar) {
48 $Configs[$i] = array();
49 $Configs[$i]['UserName'] = $SubmittedVar['UserName'];
50 $Configs[$i]['Email'] = $SubmittedVar['Email'];
51 //$Configs[$i]['UserName'] = 'def_admin'; use this to make tests, just replace with a gforge user
52 $Configs[$i]['Repository'] = $SubmittedVar['Repository'];
53 $Configs[$i]['FileName'] = $SubmittedVar['FileName'];
54 $Configs[$i]['PrevVersion'] = $SubmittedVar['PrevVersion'];
55 $Configs[$i]['ActualVersion'] = $SubmittedVar['ActualVersion'];
56 $Configs[$i]['ArtifactNumbers'] = $SubmittedVar['ArtifactNumbers'];
57 $Configs[$i]['TaskNumbers'] = $SubmittedVar['TaskNumbers'];
58 $Configs[$i]['Log'] = $SubmittedVar['Log'];
59 $Configs[$i]['GitDate'] = $SubmittedVar['GitDate'];
65 * Checks if the commit it's possible and parse arguments
66 * Checks if repository, group and user_name are right.
67 * It extract group from gitroot, and check if the plugin
68 * is availabe. It checks if the user exists.
70 * @param array $Config Config
72 * @return array Returns 'check'=true if check passed, group, group_id
74 function parseConfig(&$Config) {
75 $repos_path = forge_get_config ('repos_path', 'scmgit') ;
78 $Result['check'] = true;
79 $Repository = $Config['Repository'];
80 $UserName = $Config['UserName'];
81 $email = $Config['Email'];
83 if($repos_path[strlen($repos_path)-1]!='/') {
86 $repo_root = substr($Repository,0,strrpos($Repository,"/") + 1); //we get the directory of the repository root (with trailing slash)
87 if (fileinode($repos_path) !== false
88 && 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
89 $GroupName = substr($Repository, strrpos($Repository,"/") + 1);
90 $GroupName = preg_replace('/.git$/', '', $GroupName);
91 $Config['FileName'] = substr($Config['FileName'],strlen($Repository)); //get only the filename relative to the repo
93 $GroupName = trim(str_replace($repos_path,'',$repo_root),"/");
96 $Result['group'] = group_get_object_by_name($GroupName);
97 $Result['user'] = user_get_object_by_name($UserName);
98 if (!$Result['group'] || !is_object($Result['group']) ||
99 $Result['group']->isError() || !$Result['group']->isActive()) {
100 $Result['check'] = false;
101 $Result['error'] = 'Group Not Found';
103 $Result['group_id'] = $Result['group']->getID();
104 if (!$Result['group']->usesPlugin('scmhook')) {
105 $Result['check'] = false;
106 $Result['error'] = 'Plugin not enabled for this Group';
110 if (!$Result['user'] || !is_object($Result['user']) ||
111 $Result['user']->isError() || !$Result['user']->isActive()) {
113 // Try searching using real name or email address instead.
114 $res=db_query_params('SELECT user_name FROM users WHERE lower(realname)=$1 OR lower(email)=$2',
115 array(strtolower($UserName), strtolower($email)));
116 if ($res && db_numrows($res) > 0) {
117 $Config['UserName'] = db_result($res,0,'user_name');
118 $Result['user'] = user_get_object_by_name($Config['UserName']);
121 if (!$Result['user'] || !is_object($Result['user']) ||
122 $Result['user']->isError() || !$Result['user']->isActive()) {
123 $Result['check'] = false;
124 $Result['error'] = 'Invalid User';
131 * Add a entry in the DataBase for a Artifact associated to a commit
133 * @param array $Config Config
134 * @param string $GroupId The GroupId to insert it into
135 * @param string $Num The artifact_id
137 * @return array Returns 'check'=true if check passed, group, group_id
139 function addArtifactLog($Config, $GroupId, $Num) {
141 $Result = db_query_params ('SELECT * FROM artifact,artifact_group_list WHERE
142 artifact.group_artifact_id=artifact_group_list.group_artifact_id
143 AND artifact_group_list.group_id=$1 AND artifact.artifact_id=$2',
146 $Rows = db_numrows($Result);
148 $return['Error'] = "Artifact ".$Num." Not Found.";
153 $DBRes = db_query_params ('INSERT INTO plugin_scmhook_scmgit_committracker_data_artifact
154 (kind, group_artifact_id) VALUES
157 $HolderID= db_insertid($DBRes,'plugin_scmhook_scmgit_committracker_data_artifact','id');
158 if (!$DBRes || !$HolderID) {
159 $return['Error']='Problems with Artifact $Num: '.db_error();
162 $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)',
167 $Config['PrevVersion'],
168 $Config['ActualVersion'],
169 $Config['UserName'])) ;
171 $return['Error']="Problems with Artifact $Num: ".db_error()."\n";
180 $return['Error'] .= "Unknown problem adding Tracker: $Num.";
186 * Add a entry in the DataBase for a Tracker associated to a commit
188 * @param array $Config Config
189 * @param string $GroupId The GroupId to insert it into
190 * @param string $Num The tracker_id
192 * @return array Returns 'check'=true if check passed, group, group_id
194 function addTaskLog($Config, $GroupId, $Num) {
196 $Result = db_query_params ('SELECT * FROM project_task,project_group_list
197 WHERE project_task.group_project_id=project_group_list.group_project_id
198 AND project_task.project_task_id=$1
199 AND project_group_list.group_id=$2',
202 $Rows = db_numrows($Result);
204 $return['Error'] .= "Task:$Num Not Found.";
208 $DBRes = db_query_params ('INSERT INTO plugin_scmhook_scmgit_committracker_data_artifact
209 (kind, project_task_id) VALUES
212 $HolderID= db_insertid($DBRes,'plugin_scmhook_scmgit_committracker_data_artifact','id');
213 if (!$DBRes || !$HolderID) {
214 $return['Error']='Problems with Task $Num: '.db_error();
217 $DBRes = db_query_params ('INSERT INTO plugin_scmhook_scmgit_committracker_data_master
218 (holder_id, git_date, log_text, file, prev_version,
219 actual_version, author)
220 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 if (isset($Configs) && is_array($Configs)) {
242 foreach ($Configs as $Config) {
243 $Result = parseConfig($Config);
244 if (!$Result['check']) {
245 exit_error('Check_error:' . $Result['error']);
248 if (!is_null($Config['ArtifactNumbers'])) {
249 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) {
259 $AddResult = addTaskLog($Config, $Result['group_id'], $Num);
260 if (isset($AddResult['Error'])) {
261 exit_error('Adding TaskNumber',$AddResult['Error']);