class CommitBody: commitTypes = ("build", "chore", "ci", "docs", "feat", "fix", "perf", "refactor", "style", "test") def __init__(self, completeMessage): self.completeMessage = completeMessage self.setSubjectAndBody() self.setScope() self.setCommitType() def setSubjectAndBody(self): """set commit message subject and body (not in use yet)""" try: # find start point of commit message subStart = self.completeMessage.index(": ")+2 except: try: subStart = self.completeMessage.index("\n") except: subStart = 0 try: # find end point of commit message subEnd = self.completeMessage.index("\n") except: self.subject = self.completeMessage[subStart:] self.body = None else: self.subject = self.completeMessage[subStart:subEnd] self.body = self.completeMessage[subEnd:].strip() def setScope(self): """set commit scope if contained in message""" try: start = self.completeMessage.index("(")+1 end = self.completeMessage.index("):") except: self.scope = None else: self.scope = self.completeMessage[start:end].strip().lower() def setCommitType(self): """set commit type according to type list from conventional commits""" for commitType in self.commitTypes: if (self.completeMessage.startswith((commitType + ": "))) or (self.completeMessage.startswith((commitType + "("))) or (self.completeMessage.startswith((commitType + " ("))): self.commitType = commitType break else: self.commitType = "nonconform" def getCommitMessageWithType(self): """get commit type and message subject as concatenated string""" return self.commitType + ": " + self.subject