Skip to content

Commit

Permalink
Render PR description as template (#2563)
Browse files Browse the repository at this point in the history
<!--
Open the PR up as a draft until you feel it is ready for a proper
review.

Do not make PR:s from your own `main` branch, as that makes it difficult
for reviewers to add their own fixes.

Add any improvements to the branch as new commits to make it easier for
reviewers to follow the progress. All commits will be squashed to a
single commit once the PR is merged into `main`.

Make sure you mention any issues that this PR closes in the description,
as well as any other related issues.

To get an auto-generated PR description you can put "copilot:summary" or
"copilot:walkthrough" anywhere.
-->

### What

### Checklist
* [x] 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)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested [demo.rerun.io](https://demo.rerun.io/pr/2563) (if
applicable)

- [PR Build Summary](https://build.rerun.io/pr/2563)
- [Docs
preview](https://rerun.io/preview/pr%3Ajan%2Fsimplify-previews/docs)
- [Examples
preview](https://rerun.io/preview/pr%3Ajan%2Fsimplify-previews/examples)
  • Loading branch information
jprochazk authored Jun 30, 2023
1 parent 4c4a5d4 commit 8384714
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 40 deletions.
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)
- [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/ci/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/ci/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

0 comments on commit 8384714

Please sign in to comment.