From 3e29e4e0ec0a45bab74d0a50cfd8016e6872833c Mon Sep 17 00:00:00 2001
From: Jan-Hendrik Willms <tleilax+github@gmail.com>
Date: Tue, 23 Aug 2022 16:54:45 +0200
Subject: [PATCH] fixes #30

---
 controllers/dashboard.php |  6 +++++-
 lib/GitlabIssue.php       | 19 ++++++++++++++++++-
 views/dashboard/index.php | 18 +++++++++++++++++-
 3 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/controllers/dashboard.php b/controllers/dashboard.php
index 8954302..c4ede9c 100644
--- a/controllers/dashboard.php
+++ b/controllers/dashboard.php
@@ -104,7 +104,11 @@ final class DashboardController extends TracToGitlab\Controller
         });
 
         return array_map(function ($issue) {
-            return new TracToGitlab\GitlabIssue($issue);
+            $mrs = [];
+            if ($issue['merge_requests_count'] > 0) {
+                $mrs = $this->gitlab->issues()->relatedMergeRequests($this->gitlabProjectId, $issue['iid']);
+            }
+            return new TracToGitlab\GitlabIssue($issue, $mrs);
         }, $issues);
     }
 
diff --git a/lib/GitlabIssue.php b/lib/GitlabIssue.php
index 0d72c60..e87f1ca 100644
--- a/lib/GitlabIssue.php
+++ b/lib/GitlabIssue.php
@@ -4,10 +4,12 @@ namespace TracToGitlab;
 final class GitlabIssue
 {
     private $issue;
+    private $mrs;
 
-    public function __construct(array $issue)
+    public function __construct(array $issue, array $mrs = [])
     {
         $this->issue = $issue;
+        $this->mrs = $mrs;
     }
 
     public function __get($offset)
@@ -17,6 +19,7 @@ final class GitlabIssue
                 return $assignee['username'];
             }, $this->issue['assignees']));
         }
+
         if ($offset === 'type') {
             if ($this->isBiest()) {
                 return 'BIEST';
@@ -29,6 +32,7 @@ final class GitlabIssue
             }
             return '?';
         }
+
         return $this->issue[$offset] ?? null;
     }
 
@@ -37,6 +41,19 @@ final class GitlabIssue
         return $this->issue['state'] !== 'opened';
     }
 
+    public function hasMergeRequests(): bool
+    {
+        return $this->issue['merge_requests_count'] > 0;
+    }
+
+    public function isMerged()
+    {
+        return $this->hasMergeRequests()
+            && count(array_filter($this->mrs, function ($mr) {
+                return $mr['state'] !== 'merged';
+            })) === 0;
+    }
+
     public function isBiest()
     {
         return in_array('BIEST', $this->issue['labels']);
diff --git a/views/dashboard/index.php b/views/dashboard/index.php
index 34066f8..32ef334 100644
--- a/views/dashboard/index.php
+++ b/views/dashboard/index.php
@@ -10,6 +10,7 @@
             <th><?= _('Issue') ?></th>
             <th><?= _('Typ') ?></th>
             <th><?= _('Status') ?></th>
+            <th><?= _('MR') ?></th>
             <th><?= _('Titel') ?></th>
             <th><?= _('Autor') ?></th>
             <th><?= _('Bearbeiter') ?></th>
@@ -25,7 +26,7 @@
     <tbody>
     <? if (!$issues): ?>
         <tr>
-            <td colspan="<?= 6 + count($mapping) ?>" style="text-align: center">
+            <td colspan="<?= 7 + count($mapping) ?>" style="text-align: center">
                 <?= _('Keine Issues für diesen Meilenstein und Typ') ?>
             </td>
         </tr>
@@ -45,6 +46,21 @@
                 open
             <? endif; ?>
             </td>
+            <td>
+            <? if (!$issue->hasMergeRequests()): ?>
+                <abbr title="<?= _('Kein MR') ?>">
+                    <?= Icon::create('decline', Icon::ROLE_STATUS_RED) ?>
+                </abbr>
+            <? elseif ($issue->isMerged()): ?>
+                <abbr title="<?= _('MR bereits gemerget') ?>">
+                    <?= Icon::create('accept', Icon::ROLE_STATUS_GREEN) ?>
+                </abbr>
+            <? else: ?>
+                <abbr title="<?= _('MR noch nicht gemerget') ?>">
+                    <?= Icon::create('date', Icon::ROLE_STATUS_YELLOW) ?>
+                </abbr>
+            <? endif; ?>
+            </td>
             <td>
                 <a href="<?= htmlReady($issue->web_url) ?>" target="_blank">
                     <?= htmlReady($issue->title) ?>
-- 
GitLab