From 8cd4cc87ea875ed8a6825954a137b9c3154bc58e Mon Sep 17 00:00:00 2001 From: Matt Linville Date: Mon, 10 Feb 2025 11:30:34 -0800 Subject: [PATCH 1/2] [DOCS-1262] Add GH action for per-page preview links --- .github/workflows/changed_files.yml | 83 +++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 .github/workflows/changed_files.yml diff --git a/.github/workflows/changed_files.yml b/.github/workflows/changed_files.yml new file mode 100644 index 000000000..22d6159a5 --- /dev/null +++ b/.github/workflows/changed_files.yml @@ -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="$1" + else + file="Removed: $1" + fi + + # Apend to output (non-Markdown added in the else of the next block) + output+="
  • $file
  • " + } + + for file in ${files[@]}; do + # Generate links for changed Markdown files + if [[ $file == content/**/*.md ]] + then + generatePreviewURL "$file" + # Otherwise just list changed file paths + else + output+="
  • No preview: $file
  • " + fi + done; + body="${output[@]}" + echo "body:\n\n$body" + echo "body=$body" >> $GITHUB_ENV + + - name: Find Comment + uses: peter-evans/find-comment@v2.0.1 + 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/create-or-update-comment@v2.1.0 + with: + comment-id: ${{ steps.find-comment.outputs.comment-id }} + issue-number: ${{ github.event.pull_request.number }} + edit-mode: replace + body: | +

    HTML previews:

    Preview links work **only** after the `Cloudflare Pages` build check succeeds. Links are generated only for new and modified .md files under content/.

    Feedback about HTML previews From 4777f3613df4eb426ba518c1524d7860f998cb17 Mon Sep 17 00:00:00 2001 From: Matt Linville Date: Tue, 18 Feb 2025 15:14:35 -0800 Subject: [PATCH 2/2] Another attempt to rework processing the files --- .github/workflows/changed_files.yml | 94 +++++++++++++++++++---------- 1 file changed, 62 insertions(+), 32 deletions(-) diff --git a/.github/workflows/changed_files.yml b/.github/workflows/changed_files.yml index 22d6159a5..295d0c853 100644 --- a/.github/workflows/changed_files.yml +++ b/.github/workflows/changed_files.yml @@ -19,16 +19,36 @@ jobs: env: GH_TOKEN: ${{ github.token }} run: | - declare -a output - pr_num=${{ github.event.pull_request.number }} + declare -a output added_files modified_files removed_files no_preview_files - 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'}) + pr_num="${{ github.event.pull_request.number }}" - SAVEIFS=$IFS - IFS=$'\n' - files=($files) - IFS=$SAVEIFS + all_changed_files="$(gh pr view ${pr_num} --json files --jq '.files.[].path')" + for changed_file in "${all_changed_files[@]}"; do + status="$(echo '$changed_file' | awk -F ' ' {'print $1'})" + echo "Status: $status" + path="$(echo '$changed_file' | awk -F ' ' {'print $2'})" + echo "Path: $path" + if [ "$status" = "added" ]; then + added_files+=("$path") + elif [ "$status = "modified" ]; then + modified_files+=("$path") + elif [ "$status" = "removed" ]; then + removed_files+=("path") + fi + done + + echo "Added:" + echo "${added_files[@}" + echo + + echo "Modified:" + echo "${modified_files[@}" + echo + + echo "Removed:" + echo "${removed_files[@}" + echo function generatePreviewURL() { # Preprocess input paths to output paths @@ -38,30 +58,40 @@ jobs: # - 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="$1" - else - file="Removed: $1" - fi - - # Apend to output (non-Markdown added in the else of the next block) - output+="

  • $file
  • " - } + # Generate HTML preview links for added files in content/ + for file in ${added_files[@]}; do + if [[ "$file" =~ .*\/content\/.*\.md ]]; then + output_path=$(echo "$file" | sed -E 's#content\/##g' | sed -E 's#/_index/##g' | sed -E 's#integration-tutorials\/##g') + preview_link="$file" + output+=("
  • Added: $preview_link
  • ") + else + no_preview_links+=("Added, no preview: $file") + fi + done - for file in ${files[@]}; do - # Generate links for changed Markdown files - if [[ $file == content/**/*.md ]] - then - generatePreviewURL "$file" - # Otherwise just list changed file paths - else - output+="
  • No preview: $file
  • " - fi - done; - body="${output[@]}" + # Generate HTML preview links for modified files in content/ + for file in ${modified_files[@]}; do + if [[ "$file" =~ .*\/content\/.*\.md ]]; then + output_path=$(echo "$file" | sed -E 's#content\/##g' | sed -E 's#/_index/##g' | sed -E 's#integration-tutorials\/##g') + preview_link="$file" + output+=("
  • Modified: $preview_link
  • ") + else + no_preview_links+=("Modified, no preview: $file") + fi + done + + # Process removed files + for file in ${removed_files[@]}; do + no_preview_links+=("Removed, no preview: $file") + done + + # Process no preview + for file in ${no_preview_files[@]}; do + output+=("
  • $file
  • ") + done + } + # Compose the body + body="" echo "body:\n\n$body" echo "body=$body" >> $GITHUB_ENV @@ -80,4 +110,4 @@ jobs: issue-number: ${{ github.event.pull_request.number }} edit-mode: replace body: | -

    HTML previews:

    Preview links work **only** after the `Cloudflare Pages` build check succeeds. Links are generated only for new and modified .md files under content/.

    Feedback about HTML previews +

    HTML previews:

    Preview links work only after the `Cloudflare Pages` build check succeeds. Links are generated only for new and modified .md files under content/.

    Feedback about HTML previews