183 lines
		
	
	
		
			6.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
			
		
		
	
	
			183 lines
		
	
	
		
			6.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
<?php
 | 
						|
 | 
						|
namespace App\Services;
 | 
						|
 | 
						|
use GuzzleHttp\Client;
 | 
						|
use GuzzleHttp\Promise\Utils;
 | 
						|
 | 
						|
class JiraService
 | 
						|
{
 | 
						|
    protected $client;
 | 
						|
    protected $baseUrl;
 | 
						|
    protected $authHeader;
 | 
						|
 | 
						|
    public function __construct()
 | 
						|
    {
 | 
						|
        $this->baseUrl = env('JIRA_BASE_URL');
 | 
						|
        $this->authHeader = 'Basic ' . base64_encode(env('JIRA_USERNAME') . ':' . env('JIRA_API_TOKEN'));
 | 
						|
        $this->client = new Client([
 | 
						|
            'base_uri' => $this->baseUrl,
 | 
						|
            'headers' => [
 | 
						|
                'Authorization' => $this->authHeader,
 | 
						|
                'Accept' => 'application/json',
 | 
						|
                'Content-Type' => 'application/json'
 | 
						|
            ]
 | 
						|
        ]);
 | 
						|
    }
 | 
						|
 | 
						|
    public function getAllProjects()
 | 
						|
    {
 | 
						|
        $response = $this->client->get('/rest/api/3/project');
 | 
						|
        return json_decode($response->getBody()->getContents(), true);
 | 
						|
    }
 | 
						|
 | 
						|
    public function getIssuesAsync($projectKey, $startAt = 0, $maxResults = 50)
 | 
						|
    {
 | 
						|
        $body = [
 | 
						|
            'expand' => ['names', 'schema', 'operations'],
 | 
						|
            'fields' => ['summary', 'status', 'description', 'timeoriginalestimate', 'timespent', 'worklog', 'assignee'],
 | 
						|
            'jql' => "project = '{$projectKey}' ORDER BY created DESC",
 | 
						|
            'maxResults' => $maxResults,
 | 
						|
            'startAt' => $startAt
 | 
						|
        ];
 | 
						|
 | 
						|
        return $this->client->postAsync($this->baseUrl . '/rest/api/3/search', [
 | 
						|
            'body' => json_encode($body),
 | 
						|
            'headers' => [
 | 
						|
                'Authorization' => $this->authHeader,
 | 
						|
                'Accept' => 'application/json',
 | 
						|
                'Content-Type' => 'application/json'
 | 
						|
            ]
 | 
						|
        ]);
 | 
						|
    }
 | 
						|
    public function getIssues($projectKey, $startAt = 0)
 | 
						|
    {
 | 
						|
        $body = [
 | 
						|
            'expand' => ['names', 'schema', 'operations'],
 | 
						|
            'fields' => ['summary', 'status', 'description', 'timeoriginalestimate', 'timespent', 'worklog', 'assignee'],
 | 
						|
            'jql' => "project = '{$projectKey}' ORDER BY created DESC",
 | 
						|
            'maxResults' => 100,
 | 
						|
            'startAt' => $startAt
 | 
						|
        ];
 | 
						|
 | 
						|
        $response = $this->client->post('/rest/api/3/search', [
 | 
						|
            'body' => json_encode($body)
 | 
						|
        ]);
 | 
						|
 | 
						|
        return json_decode($response->getBody()->getContents(), true);
 | 
						|
    }
 | 
						|
 | 
						|
    public function getIssuesByUser($accountId, $startAt = 0)
 | 
						|
    {
 | 
						|
        $body = [
 | 
						|
            'expand' => ['names', 'schema', 'operations'],
 | 
						|
            'fields' => ['summary', 'status', 'timeoriginalestimate', 'timespent', 'assignee', 'project'],
 | 
						|
            'jql' => sprintf(
 | 
						|
                "assignee = '%s' AND status IN ('backlog', 'todo', 'in progress')",
 | 
						|
                $accountId
 | 
						|
            ),
 | 
						|
            'maxResults' => 50,
 | 
						|
            'startAt' => $startAt
 | 
						|
        ];
 | 
						|
 | 
						|
        $response = $this->client->post('/rest/api/3/search', [
 | 
						|
            'body' => json_encode($body)
 | 
						|
        ]);
 | 
						|
 | 
						|
        return json_decode($response->getBody()->getContents(), true);
 | 
						|
    }
 | 
						|
 | 
						|
    public function getAllUsers()
 | 
						|
    {
 | 
						|
        $response = $this->client->get('/rest/api/3/users/search', [
 | 
						|
            'headers' => [
 | 
						|
                'Authorization' => $this->authHeader,
 | 
						|
                'Accept' => 'application/json'
 | 
						|
            ]
 | 
						|
        ]);
 | 
						|
 | 
						|
        return json_decode($response->getBody()->getContents(), true);
 | 
						|
    }
 | 
						|
 | 
						|
    public function getUserWorkLogs($accountId, $startDate, $endDate)
 | 
						|
    {
 | 
						|
        $body = [
 | 
						|
            'jql' => "worklogAuthor = '{$accountId}'AND worklogDate >= '{$startDate}' AND worklogDate <= '{$endDate}'",
 | 
						|
            'fields' => ['summary', 'status', 'timeoriginalestimate', 'timespent', 'project'],
 | 
						|
            'maxResults' => 100
 | 
						|
        ];
 | 
						|
 | 
						|
        $response = $this->client->post('/rest/api/3/search', [
 | 
						|
            'body' => json_encode($body),
 | 
						|
            'headers' => [
 | 
						|
                'Authorization' => $this->authHeader,
 | 
						|
                'Accept' => 'application/json',
 | 
						|
                'Content-Type' => 'application/json'
 | 
						|
            ]
 | 
						|
        ]);
 | 
						|
 | 
						|
        $data_response = json_decode($response->getBody()->getContents(), true);
 | 
						|
 | 
						|
        if ($data_response['total'] == 0) {
 | 
						|
            return $data_response;
 | 
						|
        }
 | 
						|
 | 
						|
        $promises = [];
 | 
						|
        foreach ($data_response['issues'] as $index => $issue) {
 | 
						|
            $issueId = $issue['id'];
 | 
						|
 | 
						|
            // Get the initial worklog data to determine total number of worklogs
 | 
						|
            $promises[$index] = $this->client->getAsync("/rest/api/3/issue/{$issueId}/worklog", [
 | 
						|
                'query' => [
 | 
						|
                    'startAt' => 0,
 | 
						|
                    'maxResults' => 1
 | 
						|
                ]
 | 
						|
            ])->then(function ($checkApiResponse) use ($issueId, $index) {
 | 
						|
                $checkApi = json_decode($checkApiResponse->getBody()->getContents(), true);
 | 
						|
                $maxResults = 100;
 | 
						|
                $totalWorklogs = $checkApi['total'];
 | 
						|
                return $this->client->getAsync("/rest/api/3/issue/{$issueId}/worklog", [
 | 
						|
                    'query' => [
 | 
						|
                        'startAt' => $totalWorklogs - $maxResults,
 | 
						|
                        'maxResults' => $totalWorklogs
 | 
						|
                    ]
 | 
						|
                ])->then(function ($worklogResponse) use ($index) {
 | 
						|
                    return [
 | 
						|
                        'index' => $index,
 | 
						|
                        'worklogs' => json_decode($worklogResponse->getBody()->getContents(), true)
 | 
						|
                    ];
 | 
						|
                });
 | 
						|
            });
 | 
						|
        }
 | 
						|
 | 
						|
        // Wait for all promises to complete
 | 
						|
        $results = Utils::settle($promises)->wait();
 | 
						|
 | 
						|
        // Attach worklogs to issues
 | 
						|
        foreach ($results as $result) {
 | 
						|
            if ($result['state'] === 'fulfilled') {
 | 
						|
                $data_response['issues'][$result['value']['index']]["fields"]['worklog'] = $result['value']['worklogs'];
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        return $data_response;
 | 
						|
    }
 | 
						|
 | 
						|
    public function getAllUserWorkLogs($startDate, $endDate)
 | 
						|
    {
 | 
						|
        $users = $this->getAllUsers();
 | 
						|
        $workLogs = [];
 | 
						|
 | 
						|
        foreach ($users as $user) {
 | 
						|
            $userWorkLogs = $this->getUserWorkLogs($user['accountId'], $startDate, $endDate);
 | 
						|
            $assignTask = [];
 | 
						|
            if (count($userWorkLogs['issues']) > 0) {
 | 
						|
                $assignTask = $this->getIssuesByUser($user['accountId']);
 | 
						|
                $workLogs[] = ['username' => $user['displayName'], 'information' => $userWorkLogs, 'tasksAssign' => $assignTask, 'user' => $user];
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        return $workLogs;
 | 
						|
    }
 | 
						|
}
 |