Skip to content

Commit

Permalink
Feature TICKET 7799: Request for Redmine Integration - user impersona…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
fmancardi committed Jan 3, 2017
1 parent b4f347b commit 3939e0e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
8 changes: 6 additions & 2 deletions lib/execute/bugAdd.php
Expand Up @@ -78,7 +78,9 @@

if( $args->addLinkToTL || $hasNotes )
{
$its->addNote($args->bug_id,$gui->bug_notes);
$opt = new stdClass();
$opt->reporter = $args->user->login;
$its->addNote($args->bug_id,$gui->bug_notes,$opt);
}
}
}
Expand All @@ -95,7 +97,9 @@
$gui->msg = '';
if($gui->issueTrackerCfg->tlCanAddIssueNote && (strlen($gui->bug_notes) > 0) )
{
$its->addNote($args->bug_id,$gui->bug_notes);
$opt = new stdClass();
$opt->reporter = $args->user->login;
$its->addNote($args->bug_id,$gui->bug_notes,$opt);
}
break;
}
Expand Down
24 changes: 18 additions & 6 deletions lib/issuetrackerintegration/redminerestInterface.class.php
Expand Up @@ -6,7 +6,7 @@
* @author Francisco Mancardi
*
* @internal revisions
* @since 1.9.14
* @since 1.9.16
*
**/
require_once(TL_ABS_PATH . "/third_party/redmine-php-api/lib/redmine-rest-api.php");
Expand Down Expand Up @@ -316,8 +316,15 @@ function checkBugIDExistence($issueID)
* - custom_fields - See Custom fields
* - watcher_user_ids - Array of user ids to add as watchers (since 2.3.0)
*/
public function addIssue($summary,$description)
public function addIssue($summary,$description,$opt=null)
{
$reporter = null;
if(!is_null($opt) && property_exists($opt, 'reporter'))
{
$reporter = $opt->reporter;
}


// Check mandatory info
if( !property_exists($this->cfg,'projectidentifier') )
{
Expand Down Expand Up @@ -393,7 +400,6 @@ public function addIssue($summary,$description)
}
}

// 20150815
// In order to manage custom fields in simple way,
// it seems that is better create here plain XML String
//
Expand All @@ -406,7 +412,7 @@ public function addIssue($summary,$description)

// $op = $this->APIClient->addIssueFromSimpleXML($issueXmlObj);
// file_put_contents('/var/testlink/' . __CLASS__ . '.log', $xml);
$op = $this->APIClient->addIssueFromXMLString($xml);
$op = $this->APIClient->addIssueFromXMLString($xml,$reporter);

if(is_null($op))
{
Expand All @@ -433,14 +439,20 @@ public function addIssue($summary,$description)
/**
*
*/
public function addNote($issueID,$noteText)
public function addNote($issueID,$noteText,$opt=null)
{
try
{
// needs json or xml
$issueXmlObj = new SimpleXMLElement('<?xml version="1.0"?><issue></issue>');
$issueXmlObj->addChild('notes', htmlspecialchars($noteText));
$op = $this->APIClient->addIssueNoteFromSimpleXML($issueID,$issueXmlObj);

$reporter = null;
if(!is_null($opt) && property_exists($opt, 'reporter'))
{
$reporter = $opt->reporter;
}
$op = $this->APIClient->addIssueNoteFromSimpleXML($issueID,$issueXmlObj,$reporter);
$ret = array('status_ok' => true, 'id' => (string)$op->id,
'msg' => sprintf(lang_get('redmine_bug_created'),$summary,$issueXmlObj->project_id));
}
Expand Down
31 changes: 18 additions & 13 deletions third_party/redmine-php-api/lib/redmine-rest-api.php
Expand Up @@ -9,7 +9,7 @@
* @link http://www.testlink.org
*
* @internal revisions
* @since 1.9.14
* @since 1.9.16
*/

/**
Expand Down Expand Up @@ -84,7 +84,7 @@ public function __construct($url,$apiKey,$cfg=null)
*/
public function initCurl($cfg=null)
{
$agent = "TestLink 1.9.15";
$agent = "TestLink 1.9.16";
try
{
$this->curl = curl_init();
Expand Down Expand Up @@ -164,28 +164,28 @@ function getIssues($filters=null)

// with the help of http://tspycher.com/2011/03/using-the-redmine-api-with-php/
// public function addIssue($summary, $description)
public function addIssueFromSimpleXML($issueXmlObj)
public function addIssueFromSimpleXML($issueXmlObj,$reporter=null)
{
$op = $this->_request_xml('POST',"/issues.xml",$issueXmlObj->asXML());
$op = $this->_request_xml('POST',"/issues.xml",$issueXmlObj->asXML(),0,$reporter);
return $op;
}

/**
*
*/
public function addIssueFromXMLString($XMLString)
public function addIssueFromXMLString($XMLString,$reporter=null)
{
$op = $this->_request_xml('POST',"/issues.xml",$XMLString);
$op = $this->_request_xml('POST',"/issues.xml",$XMLString,0,$reporter);
return $op;
}


/**
*
*/
public function addIssueNoteFromSimpleXML($issueID,$issueXmlObj)
public function addIssueNoteFromSimpleXML($issueID,$issueXmlObj,$reporter=null)
{
$op = $this->_request_xml('PUT',"/issues/{$issueID}.xml",$issueXmlObj->asXML());
$op = $this->_request_xml('PUT',"/issues/{$issueID}.xml",$issueXmlObj->asXML(),0,$reporter);
return $op;
}

Expand Down Expand Up @@ -242,9 +242,10 @@ protected function _get($url)
* @internal notice
* copied and adpated from work on YouTrack API interface by Jens Jahnke <jan0sch@gmx.net>
**/
protected function _request_xml($method, $url, $body = NULL, $ignore_status = 0)
protected function _request_xml($method, $url, $body = NULL, $ignore_status = 0,
$reporter=null)
{
$r = $this->_request($method, $url, $body, $ignore_status);
$r = $this->_request($method, $url, $body, $ignore_status,$reporter);
$response = $r['response'];
$content = trim($r['content']);
$ret = ($content != '' ? $content : null);
Expand All @@ -266,7 +267,7 @@ protected function _request_xml($method, $url, $body = NULL, $ignore_status = 0)
* @internal notice
* copied and adpated from work on YouTrack API interface by Jens Jahnke <jan0sch@gmx.net>
**/
protected function _request($method, $cmd, $body = NULL, $ignoreStatusCode = 0)
protected function _request($method, $cmd, $body = NULL, $ignoreStatusCode = 0,$reporter = null)
{
// this can happens because if I save object on _SESSION PHP is not able to
// save resources.
Expand Down Expand Up @@ -316,6 +317,11 @@ protected function _request($method, $cmd, $body = NULL, $ignoreStatusCode = 0)
$header = array();
$header[] = "X-Redmine-API-Key: {$this->apiKey}";

if(!is_null($reporter))
{
$header[] = "X-Redmine-Switch-User: {$reporter}";
}

if ($method == 'PUT' || $method == 'POST')
{
// Got this info from http://tspycher.com/2011/03/using-the-redmine-api-with-php/
Expand Down Expand Up @@ -398,5 +404,4 @@ public function __destruct()
{
}

} // Class end
?>
} // Class end

0 comments on commit 3939e0e

Please sign in to comment.