From 394d410d60286cb96388d1d46ef28a7e6b387b3d Mon Sep 17 00:00:00 2001
From: Christopher Bohn <bohn@unl.edu>
Date: Tue, 28 Apr 2020 11:35:38 -0500
Subject: [PATCH] Added option to check for proper commit message formatting

---
 api/gitlab_classes.py | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/api/gitlab_classes.py b/api/gitlab_classes.py
index 38368a5..d41c96a 100644
--- a/api/gitlab_classes.py
+++ b/api/gitlab_classes.py
@@ -1,4 +1,5 @@
 from datetime import datetime
+from functools import reduce
 from typing import ClassVar, Dict, Iterable, List, Optional, Set, Union
 
 from gitlab import Gitlab, MAINTAINER_ACCESS
@@ -229,6 +230,30 @@ class GitlabCommit:
                 deletions += diff['-']
         return max(insertions, deletions)
 
+    @staticmethod
+    def _number_of_lines_too_long(lines, subject_line_length, message_line_length):
+        # noinspection PyUnusedLocal
+        lines_too_long: int
+        if len(lines) == 1:
+            lines_too_long = 0 if len(lines[0]) <= message_line_length else 1
+        else:
+            lines_too_long = 0 if len(lines[0]) <= subject_line_length else 1
+            lines_too_long += reduce((lambda x, y: x + y),
+                                     list(map(lambda line: 0 if len(line) <= message_line_length else 1, lines[1:])))
+        return lines_too_long
+
+    @staticmethod
+    def _has_blank_line_after_subject(lines):
+        if len(lines) == 1:
+            return True
+        else:
+            return lines[1] == ''
+
+    def is_well_formatted(self, subject_line_length=72, message_line_length=72) -> bool:
+        lines: List[str] = self.get_message().rstrip('\n').split('\n')
+        return self._number_of_lines_too_long(lines, subject_line_length, message_line_length) == 0 \
+            and self._has_blank_line_after_subject(lines)
+
     # git_commit fields:
     # comments
     # discussions
-- 
GitLab