gitea_issues_exporter/app/Http/Controllers/GiteaApiController/GiteaExport.php

92 lines
4.0 KiB
PHP
Raw Normal View History

2024-07-22 14:17:00 +02:00
<?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;
2024-08-12 15:55:03 +02:00
use App\Http\Controllers\ThirdPartyServices\ThirdPartyServices;
2024-07-22 14:17:00 +02:00
use OwenVoke\Gitea\Client;
use Illuminate\Support\Facades\Config;
2024-07-22 14:17:00 +02:00
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
)
2024-07-22 14:17:00 +02:00
{
$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);
2024-07-22 14:17:00 +02:00
$this->issue_validation_controller = new IssueValidationController();
$this->csv_controller = new CsvController($this->calculate_agent_cost);
2024-07-22 14:17:00 +02:00
$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);
2024-07-22 14:17:00 +02:00
}
private function handle_repos(string $from_date, string $to_date, array $repositories, array $issues_params)
{
2024-07-22 14:17:00 +02:00
$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);
2024-07-22 14:17:00 +02:00
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) {
2024-07-22 14:17:00 +02:00
$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']);
2024-07-22 14:17:00 +02:00
readfile($csv_file['file_path']);
unlink($csv_file['file_path']);
return true;
}
return false;
}
}