Skip to content
Merged
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
18 changes: 15 additions & 3 deletions scripts/ci/notify_uv_lock_conflicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,15 @@ def resolve_source_pr(
return None


def avoid_backlink(url: str) -> str:
"""Swap ``github.com`` for ``redirect.github.com`` so linking to the source
PR/commit doesn't generate an automatic backlink comment on it.

See: https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/autolinked-references-and-urls#avoiding-backlinks-to-linked-references
"""
return url.replace("https://github.com/", "https://redirect.github.com/", 1)


def build_body(source_ref_md: str) -> str:
return "\n".join(
[
Expand Down Expand Up @@ -424,13 +433,16 @@ def main() -> int:
with GitHubGraphQL(token) as client:
source_pr = resolve_source_pr(client, owner, repo, sha, short_sha)
if source_pr:
# Use redirect.github.com for both links: they point at the PR/commit
# that caused this notice, and a plain github.com link would generate
# an unwanted backlink comment there.
source_ref_md = (
f"[#{source_pr['number']}]({source_pr['url']}) "
f'("{source_pr["title"]}"), commit [`{short_sha}`]({commit_url})'
f"[#{source_pr['number']}]({avoid_backlink(source_pr['url'])}) "
f'("{source_pr["title"]}"), commit [`{short_sha}`]({avoid_backlink(commit_url)})'
)
source_ref_plain = f"#{source_pr['number']} ({source_pr['url']}) — commit {short_sha}"
else:
source_ref_md = f"commit [`{short_sha}`]({commit_url})"
source_ref_md = f"commit [`{short_sha}`]({avoid_backlink(commit_url)})"
source_ref_plain = f"commit {short_sha}"

log(f"Source of uv.lock change: {source_ref_plain}")
Expand Down
29 changes: 29 additions & 0 deletions scripts/tests/ci/test_notify_uv_lock_conflicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,35 @@ def test_previous_marker_with_different_sha_still_notifies(self, mod):
assert entry["existing"] == {"id": "C1", "body": f"{mod.MARKER}\nolder notice for deadbee"}


class TestAvoidBacklink:
@pytest.mark.parametrize(
"url,expected",
[
(
"https://github.com/apache/airflow/pull/42",
"https://redirect.github.com/apache/airflow/pull/42",
),
(
"https://github.com/apache/airflow/commit/deadbeef",
"https://redirect.github.com/apache/airflow/commit/deadbeef",
),
],
)
def test_rewrites_github_com(self, mod, url, expected):
assert mod.avoid_backlink(url) == expected

def test_leaves_non_github_url_untouched(self, mod):
url = "https://example.com/pull/42"
assert mod.avoid_backlink(url) == url

def test_only_rewrites_leading_occurrence(self, mod):
"""Only the URL's own host is rewritten, not incidental later occurrences."""
url = "https://github.com/apache/airflow/pull/42#see-https://github.com/other"
assert mod.avoid_backlink(url) == (
"https://redirect.github.com/apache/airflow/pull/42#see-https://github.com/other"
)


class TestBuildBody:
def test_includes_marker_source_and_instructions(self, mod):
body = mod.build_body("[#42](https://example/pr/42)")
Expand Down