92 lines
4.0 KiB
PHP
92 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\GiteaApiController;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Controllers\CsvController\CsvController;
|
|
use App\Http\Controllers\CsvController\CsvCostCalc\CsvCostCalc;
|
|
use App\Http\Controllers\IssueValidationController;
|
|
use App\Http\Controllers\ThirdPartyServices\ThirdPartyServices;
|
|
use OwenVoke\Gitea\Client;
|
|
use Illuminate\Support\Facades\Config;
|
|
|
|
|
|
class GiteaExport extends Controller
|
|
{
|
|
private $giteaClient;
|
|
|
|
private $issue_validation_controller;
|
|
|
|
private $gitea_fetch;
|
|
|
|
private $csv_controller;
|
|
|
|
private $third_party_integrations_allow;
|
|
private $calculate_agent_cost;
|
|
private $csv_cost_calc;
|
|
private $third_party_services;
|
|
|
|
private $email_send_allow;
|
|
|
|
public function __construct(
|
|
$calculate_agent_cost = false,
|
|
$third_party_integrations_allow = false,
|
|
$third_party_integrations_nextcloud = false,
|
|
$third_party_integrations_open_project = false,
|
|
$email_send_allow = false
|
|
)
|
|
{
|
|
|
|
$this->third_party_integrations_allow = $third_party_integrations_allow || config('app.third_party_integrations_allow', false);
|
|
$this->calculate_agent_cost = $calculate_agent_cost || config('app.gitea_calculate_agent_cost', false);
|
|
$this->email_address = config('email.email_address', false);
|
|
$this->email_prefix_subject = config('email.email_prefix_subject', false);
|
|
$gitea_url = config('app.gitea_url');
|
|
$gitea_token = config('app.gitea_token');
|
|
$this->giteaClient = new Client(null, null, $gitea_url);
|
|
$this->giteaClient->authenticate($gitea_token, null, Client::AUTH_ACCESS_TOKEN);
|
|
$this->issue_validation_controller = new IssueValidationController();
|
|
$this->csv_controller = new CsvController($this->calculate_agent_cost);
|
|
$this->gitea_fetch = new GiteaFetch();
|
|
$this->csv_cost_calc = new CsvCostCalc($this->calculate_agent_cost);
|
|
$this->third_party_services = new ThirdPartyServices($third_party_integrations_nextcloud, $third_party_integrations_open_project, $email_send_allow);
|
|
}
|
|
|
|
private function handle_repos(string $from_date, string $to_date, array $repositories, array $issues_params)
|
|
{
|
|
$data = [];
|
|
foreach ($repositories as $repository) {
|
|
$issues = $this->gitea_fetch->get_issues($repository['name'], $issues_params);
|
|
foreach ($issues as $issue) {
|
|
$issue_time = $this->gitea_fetch->get_issue_time($repository['name'], $issue['number']);
|
|
$issue_valid = $this->issue_validation_controller->handle_single_issue($from_date, $to_date, $issue, $issue_time);
|
|
if ($issue_valid) {
|
|
$company_agents = $this->calculate_agent_cost ? $this->gitea_fetch->get_company_agents() : [];
|
|
$data[] = $this->csv_controller->create_columns($issue, $issue_time, $company_agents);
|
|
}
|
|
}
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
|
|
function export_issues(string $from_date, string $to_date, array $issues_params)
|
|
{
|
|
$repositories = $this->gitea_fetch->get_repositories();
|
|
$data = $this->handle_repos($from_date, $to_date, $repositories, $issues_params);
|
|
if (count($data) > 0) {
|
|
$company_agents = $this->calculate_agent_cost ? $this->gitea_fetch->get_company_agents() : [];
|
|
$data = $this->csv_cost_calc->sum_costs($data, $company_agents);
|
|
$csv_file = $this->csv_controller->create_csv($issues_params['state'], $from_date, $to_date, $data);
|
|
if ($this->third_party_integrations_allow) {
|
|
$this->third_party_services->handle_third_party_services($csv_file['file_name'], $csv_file['file_path'], $data, $company_agents, $from_date, $to_date);
|
|
}
|
|
isset($csv_file['file_path']) ?? unlink($csv_file['file_path']);
|
|
readfile($csv_file['file_path']);
|
|
unlink($csv_file['file_path']);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|