Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render PR description as template #2563

Merged
merged 6 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
9 changes: 4 additions & 5 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ To get an auto-generated PR description you can put "copilot:summary" or "copilo
### Checklist
* [ ] I have read and agree to [Contributor Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and the [Code of Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [ ] I've included a screenshot or gif (if applicable)
* [ ] I have tested https://demo.rerun.io/pr/{{ pr-number }} (if applicable)
* [ ] I have tested [demo.rerun.io](https://demo.rerun.io/pr/{{ pr.number }}) (if applicable)

<!-- This line will get updated when the PR build summary job finishes. -->
PR Build Summary: {{ pr-build-summary }}

<!-- This comment will be replaced by a link to the documentation preview -->
- [PR Build Summary](https://build.rerun.io/pr/{{ pr.number }})
- [Docs preview](https://rerun.io/preview/{{ "pr:%s"|format(pr.branch)|encode_uri_component }}/docs)
Copy link
Member Author

@jprochazk jprochazk Jun 29, 2023

Choose a reason for hiding this comment

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

The links to the previews are a bit weird. The end result looks like

https://rerun.io/preview/pr:jan/pull-request/examples

But with pr:jan/pull-request url-encoded.

Motivations for this:

  • It's treated as a single path segment this way, and the branch name may contain any character (including /)
  • The github .zip archive link is different for branches, so the pr: prefix means we can differentiate between regular commit previews and branch previews, and choose the correct path.

Commit previews continue to work as before.

- [Examples preview](https://rerun.io/preview/{{ "pr:%s"|format(pr.branch)|encode_uri_component }}/examples)
2 changes: 1 addition & 1 deletion .github/workflows/reusable_update_pr_body.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
python-version: 3.x

- name: Install deps
run: pip install PyGithub # NOLINT
run: pip install Jinja2 PyGithub # NOLINT

- name: Update PR description
run: |
Expand Down
5 changes: 0 additions & 5 deletions scripts/generate_pr_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,6 @@ def generate_pr_summary(github_token: str, github_repository: str, pr_number: in
print(f"Uploading results to {upload_blob.name}")
upload_blob.upload_from_file(buffer, content_type="text/html")

# If there's a {{ pr-build-summary }} string in the PR description, replace it with a link to the summary page.
pr_description = pull.body
new_description = pr_description.replace("{{ pr-build-summary }}", f"https://build.rerun.io/pr/{pr_number}")
pull.edit(body=new_description)


def main() -> None:
parser = argparse.ArgumentParser(description="Generate a PR summary page")
Expand Down
51 changes: 22 additions & 29 deletions scripts/update_pr_body.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
#!/usr/bin/env python3

"""
Script to generate a link to documentation preview in PRs.
Script to update the PR description template.

This is expected to be run by the `reusable_update_pr_body.yml` GitHub workflow.

Requires the following packages:
pip install PyGithub # NOLINT
pip install Jinja2 PyGithub # NOLINT
"""
from __future__ import annotations

import argparse
import logging
import urllib.parse

from github import Github # NOLINT
from jinja2 import DebugUndefined, select_autoescape
from jinja2.sandbox import SandboxedEnvironment

EMPTY_LINK = "<!-- This comment will be replaced by a link to the documentation preview -->"
LINK_START = "<!-- pr-link-docs:start -->"
LINK_END = "<!-- pr-link-docs:end -->"

LINK_TEMPLATE = """<!-- pr-link-docs:start -->
Docs preview: {{ docs-link }}
Examples preview: {{ examples-link }}
<!-- pr-link-docs:end -->"""
def encode_uri_component(value: str) -> str:
return urllib.parse.quote(value, safe="")


def main() -> None:
logging.getLogger().setLevel(-1)
parser = argparse.ArgumentParser(description="Generate a PR summary page")
parser.add_argument("--github-token", required=True, help="GitHub token")
parser.add_argument("--github-repository", required=True, help="GitHub repository")
Expand All @@ -38,26 +38,19 @@ def main() -> None:
latest_commit = pr.get_commits().reversed[0]

print(f"Latest commit: {latest_commit.sha}")
short_sha = latest_commit.sha[:7]

body = pr.body

# update preview links
link = LINK_TEMPLATE.replace("{{ docs-link }}", f"https://rerun.io/preview/{short_sha}/docs")
link = link.replace("{{ examples-link }}", f"https://rerun.io/preview/{short_sha}/examples")
if EMPTY_LINK in body:
print("Empty link found, updating it")
body = body.replace(EMPTY_LINK, link)
else:
start = body.find(LINK_START)
end = body.find(LINK_END)
if start != -1 and end != -1:
print("Existing link found, updating it")
body = body[:start] + link + body[end + len(LINK_END) :]

# update the pr number
if "{{ pr-number }}" in body:
body = body.replace("{{ pr-number }}", str(args.pr_number))

env = SandboxedEnvironment(
autoescape=select_autoescape(),
undefined=DebugUndefined,
)
env.filters["encode_uri_component"] = encode_uri_component

body = env.from_string(pr.body).render(
pr={
"number": args.pr_number,
"branch": pr.head.ref,
},
)

if body != pr.body:
pr.edit(body=body)
Expand Down