komikku/.forgejo/workflows/upstream_status.yml

101 lines
4.3 KiB
YAML
Raw Permalink Normal View History

name: Upstream sync status
# Lightweight job: fetch upstream from GitHub, write status files, optional issue.
on:
schedule:
- cron: '0 6 * * 1'
workflow_dispatch:
permissions:
contents: write
issues: write
concurrency:
group: upstream-status
cancel-in-progress: true
jobs:
check:
runs-on: ubuntu-latest
steps:
- name: Checkout and check upstream
shell: bash
env:
FORGEJO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BEHIND_WARN: '10'
run: |
set -euo pipefail
rm -rf .git
git init .
origin_url="${GITHUB_SERVER_URL#https://}"
origin_url="${origin_url#http://}"
git remote add origin "https://x-access-token:${FORGEJO_TOKEN}@${origin_url}/${GITHUB_REPOSITORY}.git"
git -c http.sslVerify=false fetch --prune origin "${GITHUB_REF_NAME}"
git checkout --force FETCH_HEAD
git remote add upstream https://github.com/komikku-app/komikku.git
chmod +x scripts/check-upstream.sh
./scripts/check-upstream.sh --write
behind="$(python3 -c 'import json; print(json.load(open("docs/upstream-status.json"))["behind"])')"
ahead="$(python3 -c 'import json; print(json.load(open("docs/upstream-status.json"))["ahead"])')"
status="$(python3 -c 'import json; print(json.load(open("docs/upstream-status.json"))["status"])')"
echo "BEHIND=$behind" >> "$GITHUB_ENV"
echo "AHEAD=$ahead" >> "$GITHUB_ENV"
echo "STATUS=$status" >> "$GITHUB_ENV"
- name: Commit status files
shell: bash
env:
FORGEJO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -euo pipefail
git config user.name "forgejo-actions[bot]"
git config user.email "forgejo-actions[bot]@users.noreply.local"
git add UPSTREAM_STATUS.md docs/upstream-status.json
if git diff --staged --quiet; then
echo "No upstream status changes."
exit 0
fi
git commit -m "chore(ci): update upstream sync status"
origin_url="${GITHUB_SERVER_URL#https://}"
origin_url="${origin_url#http://}"
git push "https://x-access-token:${FORGEJO_TOKEN}@${origin_url}/${GITHUB_REPOSITORY}.git" "HEAD:${GITHUB_REF_NAME}"
- name: Open issue when far behind
if: env.STATUS == 'behind'
shell: bash
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -euo pipefail
title="Upstream sync: ${BEHIND} commits behind komikku-app/komikku"
body=$(cat <<EOF
The weekly upstream check found this fork is **${BEHIND} commits behind** upstream and **${AHEAD} commits ahead** with fork-specific changes.
See [UPSTREAM_STATUS.md](${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/src/branch/${GITHUB_REF_NAME}/UPSTREAM_STATUS.md) and [docs/upstream-sync.md](${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/src/branch/${GITHUB_REF_NAME}/docs/upstream-sync.md).
To merge upstream locally:
\`\`\`sh
git fetch upstream
git merge upstream/master
\`\`\`
EOF
)
api="${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/issues?state=open&labels=upstream-sync"
existing="$(curl -fsSL -H "Authorization: token ${GH_TOKEN}" "$api" | python3 -c 'import json,sys; issues=json.load(sys.stdin); print(next((str(i["number"]) for i in issues if i.get("title")==sys.argv[1]), ""))' "$title")"
if [[ -n "$existing" ]]; then
curl -fsSL -X PATCH \
-H "Authorization: token ${GH_TOKEN}" \
-H "Content-Type: application/json" \
-d "$(python3 -c 'import json,sys; print(json.dumps({"body": sys.argv[1]}))' "$body")" \
"${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/issues/${existing}"
echo "Updated issue #${existing}"
exit 0
fi
curl -fsSL -X POST \
-H "Authorization: token ${GH_TOKEN}" \
-H "Content-Type: application/json" \
-d "$(python3 -c 'import json,sys; print(json.dumps({"title": sys.argv[1], "body": sys.argv[2], "labels": ["upstream-sync"]}))' "$title" "$body")" \
"${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/issues"
echo "Created upstream sync issue"