Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion eden/scm/sapling/ext/github/mock_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ def expect_update_pr_request(
owner: str = OWNER,
name: str = REPO_NAME,
stack_pr_ids: Optional[List[int]] = None,
*,
review_url: str,
review_tool: str,
) -> "UpdatePrRequest":
if not stack_pr_ids:
stack_pr_ids = [pr_number]
Expand All @@ -296,7 +299,7 @@ def expect_update_pr_request(
("" if commit_msg.endswith("\n") else "\n") + "---\n"
"[//]: # (BEGIN SAPLING FOOTER)\n"
"Stack created with [Sapling](https://sapling-scm.com). Best reviewed"
f" with [ReviewStack](https://reviewstack.dev/{owner}/{name}/pull/{pr_number}).\n"
f" with [{review_tool}]({review_url}).\n"
+ "\n".join(pr_list)
)

Expand Down
111 changes: 109 additions & 2 deletions eden/scm/sapling/ext/github/pull_request_body.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
import re
from typing import List, Tuple, Union

from sapling import error
from sapling.i18n import _

from .gh_submit import Repository

_HORIZONTAL_RULE = "---"
_SAPLING_FOOTER_MARKER = "[//]: # (BEGIN SAPLING FOOTER)"
DEFAULT_REVIEW_URL_TEMPLATE = "https://reviewstack.dev/{owner}/{repo}/pull/{number}"
DEFAULT_REVIEW_TOOL_NAME = "ReviewStack"


