Skip to content

Commit

Permalink
feat: use full description when we only have 1 commit (#15)
Browse files Browse the repository at this point in the history
When you have a single commit. The body might contain additional information. So we include that information in the description as a suggestion.
  • Loading branch information
Joris Conijn authored Jan 27, 2022
1 parent 603baa7 commit 970ae39
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pull_request_codecommit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def main(repository_path: Optional[str]) -> None:
raise click.ClickException("Pull request was not created")

title = message.splitlines()[0]
description = "\n".join(message.splitlines()[1:])
description = "\n".join(message.splitlines()[1:]).lstrip("\n")
link = repo.create_pull_request(title, description)
click.echo(f"Please review/approve: {link}")
click.echo()
Expand Down
2 changes: 2 additions & 0 deletions pull_request_codecommit/aws/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def create_pull_request(
"--targets",
f"repositoryName={repository}, sourceReference={source}, destinationReference={destination}",
]
# print(command)
# return {"pullRequestId": 1}
response = self.__execute(command)
data = json.loads(response)
return data.get("pullRequest")
3 changes: 3 additions & 0 deletions pull_request_codecommit/git/commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ def __next__(self) -> Commit:
self.__index += 1
return item

def __len__(self) -> int:
return len(self.__commits)

@property
def issues(self) -> List[str]:
return list(
Expand Down
24 changes: 19 additions & 5 deletions pull_request_codecommit/git/message.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from typing import List


class Message:
Expand All @@ -14,20 +15,33 @@ def __init__(self, message: str) -> None:
for line in iter(message.splitlines()):
lines.append(line.strip(" "))

self.__lines = list(filter(None, lines))
self.__message = "\n".join(self.__lines)
self.__issue: str = ""
self.__lines: List[str] = list(filter(None, lines))

if "issue" in self.__lines[-1].lower():
self.__detect_issue(self.__lines.pop())

def __detect_issue(self, line: str) -> None:
match = re.search(r"(?i)issue(:|) (.*)", line)
self.__issue = f"{match.group(2)}" if match else ""

@property
def issue(self) -> str:
"""
Issue reference should be in the footer of the commit message
"""
match = re.search(r"(?i)issue(:|) (.*)", self.__lines[-1])
return f"{match.group(2)}" if match else ""
return self.__issue

@property
def subject(self) -> str:
"""
Subject is the first line of the commit message
"""
return self.__lines[0]
return self.__lines[0] if len(self.__lines) > 0 else ""

@property
def body(self) -> str:
"""
Body is all except the first line
"""
return "\n".join(self.__lines[1:]).lstrip("\n")
11 changes: 9 additions & 2 deletions pull_request_codecommit/pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ def title(self) -> str:

@property
def description(self) -> str:
description = list(map(lambda commit: commit.message.subject, self.__commits))
if len(self.__commits) == 1:
description = (
[self.__commits.first.message.body] if self.__commits.first else []
)
else:
description = list(
map(lambda commit: commit.message.subject, self.__commits)
)

if self.__commits.issues:
description.append(f"\nIssues: " + ", ".join(self.__commits.issues))

return "\n".join(description)
return "\n".join(description).lstrip("\n")
10 changes: 7 additions & 3 deletions tests/test_pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,20 @@
"commits, expected_title, expected_description",
[
(Commits(COMMIT_SIMPLE_MESSAGE), "feat: my first commit", ""),
(Commits(COMMIT_DETAILED_MESSAGE), "feat: my first commit", ""),
(
Commits(COMMIT_DETAILED_MESSAGE),
"feat: my first commit",
"Some additional information",
),
(
Commits(COMMIT_SIMPLE_MESSAGE_ISSUE),
"feat: my first commit (#1)",
"\nIssues: #1",
"Issues: #1",
),
(
Commits(COMMIT_DETAILED_MESSAGE_ISSUE),
"feat: my first commit (#1)",
"\nIssues: #1",
"Some additional information\n\nIssues: #1",
),
(
Commits(""),
Expand Down

0 comments on commit 970ae39

Please sign in to comment.