2023-10-05 11:54:33 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use OwenVoke\Gitea\Client;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use DateTime;
|
|
|
|
|
|
|
|
class GiteaApiController extends Controller
|
|
|
|
{
|
|
|
|
private $giteaClient;
|
|
|
|
private $organization;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->organization = getenv('GITEA_ORGANIZATION');
|
|
|
|
$this->giteaClient = new Client(null, null, getenv('GITEA_URL'));
|
|
|
|
$this->giteaClient->authenticate(getenv('GITEA_TOKEN'), null, Client::AUTH_ACCESS_TOKEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function get_repositories()
|
|
|
|
{
|
|
|
|
$repositories = $this->giteaClient->organizations()->repositories($this->organization, 1, 9999);
|
|
|
|
return $repositories;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function get_issues(string $repository, array $parameters = array())
|
|
|
|
{
|
|
|
|
return $issues = $this->giteaClient->repositories()->issues()->all($this->organization, $repository, $parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function get_issue_total_time(string $repository, int $id)
|
|
|
|
{
|
|
|
|
$times = $this->giteaClient->repositories()->issues()->times($this->organization, $repository, $id);
|
|
|
|
$count = 0;
|
|
|
|
foreach ($times as $time) {
|
|
|
|
$count += (int) $time['time'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function get_issue_labels(array $issue)
|
|
|
|
{
|
|
|
|
$labels = '';
|
|
|
|
foreach ($issue['labels'] as $label) {
|
|
|
|
$labels .= $label['name'] . ',';
|
|
|
|
}
|
|
|
|
return $labels;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function create_columns(array $issue)
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'Progetto' => $issue['repository']['name'],
|
|
|
|
'#' => $issue['number'],
|
|
|
|
'Titolo' => $issue['title'],
|
|
|
|
'URL' => $issue['html_url'],
|
|
|
|
'Aperto_il' => $issue['created_at'],
|
|
|
|
'Chiuso_il' => $issue['closed_at'],
|
|
|
|
'Etichette' => $this->get_issue_labels($issue),
|
|
|
|
'Tempo' => gmdate('H:i:s', $this->get_issue_total_time($issue['repository']['name'], $issue['number']))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function create_csv(string $file_name, array $data)
|
|
|
|
{
|
|
|
|
$f = fopen('php://output', 'w'); // Configure fopen to create, open, and write data.
|
|
|
|
|
|
|
|
fputcsv($f, array_keys($data[0])); // Add the keys as the column headers
|
|
|
|
// Loop over the array and passing in the values only.
|
|
|
|
foreach ($data as $row) {
|
|
|
|
fputcsv($f, $row);
|
|
|
|
}
|
|
|
|
fclose($f);
|
|
|
|
// tell the browser it's going to be a csv file
|
|
|
|
header('Content-Type: text/csv');
|
|
|
|
// tell the browser we want to save it instead of displaying it
|
|
|
|
header('Content-Disposition: attachment; filename="' . $file_name . '.csv";');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function date_to_datetime(string $date)
|
|
|
|
{
|
|
|
|
$date = str_replace('/', '-', $date);
|
|
|
|
$datetime = new DateTime($date);
|
|
|
|
|
|
|
|
return $datetime->format('Y-m-d H:i:s');
|
|
|
|
}
|
|
|
|
|
2023-10-11 12:06:29 +02:00
|
|
|
private function export_issues(string $from_date, string $to_date, array $issues_params)
|
2023-10-05 11:54:33 +02:00
|
|
|
{
|
|
|
|
$data = array();
|
|
|
|
$repositories = $this->get_repositories();
|
|
|
|
foreach ($repositories as $repository) {
|
|
|
|
$issues = $this->get_issues($repository['name'], $issues_params);
|
|
|
|
foreach ($issues as $issue) {
|
|
|
|
$from_datetime = $this->date_to_datetime($from_date);
|
2023-10-11 12:06:29 +02:00
|
|
|
$to_datetime = $this->date_to_datetime($to_date);
|
|
|
|
if (substr($issue['closed_at'], 0, 19) > $from_datetime && substr($issue['closed_at'], 0, 19) <= $to_datetime) {
|
2023-10-05 11:54:33 +02:00
|
|
|
$data[] = $this->create_columns($issue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-12 16:37:45 +02:00
|
|
|
if (count($data) > 0) {
|
|
|
|
$file_name = $issues_params['state']."_issues_from_".$from_date."_to_".$to_date;
|
2023-10-12 14:59:35 +02:00
|
|
|
$this->create_csv($file_name, $data);
|
|
|
|
return true;
|
|
|
|
}
|
2023-10-12 16:37:45 +02:00
|
|
|
return false;
|
2023-10-05 11:54:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function export_closed_issues(Request $req)
|
|
|
|
{
|
|
|
|
if ($req->input('token') != getenv('GITEA_TOKEN')) {
|
|
|
|
return redirect('/');
|
|
|
|
}
|
2023-10-11 12:06:29 +02:00
|
|
|
$from = $req->input('from_year') . '-' . $req->input('from_month') . '-01';
|
|
|
|
$to = $req->input('to_year') . '-' . $req->input('to_month') . '-31';
|
2023-10-12 16:37:45 +02:00
|
|
|
$res = $this->export_issues($from, $to, ['state' => $req->input('issues_type')]);
|
2023-10-05 11:54:33 +02:00
|
|
|
return view('backend', [
|
2023-10-12 16:37:45 +02:00
|
|
|
'token' => getenv('GITEA_TOKEN'),
|
|
|
|
'download' => $res
|
2023-10-05 11:54:33 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|