def create_pull_request_title_and_body(
Expand All @@ -18,6 +23,8 @@ def create_pull_request_title_and_body(
pr_numbers_index: int,
repository: Repository,
reviewstack: bool = True,
review_url_template: str = DEFAULT_REVIEW_URL_TEMPLATE,
review_tool_name: str = DEFAULT_REVIEW_TOOL_NAME,
) -> Tuple[str, str]:
r"""Returns (title, body) for the pull request.

Expand Down Expand Up @@ -84,6 +91,70 @@ def create_pull_request_title_and_body(
* __->__ #42
* #4

Customize the review link, e.g. for a self-hosted review tool:
>>> title, body = create_pull_request_title_and_body(
... commit_msg,
... pr_numbers_and_num_commits,
... pr_numbers_index,
... contributor_repo,
... review_url_template="https://review.example.com/{owner}/{repo}/{number}",
... review_tool_name="MyReview",
... )
>>> print(body)
Second line of message.
<BLANKLINE>
<BLANKLINE>
---
[//]: # (BEGIN SAPLING FOOTER)
Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [MyReview](https://review.example.com/facebook/sapling/42).
* #1
* #2 (2 commits)
* __->__ #42
* #4

A customized footer still parses as stack information:
>>> parse_stack_information(body)
[(False, 1), (False, 2), (True, 42), (False, 4)]

Customizing only the tool name keeps the default URL:
>>> title, body = create_pull_request_title_and_body(commit_msg, pr_numbers_and_num_commits,
... pr_numbers_index, contributor_repo, review_tool_name="MyReview")
>>> print(body.replace(reviewstack_url, "{reviewstack_url}"))
Second line of message.
<BLANKLINE>
<BLANKLINE>
---
[//]: # (BEGIN SAPLING FOOTER)
Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [MyReview]({reviewstack_url}).
* #1
* #2 (2 commits)
* __->__ #42
* #4

The templates are ignored when the review link is disabled entirely:
>>> title, body = create_pull_request_title_and_body(commit_msg, pr_numbers_and_num_commits,
... pr_numbers_index, contributor_repo, reviewstack=False,
... review_url_template="https://review.example.com/{owner}/{repo}/{number}",
... review_tool_name="MyReview")
>>> print(body)
Second line of message.
<BLANKLINE>
<BLANKLINE>
---
[//]: # (BEGIN SAPLING FOOTER)
* #1
* #2 (2 commits)
* __->__ #42
* #4

An invalid URL template aborts with a message naming the config:
>>> create_pull_request_title_and_body(commit_msg, pr_numbers_and_num_commits,
... pr_numbers_index, contributor_repo,
... review_url_template="https://review.example.com/{bogus}")
Traceback (most recent call last):
...
sapling.error.Abort: invalid github.pull-request-review-url-template 'https://review.example.com/{bogus}': 'bogus'

Single commit stack:
>>> title, body = create_pull_request_title_and_body("Foo", [(1, 1)], 0, contributor_repo)
>>> print(title)
Expand All @@ -108,8 +179,24 @@ def create_pull_request_title_and_body(
extra = []
if len(pr_numbers_and_num_commits) > 1:
if reviewstack:
reviewstack_url = f"https://reviewstack.dev/{owner}/{name}/pull/{pr}"
review_stack_message = f"Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack]({reviewstack_url})."
# Pull requests live in the upstream repository, so - like `owner`
# and `name` above (see get_upstream_owner_and_name()) - the
# {hostname} placeholder expands from the upstream when submitting
# from a fork. For non-fork clones, repository.hostname is the
# same host.
hostname = (
repository.upstream.hostname
if repository.upstream
else repository.hostname
)
Comment on lines +187 to +191

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you help add a comment for this logic?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added — it mirrors get_upstream_owner_and_name() just above: PRs live in the upstream repository, so {hostname} expands from the upstream when submitting from a fork; for non-fork clones it's the same host.

review_url = _format_review_url(
review_url_template,
owner=owner,
repo=name,
number=pr,
hostname=hostname,
)
review_stack_message = f"Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [{review_tool_name}]({review_url})."
extra.append(review_stack_message)
bulleted_list = "\n".join(
_format_stack_entry(pr_number, index, pr_numbers_index, num_commits)
Expand All @@ -125,6 +212,26 @@ def create_pull_request_title_and_body(
return title, body


def _format_review_url(
template: str, *, owner: str, repo: str, number: int, hostname: str
) -> str:
r"""Expands the github.pull-request-review-url-template config value.

>>> _format_review_url(DEFAULT_REVIEW_URL_TEMPLATE,
... owner="facebook", repo="sapling", number=42, hostname="github.com")
'https://reviewstack.dev/facebook/sapling/pull/42'
>>> _format_review_url("https://{hostname}/{owner}/{repo}/reviews/{number}",
... owner="facebook", repo="sapling", number=42, hostname="github.example.com")
'https://github.example.com/facebook/sapling/reviews/42'
"""
try:
return template.format(owner=owner, repo=repo, number=number, hostname=hostname)
except (IndexError, KeyError, ValueError) as e:
raise error.Abort(
_("invalid github.pull-request-review-url-template %r: %s") % (template, e)
)


_STACK_ENTRY = re.compile(r"^\* (__->__ )?#([1-9]\d*).*$")

# Pair where the first value is True if this entry was noted as the "current"
Expand Down
17 changes: 16 additions & 1 deletion eden/scm/sapling/ext/github/submit.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
from .github_repo_util import check_github_repo, GitHubRepo
from .none_throws import none_throws
from .pr_parser import get_pull_request_for_context
from .pull_request_body import create_pull_request_title_and_body, title_and_body
from .pull_request_body import (
create_pull_request_title_and_body,
DEFAULT_REVIEW_TOOL_NAME,
DEFAULT_REVIEW_URL_TEMPLATE,
title_and_body,
)
from .pullrequest import PullRequestId
from .pullrequeststore import PullRequestStore
from .run_git_command import run_git_command
Expand Down Expand Up @@ -341,12 +346,22 @@ async def rewrite_pull_request_body(
else:
commit_msg_or_title_body = head_commit_data.get_msg()

review_url_template = (
ui.config("github", "pull-request-review-url-template")
or DEFAULT_REVIEW_URL_TEMPLATE
)
review_tool_name = (
ui.config("github", "pull-request-review-tool-name")
or DEFAULT_REVIEW_TOOL_NAME
)
title, body = create_pull_request_title_and_body(
commit_msg_or_title_body,
pr_numbers_and_num_commits,
index,
repository,
reviewstack=ui.configbool("github", "pull-request-include-reviewstack"),
review_url_template=review_url_template,
review_tool_name=review_tool_name,
)

if pr.state != PullRequestState.OPEN:
Expand Down
29 changes: 27 additions & 2 deletions eden/scm/tests/github/mock_create_one_pr_placeholder_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,18 @@

from sapling import extensions
from sapling.ext.github import github_gh_cli, submit
from sapling.ext.github.mock_utils import mock_run_git_command, MockGitHubServer
from sapling.ext.github.consts import GITHUB_HOSTNAME
from sapling.ext.github.mock_utils import (
mock_run_git_command,
MockGitHubServer,
OWNER,
REPO_NAME,
)
from sapling.ext.github.pull_request_body import (
_format_review_url,
DEFAULT_REVIEW_TOOL_NAME,
DEFAULT_REVIEW_URL_TEMPLATE,
)

# An extension to mock network requests by replacing the `github_gh_cli.make_request`
# and `submit.run_git_command` with the corresponding wrapper functions. Check `uisetup`
Expand All @@ -31,7 +42,21 @@ def setup_mock_github_server() -> MockGitHubServer:
pr_id = f"PR_id_{pr_number}"
github_server.expect_get_pr_details_request(pr_number).and_respond(pr_id)

github_server.expect_update_pr_request(pr_id, pr_number, body).and_respond()
# Single-PR update: no stack footer is rendered, but the review link
# arguments are required by expect_update_pr_request.
github_server.expect_update_pr_request(
pr_id,
pr_number,
body,
review_url=_format_review_url(
DEFAULT_REVIEW_URL_TEMPLATE,
owner=OWNER,
repo=REPO_NAME,
number=pr_number,
hostname=GITHUB_HOSTNAME,
),
review_tool=DEFAULT_REVIEW_TOOL_NAME,
).and_respond()
github_server.expect_get_username_request().and_respond()

head = "3a120a3a153f7d2960967ce6f1d52698a4d3a436"
Expand Down
29 changes: 26 additions & 3 deletions eden/scm/tests/github/mock_create_prs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@

from sapling import extensions
from sapling.ext.github import github_gh_cli, submit
from sapling.ext.github.mock_utils import mock_run_git_command, MockGitHubServer
from sapling.ext.github.pull_request_body import title_and_body
from sapling.ext.github.consts import GITHUB_HOSTNAME
from sapling.ext.github.mock_utils import (
mock_run_git_command,
MockGitHubServer,
OWNER,
REPO_NAME,
)
from sapling.ext.github.pull_request_body import (
_format_review_url,
DEFAULT_REVIEW_TOOL_NAME,
DEFAULT_REVIEW_URL_TEMPLATE,
title_and_body,
)

# An extension to mock network requests by replacing the `github_gh_cli.make_request`
# and `submit.run_git_command` with the corresponding wrapper functions. Check `uisetup`
Expand Down Expand Up @@ -47,7 +58,19 @@ def setup_mock_github_server(ui) -> MockGitHubServer:
github_server.expect_get_pr_details_request(num).and_respond(pr_id)

github_server.expect_update_pr_request(
pr_id, num, msg, base=base, stack_pr_ids=[pr[0] for pr in prs]
pr_id,
num,
msg,
base=base,
stack_pr_ids=[pr[0] for pr in prs],
review_url=_format_review_url(
DEFAULT_REVIEW_URL_TEMPLATE,
owner=OWNER,
repo=REPO_NAME,
number=num,
hostname=GITHUB_HOSTNAME,
),
review_tool=DEFAULT_REVIEW_TOOL_NAME,
).and_respond()

github_server.expect_get_username_request().and_respond()
Expand Down
92 changes: 92 additions & 0 deletions eden/scm/tests/github/mock_create_prs_footer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2.

from sapling import extensions
from sapling.ext.github import github_gh_cli, submit
from sapling.ext.github.consts import GITHUB_HOSTNAME
from sapling.ext.github.mock_utils import (
mock_run_git_command,
MockGitHubServer,
OWNER,
REPO_NAME,
)
from sapling.ext.github.pull_request_body import (
_format_review_url,
DEFAULT_REVIEW_TOOL_NAME,
DEFAULT_REVIEW_URL_TEMPLATE,
title_and_body,
)

# An extension to mock network requests for `sl pr submit` with customized
# github.pull-request-review-url-template / github.pull-request-review-tool-name
# configs. The expected review link is computed from the same configs the test
# sets, so the mock server only matches if the customized footer was produced.


def setup_mock_github_server(ui) -> MockGitHubServer:
"""Setup mock GitHub Server for testing `sl pr submit` with a custom review link."""
github_server = MockGitHubServer()

github_server.expect_get_repository_request().and_respond()

github_server.expect_guess_next_pull_request_number().and_respond()

url_template = (
ui.config("github", "pull-request-review-url-template")
or DEFAULT_REVIEW_URL_TEMPLATE
)
review_tool = (
ui.config("github", "pull-request-review-tool-name")
or DEFAULT_REVIEW_TOOL_NAME
)

prs = [
(42, "one\n"),
(43, "two\n"),
]

for num, msg in prs:
title, body = title_and_body(msg)
head = f"pr{num}"
base = "main"

github_server.expect_create_pr_request(
body=body,
title=title,
head=head,
base=base,
).and_respond(number=num)

pr_id = f"PR_id_{num}"
github_server.expect_get_pr_details_request(num).and_respond(pr_id)

review_url = _format_review_url(
url_template, owner=OWNER, repo=REPO_NAME, number=num, hostname=GITHUB_HOSTNAME
)

github_server.expect_update_pr_request(
pr_id,
num,
msg,
base=base,
stack_pr_ids=[pr[0] for pr in prs],
review_url=review_url,
review_tool=review_tool,
).and_respond()

github_server.expect_get_username_request().and_respond()

head = "1a67244b0a776bfcc3be6bf811e98c993d78ce47"
github_server.expect_merge_into_branch(head).and_respond()

return github_server


def uisetup(ui):
mock_github_server = setup_mock_github_server(ui)
extensions.wrapfunction(
github_gh_cli, "_make_request", mock_github_server.make_request
)
extensions.wrapfunction(submit, "run_git_command", mock_run_git_command)
Loading