name: Test & Maybe Release
on: [push, pull_request]
jobs:
  test:
    strategy:
      fail-fast: false
      matrix:
        node: [lts/*, current]
        # not quite windows ready, halp! os: [macos-latest, ubuntu-latest, windows-latest]
        os: [macos-latest, ubuntu-latest]
    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v7.0.0
        with:
          fetch-depth: 0
      - name: Use Node.js ${{ matrix.node }}
        uses: actions/setup-node@v6.4.0
        with:
          node-version: ${{ matrix.node }}
      - name: Set up ghauth config (Ubuntu)
        run: |
          mkdir -p ~/.config/changelog-maker/
          echo '{"user": "nodejs", "token": "'${{ secrets.GITHUB_TOKEN }}'"}' > ~/.config/changelog-maker/config.json
        if: startsWith(matrix.os, 'ubuntu')

      - name: Set up ghauth config (macOS)
        run: |
          mkdir -p ~/Library/Application\ Support/changelog-maker/
          echo '{"user": "nodejs", "token": "'${{ secrets.GITHUB_TOKEN }}'"}' > ~/Library/Application\ Support/changelog-maker/config.json
        if: startsWith(matrix.os, 'macos')

      - name: Set up ghauth config (Windows)
        run: |
          mkdir "%LOCALAPPDATA%\changelog-maker\"
          echo {"user": "nodejs", "token": "${{ secrets.GITHUB_TOKEN }}"} > "%LOCALAPPDATA%\changelog-maker\config.json"
        shell: cmd
        if: startsWith(matrix.os, 'windows')
      - name: Install Dependencies
        run: |
          npm install --no-progress
      - name: Run tests
        run: |
          npm config set script-shell bash
          npm run test:ci
  release:
    name: Release
    permissions:
      contents: write
      id-token: write
    needs: test
    runs-on: ubuntu-latest
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    steps:
      - name: Checkout
        uses: actions/checkout@v7.0.0
        with:
          fetch-depth: 0
      - name: Setup Node.js
        uses: actions/setup-node@v6.4.0
        with:
          node-version: lts/*
          registry-url: 'https://registry.npmjs.org'
      - name: Install dependencies
        run: npm install --no-progress
      - name: Build
        run: npm run build
      - name: Release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_CONFIG_PROVENANCE: true
        run: npx semantic-release

