// Jenkins declarative pipeline example: judge a change against a base commit, record the SARIF file with the
// Warnings Next Generation plugin (recordIssues ... sarif), and archive the evidence folder.
// The agent needs bash, OpenSSL (3.5 or later for the TLS cross-check), git and Python 3.9 or later.
// BASE_COMMIT is the commit to compare with, for example the target branch of a pull request.
// The converted tree is the workspace (.), and anything inside it is judged as part of it. So the venv, the original
// tree, the evidence folder and the SARIF file live in WORKSPACE_TMP (outside the workspace) while PQ-Proof runs, and
// the report is copied into pqproof-report/ afterwards; the next build deletes that folder before it judges.
pipeline {
  agent any
  parameters {
    string(name: 'BASE_COMMIT', defaultValue: 'origin/main', description: 'commit before the migration')
  }
  environment {
    REQUIRE_REVIEW = '1'
  }
  stages {
    stage('PQ-Proof') {
      steps {
        sh '''
          rm -rf pqproof-report
          T="${WORKSPACE_TMP:-$(mktemp -d)}/pqproof"
          rm -rf "$T" && mkdir -p "$T"
          python3 -m venv "$T/venv"
          "$T/venv/bin/pip" install --quiet "pqproof==0.6.0"
          # Not git archive: it drops export-ignore paths, and unchanged files would look added.
          GIT_INDEX_FILE="$T/base.idx" git read-tree "$BASE_COMMIT"
          GIT_INDEX_FILE="$T/base.idx" git checkout-index -a --prefix="$T/original/"
          if git cat-file -e "$BASE_COMMIT:notes/unconverted.json" 2>/dev/null; then
            git show "$BASE_COMMIT:notes/unconverted.json" > "$T/base-unconverted.json"
            export ALLOW="$T/base-unconverted.json"
          else
            export ALLOW=none
          fi
          code=0
          OUT="$T/evidence" "$T/venv/bin/pqproof" "$T/original" . || code=$?
          "$T/venv/bin/pqproof" --verify "$T/evidence"
          "$T/venv/bin/pqproof" sarif "$T/evidence" -o "$T/pqproof.sarif"
          mkdir pqproof-report && cp -R "$T/evidence" pqproof-report/evidence && cp "$T/pqproof.sarif" pqproof-report/
          exit $code
        '''
      }
      post {
        always {
          recordIssues(tools: [sarif(pattern: 'pqproof-report/pqproof.sarif', id: 'pqproof', name: 'PQ-Proof')])
          archiveArtifacts artifacts: 'pqproof-report/**', allowEmptyArchive: true
        }
      }
    }
  }
}
