Skip to content

Commit

Permalink
feat: clean up source code with dedent
Browse files Browse the repository at this point in the history
  • Loading branch information
beckermr committed Sep 30, 2024
1 parent 3b26449 commit b9a4154
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 47 deletions.
26 changes: 15 additions & 11 deletions conda_forge_webservices/github_actions_integration/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from .utils import (
comment_and_push_if_changed,
dedent_with_escaped_continue,
flush_logger,
mark_pr_as_ready_for_review,
)
Expand Down Expand Up @@ -94,17 +95,20 @@ def _push_rerender_changes(
pr_repo,
repo_name,
):
more_info_message = """\
\nThe following suggestions might help debug any issues:
* Is the `recipe/{{meta.yaml,recipe.yaml}}` file valid?
* If there is a `recipe/conda-build-config.yaml` file in the feedstock make sure
that it is compatible with the current [global pinnnings]({}).
* Is the fork used for this PR on an organization or user GitHub account? \
Automated rerendering via the webservices admin bot only works for user \
GitHub accounts.
""".format(
"https://github.com/conda-forge/conda-forge-pinning-feedstock/"
"blob/master/recipe/conda_build_config.yaml"
more_info_message = "\n" + dedent_with_escaped_continue(
"""
The following suggestions might help debug any issues:
* Is the `recipe/{{meta.yaml,recipe.yaml}}` file valid?
* If there is a `recipe/conda-build-config.yaml` file in \\
the feedstock make sure that it is compatible with the current \\
[global pinnnings]({}).
* Is the fork used for this PR on an organization or user GitHub account? \\
Automated rerendering via the webservices admin bot only works for user \\
GitHub accounts.
""".format(
"https://github.com/conda-forge/conda-forge-pinning-feedstock/"
"blob/master/recipe/conda_build_config.yaml"
)
)
if rerender_error:
if info_message is None:
Expand Down
99 changes: 63 additions & 36 deletions conda_forge_webservices/github_actions_integration/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
import sys
import textwrap

import requests
from git import GitCommandError
Expand All @@ -14,6 +15,31 @@ def get_gha_run_link():
return f"https://github.com/conda-forge-webservices/actions/runs/{run_id}"


def dedent_with_escaped_continue(text):
"""This routine dedents `text` using `textwrap.dedent`.
Any line that ends with an escaped backslash is concatenated with the next line.
"""
text = textwrap.dedent(text)
msg = ""
for line in text.splitlines():
if not msg and not line.strip():
continue
line = line.rstrip()
if msg.endswith("\\"):
msg = msg[:-2]
msg += " "
msg += line
else:
if msg:
msg += "\n"
msg += line

if not msg.endswith("\n"):
msg += "\n"
return msg


def comment_and_push_if_changed(
*,
action,
Expand Down Expand Up @@ -53,50 +79,51 @@ def comment_and_push_if_changed(
except GitCommandError as e:
push_error = True
LOGGER.critical(repr(e))
message = f"""\
Hi! This is the friendly automated conda-forge-webservice.
I tried to {action} for you, but it looks like I wasn't \
able to push to the `{pr_branch}` \
branch of `{pr_owner}`/`{pr_repo}`. Did you check the "Allow edits from \
maintainers" box?
**NOTE**: Our webservices cannot push to PRs from organization accounts \
or PRs from forks made from \
organization forks because of GitHub \
permissions. Please fork the feedstock directly from conda-forge \
into your personal GitHub account.
"""
message = dedent_with_escaped_continue(f"""
Hi! This is the friendly automated conda-forge-webservice.
I tried to {action} for you, but it looks like I wasn't \\
able to push to the `{pr_branch}` \\
branch of `{pr_owner}`/`{pr_repo}`. Did you check the "Allow \\
edits from maintainers" box?
**NOTE**: Our webservices cannot push to PRs from organization \\
accounts or PRs from forks made from \\
organization forks because of GitHub \\
permissions. Please fork the feedstock directly from conda-forge \\
into your personal GitHub account.
""")
finally:
git_repo.remotes.origin.set_url(
f"https://github.com/{pr_owner}/{pr_repo}.git",
push=True,
)
else:
if error:
message = f"""\
Hi! This is the friendly automated conda-forge-webservice.
I tried to {action} for you but ran into some issues. \
Please check the output logs of the GitHub actions workflow below for more details. \
You can also ping conda-forge/core for further assistance{help_message}.
"""
message = dedent_with_escaped_continue(f"""
Hi! This is the friendly automated conda-forge-webservice.
I tried to {action} for you but ran into some issues. \\
Please check the output logs of the GitHub actions workflow \\
below for more details. You can also ping conda-forge/core \\
for further assistance{help_message}.
""")
else:
message = f"""\
Hi! This is the friendly automated conda-forge-webservice.
message = dedent_with_escaped_continue(f"""
Hi! This is the friendly automated conda-forge-webservice.
I tried to {action} for you, but it looks like there was nothing to do.
"""
I tried to {action} for you, but it looks like there was nothing to do.
""")
if close_pr_if_no_changes_or_errors:
message += "\nI'm closing this PR!"

if info_message:
if message is None:
message = f"""\
Hi! This is the friendly automated conda-forge-webservice.
message = dedent_with_escaped_continue(f"""
Hi! This is the friendly automated conda-forge-webservice.
{info_message}
"""
{info_message}
""")
else:
message += "\n" + info_message

Expand All @@ -120,13 +147,13 @@ def mark_pr_as_ready_for_review(pr):
if not pr.draft:
return True

mutation = f"""\
mutation {{
markPullRequestReadyForReview(input:{{pullRequestId: "{pr.node_id:s}"}}) {{
pullRequest{{id, isDraft}}
}}
}}
"""
mutation = textwrap.dedent(f"""
mutation {{
markPullRequestReadyForReview(input:{{pullRequestId: "{pr.node_id:s}"}}) {{
pullRequest{{id, isDraft}}
}}
}}
""")

token = os.environ["GH_TOKEN"]
headers = {"Authorization": f"bearer {token}"}
Expand Down

0 comments on commit b9a4154

Please sign in to comment.