name: SLSA Provenance
on:
  release:
    types: [published]

permissions:
  contents: read

jobs:
  provenance:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      id-token: write
      attestations: write
    steps:
      - uses: step-security/harden-runner@ab7a9404c0f3da075243ca237b5fac12c98deaa5 # v2.19.3
        with:
          egress-policy: audit
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          ref: ${{ github.event.release.tag_name }}
          persist-credentials: false
      - uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          node-version: 22
      - run: npm ci --ignore-scripts
      - run: npm run build
      - id: attest
        uses: actions/attest-build-provenance@a2bbfa25375fe432b6a289bc6b6cd05ecd0c4c32 # v4.1.0
        with:
          subject-path: 'dist/purify.js,dist/purify.min.js,dist/purify.cjs.js,dist/purify.es.mjs'
      - name: Upload provenance to release
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          BUNDLE_PATH: ${{ steps.attest.outputs.bundle-path }}
          TAG: ${{ github.event.release.tag_name }}
        run: |
          cp "$BUNDLE_PATH" "${TAG}.intoto.jsonl"
          gh release upload "$TAG" "${TAG}.intoto.jsonl" --clobber
