/** * Find project objects in commit message, make them links and * save the relations to database * * @param string $commit_message * @param string $commit_author * @param integer $revision * @param Repository $repository * @param Project $project * @return string */ function analyze_message($commit_message, $commit_author, $revision, $repository, $project) { if (define('PURIFY_HTML') && PURIFY_HTML) { $commit_message = purify_html($commit_message); // Clean! } // if $pattern = '/((complete[d]*)[\\s]+)?(ticket|milestone|discussion|task)[s]*[\\s]+[#]*\\d+/i'; if (preg_match_all($pattern, $commit_message, $matches)) { $i = 0; $search = array(); $replace = array(); $matches_unique = array_unique($matches['0']); foreach ($matches_unique as $key => $match) { $match_data = preg_split('/[\\s,]+/', $match, null, PREG_SPLIT_NO_EMPTY); // check if the object got completed by this commit $object_completed = false; if (strpos(strtolower($match_data['0']), 'complete') !== false) { $object_completed = true; unset($match_data['0']); $match_data = array_values($match_data); } // if $object_class_name = $match_data['0']; $module_name = Inflector::pluralize($object_class_name); $object_id = trim($match_data['1'], '#'); $search[$i] = $match; if (class_exists($module_name) && class_exists($object_class_name)) { $object = null; switch (strtolower($module_name)) { case 'tickets': $object = Tickets::findByTicketId($project, $object_id); break; case 'discussions': $object = Discussions::findById($object_id); break; case 'milestones': $object = Milestones::findById($object_id); break; case 'tasks': $object = Tasks::findById($object_id); break; } // switch if (instance_of($object, $object_class_name)) { $link_already_created = CommitProjectObjects::count("object_id = '" . $object->getId() . "' AND revision = '{$revision}'") > 0; if (!$link_already_created) { $comit_project_object = new CommitProjectObject(); $comit_project_object->setProjectId($object->getProjectId()); $comit_project_object->setObjectId($object->getId()); $comit_project_object->setObjectType(ucfirst($object_class_name)); $comit_project_object->setRepositoryId($repository->getId()); $comit_project_object->setRevision($revision); db_begin_work(); $save = $comit_project_object->save(); if ($save && !is_error($save)) { db_commit(); } else { db_rollback(); } // if save } // if $replace[$i] = ($object_completed ? 'Completed ' : '') . '<a href="' . $object->getViewUrl() . '">' . $match_data['0'] . ' ' . $match_data['1'] . '</a>'; // set the object as completed if ($object_completed && !instance_of($object, 'Discussion')) { $completed_by = $repository->getMappedUser($commit_author); $object->complete($completed_by); } // if } else { $replace[$i] = ($object_completed ? 'Completed ' : '') . '<a href="#" class="project_object_missing" title="' . lang('Project object does not exist in this project') . '">' . $match_data['0'] . ' ' . $match_data['1'] . '</a>'; } // if instance_of $i++; } // if module loaded } // foreach return str_ireplace($search, $replace, htmlspecialchars($commit_message)); // linkify } // if preg_match return $commit_message; }
/** * Add AC object links to commit messages * * @param string * @return string **/ private function analyzeCommitMessage($commit_message) { $pattern = '/(ticket|milestone|discussion|task)[s]*[\\s]+[#]*(\\d+)/i'; if (preg_match_all($pattern, $commit_message, $matches)) { $i = 0; $search = array(); $replace = array(); $matches_unique = array_unique($matches['0']); foreach ($matches_unique as $key => $match) { $match_data = preg_split('/[\\s,]+/', $match, null, PREG_SPLIT_NO_EMPTY); $object_class_name = $match_data['0']; $module_name = Inflector::pluralize($object_class_name); $object_id = trim($match_data['1'], '#'); $search[$i] = $match; if (class_exists($module_name) && class_exists($object_class_name)) { $object = null; switch (strtolower($module_name)) { case 'tickets': $object = Tickets::findByTicketId($this->active_project, $object_id); break; case 'discussions': $object = Discussions::findById($object_id); break; case 'milestones': $object = Milestones::findById($object_id); break; case 'tasks': $object = Tasks::findById($object_id); break; } // switch if (instance_of($object, $object_class_name)) { $replace[$i] = '<a href="' . $object->getViewUrl() . '">' . $match_data['0'] . ' ' . $match_data['1'] . '</a>'; } else { $replace[$i] = '<a href="#" class="project_object_missing" title="' . lang('Project object does not exist in this project') . '">' . $match_data['0'] . ' ' . $match_data['1'] . '</a>'; } // if instance_of $i++; } // if module loaded } // foreach return str_ireplace($search, $replace, htmlspecialchars($commit_message)); // linkify } // if preg_match return $commit_message; }