name: Release Obsidian Plugin
on:
  workflow_dispatch:
    inputs:
      tag:
        description: Release tag to build
        required: true
        type: string
      draft:
        description: Create the GitHub Release as a draft
        required: false
        type: boolean
        default: true
      prerelease:
        description: Mark the GitHub Release as a pre-release
        required: false
        type: boolean
        default: false

jobs:
  build:
    runs-on: ubuntu-latest
    environment: release
    permissions:
      contents: write
      id-token: write
      attestations: write
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 0
        ref: ${{ inputs.tag }}
    - name: Use Node.js
      uses: actions/setup-node@v4
      with:
        node-version: '24.x'
    - name: Validate release
      env:
        TAG: ${{ inputs.tag }}
      run: |
        set -euo pipefail
        node utils/release-notes.mjs validate "${TAG}"
        HEAD_SHA="$(git rev-parse HEAD)"
        TAG_SHA="$(git rev-parse "refs/tags/${TAG}^{commit}")"
        if [[ "${HEAD_SHA}" != "${TAG_SHA}" ]]; then
          echo "Checked-out commit is ${HEAD_SHA}, but tag ${TAG} points to ${TAG_SHA}." >&2
          exit 1
        fi
    # Build the plugin
    - name: Build
      id: build
      run: |
        npm ci
        npm run build --if-present
    # Attest
    - name: Attest Plugin Artifacts
      uses: actions/attest-build-provenance@v4
      with:
          subject-path: |
              main.js
              manifest.json
              styles.css
    - name: Create Release and Upload Assets
      uses: softprops/action-gh-release@v2
      with:
        files: |
          main.js
          manifest.json
          styles.css
        name: ${{ inputs.tag }}
        tag_name: ${{ inputs.tag }}
        draft: ${{ inputs.draft }}
        prerelease: ${{ inputs.prerelease }}
