name: svf-build

# Triggers the workflow on push or pull request events
on: [push, pull_request]

permissions:
  contents: read
  id-token: write 

jobs:
  build:
    runs-on: ubuntu-latest
    environment: npm-publish
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: 20
          registry-url: https://registry.npmjs.org/
        
      - name: publish-svf-lib
        env:
          NODE_AUTH_TOKEN: ""
          NPM_CONFIG_USERCONFIG: ""
        run: |
          npm i -g npm@latest
          if [ "$RUNNER_OS" = "Linux" ]; then sudo apt-get update ; fi
          cat package.json
          npm publish --access public

  trigger-svf-python:
    if: github.event_name == 'push'  # Only run on push events
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout source repo
        uses: actions/checkout@v2

      - name: Check for changes in SVF-linux-x86_64
        id: changes
        run: |
          git fetch origin master --depth=3
          CHANGED=$(git diff --name-only HEAD~2 HEAD~1 | grep '^SVF-linux-x86_64' || true)
          echo "Changed files: $CHANGED"
          if [ -z "$CHANGED" ]; then
            echo "should_trigger=false" >> $GITHUB_OUTPUT
          else
            echo "should_trigger=true" >> $GITHUB_OUTPUT
          fi

      - name: Trigger SVF-Python workflow if needed
        if: steps.changes.outputs.should_trigger == 'true'
        run: |
          git clone https://github.com/SVF-tools/SVF-Python.git
          cd SVF-Python
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git commit --allow-empty -m "trigger: changes in SVF-linux-x86_64"
          git push https://yuleisui:${{secrets.yulei_git_key}}@github.com/SVF-tools/SVF-Python.git HEAD:main
