Hey there, fellow developer! Ready to dive into the world of Jira Service Management API integration? You're in for a treat. We'll be using the awesome lesstif/php-jira-rest-client
package to make our lives easier. This guide assumes you're already familiar with PHP and Jira, so we'll skip the basics and get right to the good stuff.
Before we jump in, make sure you've got:
Let's kick things off by installing our star player:
composer require lesstif/php-jira-rest-client
Easy peasy, right?
Now, let's set up our Jira client. First, we need to configure our API credentials:
$jiraConfig = [ 'jiraHost' => 'https://your-jira-instance.atlassian.net', 'jiraUser' => '[email protected]', 'jiraPassword' => 'your-api-token' ]; $jira = new \JiraRestApi\Configuration\ArrayConfiguration($jiraConfig);
Pro tip: Never hardcode your credentials. Use environment variables or a config file instead.
Let's fetch some issues:
$issueService = new \JiraRestApi\Issue\IssueService($jira); $issue = $issueService->get('PROJ-123');
Time to create a new issue:
$issueField = new \JiraRestApi\Issue\IssueField(); $issueField->setProjectKey("TEST") ->setSummary("Something's wrong") ->setIssueType("Bug") ->setDescription("It's not working!"); $issueService->create($issueField);
Let's update that issue we just created:
$issue = $issueService->get('TEST-456'); $issue->fields->summary = "Something's really wrong"; $issueService->update($issue->id, $issue);
Oops, false alarm. Let's delete that issue:
$issueService->deleteIssue("TEST-456");
Custom fields are a breeze:
$issueField->addCustomField('customfield_10100', 'Custom value');
Attaching files is straightforward:
$attachmentService = new \JiraRestApi\Issue\AttachmentService($jira); $attachmentService->upload('TEST-456', '/path/to/file.txt');
Transitioning issues is a piece of cake:
$transition = new \JiraRestApi\Issue\Transition(); $transition->setTransitionName('In Progress'); $issueService->transition('TEST-456', $transition);
Always wrap your API calls in try-catch blocks:
try { $issue = $issueService->get('NONEXISTENT-123'); } catch (\JiraRestApi\JiraException $e) { error_log('Jira API error: ' . $e->getMessage()); }
Unit testing is crucial. Mock Jira responses to test your integration:
use PHPUnit\Framework\TestCase; use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\Psr7\Response; class JiraIntegrationTest extends TestCase { public function testGetIssue() { $mock = new MockHandler([ new Response(200, [], '{"key": "TEST-123"}') ]); // Set up your Jira client with the mock handler // Then run your tests } }
And there you have it! You're now equipped to integrate Jira Service Management API into your PHP projects like a pro. Remember, the lesstif/php-jira-rest-client
package documentation is your best friend for more advanced use cases.
Happy coding, and may your tickets always be resolved!