Files
eatvio_app/.gitea/workflows/contract-sync.yml

65 lines
3.6 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
name: "Contract sync: flutter manifest -> eatvio-web"
# Автоматический перенос манифеста API-контракта из eatvio_app в eatvio-web.
# Триггер: push в main с изменениями api_contract/**.
# Результат: PR в eatvio-web с обновлённым tests/Fixtures/Contract/flutter_model_contract.json.
# Далее CI eatvio-web сам прогонит FlutterApiContractTest и покажет,
# выполняет ли бэкенд требования новых моделей.
on:
push:
branches:
- main
paths:
- "api_contract/**"
workflow_dispatch: # ручной запуск для проверки секрета/прав: Actions → Contract sync → Run workflow
jobs:
sync-manifest:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Open PR in eatvio-web with updated manifest
env:
TOKEN: ${{ secrets.CONTRACT_SYNC_TOKEN }} # PAT с write на eatvio/eatvio-web
API: http://gitea.office.local:3000/api/v1
BACKEND_REPO: eatvio/eatvio-web
SRC: api_contract/flutter_model_contract.json
DST: tests/Fixtures/Contract/flutter_model_contract.json
run: |
set -e
SHORT_SHA="${GITHUB_SHA::8}"
BRANCH="contract-sync/manifest-${SHORT_SHA}"
auth="Authorization: token ${TOKEN}"
DEFAULT_BRANCH=$(curl -sf -H "$auth" "${API}/repos/${BACKEND_REPO}" | grep -o '"default_branch":"[^"]*"' | head -1 | cut -d'"' -f4)
# Файл не изменился относительно предыдущего синка — выходим тихо.
REMOTE_SHA=$(curl -sf -H "$auth" "${API}/repos/${BACKEND_REPO}/contents/${DST}?ref=${DEFAULT_BRANCH}" | grep -o '"sha":"[^"]*"' | head -1 | cut -d'"' -f4)
LOCAL_SHA=$(git hash-object -- "${SRC}")
if [ "${REMOTE_SHA}" = "${LOCAL_SHA}" ]; then
echo "Manifest already in sync with ${BACKEND_REPO}."
exit 0
fi
# 1. Ветка от дефолтной (если уже существует — переиспользуем).
curl -sf -X POST -H "$auth" -H "Content-Type: application/json" \
-d "{\"new_branch_name\":\"${BRANCH}\",\"old_branch_name\":\"${DEFAULT_BRANCH}\"}" \
"${API}/repos/${BACKEND_REPO}/branches" || echo "branch ${BRANCH} already exists"
# 2. Обновить файл (contents API требует sha текущего файла).
CONTENT_B64=$(base64 -w0 "${SRC}")
curl -sf -X PUT -H "$auth" -H "Content-Type: application/json" \
-d "{\"content\":\"${CONTENT_B64}\",\"message\":\"chore(contract): sync flutter manifest (${SHORT_SHA})\",\"sha\":\"${REMOTE_SHA}\",\"branch\":\"${BRANCH}\"}" \
"${API}/repos/${BACKEND_REPO}/contents/${DST}" > /dev/null
# 3. PR (если уже открыт — пропускаем).
curl -sf -X POST -H "$auth" -H "Content-Type: application/json" \
-d "{\"title\":\"[contract-sync] flutter manifest ${SHORT_SHA}\",\"head\":\"${BRANCH}\",\"base\":\"${DEFAULT_BRANCH}\",\"body\":\"Автоматический синк манифеста из eatvio_app@${GITHUB_SHA}.\\n\\nCI бэкенда прогонит FlutterApiContractTest — если бэкенд не выполняет новые требования моделей, тест упадёт на этом PR.\"}" \
"${API}/repos/${BACKEND_REPO}/pulls" > /dev/null \
|| echo "PR already exists for ${BRANCH}"
echo "Sync PR opened: ${BACKEND_REPO} <- ${BRANCH}"