name: Build, tag and release

on:
  push:
    branches:
      - main
  pull_request:
    types:
      - opened
      - synchronize

env:
  environment: test
  release_channel: latest
  DO_NOT_TRACK: "1"
  NODE_VERSION: "23.x"
  DOCKER_REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}
  PNPM_VERSION: 10.6.0

permissions:
  contents: read
  packages: read

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: "${{ env.NODE_VERSION }}"
          registry-url: "${{ env.NODE_REGISTRY }}"

      - uses: pnpm/action-setup@v4
        name: Install pnpm
        with:
          version: ${{ env.PNPM_VERSION }}
          run_install: false

      - name: Get pnpm store directory
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV

      - uses: actions/cache@v4
        name: Setup pnpm cache
        with:
          path: ${{ env.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-

      - name: Install dependencies
        run: pnpm install
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: Run tests
        run: pnpm test

  update-release-draft:
    name: Update release drafter
    if: github.ref == 'refs/heads/main'
    permissions:
      contents: write
      pull-requests: write
    needs: build
    environment: release
    runs-on: ubuntu-latest
    steps:
      - id: create-release
        uses: release-drafter/release-drafter@v6
        with:
          config-name: release-drafter-config.yml
          publish: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - uses: actions/checkout@v4

      - name: Upload Release Asset
        id: upload-release-asset 
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create-release.outputs.upload_url }}
          asset_path: ./operator.yaml
          asset_name: operator.yaml
          asset_content_type: application/yaml