FayP / JIRA-php

a php class specifically to be used for JIRA's REST API and also a REST Request class which can be more widely used.
26 stars 22 forks source link

how to use the jql? #3

Open tefz opened 11 years ago

tefz commented 11 years ago

I need to get a list of issue and then do some action on them. If i use the queryIssue function i don't have any accessible result? i tryed something like this $mylist = $queryissues->request->responseBody;

there isn't any object defined for deconding the jql result?

sperera4you commented 11 years ago

this is not how you should call it...

$issue = new jira(); $query->project = 'REC'; $array = $issue->queryIssue($query); $response = json_decode($array); print_r($response);

the 'project' key word can be differ according to what you want to query..:)..eg - key,id,etc enjoy..:)

tefz commented 11 years ago

ok i will test it. if i need something different as jql like "issue in linkedissue(myid)" can i do it?

tefz commented 11 years ago

as before: my responsebody is full (the debug line added in queryIssue function is printing the entire information) but i can't read it outside this class.

$response = json_decode($array); print_r($response);

gives me "1"

thanks for ur support

sperera4you commented 11 years ago

ok...let me give you the full library by FayP...that i modified according to my needs...this will help you get whatever you want...

this is the class.jira.php

<?php require_once('class.restrequest.php'); class Jira { protected $project; protected $host; function __construct($config){ $this->request = new RestRequest; $this->request->username = $config->username; $this->request->password = $config->password; $this->host = $config->host; } public function createIssue($json){ $this->request->openConnect('https://'.$this->host.'/rest/api/latest/issue/', 'POST', $json); $this->request->execute(); return $this->request;
} public function addAttachment($filename, $issueKey){ $this->request->openConnect('https://'.$this->host.'/rest/api/latest/issue/'.$issueKey.'/attachments', 'POST', null, $filename); $this->request->execute(); return $this->request; } public function updateIssue($json, $issueKey){ $this->request->openConnect('https://'.$this->host.'/rest/api/latest/issue/'.$issueKey, 'PUT', $json); $this->request->execute();
} public function queryIssue($query){ $qs = $this->createPairs($query); $this->request->OpenConnect('https://'.$this->host.'/rest/api/latest/search?jql='.$qs); $this->request->execute(); return $this->request; } public function queryMoreIssue($query){ $this->request->OpenConnect('https://'.$this->host.'/rest/api/latest/search?jql=', 'POST', $query); $this->request->execute();
return $this->request; } public function updateTransition($issueKey,$json){ $this->request->openConnect('https://'.$this->host.'/rest/api/latest/issue/'.$issueKey.'/transitions?', 'POST', $json); $this->request->execute(); return $this->request; } function createPairs($obj){ $str = ""; foreach ($obj as $key => $value) { $str .= "$key=$value&"; } return rtrim($str, '&'); } public function queryUser($query){

    function createPairs($obj){
        $str = "";
        foreach ($obj as $key => $value) {
            $str .= "$key=$value&";
        }
        return rtrim($str, '&');
    }
    $qs = createPairs($query);
    $this->request->OpenConnect('https://'.$this->host.'/rest/api/latest/user/assignable/search?'.$qs);
    $this->request->execute();  
    return $this->request;
}
public function queryStatus($issueKey){
    $this->request->openConnect('https://'.$this->host.'/rest/api/latest/issue/'.$issueKey.'/transitions?');
    $this->request->execute();
    return $this->request;
}
public function queryAllStatus(){
    $this->request->openConnect('https://'.$this->host.'/rest/api/latest/status');
    $this->request->execute();
    return $this->request;
}
public function queryAllPriority(){
    $this->request->openConnect('https://'.$this->host.'/rest/api/latest/priority');
    $this->request->execute();
    return $this->request;
}
public function getStatusbyId($jiraid){
    $this->request->openConnect('https://'.$this->host.'/rest/api/latest/status/'.$jiraid);
    $this->request->execute(); 
    return $this->request;
}
public function queryChangelog($issueKey){
    $this->request->openConnect('https://'.$this->host.'/rest/api/latest/issue/'.$issueKey.'?expand=changelog');
    $this->request->execute();
    return $this->request;
}
public function deleteIssue($issueKey){
    $un=$this->request->openConnect('https://'.$this->host.'/rest/api/latest/issue/'.$issueKey, 'DELETE');
    $this->request->execute();  
    return $this->request;
}
public function queryUserGroup($username){
    $this->request->OpenConnect('https://'.$this->host.'/rest/api/latest/user?username='.$username.'&expand=groups');
    $this->request->execute();
    return $this->request;
}
public function queryComment($issueKey){
    $this->request->openConnect('https://'.$this->host.'/rest/api/latest/issue/'.$issueKey.'/comment');
    $this->request->execute();
    return $this->request;
}
public function updateComment($issueKey,$commentID,$json){
    $this->request->openConnect('https://'.$this->host.'/rest/api/latest/issue/'.$issueKey.'/comment/'.$commentID,'PUT',$json);
    $this->request->execute();
    return $this->request;
}
public function deleteComment($issueKey,$commentID){
    $this->request->openConnect('https://'.$this->host.'/rest/api/latest/issue/'.$issueKey.'/comment/'.$commentID,'DELETE');
    $this->request->execute();
    return $this->request;
}
public function addComment($issueKey,$json){
    $this->request->openConnect('https://'.$this->host.'/rest/api/latest/issue/'.$issueKey.'/comment/','POST',$json);
    $this->request->execute();
    return $this->request;
}
public function queryPermission($project){
    $this->request->OpenConnect('https://'.$this->host.'/rest/api/latest/mypermissions?projectKey='.$project);
    $this->request->execute();
    return $this->request;
}

} ?>

