name: update swagger files

on:
  schedule:
    - cron:  '0 */12 * * *' # every 12 hours on the hour
  workflow_dispatch:

jobs:
  update-swagger:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: |
          wget https://github.com/go-gitea/gitea/blob/main/templates/swagger/v1_json.tmpl
          sed -i "$@" 's/"version": "{{AppVer | JSEscape | Safe}}"/"version": "dev"/' static/swagger-latest.json
          rm v1_json.tmpl

          wget https://github.com/go-gitea/gitea/blob/v1.22.4/templates/swagger/v1_json.tmpl
          sed -i "$@" 's/"version": "{{AppVer | JSEscape | Safe}}"/"version": "1.22.6"/' static/swagger-22.json
          rm v1_json.tmpl

          if ! [[ $(git diff --shortstat) ]]; then exit 0; fi
          git config --global user.name "Gitea Bot"
          git config --global user.email "teabot@gitea.io"
          git remote set-url origin https://x-access-token:${{ secrets.DEPLOY_TOKEN }}@gitea.com/gitea/docs.git
          git add --all
          git commit -m "[skip ci] Updated swagger files"
          git push
