diff --git a/.github/scripts/update_pr_description.py b/.github/scripts/update_pr_description.py new file mode 100644 index 0000000..e929c68 --- /dev/null +++ b/.github/scripts/update_pr_description.py @@ -0,0 +1,32 @@ +from os import environ +from re import sub + +from github import Github + +token = environ['GITHUB_TOKEN'] +repo_name = environ['GITHUB_REPOSITORY'] +pr_number = int(environ['PR_NUMBER']) +preview_url = environ['PREVIEW_URL'] + +gh = Github(token) +repo = gh.get_repo(repo_name) +pr = repo.get_pull(pr_number) + +preview_label = '📚 Documentation preview 📚: ' +preview_text = f'{preview_label} {preview_url}' +current_body = pr.body or '' + +if preview_label in current_body: + new_body = sub( + f'{preview_label}.*', + preview_text, + current_body, + ) +else: + new_body = ( + f'{current_body}' + '\n\n---\n\n' + f'{preview_text}' + ) + +pr.edit(body=new_body) diff --git a/.github/workflows/publish-preview.yml b/.github/workflows/publish-preview.yml index 5635d7f..8575aa6 100644 --- a/.github/workflows/publish-preview.yml +++ b/.github/workflows/publish-preview.yml @@ -10,48 +10,45 @@ jobs: steps: - name: Checkout PR branch uses: actions/checkout@v4 - + - name: Set up Python uses: actions/setup-python@v4 with: - python-version: '3.14' - + python-version: "3.13" + - name: Install dependencies run: | pip install \ jupyter-book \ - ghp-import - + ghp-import \ + PyGithub + - name: Build the book run: | jupyter-book build content/ - - - name: Get PR number - id: pr-info - run: | - echo "number=pr-${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT - name: Deploy with ghp-import run: | git config user.name "github-actions[bot]" git config user.email "github-actions[bot]@users.noreply.github.com" + + # Fetch gh-pages branch to preserve existing content + git fetch origin gh-pages:gh-pages + ghp-import \ --no-jekyll \ --push \ --force \ - --message "Deploy preview for ${{ steps.pr-info.outputs.number}}" \ + --message "Deploy preview for ${{github.event.pull_request.number}}" \ --branch gh-pages \ --remote origin \ - --prefix=preview/pr-${{ steps.pr-info.outputs.number}} \ + --prefix=preview/pr-${{github.event.pull_request.number}} \ content/_build/html - - name: Comment PR with preview link - uses: actions/github-script@v7 - with: - script: | - github.rest.issues.createComment({ - issue_number: context.issue.number, - owner: context.repo.owner, - repo: context.repo.repo, - body: 'Preview deployed to: https://${{ github.repository_owner }}.github.io/learn.scientific-python.org/preview/${{ steps.pr-info.outputs.number }}/' - }) + - name: Add preview link to PR + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_REPOSITORY: ${{ github.repository }} + PR_NUMBER: ${{ github.event.pull_request.number }} + PREVIEW_URL: https://${{ github.repository_owner }}.github.io/learn.scientific-python.org/preview/pr-${{ github.event.pull_request.number }}/ + run: python .github/scripts/update_pr_description.py diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index b342058..a9e3db6 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -19,7 +19,7 @@ jobs: - name: Set up Python uses: actions/setup-python@v4 with: - python-version: '3.14' + python-version: "3.14" - name: Install dependencies run: pip install jupyter-book ghp-import @@ -34,7 +34,7 @@ jobs: # Fetch gh-pages branch to avoid conflicts git fetch origin gh-pages:gh-pages - + ghp-import \ --no-jekyll \ --push \