name: Publish Python Package

on:
  release:
    types:
      - released
  pull_request:
    branches:
      - master
  push:
    branches:
      - master
    tags:
     - RELEASE_3_*_*
     - PRERELEASE_3_*_*
  workflow_run:
    workflows: ["Python package"]
    branches: [main]
    types:
      - completed
jobs:
  deploy:
    # the 2nd condition
    if: ${{ github.event.workflow_run.conclusion == 'success' }}
    runs-on: ubuntu-latest
    steps:
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: "3.10"
    - name: Download rpy2-rinterface (sdist).
      uses: actions/upload-artifact@v4
      with:
        name: rpy2-rinterface-sdist
        path: rpy2-rinterface/dist/rpy2_rinterface-*.tar.gz
    - name: Download rpy2-rinterface (wheel).
      uses: actions/download-artifact@v4.1.7
      with:
        pattern: rpy2-rinterface-*-py*-rrelease
        path: rpy2-rinterface/dist/
    - name: Download rpy2-rinterface (wheel)
      uses: actions/download-artifact@v4.1.7
      with:
        pattern: rpy2-robjects-*
        path: rpy2-robjects/dist/
    - name: rpy2 (sdist)
      uses: actions/download-artifact@v4.1.7
      with:
        name: rpy2-sdist
        path: dist/rpy2-*.tar.gz
    - name: Publish rinterface
      if: github.event_name == 'release'
      uses: pypa/gh-action-pypi-publish@release/v1.12
      with:
        user: __token__
        password: ${{ secrets.PYPI_API_TOKEN }}
        packages-dir: rpy2-rinterface-dist/
    - name: Publish robjects
      if: github.event_name == 'release'
      uses: pypa/gh-action-pypi-publish@release/v1.12
      with:
        user: __token__
        password: ${{ secrets.PYPI_API_TOKEN }}
        packages-dir: rpy2-robjects-dist/
