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

[DOCS-1262] Add GH action for per-page preview links #1091

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions .github/workflows/changed_files.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
name: List files changed as GitHub comment

on:
pull_request:
branches:
- main

jobs:
list-files-changed:
if: github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 2

- name: Format list of changed files
id: format
env:
GH_TOKEN: ${{ github.token }}
run: |
declare -a output
pr_num=${{ github.event.pull_request.number }}

files=$(gh pr view ${pr_num} --json files --jq '.files.[].path')
removed_files=$(gh api repos/wandb/docs/pulls/${pr_num}/files -q '.[] | [.status,.filename] | @tsv' |grep 'removed' | awk -F ' ' {'print $2'})

SAVEIFS=$IFS
IFS=$'\n'
files=($files)
IFS=$SAVEIFS

function generatePreviewURL() {
# Preprocess input paths to output paths
# using # as sed separation character
#
# - Remove content/
# - Remove _index/
# - Remove integration_tutorials/

# Generate HTML preview for added and modified files only
if [[ ! " ${removed_files[*]} " =~ $1 ]]; then
html_file=$(echo "${1%.md}/" | sed -E 's#content\/##g' | sed -E 's#/_index/##g' | sed -E 's#integration-tutorials\/##g')
# Save a URL instead of just a plaintext file
file="<a href=\"https://docs-$pr_num.docodile.pages.dev/$html_file\" target=\"_blank\" rel=\"noopener\">$1</a>"
else
file="<b>Removed</b>: $1"
fi

# Apend to output (non-Markdown added in the else of the next block)
output+="<li>$file</li>"
}

for file in ${files[@]}; do
Copy link
Contributor

Choose a reason for hiding this comment

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

The format of the markdown is a little confusing.

Can separate the non generated vs generated links into separate sections or add "Generated preview" to the second bullet point in body?

i.e. From this:

HTML previews:

  • No preview: filename
  • filename (generated preview)

To something like:

HTML previews:

  • No preview generated for: filename
  • Generated preview: filename, filename, filename

or perhaps even

HTML previews:

  • No preview generated for:
    • filename
    • filename
  • Generated preview:
    • filename
    • filename

# Generate links for changed Markdown files
if [[ $file == content/**/*.md ]]
then
generatePreviewURL "$file"
# Otherwise just list changed file paths
else
output+="<li><b>No preview</b>: $file</li>"
fi
done;
body="${output[@]}"
echo "body:\n\n$body"
echo "body=$body" >> $GITHUB_ENV

- name: Find Comment
uses: peter-evans/[email protected]
id: find-comment
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
body-includes: 'HTML previews:'

- name: Create comment
uses: peter-evans/[email protected]
with:
comment-id: ${{ steps.find-comment.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
edit-mode: replace
body: |
<h3>HTML previews:</h3><ul>${{ env.body }}</ul><p>Preview links work **only** after the `Cloudflare Pages` build check succeeds. Links are generated only for new and modified <code>.md</code> files under <code>content/</code>.</p><p><a href="https://github.com/wandb/docs/issues/new?title=Feedback+about+HTML+preview+comments&assignees=mdlinville&body=Link+to+PR+and+describe+the+issue">Feedback about HTML previews</a>
Copy link
Contributor

Choose a reason for hiding this comment

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

If we want to be concise with the comment, we can move:

"Links are generated only for new and modified .md files under content/."

to a CONTRIBUTING.md file.