Skip to content

Commit

Permalink
Link to preview of latest commit in PR body (#2287)
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
* [ ] 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)

<!-- This line will get updated when the PR build summary job finishes.
-->
PR Build Summary: https://build.rerun.io/pr/2287

<!-- pr-link-docs:start -->
Docs preview: https://rerun.io/preview/49fbf30/docs
<!-- pr-link-docs:end -->
  • Loading branch information
jprochazk authored and emilk committed Jun 15, 2023
1 parent bc9f48c commit 0965b15
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ To get an auto-generated PR description you can put "copilot:summary" or "copilo

<!-- 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 -->
8 changes: 8 additions & 0 deletions .github/workflows/on_pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,11 @@ jobs:
CONCURRENCY: pr-${{ github.event.pull_request.number }}
PR_NUMBER: ${{ github.event.pull_request.number }}
secrets: inherit

link-docs:
name: 'Link Docs'
uses: ./.github/workflows/reusable_pr_link_docs.yml
with:
CONCURRENCY: pr-${{ github.event.pull_request.number }}
PR_NUMBER: ${{ github.event.pull_request.number }}
secrets: inherit
49 changes: 49 additions & 0 deletions .github/workflows/reusable_pr_link_docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Reusable PR Link Docs

on:
workflow_call:
inputs:
CONCURRENCY:
required: true
type: string
PR_NUMBER:
required: true
type: string

concurrency:
group: ${{ inputs.CONCURRENCY }}-pr-summary
cancel-in-progress: true

jobs:
pr-link-docs:
name: Link to docs preview in PR

permissions:
contents: "read"
id-token: "write"
pull-requests: "write"

runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.x

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

- name: Link to docs
run: |
python scripts/pr_link_docs.py \
--github-token ${{ secrets.GITHUB_TOKEN }} \
--github-repository ${GITHUB_REPOSITORY} \
--pr-number ${{ inputs.PR_NUMBER }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ inputs.PR_NUMBER }}

53 changes: 53 additions & 0 deletions scripts/pr_link_docs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python3

"""
Script to generate a link to documentation preview in PRs.
This is expected to be run by the `reusable_pr_link_docs.yml` GitHub workflow.
Requires the following packages:
pip install PyGithub # NOLINT
"""

import argparse

from github import Github # NOLINT

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 -->\nDocs preview: {{ link }}\n<!-- pr-link-docs:end -->"


def main() -> None:
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")
parser.add_argument("--pr-number", required=True, type=int, help="PR number")
args = parser.parse_args()

gh = Github(args.github_token) # NOLINT
repo = gh.get_repo(args.github_repository)
pr = repo.get_pull(args.pr_number)

latest_commit = pr.get_commits().reversed[0]

print(f"Latest commit: {latest_commit.sha}")

link = LINK_TEMPLATE.replace("{{ link }}", f"https://rerun.io/preview/{latest_commit.sha[:7]}/docs")
if EMPTY_LINK in pr.body:
print("Empty link found, updating it")
new_body = pr.body.replace(EMPTY_LINK, link)
pr.edit(body=new_body)
else:
start = pr.body.find(LINK_START)
end = pr.body.find(LINK_END)
if start != -1 and end != -1:
print("Existing link found, updating it")
new_body = pr.body[:start] + link + pr.body[end + len(LINK_END) :]
pr.edit(body=new_body)


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions scripts/requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@

cryptography==38.0.4 # for scripts/upload_image.py
google-cloud-storage==2.9.0 # for scripts/upload_image.py
PyGithub==1.58.2 # for scripts/generate_pr_summary.py and scripts/pr_link_docs_preview.py

0 comments on commit 0965b15

Please sign in to comment.