Commit 24e82ae1 authored by Stefan Busemann's avatar Stefan Busemann

Merge branch '461-provide-information-about-contributions-for-dad-blog-posts' into 'develop'

Add possibility to fetch contributor information

Closes #461

See merge request !338
parents cf92516f b7bcf0d2
Pipeline #8368 passed with stages
in 4 minutes and 5 seconds
<?php
namespace T3o\T3orgLayout\Middleware;
/*
* This file is part of a TYPO3 extension.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
class Contributions implements \Psr\Http\Server\MiddlewareInterface
{
protected $notListedIds = [106];
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if (!isset($request->getQueryParams()['contributions'])) {
return $handler->handle($request);
}
$response = new \GuzzleHttp\Psr7\Response(200, ['Content-Type' => 'application/json']);
$contributions = [];
$day = $request->getQueryParams()['d'] ?: 1;
$month = $request->getQueryParams()['m'] ?: date('m');
$year = $request->getQueryParams()['y'] ?: date('Y');
$apiToken = $request->getParsedBody()['token'];
if (!$apiToken) {
$response->getBody()->write(json_encode(['result' => 'error', 'error' => 'No required "token" provided.']));
return $response;
}
$beginDate = $year . '-' . $month . '-' . $day . 'T00:00:00';
$endDate = $year . '-' . $month . '-' . ($request->getQueryParams()['d'] ?: date('t', strtotime($day . '.' . $month . '.' . $year))) . 'T23:59:59';
$gitlabClient = new \GuzzleHttp\Client(['base_uri' => 'https://gitlab.typo3.org/api/v4/']);
$this->countContributions($gitlabClient, $contributions, $beginDate, $endDate, $apiToken, 'issues');
$this->countContributions($gitlabClient, $contributions, $beginDate, $endDate, $apiToken, 'merge_requests');
$contributions['result'] = 'success';
if ($contributions['contributors']) {
foreach ($contributions['contributors'] as $id => $contributor) {
if (!in_array($id, $this->notListedIds)) {
$contributors[$id] = $contributor['user']['name'];
}
}
uasort($contributors, [$this, 'lastNameSort']);
$contributions['contributors']['nameList'] = implode(', ', $contributors);
}
$response->getBody()->write(json_encode($contributions));
return $response;
}
private function countContributions(\GuzzleHttp\Client $gitlabClient, array &$contributions, string $beginDate, string $endDate, string $apiToken, string $type): void
{
$uri = $type . '/?scope=all&created_after=' . $beginDate . '&created_before=' . $endDate;
$request = new \GuzzleHttp\Psr7\Request('GET', $uri, ['Private-Token', $apiToken]);
$response = $gitlabClient->send($request);
$data = json_decode($response->getBody(), true);
$contributions['statistics'][$type] = count($data);
foreach ($data as $item) {
if (!isset($contributions['contributors'][$item['author']['id']])) {
$contributions['contributors'][$item['author']['id']] = [
'contributions' => [
$type => 0
],
'user' => $item['author']
];
}
$contributions['contributors'][$item['author']['id']]['contributions'][$type]++;
}
}
private function lastNameSort(string $a, string $b): int
{
$aLast = end(explode(' ', $a));
$bLast = end(explode(' ', $b));
return strcasecmp($aLast, $bLast);
}
}
<?php
return [
'frontend' => [
't3org-contributions' => [
'target' => \T3o\T3orgLayout\Middleware\Contributions::class,
'before' => [
'typo3/cms-frontend/timetracker'
]
]
]
];
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment