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

Link to preview of latest commit in PR body #2287

Merged
merged 4 commits into from
May 31, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
Copy link
Member

Choose a reason for hiding this comment

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

I think it is time to put ci-related scripts under scripts/ci to distinguish them from scripts meant for humans.

Other candidates:

  • generate_pr_summary.py
  • version_util.py
  • verify_wheels.py
  • generate_prerelease_pip_index.py
  • check_large_files.sh
  • check_large_files_allow_list.txt

Copy link
Member

Choose a reason for hiding this comment

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

(but perhaps in a follow-up PR)

Copy link
Member Author

Choose a reason for hiding this comment

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

It's a small thing, but #2292 so we don't forget


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