public function create(\Request $request) { $data = Request::json()->all(); $issue_service = new IssueService(app(ConfigurationInterface::class)); $url_parts = parse_url($data['worklog']['self']); $uri = $url_parts['path']; $regex = '~^/rest/api/./issue/(?P<issue_id>.*?)/(?P<ignore>.*?)/?$~'; $matched = preg_match($regex, $uri, $matches); if (!$matched) { return null; } $issue = $issue_service->get($matches['issue_id']); $project_key = $issue->fields->project->key; $project = Project::whereJiraKey($project_key)->first(); $employee = Staff::whereUserName($data['worklog']['author']['name'])->first(); //return $project; $time_spent = $data['worklog']['timeSpentSeconds']; $started = $data['worklog']['started']; $timelog_jira_id = $data['worklog']['id']; $timelog = Timelog::whereJiraId($timelog_jira_id)->first(); if ($timelog == null) { $timelog = new Timelog(); } $timelog->project_id = $project->id; $timelog->staff_id = $employee->id; $timelog->started = $started; $timelog->time_spent = $time_spent; $timelog->jira_id = $timelog_jira_id; $timelog->save(); //debugInfo($timelog); return $timelog; }
/** * Execute the console command. * @return mixed */ public function handle() { $client = new Client(); //Call to EMS API to get the employee details $date = Carbon::yesterday()->format('Y-m-d'); //$date = '2016-03-03'; $ems_data = $client->get(env(APP_HOST) . "/" . EMS_API_PATH . 'employee_list/' . $date); if ($ems_data->getStatusCode() == STATUS_OK) { $data = json_decode($ems_data->getBody()->getContents()); foreach ($data as $key => $ems) { $staff = Staff::whereEmail($ems->employee_email_id)->first(); if (!empty($staff)) { //Find JIRA hours $worklog = Timelog::whereStaffId($staff->id)->whereStarted($date)->sum('time_spent'); $actual_jira_hours = gmdate('H:i:s', $worklog); $actual_ems_hours = $ems->actual_hours; //Comparing EMS and JIRA hours if ($actual_jira_hours != NULL && $actual_jira_hours != '00:00:00' && $actual_ems_hours != NULL && $actual_ems_hours != '00:00:00') { $diffrence = $actual_ems_hours - $actual_jira_hours; //IF difference is greater then 1 hour, then update EMS // Call back to EMS to mark employee as half absent $client->get(env(APP_HOST) . "/" . EMS_API_PATH . 'update_employee_timesheet/' . $ems->emp_id . '/' . $date . ($diffrence > ONE && $diffrence < FOUR) ? '/half' : '/full'); } } } } }
public function syncWithJira() { $staff_list = Staff::all(); Timelog::where('project_id', '=', $this->id)->delete(); $issue_service = new IssueService(app(ConfigurationInterface::class)); $start = 0; while (1) { try { $issues = $issue_service->search("project={$this->jira_key} and timespent>0 and updated >= {$this->start_date}", $start, 250, ["key", "worklog"]); } catch (JiraException $exception) { return false; } if (count($issues->getIssues()) == 0) { break; } $timelogs = []; foreach ($issues->getIssues() as $issue) { $jira_work_logs = $issue->fields->worklog->worklogs; foreach ($jira_work_logs as $jira_work_log) { if (!isset($jira_work_log->author)) { continue; } $employee = $staff_list->where('email', $jira_work_log->author->emailAddress)->first(); if ($employee == null) { continue; } $timelogs[] = ['project_id' => $this->id, 'staff_id' => $employee->id, 'time_spent' => $jira_work_log->timeSpentSeconds, 'jira_id' => $jira_work_log->id, 'started' => $jira_work_log->started]; } } Timelog::insert($timelogs); $start += count($issues->getIssues()); } return true; }