# Ensures version numbers in pyproject.toml and version.py stay in sync.
#
# (Prevents releases with mismatched version numbers)

name: "🔍 Check Version Equality"

on:
  pull_request:
    paths:
      - "libs/core/pyproject.toml"
      - "libs/core/langchain_core/version.py"
      - "libs/partners/anthropic/pyproject.toml"
      - "libs/partners/anthropic/langchain_anthropic/_version.py"

permissions:
  contents: read

jobs:
  check_version_equality:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6

      - name: "✅ Verify pyproject.toml & version.py Match"
        run: |
          # Check core versions
          CORE_PYPROJECT_VERSION=$(grep -Po '(?<=^version = ")[^"]*' libs/core/pyproject.toml)
          CORE_VERSION_PY_VERSION=$(grep -Po '(?<=^VERSION = ")[^"]*' libs/core/langchain_core/version.py)

          # Compare core versions
          if [ "$CORE_PYPROJECT_VERSION" != "$CORE_VERSION_PY_VERSION" ]; then
            echo "langchain-core versions in pyproject.toml and version.py do not match!"
            echo "pyproject.toml version: $CORE_PYPROJECT_VERSION"
            echo "version.py version: $CORE_VERSION_PY_VERSION"
            exit 1
          else
            echo "Core versions match: $CORE_PYPROJECT_VERSION"
          fi

          # Check langchain_v1 versions
          LANGCHAIN_PYPROJECT_VERSION=$(grep -Po '(?<=^version = ")[^"]*' libs/langchain_v1/pyproject.toml)
          LANGCHAIN_INIT_PY_VERSION=$(grep -Po '(?<=^__version__ = ")[^"]*' libs/langchain_v1/langchain/__init__.py)

          # Compare langchain_v1 versions
          if [ "$LANGCHAIN_PYPROJECT_VERSION" != "$LANGCHAIN_INIT_PY_VERSION" ]; then
            echo "langchain_v1 versions in pyproject.toml and __init__.py do not match!"
            echo "pyproject.toml version: $LANGCHAIN_PYPROJECT_VERSION"
            echo "version.py version: $LANGCHAIN_INIT_PY_VERSION"
            exit 1
          else
            echo "Langchain v1 versions match: $LANGCHAIN_PYPROJECT_VERSION"
          fi

          # Check langchain-anthropic versions
          ANTHROPIC_PYPROJECT_VERSION=$(grep -Po '(?<=^version = ")[^"]*' libs/partners/anthropic/pyproject.toml)
          ANTHROPIC_VERSION_PY_VERSION=$(grep -Po '(?<=^__version__ = ")[^"]*' libs/partners/anthropic/langchain_anthropic/_version.py)

          # Compare langchain-anthropic versions
          if [ "$ANTHROPIC_PYPROJECT_VERSION" != "$ANTHROPIC_VERSION_PY_VERSION" ]; then
            echo "langchain-anthropic versions in pyproject.toml and _version.py do not match!"
            echo "pyproject.toml version: $ANTHROPIC_PYPROJECT_VERSION"
            echo "_version.py version: $ANTHROPIC_VERSION_PY_VERSION"
            exit 1
          else
            echo "Langchain-anthropic versions match: $ANTHROPIC_PYPROJECT_VERSION"
          fi
