![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/ts.corals.io/corals-api/Corals/modules/Timesheet/Jobs/ |
<?php namespace Corals\Modules\Timesheet\Jobs; use Carbon\Carbon; use Corals\Modules\Timesheet\Integration\JIRA\Requests\GetIssue; use Corals\Modules\Timesheet\Models\Entry; use Corals\Modules\Timesheet\Models\Project; use Corals\Modules\Utility\Models\Webhook\Webhook; use Corals\User\Models\User; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; class HandleJiraEvent implements ShouldQueue { use Queueable; /** * @var Webhook */ protected $webhook; /** * @var GetIssue */ protected $getIssueRequest; /** * constructor. * @param Webhook $webhook * @param GetIssue $getIssue */ public function __construct(Webhook $webhook) { $this->webhook = $webhook; } /** * Execute the job. */ public function handle() { logger("======================================================"); logger("HandleJiraWorklog Job"); logger("{$this->webhook->event_name}::{$this->webhook->id} Job Started"); logger("------------------------------------------------------"); try { $webhook = $this->webhook; if ($webhook->status == 'processed') { $this->webhook->saveException(new \Exception(trans( 'Utility::exceptions.webhook.already_processed', ['name' => $webhook->event_name, 'id' => $webhook->id] ))); return; } $payload = $webhook->payload; switch (data_get($payload, 'webhookEvent')) { case 'worklog_created': case 'worklog_updated': case 'worklog_deleted': $this->handleWorklog($payload); break; default: throw new \Exception('The Payload Not Handled'); } $webhook->markAs('processed'); } catch (\Exception $exception) { if ($exception->getCode() === 404) { $webhook->exception = [ 'message' => $exception->getMessage(), ]; $webhook->markAs('processed'); } else { $this->webhook->saveException($exception); logger("{$this->webhook->event_name}::{$this->webhook->id} Exception"); logger('Exception: ' . $exception->getMessage()); } } finally { logger("------------------------------------------------------"); logger("{$this->webhook->event_name}::{$this->webhook->id} Job Ended"); logger("HandleJiraWorklog Job Ended"); logger("======================================================"); } } protected function handleWorklog($payload) { $worklogLink = data_get($payload, 'worklog.self'); //get the jira host from the worklog link $host = $this->getHostFromURL($worklogLink); $this->getIssueRequest = new GetIssue($host); $response = $this->getIssueRequest->handle(data_get($payload, 'worklog.issueId')); $jiraProjectId = data_get($response, 'fields.project.key'); $project = Project::query() ->where('integration_id', $jiraProjectId) ->first(); if (!$project) { throw new \Exception('Project with code [' . $jiraProjectId . '] not found!'); } $workLogId = data_get($payload, 'worklog.id'); if ($payload['webhookEvent'] == 'worklog_deleted') { Entry::query()->where('integration_id', $workLogId)->delete(); return; } $worklog = collect(data_get($response, 'fields.worklog.worklogs')) ->where('id', $workLogId) ->first(); if (!$worklog) { throw new \Exception("Work log not found in the issue work logs", 404); } $timeSpentSeconds = $worklog['timeSpentSeconds']; $totalHours = intval($timeSpentSeconds / 3600); $totalMinutes = intval($timeSpentSeconds % 3600 / 60); $spentAt = Carbon::parse($worklog['started']); $userEmail = data_get($worklog, 'author.emailAddress'); $user = User::query() ->where('email', $userEmail) ->first(); if (!$user) { throw new \Exception('User with email [' . $userEmail . '] not found!'); } if (!$user->defaultActivity) { throw new \Exception('User with email [' . $userEmail . '] does not have a default activity'); } $entryDataToInsert = [ 'project_id' => $project->id, 'user_id' => $user->id, 'client_id' => $project->client_id, 'integration_id' => $workLogId, 'hours' => $totalHours, 'evaluation_hours' => $totalHours, 'minutes' => $totalMinutes, 'evaluation_minutes' => $totalMinutes, 'spent_at' => $spentAt, 'activity_id' => $user->defaultActivity->id, 'description' => sprintf("%s\n%s\n%s", $response['key'], data_get($response, 'fields.summary'), $response['self']) ]; Entry::query()->where('integration_id', $workLogId)->delete(); Entry::create($entryDataToInsert); } protected function getHostFromURL($worklogLink) { $host = parse_url($worklogLink, PHP_URL_HOST); if (!$host) { return null; } $scheme = parse_url($worklogLink, PHP_URL_SCHEME); if (!$scheme) { $scheme = 'https'; } $port = parse_url($worklogLink, PHP_URL_PORT); return sprintf("%s://%s%s", $scheme, $host, ($port ? (':' . $port) : '')); } }