Skip to content
Merged
Changes from all commits
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
151 changes: 121 additions & 30 deletions .github/workflows/docs-dispatch.yml
Original file line number Diff line number Diff line change
@@ -1,45 +1,107 @@
name: Docs Dispatch

on:
push:
branches: [main]
workflow_dispatch:
inputs:
base_sha:
description: "Base SHA or ref for the docs diff. Defaults to head^."
required: false
head_sha:
description: "Head SHA or ref to document. Defaults to main."
required: false
reason:
description: "Why docs should be audited."
required: false

permissions:
contents: read

jobs:
check-docs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- uses: actions/checkout@v6
with:
fetch-depth: 0

- name: Get changed files
id: changed
env:
EVENT_NAME: ${{ github.event_name }}
INPUT_BASE_REF: ${{ inputs.base_sha }}
INPUT_HEAD_REF: ${{ inputs.head_sha }}
INPUT_REASON: ${{ inputs.reason }}
PUSH_BASE_REF: ${{ github.event.before }}
PUSH_HEAD_REF: ${{ github.event.after }}
run: |
BASE_SHA="${{ github.event.before }}"
HEAD_SHA="${{ github.event.after }}"
ZERO_SHA="0000000000000000000000000000000000000000"

if [ "$EVENT_NAME" = "workflow_dispatch" ]; then
HEAD_REF="${INPUT_HEAD_REF:-main}"
BASE_REF="$INPUT_BASE_REF"
REASON="${INPUT_REASON:-Manual docs dispatch for ${HEAD_REF}}"
else
HEAD_REF="$PUSH_HEAD_REF"
BASE_REF="$PUSH_BASE_REF"
REASON="Push to main"
fi

HEAD_SHA=$(git rev-parse "$HEAD_REF")

if [ "$BASE_SHA" = "0000000000000000000000000000000000000000" ]; then
BASE_SHA=$(git rev-list --max-parents=0 "$HEAD_SHA")
if [ -z "$BASE_REF" ]; then
if git rev-parse --verify "${HEAD_REF}^" >/dev/null 2>&1; then
BASE_REF="${HEAD_REF}^"
else
BASE_REF=$(git rev-list --max-parents=0 --max-count=1 "$HEAD_SHA")
fi
fi

if [ "$BASE_REF" = "$ZERO_SHA" ]; then
echo "::warning::No previous SHA found, diffing against first commit"
BASE_SHA=$(git rev-list --max-parents=0 --max-count=1 "$HEAD_SHA")
else
BASE_SHA=$(git rev-parse "$BASE_REF")
fi

COMMIT_URL="https://github.com/${GITHUB_REPOSITORY}/commit/${HEAD_SHA}"
COMPARE_URL="https://github.com/${GITHUB_REPOSITORY}/compare/${BASE_SHA}...${HEAD_SHA}"
FILES=$(git diff --name-only "$BASE_SHA" "$HEAD_SHA" | jq -R -s -c 'split("\n") | map(select(. != ""))')
echo "files=$FILES" >> $GITHUB_OUTPUT

{
echo "base_sha=$BASE_SHA"
echo "head_sha=$HEAD_SHA"
echo "files=$FILES"
echo "commit_url=$COMMIT_URL"
echo "compare_url=$COMPARE_URL"
echo "reason<<EOF"
echo "$REASON"
echo "EOF"
} >> "$GITHUB_OUTPUT"

- name: Check file-doc mapping
id: check
env:
GH_TOKEN: ${{ secrets.RELEASE_PLEASE_TOKEN }}
CHANGED_FILES: ${{ steps.changed.outputs.files }}
run: |
if ! MAP=$(curl -sf "https://raw.githubusercontent.com/verygoodplugins/automem-website/main/scripts/file-doc-map.json"); then
echo "Failed to fetch file-doc-map.json, skipping"
echo "affected=none" >> $GITHUB_OUTPUT
if ! MAP=$(curl -fsSL --connect-timeout 10 --max-time 30 --retry 2 --retry-connrefused \
-H "Authorization: Bearer $GH_TOKEN" \
-H "Accept: application/vnd.github.raw" \
"https://api.github.com/repos/verygoodplugins/automem-website/contents/scripts/file-doc-map.json"); then
echo "::warning::Failed to fetch file-doc-map.json, dispatching anyway"
echo "affected=[]" >> "$GITHUB_OUTPUT"
echo "mapping_status=unknown" >> "$GITHUB_OUTPUT"
exit 0
fi

