Skip to content

Commit

Permalink
Merge pull request #11 from RobertLD/add-support-for-defaults
Browse files Browse the repository at this point in the history
Add support for default issues
  • Loading branch information
avilaton authored Aug 25, 2023
2 parents 7fecc7f + 4654aaf commit e225906
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions add_msg_issue_prefix_hook/add_msg_issue_prefix.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ def modify_commit_message(content: str, issue_number: str,
str: modified commit message
"""
match = re.search(pattern, content)
if match:
if match := re.search(pattern, content):
return " ".join(
[
match.group().strip(),
Expand Down Expand Up @@ -74,10 +73,16 @@ def main():
default="[a-zA-Z0-9]{1,10}-[0-9]{1,5}",
help="Regex pattern describing the issue key.",
)
parser.add_argument(
"-d",
"--default",
help="Default prefix if no issue is found",
)

args = parser.parse_args()
commit_msg_filepath = args.commit_msg_filepath
template = args.template
default = args.default
insert_after = re.compile(args.insert_after)
pattern = re.compile(args.pattern)

Expand All @@ -89,12 +94,10 @@ def main():
except Exception as e:
print(e)

result = get_ticket_id_from_branch_name(pattern, branch)
issue_number = ""

if result:
if result := get_ticket_id_from_branch_name(pattern, branch):
issue_number = result.upper()

else:
issue_number = ""
with open(commit_msg_filepath, "r+") as f:
content = f.read()
content_subject = content.split("\n", maxsplit=1)[0].strip()
Expand All @@ -103,6 +106,9 @@ def main():
prefix = template.format(issue_number)
new_msg = modify_commit_message(content, prefix, insert_after)
f.write(new_msg)
elif default:
new_msg = modify_commit_message(content, default, insert_after)
f.write(new_msg)
else:
f.write(content)

Expand Down

0 comments on commit e225906

Please sign in to comment.