Skip to content

[CI] Added job adding preview links #24

[CI] Added job adding preview links

[CI] Added job adding preview links #24

name: "Post preview links for changed files"
on:
pull_request:
paths:
- 'docs/**.md'
workflow_call:
inputs:
project:
description: 'The project to build (dev doc, user doc)'
default: ''
required: false
type: string
jobs:
post-preview-links:
name: Post preview links for changed files
runs-on: ubuntu-latest
permissions:
# Needed to manage the comment
pull-requests: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Create comment for changed files
run: |
set -x #debug
file_limit=100
build_url="https://ez-systems-developer-documentation--${{ github.event.pull_request.number }}.com.readthedocs.build/${{inputs.project}}en/${{ github.event.pull_request.number }}/"
changed_files=$(git diff --name-only HEAD "origin/$GITHUB_BASE_REF" | grep -E ".md$" || [[ $? == 1 ]])
number_of__changed_files=$(echo "$changed_files" | wc -l)
if [[ $changed_files -eq "" ]] ; then
comment="Preview of modified files:: no markdown files changed, no preview needed."
elif [[ $number_of__changed_files -gt file_limit ]] ; then
comment="Preview of modified files: too many files modified in a single PR. Unable to post preview links, sorry!"
else
filenames=$(echo "$changed_files" | rev | cut -d / -f 1 | cut -d . -f 2- | rev)
# Guess the URL based on Markdown file location. Remove the .md extension.
urls=$(echo "$changed_files" | cut -d / -f 2- | rev | cut -d '.' -f 2- | rev | sed -e "s ^ $build_url ")
left=$(yes "+ [" | head -n "$number_of__changed_files" )
middle=$(yes "](" | head -n "$number_of__changed_files" )
right=$(yes ")\n" | head -n "$number_of__changed_files" )
comment=$(paste -d'\0' <(echo "$left") <(echo "$filenames") <(echo "$middle") <(echo "$urls") <(echo "$right"))
comment="Preview of modified files:\n\n$comment"
fi
echo -e $comment > comment.md
cat comment.md #DEBUG
- name: Find comment
id: find-comment
uses: peter-evans/find-comment@v3
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
body-includes: 'Preview of modified files'
- name: Create or update comment
uses: peter-evans/create-or-update-comment@v4
with:
comment-id: ${{ steps.preview-links-comment.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body-path: comment.md
edit-mode: replace