name: Release Build
on:
  push:
    tags:
      - v*

jobs:
  release:
    runs-on: ubuntu-latest
    container:
      image: golang:1.21
    steps:
      - name: Checkout repository
        run: |
          git clone https://oauth2:${{ secrets.GITEATOKEN }}@direct-dev.ru/gitea/GiteaAdmin/hello_gitea.git hello_gitea
          cd hello_gitea
          git checkout ${{ github.ref }}

      - name: Setup Go
        run: |
          # Install jq for JSON parsing
          apt-get update && apt-get install -y jq
          git --version
          go version
          jq --version

      - name: Build
        run: |
          cd hello_gitea
          mkdir -p bin
          go build -o bin/hello-api-${{ github.ref_name }} main.go
          ls -la bin/

      - name: Create Release
        run: |
          cd hello_gitea
          # Create release using Gitea API
          curl -X POST \
            -H "Authorization: token ${{ secrets.GITEATOKEN }}" \
            -H "Content-Type: application/json" \
            -d '{
              "tag_name": "${{ github.ref_name }}",
              "name": "Release ${{ github.ref_name }}",
              "body": "Automated release",
              "draft": false,
              "prerelease": false
            }' \
            "https://direct-dev.ru/gitea/api/v1/repos/GiteaAdmin/hello_gitea/releases"
          
          # Upload asset
          RELEASE_ID=$(curl -s -H "Authorization: token ${{ secrets.GITEATOKEN }}" \
            "https://direct-dev.ru/gitea/api/v1/repos/GiteaAdmin/hello_gitea/releases/tags/${{ github.ref_name }}" | \
            jq -r '.id')
          
          curl -X POST \
            -H "Authorization: token ${{ secrets.GITEATOKEN }}" \
            -H "Content-Type: application/octet-stream" \
            --data-binary @bin/hello-api-${{ github.ref_name }} \
            "https://direct-dev.ru/gitea/api/v1/repos/GiteaAdmin/hello_gitea/releases/$RELEASE_ID/assets?name=hello-api-${{ github.ref_name }}"