From 398ff1150aa0334565b6aa614a08a910d238bfbc Mon Sep 17 00:00:00 2001
From: Daniel Fütterer <df89@outlook.com>
Date: Do., 28 Jan. 2021 11:54:08 +0100
Subject: [PATCH] feat: Scopes

---
 changelog_generator.py |   70 +++++++++++++++++++++++++---------
 1 files changed, 51 insertions(+), 19 deletions(-)

diff --git a/changelog_generator.py b/changelog_generator.py
index 40e877d..087dcf5 100644
--- a/changelog_generator.py
+++ b/changelog_generator.py
@@ -2,9 +2,9 @@
 # -*- coding: utf-8 -*-
 
 ##### To Do #####
-# - handle different keywords/types (what to process/ignore?)
-# - scopes
-# - at the moment the script requires a tag in the latest commit to work properly
+# - handle further keywords/types other than "feat" and "fix"
+# - deal with parameters configuring the output (dev/usr; types, hash y/n, ...)
+# Refactoring: rewrite using oop (-> UML)
 # the processing ang recognizing of the commit components should be improved
 
 import os
@@ -42,35 +42,67 @@
 
     # put commit into list
     commitHistory.append(commit)
-    
-    # handle commit types (work in progress)
-    # variant a
-    #if commit[0].startswith("feat"):
-    #    print("Feature recognized!")
-    #elif commit[0].startswith("chore"):
-    #    print("Chore recognized!")
-    # variant b
-    #for i in keywords:
-    #    if commit[0].startswith(i):
-    #        print(i)
-    #        pass
 
 
 # Grouping History using tags
-for commit in commitHistory: # latest commit must have a tag
+for commit in commitHistory:
     # Look if commit has a tag
     if (len(commit) == 3) and ("tag: v" in commit[2]):
         taggedHistory.append([commit[2][(commit[2].rfind(": v")+3):-1],commit[0:2]])
     else:
-        taggedHistory[-1].append(commit)
+        # Check if latest commit has a tag
+        if len(taggedHistory)==0:
+            # If there is no tag, create empty string instead
+            taggedHistory.append(["", commit])
+        else:
+            taggedHistory[-1].append(commit)
 
 
 # Construction of the changelog-file
 fileTemplate = ["# Changelog"]
 for tag in taggedHistory:
-    fileTemplate.append("\n## Version " + tag[0])
+    # A Dictionairy to store grouped commits
+    commitsByType = {"Fixes":[],"Features":[]}
+
+    # If latest commit has no tag:
+    if tag[0] == "":
+        fileTemplate.append("\n## No version number yet ")
+    else:
+        fileTemplate.append("\n## Version " + tag[0])
+    
+    # Grouping by type "feat" and "fix"
     for commit in tag[1:]:
-        fileTemplate.append("\t- " + commit[0] + " (" + commit[1] + ")")
+        old = commit[0]
+        if commit[0].startswith(keywords[5]): # "fix"
+            if commit[0].startswith("fix: "):
+                new = old[(old.index(":")+2):]
+                commit[0] = new
+                commitsByType["Fixes"].append(commit)
+            else:
+                new = "**" + old[(old.index("(")+1):old.index(")")] + "**: " + old[(old.index(":")+2):]
+                commit[0] = new
+                commitsByType["Fixes"].append(commit)
+        elif commit[0].startswith(keywords[4]): # "feat"
+            if commit[0].startswith("feat: "):
+                new = old[(old.index(":")+2):]
+                commit[0] = new
+                commitsByType["Features"].append(commit)
+            else:
+                new = "**" + old[(old.index("(")+1):old.index(")")] + "**: " + old[(old.index(":")+2):]
+                commit[0] = new
+                commitsByType["Features"].append(commit)
+                
+    
+    # Creating grouped output
+    if len(commitsByType["Features"]) != 0:
+        fileTemplate.append("### Features")
+        for feature in commitsByType["Features"]:
+            fileTemplate.append("- " + feature[0] + " (" + commit[1][:6] + ")")
+    if len(commitsByType["Fixes"]) != 0:
+        fileTemplate.append("### Fixes")
+        for fix in commitsByType["Fixes"]:
+            fileTemplate.append("- " + fix[0] + " (" + commit[1][:6] + ")")
+
 # write into changelog
 with open("changelog.md", "w") as file:
     for line in fileTemplate:

--
Gitblit v1.9.1