name: Test build

on:
  push:
    tags-ignore:
      - "*"
    branches-ignore:
      - master
      - ci

jobs:
  build:
    runs-on: ubuntu
    steps:
      - uses: actions/checkout@v6
        with:
          submodules: recursive
      - name: Check commit
        id: check
        run: |
          git fetch && git checkout master
          sha=$(git rev-parse HEAD)
          [[ "${{ github.sha }}" == "${sha}" ]] && test=false || test=true
          echo "test=${test}" >> "$FORGEJO_OUTPUT"
      - name: Restore cargo cache
        id: cache-cargo-restore
        uses: actions/cache/restore@v5
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: ${{ runner.os }}-test-cargo-${{ hashFiles('**/Cargo.lock') }}
      - name: Setup registry
        run: |
          echo -e '[registries.nexus]\nindex = "sparse+${{ secrets.MAVEN_HOST }}/repository/cargo/"\n[registry]\ndefault = "nexus"\n[source.crates-io]\nreplace-with = "nexus"\n[source.nexus]\nregistry = "sparse+${{ secrets.MAVEN_HOST }}/repository/cargo/"' > ~/.cargo/config.toml
      - name: Build
        if: ${{ steps.check.outputs.test == 'true' }}
        run: cargo build --release
      - name: Save cargo cache
        uses: actions/cache/save@v5
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: ${{ steps.cache-cargo-restore.outputs.cache-primary-key }}
      - name: Telegram Notify Channel
        if: ${{ steps.check.outputs.test == 'true' && (success() || failure()) }}
        uses: actions/telegram-notifier@main
        with:
          api_url: ${{ secrets.TELEGRAM_API_URL }}
          token: ${{ secrets.TELEGRAM_BOT_TOKEN }}
          chat_id: ${{ secrets.TELEGRAM_CHANNEL_ID }}
          status: ${{ job.status }}
          notify_fields: "actor,repository,workflow,branch,commit"
      - name: Telegram Notify Group
        if: ${{ steps.check.outputs.test == 'true' && (success() || failure()) }}
        uses: actions/telegram-notifier@main
        with:
          api_url: ${{ secrets.TELEGRAM_API_URL }}
          token: ${{ secrets.TELEGRAM_BOT_TOKEN }}
          chat_id: ${{ secrets.TELEGRAM_GROUP_ID }}
          status: ${{ job.status }}
          notify_fields: "actor,repository,workflow,branch,commit"