This commit is contained in:
Michael 2024-07-22 14:17:00 +02:00
parent a15319c4d1
commit eebf859afa
39 changed files with 2742 additions and 937 deletions

View file

@ -0,0 +1,31 @@
<?php
namespace App\Http\Controllers\GiteaApiController;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class GiteaApiController extends Controller
{
private $gitea_export;
public function __construct()
{
$this->gitea_export = new GiteaExport();
}
public function export_closed_issues(Request $req)
{
if ($req->input('token') != getenv('GITEA_TOKEN')) {
return redirect('/');
}
$from = $req->input('from_year') . '-' . $req->input('from_month') . '-01';
$to = $req->input('to_year') . '-' . $req->input('to_month') . '-31';
$res = $this->gitea_export->export_issues($from, $to, ['state' => $req->input('issues_type')]);
return view('backend', [
'token' => getenv('GITEA_TOKEN'),
'download' => $res
]);
}
}

View file

@ -0,0 +1,80 @@
<?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;
use Illuminate\Support\Facades\Storage;
use OwenVoke\Gitea\Client;
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()
{
$this->third_party_integrations_allow = env('THIRD_PARTY_INTEGRATIONS_ALLOW', false);
$this->calculate_agent_cost = env('GITEA_CALCULATE_AGENT_COST', false);
$this->email_send_allow = env('EMAIL_SEND_ALLOW', false);
$this->email_address = env('EMAIL_ADDRESS', false);
$this->email_prefix_subject = env('EMAIL_PREFIX_SUBJECT', false);
$this->giteaClient = new Client(null, null, getenv('GITEA_URL'));
$this->giteaClient->authenticate(getenv('GITEA_TOKEN'), null, Client::AUTH_ACCESS_TOKEN);
$this->issue_validation_controller = new IssueValidationController();
$this->csv_controller = new CsvController();
$this->gitea_fetch = new GiteaFetch();
$this->csv_cost_calc = new CsvCostCalc();
$this->third_party_services = new ThirdPartyServices();
}
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;
}
}

View file

@ -0,0 +1,61 @@
<?php
namespace App\Http\Controllers\GiteaApiController;
use App\Http\Controllers\Controller;
use App\Http\Controllers\CsvController\CsvController;
use App\Http\Controllers\IssueValidationController;
use GuzzleHttp\Client as GuzzleClient;
use Illuminate\Http\Request;
use OwenVoke\Gitea\Client;
class GiteaFetch extends Controller
{
private $giteaClient;
private $partner_organization;
private $main_company_organization;
public function __construct()
{
$this->main_company_organization = getenv('GITEA_MAIN_COMPANY_ORGANIZATION');
$this->partner_organization = getenv('GITEA_PARTNER_ORGANIZATION');
$this->gitea_url = getenv('GITEA_URL');
$this->gitea_token = getenv('GITEA_TOKEN');
$this->giteaClient = new Client(null, null, $this->gitea_url);
$this->giteaClient->authenticate($this->gitea_token, null, Client::AUTH_ACCESS_TOKEN);
}
function get_repositories()
{
$repositories = $this->giteaClient->organizations()->repositories($this->partner_organization, 1, 9999);
return $repositories;
}
function get_issues(string $repository, array $parameters = array())
{
return $this->giteaClient->repositories()->issues()->all($this->partner_organization, $repository, $parameters);
}
function get_issue_time(string $repository_name, int $issue_number){
return $this->giteaClient->repositories()->issues()->times($this->partner_organization, $repository_name, $issue_number);
}
function get_company_agents()
{
$gitea_api_call_url = $this->gitea_url . '/api/v1/orgs/' . $this->main_company_organization . '/members';
$http_client = new GuzzleClient();
$res = $http_client->get($gitea_api_call_url, [
'headers' => [
'Authorization' => 'token ' . $this->gitea_token
]
]);
$responseBody = $res->getBody()->getContents();
$users = json_decode($responseBody, true);
//removes 1st user, usually it's the admin
array_shift($users);
$users = array_column($users, 'username');
return $users;
}
}