Skip to content

Commit

Permalink
introduce generic print_dry_run_message for printing dry run messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ytausch committed Oct 18, 2024
1 parent 2617679 commit c37ea4f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 27 deletions.
62 changes: 39 additions & 23 deletions conda_forge_tick/git_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,31 @@ def _sync_default_branch(self, upstream_owner: str, upstream_repo: str):
def user(self) -> str:
return self._USER

@staticmethod
def print_dry_run_message(title: str, data: dict[str, str]):
"""
Print a dry run output message.
:param title: The title of the message.
:param data: The data to print. The keys are the field names and the values are the field values.
Please capitalize the keys for consistency.
"""
border = "=============================================================="
output = textwrap.dedent(
f"""
{border}
Dry Run: {title}"""
)

def format_field(key: str, value: str) -> str:
if "\n" in value:
return f"{key}:\n{value}"
return f"{key}: {value}"

output += "".join(format_field(key, value) for key, value in data.items())
output += f"\n{border}"

logger.debug(output)

def create_pull_request(
self,
target_owner: str,
Expand All @@ -1059,19 +1084,15 @@ def create_pull_request(
title: str,
body: str,
) -> PullRequestDataValid:
debug_out = textwrap.dedent(
f"""
==============================================================
Dry Run: Create Pull Request
Title: "{title}"
Target Repository: {target_owner}/{target_repo}
Branches: {self.user}:{head_branch} -> {target_owner}:{base_branch}
Body:
"""
self.print_dry_run_message(
"Create Pull Request",
{
"Title": f'"{title}"',
"Target Repository": f"{target_owner}/{target_repo}",
"Branches": f"{self.user}:{head_branch} -> {target_owner}:{base_branch}",
"Body": body,
},
)
debug_out += body
debug_out += "\n=============================================================="
logger.debug(debug_out)

now = datetime.now()
return PullRequestDataValid.model_validate(
Expand Down Expand Up @@ -1100,17 +1121,12 @@ def comment_on_pull_request(
f"Repository {repo_owner}/{repo_name} not found."
)

logger.debug(
textwrap.dedent(
f"""
==============================================================
Dry Run: Comment on Pull Request
Pull Request: {repo_owner}/{repo_name}#{pr_number}
Comment:
{comment}
==============================================================
"""
)
self.print_dry_run_message(
"Comment on Pull Request",
{
"Pull Request": f"{repo_owner}/{repo_name}#{pr_number}",
"Comment": comment,
},
)


Expand Down
12 changes: 8 additions & 4 deletions tests/test_git_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1690,13 +1690,15 @@ def test_dry_run_backend_create_pull_request(caplog):
backend = DryRunBackend()
caplog.set_level(logging.DEBUG)

body_text = "This is a long\nbody text"

pr_data = backend.create_pull_request(
"conda-forge",
"pytest-feedstock",
"BASE_BRANCH",
"HEAD_BRANCH",
"TITLE",
"BODY_TEXT",
body_text,
)

# caplog validation
Expand All @@ -1707,7 +1709,7 @@ def test_dry_run_backend_create_pull_request(caplog):
f"Branches: {backend.user}:HEAD_BRANCH -> conda-forge:BASE_BRANCH"
in caplog.text
)
assert "Body:\nBODY_TEXT" in caplog.text
assert f"Body:\n{body_text}" in caplog.text

# pr_data validation
assert pr_data.e_tag == "GITHUB_PR_ETAG"
Expand All @@ -1727,15 +1729,17 @@ def test_dry_run_backend_comment_on_pull_request(caplog):
backend = DryRunBackend()
caplog.set_level(logging.DEBUG)

comment_text = "This is a long\ncomment text"

backend.comment_on_pull_request(
"conda-forge",
"pytest-feedstock",
1337,
"COMMENT",
comment_text,
)

assert "Comment on Pull Request" in caplog.text
assert "Comment:\nCOMMENT" in caplog.text
assert f"Comment:\n{comment_text}" in caplog.text
assert "Pull Request: conda-forge/pytest-feedstock#1337" in caplog.text


Expand Down

0 comments on commit c37ea4f

Please sign in to comment.