# SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
#
# SPDX-License-Identifier: CC0-1.0

name: Build
on:
    - push
    - pull_request

permissions:
    pull-requests: write

jobs:
    build:
        name: Build
        runs-on: ubuntu-latest
        steps:
            -   uses: actions/checkout@v4
                name: Checkout repository
                with:
                    fetch-tags: true
                    fetch-depth: 0
                    filter: 'tree:0'
            -   uses: ./.github/composite/gradle
            -   name: Build with gradle
                run: |
                    ./gradlew remapJar --scan
            -   name: Move build artifact around and print check sum
                run: |
                    rm -f build/libs/*sources*.jar
                    sha256sum build/libs/*.jar
            -   name: Upload build artifact
                uses: actions/upload-artifact@v4
                with:
                    name: built-mod-jar
                    path: build/libs/Firmament-*.jar

    test:
        name: Test
        needs: build
        runs-on: ubuntu-latest
        steps:
            -   uses: actions/checkout@v4
                name: Checkout repository
                with:
                    fetch-tags: true
                    fetch-depth: 0
                    filter: 'tree:0'
            -   uses: ./.github/composite/gradle
            -   name: Build with gradle
                run: |
                    ./gradlew test --scan

    upload:
        name: Upload
        runs-on: ubuntu-latest
        needs: build
        if: ${{ 'push' == github.event_name && 'master' == github.ref_name && '637563904' == github.repository_id }}
        steps:
            -   name: Download generated artifact
                uses: actions/download-artifact@v4
                with:
                    name: built-mod-jar
            -   name: Upload to discord
                run: |
                    ls -lahR .
                    curl "$WEBHOOK_URL" -X POST -H "Content-type: multipart/form-data" --form "files[0]=@$(echo *.jar)"
                env:
                    WEBHOOK_URL: ${{ secrets.WEBHOOK_URL }}
