121 lines
3.9 KiB
PHP
121 lines
3.9 KiB
PHP
<?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";');
|
|
exit();
|
|
}
|
|
|
|
private function date_to_datetime(string $date)
|
|
{
|
|
$date = str_replace('/', '-', $date);
|
|
$datetime = new DateTime($date);
|
|
|
|
return $datetime->format('Y-m-d H:i:s');
|
|
}
|
|
|
|
private function export_issues(string $from_date, array $issues_params)
|
|
{
|
|
$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);
|
|
if (substr($issue['closed_at'], 0, 19) > $from_datetime) {
|
|
$data[] = $this->create_columns($issue);
|
|
}
|
|
}
|
|
}
|
|
|
|
$file_name = date('Y_F', strtotime(explode(' ', $data[0]['Chiuso_il'])[0]));
|
|
$this->create_csv($file_name, $data);
|
|
}
|
|
|
|
public function export_closed_issues(Request $req)
|
|
{
|
|
if ($req->input('token') != getenv('GITEA_TOKEN')) {
|
|
return redirect('/');
|
|
}
|
|
$date = $req->input('year') . '-' . $req->input('month') . '-01';
|
|
$this->export_issues($date, ['state' => 'closed']);
|
|
return view('backend', [
|
|
'token' => getenv('GITEA_TOKEN')
|
|
]);
|
|
}
|
|
}
|