Skip to content

Create observability.md #1349

Create observability.md

Create observability.md #1349

Workflow file for this run

name: Auto Comment on PR
on:
pull_request:
paths:
- 'content/**'
types:
- opened
- synchronize
permissions:
pull-requests: write
contents: read
jobs:
auto-comment:
runs-on: ubuntu-latest
steps:
- name: 'Checkout'
uses: 'actions/checkout@v3'
- name: 'Install jq'
run: sudo apt-get install jq
- name: Create Comment
run: |
set -e
PR_NUMBER=${{ github.event.pull_request.number }}
BRANCH_NAME=${{ github.head_ref }}
GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}
CREATE_COMMENT_URL="https://api.github.com/repos/${{ github.repository }}/issues/${PR_NUMBER}/comments"
UPDATE_COMMENT_URL="https://api.github.com/repos/${{ github.repository }}/issues/comments"
FILES_URL="https://api.github.com/repos/${{ github.repository }}/pulls/${PR_NUMBER}/files"
# Check if comment already exists
EXISTING_COMMENT_JSON=$(curl -Ls \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
$CREATE_COMMENT_URL \
| jq -r '.[] | select(.user.login == "github-actions[bot]")'
)
EXISTING_COMMENT_ID=$(jq -r '.id' <<<"$EXISTING_COMMENT_JSON")
EXISTING_COMMENT=$(jq -r '.body' <<<"$EXISTING_COMMENT_JSON")
# Get all changed md files
CHANGED_MD_FILES=$(curl -Ls \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
$FILES_URL \
| jq -r --arg prefix $BRANCH_NAME/ '.[] | select(((.filename | test("content\/.+\\.md")) and .status != "removed")) | ($prefix + .filename)' \
| sed -E -e 's|(^[^/]+/)([^/]+/)|\1|' -e 's|^|https://redis.io/docs/staging/|' -e 's|(_?index)?\.md||' \
| uniq \
| xargs \
| sed 's/ /<br>/g')
# Get all changed image files
CHANGED_IMAGE_FILES=$(curl -Ls \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
$FILES_URL \
| jq -r '.[] | select(.filename | test("^static/images\/.+(.png|.svg|.gif|.)")) | .filename | sub("^static/";"")')
if [[ -n "$CHANGED_IMAGE_FILES" ]]
then
# For each image, work out in which README it is present
MD_FILES_WITH_IMAGE=()
for CHANGED_IMAGE_FILE in $CHANGED_IMAGE_FILES; do
MD_FILE_WITH_IMAGE=$(grep -ro "$CHANGED_IMAGE_FILE" content \
| sed -E -e 's|:.+||' -e "s|^content/|https://redis.io/docs/staging/$BRANCH_NAME/|" -e 's|(_?index)?\.md||' \
| uniq)
MD_FILES_WITH_IMAGE+=($MD_FILE_WITH_IMAGE)
done
CHANGED_MD_FILES_WITH_IMAGE=$(printf "%s\n" "${MD_FILES_WITH_IMAGE[@]}" \
| uniq \
| xargs \
| sed 's/ /<br>/g')
CHANGED_MD_FILES="${CHANGED_MD_FILES}<br>${CHANGED_MD_FILES_WITH_IMAGE}"
fi
if [[ -z "$CHANGED_MD_FILES" ]]
then
if [[ -z "$EXISTING_COMMENT_ID" ]]
then
printf '%s\n' 'Nothing to do!'
exit 0
else
# If a comment already exists but there are no changed files in the PR anymore, delete the comment
curl -Ls \
-X DELETE \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Authorization: token ${GITHUB_TOKEN}" \
"${UPDATE_COMMENT_URL}/${EXISTING_COMMENT_ID}"
printf '%s\n' 'Comment deleted!'
exit 0
fi
fi
COMMENT="Staging links:<br> $CHANGED_MD_FILES"
printf '%s %s\n' 'Writing comment: ' "$COMMENT"
generate_post_data()
{
cat <<EOF
{
"body": "$COMMENT"
EOF
}
if [[ -n "$EXISTING_COMMENT_ID" ]]
then
# Update comment on pull request if comment is actually different
if [[ "$COMMENT" != "$EXISTING_COMMENT" ]]
then
curl -Ls \
-X PATCH \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Authorization: token ${GITHUB_TOKEN}" \
"${UPDATE_COMMENT_URL}/${EXISTING_COMMENT_ID}" \
--data "$(generate_post_data)" 1>/dev/null
printf '%s\n' 'Comment updated!'
else
printf '%s\n' 'Nothing changed!'
fi
else
# Write comment on pull request
curl -Ls \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Authorization: token ${GITHUB_TOKEN}" \
$CREATE_COMMENT_URL \
--data "$(generate_post_data)" 1>/dev/null
printf '%s\n' 'Comment written!'
fi