this is the class.restrequest.php

<?php class RestRequest { public $username;
public $password; protected $url;
protected $verb;
public $requestBody;
protected $requestLength;
protected $acceptType; public $responseBody; protected $responseInfo;

public function openConnect ($url = null, $verb = 'GET', $requestBody = null, $filename = null){  
    $this->url               = $url;  
    $this->verb              = $verb;  
    $this->requestBody       = $requestBody;  
    $this->requestLength     = 0;
    $this->acceptType        = 'application/json';
    $this->responseBody      = null;  
    $this->responseInfo      = null;  
    $this->filename          = $filename;
    $this->contentType       = 'Content-Type: application/json';

    if ($this->requestBody !== null || $this->filename !== null)
        $this->buildPostBody();  
}  

public function flush (){  
    $this->requestBody       = null;  
    $this->requestLength     = 0;  
    $this->verb              = 'GET';  
    $this->responseBody      = null;  
    $this->responseInfo      = null;  
}  

public function execute (){  
    $ch = curl_init();  
    $this->setAuth($ch);  

    try{  
        switch (strtoupper($this->verb)){  
            case 'GET':  
                $this->executeGet($ch);  
                break;  
            case 'POST':  
                $this->executePost($ch);  
                break;  
            case 'PUT':  
                $this->executePut($ch);  
                break;  
            case 'DELETE':  
                $this->executeDelete($ch);  
                break;  
            default:  
                throw new InvalidArgumentException('Current verb (' . $this->verb . ') is an invalid REST verb.');  
        }  
    }catch (InvalidArgumentException $e){  
        curl_close($ch);  
        throw $e;  
    }catch (Exception $e){  
        curl_close($ch);  
        throw $e;  
    }
}

public function buildPostBody ($data = null){  
    if ($data == null) {
        if ($this->filename !== null) {
            $fileContents = file_get_contents($this->filename);
            $boundary = "----------------------------".substr(md5(rand(0,32000)), 0, 12);

            $data = "--".$boundary."\r\n";
            $data .= "Content-Disposition: form-data; name=\"file\"; filename=\"".basename($this->filename)."\"\r\n";
            $data .= "Content-Type: ".mime_content_type($this->filename)."\r\n";
            $data .= "\r\n";
            $data .= $fileContents."\r\n";
            $data .= "--".$boundary."--";

            $this->requestBody = $data;
            $this->contentType = 'Content-Type: multipart/form-data; boundary='.$boundary;
        }
        else 
            $this->requestBody = json_encode($this->requestBody);
    }
    else
        $this->requestBody = json_encode($data);
}  

protected function executeGet ($ch){         
    $this->doExecute($ch);
}  

protected function executePost ($ch){
    curl_setopt($ch, CURLOPT_POST, true);  
    curl_setopt($ch, CURLOPT_POSTFIELDS, $this->requestBody);  

    $this->doExecute($ch);  
}  

protected function executePut ($ch){ 
    curl_setopt($ch, CURLOPT_POST, true);  
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $this->requestBody);  

    $this->doExecute($ch);  
}  

protected function executeDelete ($ch){  
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");  

    $this->doExecute($ch);  
}  

protected function doExecute (&$ch){  
    $this->setCurlOpts($ch);
    $this->responseBody = curl_exec($ch);  
    $this->responseInfo = curl_getinfo($ch); 
    curl_close($ch);
}

protected function setCurlOpts (&$ch){  
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);  
    curl_setopt($ch, CURLOPT_URL, $this->url);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //curl_setopt($ch, CURLOPT_HEADER, true); //displays header in output.
    curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Accept: ' . $this->acceptType, $this->contentType, 'X-Atlassian-Token: nocheck'));  
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // ignore self signed certs
    //curl_setopt($ch, CURLOPT_VERBOSE, true); // set to true for CURL debug output
} 

protected function setAuth (&$ch){
    if ($this->username !== null && $this->password !== null){   
        curl_setopt($ch, CURLOPT_USERPWD, $this->username . ':' . $this->password);  
    }    
}  

} ?>

do see that i have made some variables public in order to decode them..now use my previous code to get whatever you want. and i hope FayP would update his class.jira.php as i have made different functions for alot of services..have fun..adios