name: generate-srcinfo

concurrency: ci-${{ github.ref }}

on:
  push:
    branches:
      - master
  workflow_dispatch:

jobs:
  update-srcinfo:
    runs-on: windows-latest
    if: ${{ github.repository == 'msys2/MINGW-packages' || github.event_name == 'workflow_dispatch' }}
    defaults:
      run:
        shell: msys2 {0}
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - uses: msys2/setup-msys2@v2
        with:
          msystem: MSYS
          install: python git binutils
          update: true

      - run: |
          # XXX: we don't need the toolchains for --printsrcinfo, but makepkg-mingw complains if gcc doesn't exist
          touch /mingw64/bin/gcc.exe /mingw32/bin/gcc.exe /ucrt64/bin/gcc.exe /clang64/bin/clang.exe /clang32/bin/clang.exe
          curl --fail -L --retry 5 -o srcinfo.json "https://github.com/$GITHUB_REPOSITORY/releases/download/srcinfo-cache/srcinfo.json"
          python .ci/ci-generate-srcinfo.py --time-limit 19800 mingw . srcinfo.json

      - uses: actions/upload-artifact@v2
        with:
          name: result
          path: srcinfo.json

  upload-srcinfo:
    needs: update-srcinfo
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/download-artifact@v2
        with:
          name: result

      - uses: eine/tip@master
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          tag: srcinfo-cache
          rm: true
          files: srcinfo.json

      - run: |
          curl -X POST 'https://packages.msys2.org/api/trigger_update'
