name: Cron

on:
  # Manual runs through the web UI and also non-main cron job triggering
  workflow_dispatch:
  # We also want this workflow triggered if the 'Run cron CI' label is added
  # or present when PR is updated
  pull_request:
    types:
      - synchronize
      - labeled
  # We want this workflow to always run on release branches.
  push:
    branches:
      - '*.*'
      - '!*backport*'

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

jobs:
  cron:
    # Run on all triggers other than pull_request unless there's a label
    if: (github.event_name != 'pull_request' || contains(github.event.pull_request.labels.*.name, 'Run cron CI'))
    uses: OpenAstronomy/github-actions-workflows/.github/workflows/tox.yml@a138926f6e4f9667d1306c24f24f5bdcaa01fbab # v2.5.0
    with:
      default_python: '3.12'
      submodules: false
      coverage: codecov
      toxdeps: tox-pypi-filter
      envs: |
        - linux: py313-minimal-noana
        - linux: py314-devdeps-reportlog
          libraries:
            apt:
              - libopenjp2-7
          artifact-path: |
            .tmp/py314-devdeps-reportlog/pytest-log.jsonl

    secrets:
      CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

  open-issue:
    if: failure() && github.ref == 'refs/heads/main'
    needs: [cron]
    runs-on: ubuntu-latest
    permissions:
      issues: write
    steps:
      - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          name: py314-devdeps-reportlog-(ubuntu-latest)
          path: pytest-log

      - run: ls -lha pytest-log/

      - uses: scientific-python/issue-from-pytest-log-action@8e905db353437cda1d6a773de245343fbfc940dd # v1.5.0
        with:
          log-path: pytest-log/pytest-log.jsonl
          issue-title: "Cron devdeps build failure"
          issue-label: "Infrastructure"

  cron-online:
    # Run on all triggers other than pull_request unless there's a label
    if: (github.event_name != 'pull_request' || contains(github.event.pull_request.labels.*.name, 'Run cron CI'))
    uses: OpenAstronomy/github-actions-workflows/.github/workflows/tox.yml@a138926f6e4f9667d1306c24f24f5bdcaa01fbab # v2.5.0
    with:
      default_python: '3.12'
      submodules: false
      coverage: codecov
      toxdeps: tox-pypi-filter
      libraries: |
        apt:
          - libopenjp2-7
      envs: |
        - linux: linkcheck
          pytest: false
        - linux: py314-devdeps-online
          posargs: -m "remote_data"
        - windows: py313-online
          posargs: -m "remote_data"

    secrets:
      CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

  conda:
    if: (github.event_name != 'pull_request' || contains(github.event.pull_request.labels.*.name, 'Run cron CI'))
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-depth: 0
          lfs: true
          persist-credentials: false

      - uses: conda-incubator/setup-miniconda@fc2d68f6413eb2d87b895e92f8584b5b94a10167 # v3.3.0
        with:
          activate-environment: sunpy-test
          environment-file: sunpy-dev-env.yml
          python-version: "3.13"
          conda-remove-defaults: "true"

      - name: Install sunpy
        shell: bash -el {0}
        run: |
          pip install --no-deps --no-build-isolation .

      - name: Run test
        shell: bash -el {0}
        run: |
          conda list
          cd /tmp
          pytest -vvv -r a --pyargs sunpy --cov-report=xml --cov=sunpy --cov-config=$GITHUB_WORKSPACE/pyproject.toml $GITHUB_WORKSPACE/docs -n auto --color=yes
      - uses: codecov/codecov-action@671740ac38dd9b0130fbe1cec585b89eea48d3de # v5.5.2
        with:
          token: ${{ secrets.CODECOV_TOKEN }}  # zizmor: ignore[secrets-outside-env]
          files: ./coverage.xml

  notify:
    if: ${{ !cancelled() && github.event_name == 'workflow_dispatch' }}
    needs: [cron, cron-online, conda]
    runs-on: ubuntu-latest
    environment:
      name: matrix
    steps:
      - uses: Cadair/matrix-notify-action@31c7cc36051ee7dc4157a22c1f13dbc7d68e5120 # v2
        with:
          workflow_description: "Cron Workflow"
          matrix_token: '${{ secrets.MATRIX_ACCESS_TOKEN }}'
          github_token: '${{ secrets.GITHUB_TOKEN }}'
          homeserver: '${{ secrets.MATRIX_HOMESERVER }}'
          roomid: '!JYqfIVJjWANcHnfktY:cadair.com'
          ignore_pattern: '.*(Load|report overall).*'
          summarise_success: true
