Skip to content
Snippets Groups Projects
Commit ce8443bc authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms
Browse files

fixes #57

parent f14d4d04
No related branches found
No related tags found
1 merge request!12fixes #57
<?php
use Http\Factory\Guzzle\RequestFactory;
use Http\Factory\Guzzle\StreamFactory;
use Http\Factory\Guzzle\UriFactory;
use Psr\Container\ContainerInterface;
use Psr\Http\Client\ClientInterface;
use TracToGitlab\BytistConnector;
return [
BytistConnector::class => function () {
return new BytistConnector(Config::get()->getValue('TRAC2GITLAB_BYTIST_TOKEN'));
},
ClientInterface::class => function () {
return new GuzzleHttp\Client();
},
Gitlab\Client::class => function (ContainerInterface $container) {
$builder = new Gitlab\HttpClient\Builder(
$container->get(ClientInterface::class),
new RequestFactory(),
new StreamFactory(),
new UriFactory()
);
$client = new Gitlab\Client($builder);
$client->setUrl(Config::get()->getValue('TRAC2GITLAB_GITLAB_URL'));
$client->authenticate(
Config::get()->getValue('TRAC2GITLAB_GITLAB_TOKEN'),
Gitlab\Client::AUTH_HTTP_TOKEN
);
return $client;
},
Parsedown::class => function () {
$parsedown = new Parsedown();
$parsedown->setSafeMode(true);
return $parsedown;
},
];
......@@ -17,7 +17,7 @@ final class DashboardController extends TracToGitlab\GitlabController
PageLayout::setTitle(_('Übersicht der Issues in gitlab'));
$this->activateNavigation('dashboard');
$this->setupSidebar();
$this->setupSidebar($action);
}
private function getSelected(string $what)
......@@ -67,8 +67,10 @@ final class DashboardController extends TracToGitlab\GitlabController
public function index_action()
{
$cache_index = implode('-', ['issues', $this->getSelected('milestone'), $this->getSelected('types')]);
$this->issues = $this->cached(
implode('-', ['issues', $this->getSelected('milestone'), $this->getSelected('types')]),
$cache_index,
fn() => $this->getIssues()
);
$this->mapping = $this->getQMLabelMapping();
......@@ -77,7 +79,7 @@ final class DashboardController extends TracToGitlab\GitlabController
Sidebar::get()->addWidget(new TemplateWidget(
_('Stand der Daten'),
$this->get_template_factory()->open('sidebar'),
['time' => $this->getCachedDate('issues')]
['time' => $this->getCachedDate($cache_index)]
));
}
......@@ -99,8 +101,28 @@ final class DashboardController extends TracToGitlab\GitlabController
}
}
private function setupSidebar()
public function testsystems_action(): void
{
PageLayout::setTitle(_('Übersicht der Testsysteme'));
$this->systems = \TracToGitlab\Models\GitlabReviewApp::findBySQL('1 ORDER BY branch');
}
private function setupSidebar(string $action)
{
$views = Sidebar::get()->addWidget(new ViewsWidget());
$views->addLink(
_('Dashboard'),
$this->indexURL(),
Icon::create('list')
)->setActive($action !== 'testsystems');
$views->addLink(
_('Testsysteme'),
$this->testsystemsURL(),
Icon::create('computer')
)->setActive($action === 'testsystems');
if ($action !== 'testsystems') {
$options = Sidebar::get()->addWidget(new OptionsWidget());
$options->addSelect(
_('Meilenstein'),
......@@ -122,6 +144,7 @@ final class DashboardController extends TracToGitlab\GitlabController
$this->getSelected('types')
);
}
}
private function getIssues(): array
{
......@@ -148,13 +171,16 @@ final class DashboardController extends TracToGitlab\GitlabController
return $a['id'] - $b['id'];
});
return array_map(function (array $issue): GitlabIssue {
return array_map(
function (array $issue): GitlabIssue {
$mrs = $this->gitlab->issues()->closedByMergeRequests(
$this->gitlab_project_id,
$issue['iid']
);
return new GitlabIssue($issue, $mrs);
}, $issues);
},
$issues
);
}
private function getMilestones()
......
<?php
namespace TracToGitlab;
use DI\NotFoundException;
use Psr\Container\ContainerInterface;
final class CombinedContainer implements ContainerInterface
{
private array $containers = [];
public function __construct(ContainerInterface ...$containers)
{
$this->containers = $containers;
}
public function get(string $id)
{
foreach ($this->containers as $container) {
if (!$container->has($id)) {
continue;
}
return $container->get($id);
}
throw new NotFoundException();
}
public function has(string $id)
{
foreach ($this->containers as $container) {
if (!$container->has($id)) {
return true;
}
}
return false;
}
public function make($name, array $parameters = [])
{
foreach ($this->containers as $container) {
if ($container->has($name)) {
return $this->doMake($container, $name, $parameters);
}
}
throw new \Exception('Nope');
}
private function doMake($container, $name, $parameters)
{
return $container->make($name, $parameters);
}
}
......@@ -3,7 +3,7 @@ namespace TracToGitlab;
use Config;
use Gitlab\Api\AbstractApi;
use Gitlab\Client as GitlabClient;
use Gitlab\Client;
use Gitlab\ResultPager;
use StudipCacheFactory;
use TracToGitlab\Traits\Cached;
......@@ -13,7 +13,7 @@ abstract class GitlabController extends Controller
use Cached;
protected int $gitlab_project_id;
protected GitlabClient $gitlab;
protected Client $gitlab;
public function before_filter(&$action, &$args)
{
......@@ -22,7 +22,7 @@ abstract class GitlabController extends Controller
$this->setCache(StudipCacheFactory::getCache(), 'gitlab/');
$this->gitlab_project_id = Config::get()->getValue('TRAC2GITLAB_GITLAB_PROJECT_ID');
$this->gitlab = app(GitlabClient::class);
$this->gitlab = app(Client::class);
}
protected function fetchAll(AbstractApi $api, string $method, array $parameters = []): array
......
......@@ -109,15 +109,10 @@ final class GitlabIssue implements \JsonSerializable
{
$mr_branches = array_column($this->mrs, 'source_branch');
$mr_branches = array_unique($mr_branches);
$mr_branches = array_filter($mr_branches, function (string $branch): bool {
return 0 < GitlabReviewApp::countBySql(
return GitlabReviewApp::findAndMapBySQL(
fn(GitlabReviewApp $app): string => $app->url,
'branch = ? AND chdate < UNIX_TIMESTAMP(NOW() - INTERVAL 2 MINUTE)',
[$branch]
);
});
return array_map(
fn(string $branch) => "https://{$branch}.studip.bytist.de",
$mr_branches
[$mr_branches]
);
}
......
<?php
namespace TracToGitlab\Models;
/**
* @property string $id
* @property string $branch
* @property int $mkdate
* @property int $chdate
*
* @property-read string $url
*/
final class GitlabReviewApp extends \SimpleORMap
{
protected static function configure($config = [])
{
$config['db_table'] = 'gitlab_review_apps';
$config['additional_fields'] = [
'url' => [
'get' => fn(GitlabReviewApp $app): string => "https://{$app->branch}.studip.bytist.de",
]
];
parent::configure($config);
}
}
<?php
namespace TracToGitlab;
use Config;
use Gitlab;
use GuzzleHttp\Client as GuzzleClient;
use Http\Factory\Guzzle\RequestFactory;
use Http\Factory\Guzzle\StreamFactory;
use Http\Factory\Guzzle\UriFactory;
use Parsedown;
use Psr\Http\Client\ClientInterface as HttpClientInterface;
use DI;
use Studip\DIContainer;
use StudIPPlugin;
......@@ -16,34 +9,17 @@ abstract class Plugin extends StudIPPlugin
{
protected function setupDiContainer()
{
$container = DIContainer::getInstance();
$container->set(BytistConnector::class, function () {
return new BytistConnector(Config::get()->getValue('TRAC2GITLAB_BYTIST_TOKEN'));
});
$container->set(HttpClientInterface::class, function () {
return new GuzzleClient();
});
$container->set(Gitlab\Client::class, function (HttpClientInterface $client) {
$builder = new Gitlab\HttpClient\Builder(
$client,
new RequestFactory(),
new StreamFactory(),
new UriFactory()
);
$builder = new DI\ContainerBuilder();
$builder->useAutowiring(false);
$builder->addDefinitions(__DIR__ . '/../bootstrap-definitions.php');
$container = $builder->build();
$client = new Gitlab\Client($builder);
$client->setUrl(Config::get()->getValue('TRAC2GITLAB_GITLAB_URL'));
$client->authenticate(
Config::get()->getValue('TRAC2GITLAB_GITLAB_TOKEN'),
Gitlab\Client::AUTH_HTTP_TOKEN
$combinedContainer = new CombinedContainer(
$container,
DIContainer::getInstance()
);
return $client;
});
$container->set(Parsedown::class, function () {
$parsedown = new Parsedown();
$parsedown->setSafeMode(true);
return $parsedown;
});
DIContainer::setInstance($combinedContainer);
}
public function perform($unconsumed_path)
......
<?php
/**
* @var \TracToGitlab\Models\GitlabReviewApp[] $systems
*/
?>
<table class="default">
<thead>
<tr>
<th><?= _('Name/Branch') ?></th>
<th><?= _('Erstellt') ?></th>
<th><?= _('Geändert') ?></th>
</tr>
</thead>
<tbody>
<? foreach ($systems as $system): ?>
<tr>
<td>
<a href="<?= htmlReady($system->url) ?>" target="_blank">
<?= htmlReady($system->branch) ?>
</a>
</td>
<td><?= strftime('%x %X', $system->mkdate) ?></td>
<td><?= strftime('%x %X', $system->chdate) ?></td>
</tr>
<? endforeach; ?>
</tbody>
</table>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment