Skip to content

Kolada API Auto-Sync #66

Kolada API Auto-Sync

Kolada API Auto-Sync #66

Workflow file for this run

name: Kolada API Auto-Sync
on:
schedule:
- cron: '0 4 * * *' # Körs 04:00 varje natt
workflow_dispatch: # Möjlighet att starta manuellt
jobs:
sync-and-verify:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v6
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: 🔒 Backup current state
id: backup
run: |
echo "💾 Creating safety backup..."
git add -A
git stash push -m "Auto-backup before API sync $(date +%Y-%m-%d_%H-%M-%S)" || echo "No changes to stash"
echo "backup_created=true" >> $GITHUB_OUTPUT
- name: Generate API Types
id: generate
continue-on-error: true
run: |
OUTPUT_FILE="src/config/kolada-schema.generated.ts"
echo "🔄 Fetching OpenAPI spec from Kolada..."
npx openapi-typescript https://api.kolada.se/v3/openapi.json -o $OUTPUT_FILE
# Lägg till header
sed -i '1i\/** AUTO-GENERATED FILE - DO NOT EDIT **/' $OUTPUT_FILE
# Kolla om något faktiskt ändrades
if [[ -n $(git status --porcelain $OUTPUT_FILE) ]]; then
echo "changed=true" >> $GITHUB_OUTPUT
echo "✅ New changes detected in API definition"
else
echo "changed=false" >> $GITHUB_OUTPUT
echo "No changes detected"
fi
- name: Safety Check (Gatekeeper)
id: safety_check
if: steps.generate.outputs.changed == 'true' && steps.generate.outcome == 'success'
continue-on-error: true
run: |
echo "🛡️ Verifying compatibility..."
npm test tests/contract/type-safety.test.ts
npm run build
- name: 🔄 Auto-restore on failure
if: steps.generate.outcome == 'failure' || steps.safety_check.outcome == 'failure'
run: |
echo "❌ Generation or tests failed - restoring previous state..."
git reset --hard HEAD
git stash pop || echo "No stash to restore"
echo "✅ Restored to safe state"
exit 1
- name: 🧹 Cleanup backup on success
if: steps.generate.outcome == 'success' && (steps.safety_check.outcome == 'success' || steps.generate.outputs.changed == 'false')
run: |
git stash drop || echo "No stash to drop"
- name: Create Pull Request
if: steps.generate.outputs.changed == 'true' && success()
uses: peter-evans/create-pull-request@v7
with:
token: ${{ secrets.GITHUB_TOKEN }}
branch: kolada-api-update
title: "🤖 API Update: Kolada structure changed"
body: |
### 🤖 Kolada API Update Detected
Changes detected in Kolada API v3.
**Safety Report:**
- ✅ Tests Passed
- ✅ Build Passed
This PR updates the internal schema to match the live API.