REPO_KEY="${{ github.event.repository.name }}"
CHANGED='${{ steps.changed.outputs.files }}'
REPO_KEY="${GITHUB_REPOSITORY#*/}"

AFFECTED=$(echo "$MAP" | jq -r --arg repo "$REPO_KEY" --argjson changed "$CHANGED" '
AFFECTED=$(echo "$MAP" | jq -r --arg repo "$REPO_KEY" --argjson changed "$CHANGED_FILES" '
def matches_pattern($file; $pattern):
if ($pattern | endswith("/**")) then
($file | startswith($pattern[0:-3]))
($file | startswith($pattern[0:-2]))
else
$file == $pattern
end;
Expand All @@ -50,25 +112,54 @@ jobs:
')

if [ -z "$AFFECTED" ]; then
echo "affected=none" >> $GITHUB_OUTPUT
echo "No doc pages affected by this change"
echo "affected=[]" >> "$GITHUB_OUTPUT"
echo "mapping_status=none" >> "$GITHUB_OUTPUT"
else
AFFECTED_JSON=$(echo "$AFFECTED" | jq -R -s -c 'split("\n") | map(select(. != ""))')
echo "affected=$AFFECTED_JSON" >> $GITHUB_OUTPUT
echo "Affected doc pages: $AFFECTED_JSON"
echo "affected=$AFFECTED_JSON" >> "$GITHUB_OUTPUT"
echo "mapping_status=matched" >> "$GITHUB_OUTPUT"
fi

- name: Dispatch to automem-website
if: steps.check.outputs.affected != 'none'
uses: peter-evans/repository-dispatch@v3
with:
token: ${{ secrets.RELEASE_PLEASE_TOKEN }}
repository: verygoodplugins/automem-website
event-type: docs-update
client-payload: |
{
"source_repo": "${{ github.event.repository.full_name }}",
"source_sha": "${{ github.event.after }}",
"changed_files": ${{ steps.changed.outputs.files }},
"affected_docs": ${{ steps.check.outputs.affected }},
"commit_url": "${{ github.event.head_commit.url }}",
"compare_url": "${{ github.event.compare }}"
}
if: steps.check.outputs.mapping_status != 'none'
env:
GH_TOKEN: ${{ secrets.RELEASE_PLEASE_TOKEN }}
SOURCE_REPO: ${{ github.repository }}
HEAD_SHA: ${{ steps.changed.outputs.head_sha }}
BASE_SHA: ${{ steps.changed.outputs.base_sha }}
CHANGED_FILES: ${{ steps.changed.outputs.files }}
AFFECTED_DOCS: ${{ steps.check.outputs.affected }}
MAPPING_STATUS: ${{ steps.check.outputs.mapping_status }}
REASON: ${{ steps.changed.outputs.reason }}
COMMIT_URL: ${{ steps.changed.outputs.commit_url }}
COMPARE_URL: ${{ steps.changed.outputs.compare_url }}
run: |
jq -n \
--arg event_type "docs-update" \
--arg source_repo "$SOURCE_REPO" \
--arg source_sha "$HEAD_SHA" \
--arg base_sha "$BASE_SHA" \
--arg head_sha "$HEAD_SHA" \
--arg changed_files "$CHANGED_FILES" \
--arg affected_docs "$AFFECTED_DOCS" \
--arg mapping_status "$MAPPING_STATUS" \
--arg reason "$REASON" \
--arg commit_url "$COMMIT_URL" \
--arg compare_url "$COMPARE_URL" \
'{
event_type: $event_type,
client_payload: {
source_repo: $source_repo,
source_sha: $source_sha,
base_sha: $base_sha,
head_sha: $head_sha,
changed_files: ($changed_files | fromjson),
affected_docs: ($affected_docs | fromjson),
mapping_status: $mapping_status,
reason: $reason,
commit_url: $commit_url,
compare_url: $compare_url
}
}' | gh api repos/verygoodplugins/automem-website/dispatches --input -
Loading