From 33fdf6908eed5619027db620f9900e9fb47ecc65 Mon Sep 17 00:00:00 2001 From: jprochazk <1665677+jprochazk@users.noreply.github.com> Date: Wed, 31 May 2023 13:29:15 +0200 Subject: [PATCH 1/4] Link to preview of latest commit in PR body --- .github/pull_request_template.md | 2 + .github/workflows/reusable_pr_link_docs.yml | 49 +++++++++++++++++++ scripts/pr_link_docs.py | 53 +++++++++++++++++++++ scripts/requirements-dev.txt | 1 + 4 files changed, 105 insertions(+) create mode 100644 .github/workflows/reusable_pr_link_docs.yml create mode 100755 scripts/pr_link_docs.py diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 45d2179fb6c1..106a9bbd9245 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -18,3 +18,5 @@ To get an auto-generated PR description you can put "copilot:summary" or "copilo PR Build Summary: {{ pr-build-summary }} + + diff --git a/.github/workflows/reusable_pr_link_docs.yml b/.github/workflows/reusable_pr_link_docs.yml new file mode 100644 index 000000000000..baca5d510efb --- /dev/null +++ b/.github/workflows/reusable_pr_link_docs.yml @@ -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: Create HTML summary for 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 }} + diff --git a/scripts/pr_link_docs.py b/scripts/pr_link_docs.py new file mode 100755 index 000000000000..929172687c4a --- /dev/null +++ b/scripts/pr_link_docs.py @@ -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 = "" +LINK_START = "" +LINK_END = "" + +LINK_TEMPLATE = "\nDocs preview: {{ link }}\n" + + +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) + 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() diff --git a/scripts/requirements-dev.txt b/scripts/requirements-dev.txt index 8f1406c7bc9f..bea0b785a2f8 100644 --- a/scripts/requirements-dev.txt +++ b/scripts/requirements-dev.txt @@ -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 From 2de3cfad9504f0b696bc10ba512e7cbf592dde23 Mon Sep 17 00:00:00 2001 From: jprochazk <1665677+jprochazk@users.noreply.github.com> Date: Wed, 31 May 2023 13:32:54 +0200 Subject: [PATCH 2/4] run link docs on every pr --- .github/workflows/on_pull_request.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.github/workflows/on_pull_request.yml b/.github/workflows/on_pull_request.yml index afddc5531171..a8b2ff16b04b 100644 --- a/.github/workflows/on_pull_request.yml +++ b/.github/workflows/on_pull_request.yml @@ -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 From 836b8dc276452588dc3c38ebed23e7858d5443d8 Mon Sep 17 00:00:00 2001 From: jprochazk <1665677+jprochazk@users.noreply.github.com> Date: Wed, 31 May 2023 13:36:34 +0200 Subject: [PATCH 3/4] update `reusable_pr_link_docs.yml` name --- .github/workflows/reusable_pr_link_docs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/reusable_pr_link_docs.yml b/.github/workflows/reusable_pr_link_docs.yml index baca5d510efb..04c9da2d09ed 100644 --- a/.github/workflows/reusable_pr_link_docs.yml +++ b/.github/workflows/reusable_pr_link_docs.yml @@ -16,7 +16,7 @@ concurrency: jobs: pr-link-docs: - name: Create HTML summary for PR + name: Link to docs preview in PR permissions: contents: "read" From 49fbf30fe0b6ab1f200af563d74d77260279cd1f Mon Sep 17 00:00:00 2001 From: jprochazk <1665677+jprochazk@users.noreply.github.com> Date: Wed, 31 May 2023 13:38:01 +0200 Subject: [PATCH 4/4] silence `Github` lint --- scripts/pr_link_docs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/pr_link_docs.py b/scripts/pr_link_docs.py index 929172687c4a..d7c38bb183a0 100755 --- a/scripts/pr_link_docs.py +++ b/scripts/pr_link_docs.py @@ -27,7 +27,7 @@ def main() -> None: parser.add_argument("--pr-number", required=True, type=int, help="PR number") args = parser.parse_args() - gh = Github(args.github_token) + gh = Github(args.github_token) # NOLINT repo = gh.get_repo(args.github_repository) pr = repo.get_pull(args.pr_number)