Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Subversion post-commit add a note to SugarCRM
<?php
/**
* Utility class that use SugarCRM Rest to create a Note and associate to a Case
*/
class nepoUtils {
const HOME = 'http://<yourcrm>/service/v4_1/rest.php';
protected $curl;
protected $session;
protected $user_id;
/**
* set the setting for our REST connection
*/
public function __construct() {
// specify the REST web service to interact with
$this->curl = curl_init(self::HOME);
// Open a curl session for making the call
// Tell curl to use HTTP POST
curl_setopt($this->curl, CURLOPT_POST, true);
// Tell curl not to return headers, but do return the response
curl_setopt($this->curl, CURLOPT_HEADER, false);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
// Login to the Server
$parameters = array(
'user_auth' => array(
'user_name' => '<username>',
'password' => md5('<password>'),
),
);
$json = json_encode($parameters);
$postArgs = 'method=login&input_type=JSON&response_type=JSON&rest_data=' . $json;
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $postArgs);
// Make the REST call, returning the result
$response = curl_exec($this->curl);
// Convert the result from JSON format to a PHP array
$result = json_decode($response);
$this->session = $result->id;
$this->user_id = $result->name_value_list->user_id->value;
}
/**
* Find the SugarCRM user based upon the Subversion Author
*
*/
public function getUser($author) {
$parameters = array(
'session' => $this->session,
'module' => 'Users',
'query' => "users.user_name = '$author'",
'order_by' => 'id',
'offset' => '0',
'select_fields' => array('id', 'first_name', 'last_name', 'user_name', 'name'),
);
$json = json_encode($parameters);
$postArgs = 'method=get_entry_list&input_type=json&response_type=json&rest_data=' . $json;
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $postArgs);
$response = curl_exec($this->curl);
$result = json_decode($response);
$focus = $result->entry_list[0];
return $focus;
}
public function logFiles($caseNumber = '', $description = "", $author, $revision = '') {
/*
* Find information about this Case
*/
$parameters = array(
'session' => $this->session,
'module' => 'Cases',
'query' => "case_number = '$caseNumber'",
'order_by' => 'id',
'offset' => '0',
'select_fields' => 'id',
);
$json = json_encode($parameters);
$postArgs = 'method=get_entry_list&input_type=json&response_type=json&rest_data=' . $json;
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $postArgs);
$response = curl_exec($this->curl);
$result = json_decode($response);
$focus = $result->entry_list[0];
$user = $this->getUser($author);
/* Update the Note */
$parameters = array(
'session' => $this->session,
'module' => 'Notes',
'name_value_list' => array(
array(
'name' => 'parent_id',
'value' => $focus->id,
),
array(
'name' => 'parent_type',
'value' => 'Cases' ,
),
array(
'name' => 'name',
'value' => "[CASE: {$caseNumber}] files updated by " . $user->name_value_list->name->value,
),
array(
'name' => 'description',
'value' => $description,
),
array(
'name' => 'assigned_user_id',
'value' => $user->id,
),
array(
'name' => 'created_by',
'value' => $user->id,
),
array(
'name' => 'revision_c',
'value' => $revision,
),
),
);
$json = json_encode($parameters);
$postArgs = 'method=set_entry&input_type=json&response_type=json&rest_data=' . $json;
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $postArgs);
// Make the REST call, returning the result
$response = curl_exec($this->curl);
// Convert the result from JSON format to a PHP array
$result = json_decode($response);
// Close the connection
curl_close($this->curl);
}
}
?>
<?php
require_once('nepoUtils.php');
$repository = isset($argv[1]) ? $argv[1] : '';
$revision = isset($argv[2]) ? $argv[2] : '';
/* Who committed the change */
$author = exec('svnlook author ' . $repository);
/* What changed */
$changed = '';
$fp = popen("svnlook changed $repository | sed 's/trunk\///g'", "r");
while(!feof($fp)) {
$changed .= fread($fp, 1024);
flush();
}
pclose($fp);
/* What did the have to say */
$comments = '';
$fp = popen("svnlook log {$repository}", "r");
while(!feof($fp)) {
$comments .= fread($fp, 1024);
flush();
}
pclose($fp);
$comments = trim($comments);
/* What is the Case Number */
function getCaseId($emailName) {
//$emailSubjectMacro
$exMacro = explode('%1', '[CASE:%1]');
$open = $exMacro[0];
$close = $exMacro[1];
if ($sub = stristr($emailName, $open)) { // eliminate everything up to the beginning of the macro and return the rest
// $sub is [CASE:XX] xxxxxxxxxxxxxxxxxxxxxx
$sub2 = str_replace($open, '', $sub);
// $sub2 is XX] xxxxxxxxxxxxxx
$sub3 = substr($sub2, 0, strpos($sub2, $close));
return trim($sub3);
}
return FALSE;
}
$id = getCaseId($comments);
if ($id === FALSE) {
//noop
} else {
/* Log the Changes to SugarCRM */
$nepo = new nepoUtils();
$nepo->logFiles($id, "$comments.\n\nThe following files have changed :\n\n$changed", $author, $revision);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment