To implement the withCoAuthors() method in the AcnCommitMessageBuilder class with the requirements you specified, we need two separate behaviors based on the type of the argument passed:
-
When a List of Strings is Passed: Each string in the list is assumed to be a user identifier in the format "User Full Name emailAddress@domain.tld". The method will directly use this list to set the
_co_authors_listattribute. -
When a Single String is Passed: The method needs to parse the string based on different possible formats:
- A single user identifier ("User Full Name emailAddress@domain.tld").
- A string joined by "," or "\n" from a list of users.
- A single-line or multi-line string with "Co-Authored-By:" on each line.
After parsing, the method will convert this into a list of user identifiers (without "Co-Authored-By:") and then proceed as in the first case.
Here is the implementation of withCoAuthors():
class AcnCommitMessageBuilder:
# ... other methods ...
def withCoAuthors(self, co_authors):
if isinstance(co_authors, list):
# Directly use the list after stripping 'Co-Authored-By:'
self._co_authors_list = [self._strip_prefix(author) for author in co_authors]
elif isinstance(co_authors, str):
# Process the string to create a list of users
processed_co_authors = self._process_co_authors_string(co_authors)
# Call the method again with the processed list
self.withCoAuthors(processed_co_authors)
return self
def _process_co_authors_string(self, co_authors_str):
# Remove 'Co-Authored-By:' if present and split by ',' or '\n'
co_authors_list = co_authors_str.replace('Co-Authored-By:', '').split(',')
co_authors_list = [author.strip() for author in co_authors_list]
co_authors_list = [author for line in co_authors_list for author in line.split('\n') if author.strip()]
return co_authors_list
def _strip_prefix(self, author):
# Utility function to strip 'Co-Authored-By:' prefix if present
return author.replace('Co-Authored-By:', '').strip()
# Example usage
builder = AcnCommitMessageBuilder()
builder.withCoAuthors("Jane Doe <jane@example.com>, John Doe <john@example.com>\nAlex Smith <alex@example.com>")
# or
builder.withCoAuthors(["Jane Doe <jane@example.com>", "John Doe <john@example.com>", "Alex Smith <alex@example.com>"])In this implementation:
- The
withCoAuthors()method checks the type ofco_authorsand processes it accordingly. _process_co_authors_string()is a helper method that processes a string containing co-authors._strip_prefix()is a utility method to remove the "Co-Authored-By:" prefix if present.
This approach ensures flexibility in how co-authors can be added to the commit message, accommodating various input formats.
================================================================
# class AcnCommitMessageBuilder:
# ... other methods ...
def build(self):
# Construct the commit message here
# Header section
header = f"{self._risk}/{self._intent} - {self._summary}"
# Description section
description = self._description
# Co-authors section
co_authors_list = []
if self._include_author_in_co_authors and self._author:
co_authors_list.append(f"Co-Authored-By: {self._author}")
co_authors_list.extend([f"Co-Authored-By: {co_author}" for co_author in self._co_authors])
co_authors_section = "\n".join(co_authors_list)
# Combine all sections into a multi-line string
commit_message_parts = [header, description, co_authors_section]
commit_message = "\n".join(part for part in commit_message_parts if part.strip())
# Adjust trailing newline
if "\n" in commit_message:
# Ensure a trailing newline for multi-line messages
commit_message += "\n"
return commit_message================================================================
class AcnCommitMessageBuilder: def init(self): self._summary = None self._description = None self._author = None self._co_authors = [] self._risk = None self._intent = None self._include_author_in_co_authors = False
def withSummary(self, summary):
self._summary = summary
return self
def withDescription(self, description):
self._description = description
return self
def withAuthor(self, author):
self._author = author
return self
def withCoAuthors(self, co_authors):
self._co_authors = co_authors
return self
def withAuthorAlsoAsCoAuthor(self):
self._include_author_in_co_authors = True
return self
def withRisk(self, risk):
self._risk = risk
return self
def withIntent(self, intent):
self._intent = intent
return self
def _loadRiskCodes(self):
# Private method implementation to load risk codes
pass
def _loadIntentCodes(self):
# Private method implementation to load intent codes
pass
def build(self):
# Implementation to return a CommitMessage object
# Example implementation could involve combining the private attributes
# to form the final commit message
# Include author in co-authors if applicable
if self._include_author_in_co_authors and self._author:
self._co_authors.append(self._author)
# Construct the commit message here
# This is just an example; modify as needed
commit_message = f"Summary: {self._summary}\nDescription: {self._description}\nAuthor: {self._author}\nCo-Authors: {', '.join(self._co_authors)}\nRisk: {self._risk}\nIntent: {self._intent}"
return commit_message
builder = AcnCommitMessageBuilder() commit_message = (builder.withSummary("Fixed bug in data processing") .withDescription("Resolved an issue causing data corruption.") .withAuthor("John Doe") .withCoAuthors(["Jane Smith", "Alex Johnson"]) .withRisk("Low") .withIntent("Bug Fix") .build())
print(commit_message)