Skip to content
Snippets Groups Projects
Select Git revision
  • main
  • issue-56
  • testing-systems
3 results

mergerequests.php

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    mergerequests.php 2.35 KiB
    <?php
    final class MergerequestsController extends TracToGitlab\GitlabController
    {
        public function before_filter(&$action, &$args)
        {
            parent::before_filter($action, $args);
    
            PageLayout::setTitle(_('Merge Requests'));
            $this->activateNavigation('merge-requests');
        }
    
        public function index_action()
        {
            $this->mrs = $this->cached('mergerequests', fn() => $this->fetchMergeRequests());
    
            Sidebar::get()->addWidget(new TemplateWidget(
                _('Aus dem Cache'),
                $this->get_template_factory()->open('sidebar'),
                ['time' => $this->getCachedDate('mergerequests')]
            ));
        }
    
        private function fetchMergeRequests(): array
        {
            $mrs = $this->fetchAll(
                $this->gitlab->mergeRequests(),
                'all',
                [
                    $this->gitlab_project_id,
                    [
                        'sort'  => 'asc',
                        'scope' => 'all',
                        'state' => 'opened',
                    ]
                ]
            );
    
            $mrs = array_filter($mrs, function ($mr) {
                if ($mr['draft'] || $mr['work_in_progress'] || $mr['merge_when_pipeline_succeeds']) {
                    return false;
                }
    
                return true;
            });
    
            $mrs = array_map(function ($mr) {
                $mr['approvals'] = $this->gitlab->mergeRequests()->approvals(
                    $this->gitlab_project_id,
                    $mr['iid']
                );
                return $mr;
            }, $mrs);
    
            usort($mrs, function ($a, $b) {
                return strcmp($b['updated_at'], $a['updated_at']);
            });
    
            return $mrs;
        }
    
        private function fetchRelatedMergeRequest(int $issue_id)
        {
            $mrs = $this->gitlab->issues()->closedByMergeRequests(
                $this->gitlab_project_id,
                $issue_id
            );
            foreach ($mrs as $mr) {
                if ($mr['state'] === 'merged') {
                    return $mr['iid'];
                }