PHP code example of tomek-swienty / php-jira-rest-client
1. Go to this page and download the library: Download tomek-swienty/php-jira-rest-client library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tomek-swienty / php-jira-rest-client example snippets
use JiraRestApi\Configuration\ArrayConfiguration;
use JiraRestApi\Issue\IssueService;
$iss = new IssueService(new ArrayConfiguration(
array(
'jiraHost' => 'https://your-jira.host.com',
// for basic authorization:
'jiraUser' => 'jira-username',
'jiraPassword' => 'jira-password-OR-api-token',
// to enable session cookie authorization (with basic authorization only)
'cookieAuthEnabled' => true,
'cookieFile' => storage_path('jira-cookie.txt'),
// if you are behind a proxy, add proxy settings
"proxyServer" => 'your-proxy-server',
"proxyPort" => 'proxy-port',
"proxyUser" => 'proxy-username',
"proxyPassword" => 'proxy-password',
)
));
JiraRestApi\Issue\IssueService;
use JiraRestApi\Issue\IssueField;
use JiraRestApi\JiraException;
try {
$issueField = new IssueField();
$issueField->setProjectKey("TEST")
->setSummary("something's wrong")
->setAssigneeName("lesstif")
->setPriorityName("Critical")
->setIssueType("Bug")
->setDescription("Full description for issue")
->addVersion(["1.0.1", "1.0.3"])
->addComponents(['Component-1', 'Component-2'])
// set issue security if you need.
->setSecurityId(10001 /* security scheme id */)
->setDueDate('2019-06-19')
;
$issueService = new IssueService();
$ret = $issueService->create($issueField);
//If success, Returns a link to the created issue.
var_dump($ret);
} catch (JiraRestApi\JiraException $e) {
print("Error Occured! " . $e->getMessage());
}
try {
$issueField = new IssueField();
$issueField->setProjectKey("TEST")
->setSummary("something's wrong")
->setAssigneeName("lesstif")
->setPriorityName("Critical")
->setIssueType("Bug")
->setDescription("Full description for issue")
->addVersion("1.0.1")
->addVersion("1.0.3")
->addCustomField('customfield_10100', 'text area body text') // String type custom field
->addCustomField('customfield_10200', ['value' => 'Linux']) // Select List (single choice)
->addCustomField('customfield_10408', [
['value' => 'opt2'], ['value' => 'opt4']
]) // Select List (multiple choice)
;
$issueService = new IssueService();
$ret = $issueService->create($issueField);
//If success, Returns a link to the created issue.
var_dump($ret);
} catch (JiraRestApi\JiraException $e) {
print("Error Occured! " . $e->getMessage());
}
JiraRestApi\Issue\IssueService;
use JiraRestApi\Issue\IssueField;
use JiraRestApi\JiraException;
try {
$issueFieldOne = new IssueField();
$issueFieldOne->setProjectKey("TEST")
->setSummary("something's wrong")
->setPriorityName("Critical")
->setIssueType("Bug")
->setDescription("Full description for issue");
$issueFieldTwo = new IssueField();
$issueFieldTwo->setProjectKey("TEST")
->setSummary("something else is wrong")
->setPriorityName("Critical")
->setIssueType("Bug")
->setDescription("Full description for second issue");
$issueService = new IssueService();
$ret = $issueService->createMultiple([$issueFieldOne, $issueFieldTwo]);
//If success, returns an array of the created issues
var_dump($ret);
} catch (JiraRestApi\JiraException $e) {
print("Error Occured! " . $e->getMessage());
}
JiraRestApi\Issue\IssueService;
use JiraRestApi\Issue\IssueField;
use JiraRestApi\JiraException;
try {
$issueField = new IssueField();
$issueField->setProjectKey("TEST")
->setSummary("something's wrong")
->setAssigneeName("lesstif")
->setPriorityName("Critical")
->setDescription("Full description for issue")
->addVersion("1.0.1")
->addVersion("1.0.3")
->setIssueType("Sub-task") //issue type must be Sub-task
->setParentKeyOrId('TEST-143') //Issue Key
;
$issueService = new IssueService();
$ret = $issueService->create($issueField);
//If success, Returns a link to the created sub task.
var_dump($ret);
} catch (JiraRestApi\JiraException $e) {
print("Error Occured! " . $e->getMessage());
}
JiraRestApi\Issue\IssueService;
use JiraRestApi\Issue\IssueFieldV3;
use JiraRestApi\JiraException;
try {
$issueField = new IssueFieldV3();
$paraDesc =<<< DESC
Full description for issue
- order list 1
- order list 2
-- sub order list 1
-- sub order list 1
- order list 3
DESC;
$issueField->setProjectKey("TEST")
->setSummary("something's wrong")
->setAssigneeAccountId("user-account-id-here")
->setPriorityName("Critical")
->setIssueType("Bug")
->addDescriptionHeading(3, 'level 3 heading here')
->addDescriptionParagraph($paraDesc)
->addVersion(["1.0.1", "1.0.3"])
->addComponents(['Component-1', 'Component-2'])
// set issue security if you need.
->setDueDate('2019-06-19')
;
$issueService = new IssueService();
$ret = $issueService->create($issueField);
//If success, Returns a link to the created issue.
var_dump($ret);
} catch (JiraRestApi\JiraException $e) {
print("Error Occured! " . $e->getMessage());
}
try {
$issueField = new IssueField();
$issueField->setProjectKey("TEST")
->setSummary("something's wrong")
->setAssigneeName("lesstif")
->setPriorityName("Critical")
->setIssueType("Bug")
->setDescription("Full description for issue")
->addVersion("1.0.1")
->addVersion("1.0.3")
->addCustomField('customfield_10100', 'text area body text') // String type custom field
->addCustomField('customfield_10200', ['value' => 'Linux']) // Select List (single choice)
->addCustomField('customfield_10408', [
['value' => 'opt2'], ['value' => 'opt4']
]) // Select List (multiple choice)
;
$issueService = new IssueService();
$ret = $issueService->create($issueField);
//If success, Returns a link to the created issue.
var_dump($ret);
} catch (JiraRestApi\JiraException $e) {
print("Error Occured! " . $e->getMessage());
}
JiraRestApi\Issue\IssueService;
use JiraRestApi\Issue\IssueField;
use JiraRestApi\JiraException;
$issueKey = "TEST-879";
try {
$issueField = new IssueField(true);
$issueField->setAssigneeName("admin")
->setPriorityName("Blocker")
->setIssueType("Task")
->addLabel("test-label-first")
->addLabel("test-label-second")
->addVersion("1.0.1")
->addVersion("1.0.2")
->setDescription("This is a shorthand for a set operation on the summary field")
;
// optionally set some query params
$editParams = [
'notifyUsers' => false,
];
$issueService = new IssueService();
// You can set the $paramArray param to disable notifications in example
$ret = $issueService->update($issueKey, $issueField, $editParams);
var_dump($ret);
} catch (JiraRestApi\JiraException $e) {
$this->assertTrue(FALSE, "update Failed : " . $e->getMessage());
}
JiraRestApi\Issue\IssueService;
use JiraRestApi\JiraException;
use JiraRestApi\Issue\Comment;
$issueKey = "TEST-879";
try {
$commentId = 12345;
$issueService = new IssueService();
$comment = new Comment();
$comment->setBody('Updated comments');
$issueService->updateComment($issueKey, $commentId, $comment);
} catch (JiraRestApi\JiraException $e) {
$this->assertTrue(false, 'Update comment Failed : '.$e->getMessage());
}
JiraRestApi\Issue\IssueService;
use JiraRestApi\Issue\Transition;
use JiraRestApi\JiraException;
$issueKey = "TEST-879";
try {
$transition = new Transition();
$transition->setTransitionName('Resolved');
$transition->setCommentBody('performing the transition via REST API.');
$issueService = new IssueService();
$issueService->transition($issueKey, $transition);
} catch (JiraRestApi\JiraException $e) {
$this->assertTrue(FALSE, "add Comment Failed : " . $e->getMessage());
}
JiraRestApi\Issue\IssueService;
use JiraRestApi\JiraException;
$jql = 'project not in (TEST) and assignee = currentUser() and status in (Resolved, closed)';
try {
$issueService = new IssueService();
$ret = $issueService->search($jql);
var_dump($ret);
} catch (JiraRestApi\JiraException $e) {
$this->assertTrue(false, 'testSearch Failed : '.$e->getMessage());
}
JiraRestApi\Issue\IssueService;
use JiraRestApi\JiraException;
$jql = 'project not in (TEST) and assignee = currentUser() and status in (Resolved, closed)';
try {
$issueService = new IssueService();
$pagination = -1;
$startAt = 0; //the index of the first issue to return (0-based)
$maxResult = 3; // the maximum number of issues to return (defaults to 50).
$totalCount = -1; // the number of issues to return
// first fetch
$ret = $issueService->search($jql, $startAt, $maxResult);
$totalCount = $ret->total;
// do something with fetched data
foreach ($ret->issues as $issue) {
print (sprintf("%s %s \n", $issue->key, $issue->fields->summary));
}
// fetch remained data
$page = $totalCount / $maxResult;
for ($startAt = 1; $startAt < $page; $startAt++) {
$ret = $issueService->search($jql, $startAt * $maxResult, $maxResult);
print ("\nPaging $startAt\n");
print ("-------------------\n");
foreach ($ret->issues as $issue) {
print (sprintf("%s %s \n", $issue->key, $issue->fields->summary));
}
}
} catch (JiraRestApi\JiraException $e) {
$this->assertTrue(false, 'testSearch Failed : '.$e->getMessage());
}
JiraRestApi\Issue\IssueService;
use JiraRestApi\Issue\JqlQuery;
use JiraRestApi\JiraException;
use JiraRestApi\Issue\JqlFunction;
try {
$jql = new JqlQuery();
$jql->setProject('TEST')
->setType('Bug')
->setStatus('In Progress')
->setAssignee(JqlFunction::currentUser())
->setCustomField('My Custom Field', 'value')
->addIsNotNullExpression('due');
$issueService = new IssueService();
$ret = $issueService->search($jql->getQuery());
var_dump($ret);
} catch (JiraRestApi\JiraException $e) {
$this->assertTrue(false, 'testSearch Failed : '.$e->getMessage());
}
JiraRestApi\Issue\IssueService;
use JiraRestApi\JiraException;
$issueKey = 'TEST-316';
try {
$issueService = new IssueService();
$rils = $issueService->getRemoteIssueLink($issueKey);
// rils is array of RemoteIssueLink classes
var_dump($rils);
} catch (JiraRestApi\JiraException $e) {
$this->assertTrue(false, $e->getMessage());
}
JiraRestApi\Issue\IssueService;
use JiraRestApi\Issue\RemoteIssueLink;
use JiraRestApi\JiraException;
$issueKey = 'TEST-316';
try {
$issueService = new IssueService();
$ril = new RemoteIssueLink();
$ril->setUrl('http://www.mycompany.com/support?id=1')
->setTitle('Remote Link Title')
->setRelationship('causes')
->setSummary('Crazy customer support issue')
;
$rils = $issueService->createOrUpdateRemoteIssueLink($issueKey, $ril);
// rils is array of RemoteIssueLink classes
var_dump($rils);
} catch (JiraRestApi\JiraException $e) {
$this->assertTrue(false, 'Create Failed : '.$e->getMessage());
}
JiraRestApi\Issue\IssueService;
use JiraRestApi\Issue\TimeTracking;
use JiraRestApi\JiraException;
$issueKey = 'TEST-961';
try {
$issueService = new IssueService();
// get issue's time tracking info
$ret = $issueService->getTimeTracking($this->issueKey);
var_dump($ret);
$timeTracking = new TimeTracking;
$timeTracking->setOriginalEstimate('3w 4d 6h');
$timeTracking->setRemainingEstimate('1w 2d 3h');
// add time tracking
$ret = $issueService->timeTracking($this->issueKey, $timeTracking);
var_dump($ret);
} catch (JiraRestApi\JiraException $e) {
$this->assertTrue(false, 'testSearch Failed : '.$e->getMessage());
}
JiraRestApi\Issue\IssueService;
use JiraRestApi\Issue\Worklog;
use JiraRestApi\JiraException;
$issueKey = 'TEST-961';
try {
$workLog = new Worklog();
$workLog->setComment('I did some work here.')
->setStarted("2016-05-28 12:35:54")
->setTimeSpent('1d 2h 3m');
$issueService = new IssueService();
$ret = $issueService->addWorklog($issueKey, $workLog);
$workLogid = $ret->{'id'};
var_dump($ret);
} catch (JiraRestApi\JiraException $e) {
$this->assertTrue(false, 'Create Failed : '.$e->getMessage());
}
orklog example for API V3 assumes JIRA_REST_API_V3=true is configured in
// your .env file.
use JiraRestApi\Issue\ContentField;
use JiraRestApi\Issue\IssueService;
use JiraRestApi\Issue\Worklog;
use JiraRestApi\JiraException;
$issueKey = 'TEST-961';
try {
$workLog = new Worklog();
$paragraph = new ContentField();
$paragraph->type = 'paragraph';
$paragraph->content[] = [
'text' => 'I did some work here.',
'type' => 'text',
];
$comment = new ContentField();
$comment->type = 'doc';
$comment->version = 1;
$comment->content[] = $paragraph;
$workLog->setComment($comment)
->setStarted('2016-05-28 12:35:54')
->setTimeSpent('1d 2h 3m');
$issueService = new IssueService();
$ret = $issueService->addWorklog($issueKey, $workLog);
$workLogid = $ret->{'id'};
var_dump($ret);
} catch (JiraRestApi\JiraException $e) {
$this->assertTrue(false, 'Create Failed : '.$e->getMessage());
}
JiraRestApi\Issue\IssueService;
use JiraRestApi\Issue\Worklog;
use JiraRestApi\JiraException;
$issueKey = 'TEST-961';
$workLogid = '12345';
try {
$workLog = new Worklog();
$workLog->setComment('I did edit previous worklog here.')
->setStarted("2016-05-29 13:15:34")
->setTimeSpent('3d 4h 5m');
$issueService = new IssueService();
$ret = $issueService->editWorklog($issueKey, $workLog, $workLogid);
var_dump($ret);
} catch (JiraRestApi\JiraException $e) {
$this->assertTrue(false, 'Edit worklog Failed : '.$e->getMessage());
}
JiraRestApi\Issue\IssueService;
use JiraRestApi\JiraException;
$issueKey = 'TEST-961';
try {
$issueService = new IssueService();
// get issue's all worklog
$worklogs = $issueService->getWorklog($issueKey)->getWorklogs();
var_dump($worklogs);
// get worklog by id
$wlId = 12345;
$wl = $issueService->getWorklogById($issueKey, $wlId);
var_dump($wl);
} catch (JiraRestApi\JiraException $e) {
$this->assertTrue(false, 'testSearch Failed : '.$e->getMessage());
}