diff --git a/.github/workflows/changed_files.yml b/.github/workflows/changed_files.yml new file mode 100644 index 000000000..295d0c853 --- /dev/null +++ b/.github/workflows/changed_files.yml @@ -0,0 +1,113 @@ +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 added_files modified_files removed_files no_preview_files + + pr_num="${{ github.event.pull_request.number }}" + + 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 + # using # as sed separation character + # + # - Remove content/ + # - Remove _index/ + # - Remove integration_tutorials/ + + # 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 + + # 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 + + - 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