name: Issue Handler

on:
  issues:
    types: [ opened, edited, closed, reopened ]

concurrency:
  group: issue_handler
  cancel-in-progress: false

jobs:
  check-issue:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Check issue title
        id: check_title
        run: |
          ISSUE_TITLE="${{ github.event.issue.title }}"
          if [[ "$ISSUE_TITLE" == Plugin:* || "$ISSUE_TITLE" == Resource:* ]]; then
            echo "Title starts with Plugin: or Resource:."
            echo "::set-output name=title_match::true"
          else
            echo "Title does not start with Plugin: or Resource:."
            echo "::set-output name=title_match::false"
          fi

      - name: Set up Python
        uses: actions/setup-python@v2
        if: steps.check_title.outputs.title_match == 'true'
        with:
          python-version: '3.12'

      - name: Install dependencies
        if: steps.check_title.outputs.title_match == 'true'
        run: |
          echo "Installing dependencies."
          pip install -r liteyuki_flow/requirements.txt

      - name: Run Plugin/Resource issue handler
        if: steps.check_title.outputs.title_match == 'true'
        run: |
          echo "Running Plugin/Resource issue handler."
          python -m liteyuki_flow --handle
        env:
          GITHUB_TOKEN: ${{ secrets.TOKEN }}
          ISSUE_NUMBER: ${{ github.event.issue.number }}
          REPOSITORY: ${{ github.repository }}
          ACT_TYPE: ${{ github.event.action }}