1.创建components文件jira_client.php
<?php
class JiraClientComponent extends Object {
// Jira WSDL
var $wsdl = JIRA_WSDL;
// Login info
var $user = JIRA_USER;
var $password = JIRA_PWD;
var $client = null;
var $login = null;
//called before Controller::beforeFilter()
function initialize(&$controller, $settings = array()) {
// saving the controller reference for later use
$this->controller =& $controller;
// Create the soap Client
$this->client = new soapclient($wsdl);
// Login to Jira
$this->login = $this->client->login( $this->user,$this->password);
}
/**
* upload attachments
*/
function uploadAttachments( $file_path, $project_key ){
$attachment_file = $file_path;
$content = base64_encode(file_get_contents($attachment_file));
$attachmentName = basename($attachment_file);
$result = $client->addBase64EncodedAttachmentsToIssue($this->login, $project_key, array($attachmentName), array($content) );
return $result;
}
/**
* create Issue
*/
function createIssue($issue){
// Create the Issue
$remoteIssue = $this->client->createIssue( $this->login,$issue);
$this->log("create issue project_key:".$remoteIssue->key,"jira_soap");
return $remoteIssue;
}
/**
* jira issue key
*/
function getKey( $remoteIssue ){
return $remoteIssue->key;
}
//called after Controller::render()
function shutdown(&$controller) {
// Log out
$logout = $this->client->logout($login);
if($logout == TRUE){
$this->log("Logged out!","jira_soap");
}else{
$this->log("\nFailed to logout!","jira_soap");
}
}
}
?>
2.controller中使用
//引入component
public $components = array('JiraClient');
//创建ISSUE
$remoteIssue = $this->JiraClient->createIssue($issue);
//获得创建后的ISSUE key
$this->JiraClient->getKey( $remoteIssue );
//上传附件
$this->JiraClient->uploadAttachments( $file_path, $project_key );
备注:
$issue = array(
"project" => $project,
"type" =>$type,
"summary" => "主题",
"assignee"=>$userName,
"reporter"=>$login,
"customFieldValues" => $remoteIssue
);
$remoteIssue = array(array ("customfieldId"=>"customfield_10050", "values"=>array ($sampleName)),
array ("customfieldId"=>"customfield_10123", "values"=>array ($desc)),
array ("customfieldId"=>"customfield_10167", "values"=>array ("手机医生")));
备注:
可借鉴
<?php
require_once('lib/nusoap.php');
require_once('lib/class.wsdlcache.php');
$cache = new wsdlcache('./cache', 60);
$wsdl = $cache->get('http://localhost:8080/rpc/soap/jirasoapservice-v2?wsdl');
if (is_null($wsdl)) {
$wsdl = new wsdl('http://localhost:8080/rpc/soap/jirasoapservice-v2?wsdl');
$cache->put($wsdl);
}
$client = new soapclient($wsdl, true);
$err = $client->getError();
if ($err) {
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
}
// Login to JIRA
$credentials = array('username', 'password');
$token = $client->call('login', $credentials);
// Get the user information
$userReq = array($token,'username');
$result = $client->call('getUser', $userReq);
// Add a comment to a specific issue
//$action = array($token, 'issueId', array('body'=>"Fear me for this is my comment\n"));
//$issue = $client->call('addComment', $action);
// Check for a fault
if ($client->fault) {
echo '<h2>Fault</h2><pre>';
print_r($result);
echo '</pre>';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Error</h2><pre>' . $err . '</pre>';
echo '<h2>Debug</h2><pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
} else {
// Display the result
echo '<h2>Result</h2><pre>';
print_r($result);
echo '</pre>';
}
}
?>
方法二:
查看附件