name: Stale

on:
  # NOTE: github.event is workflow_dispatch payload:
  # https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#workflow_dispatch
  workflow_dispatch:
    inputs:
      dryrun:
        description: 'dryrun: Preview stale issues/prs without marking them (true|false)'
        required: true
        type: boolean
        default: true

  schedule:
    - cron: 0 4 * * *

permissions:
  issues: write
  pull-requests: write

jobs:
  stale:
    if: '!github.event.repository.fork'
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          - only-issue-labels: ''
            days-before-issue-stale: 365
            days-before-issue-close: 30
          # [type::support] issues have a more aggressive stale/close timeline
          - only-issue-labels: type::support
            days-before-issue-stale: 90
            days-before-issue-close: 21
    steps:
      - uses: conda/actions/read-yaml@976289d0cfd85139701b26ddd133abdd025a7b5f # v24.5.0
        id: read_yaml
        with:
          path: https://raw.githubusercontent.com/conda/infra/main/.github/messages.yml

      - uses: actions/stale@28ca1036281a5e5922ead5184a1bbf96e5fc984e # v9.0.0
        id: stale
        with:
          # Only issues with these labels are checked whether they are stale
          only-issue-labels: ${{ matrix.only-issue-labels }}

          # Idle number of days before marking issues stale
          days-before-issue-stale: ${{ matrix.days-before-issue-stale }}
          # Idle number of days before closing stale issues/PRs
          days-before-issue-close: ${{ matrix.days-before-issue-close }}
          # Idle number of days before marking PRs stale
          days-before-pr-stale: 365
          # Idle number of days before closing stale PRs
          days-before-pr-close: 30

          # Comment on the staled issues
          stale-issue-message: ${{ fromJSON(steps.read_yaml.outputs.value)['stale-issue'] }}
          # Label to apply on staled issues
          stale-issue-label: stale
          # Label to apply on closed issues
          close-issue-label: stale::closed
          # Reason to use when closing issues
          close-issue-reason: not_planned

          # Comment on the staled PRs
          stale-pr-message: ${{ fromJSON(steps.read_yaml.outputs.value)['stale-pr'] }}
          # Label to apply on staled PRs
          stale-pr-label: stale
          # Label to apply on closed PRs
          close-pr-label: stale::closed

          # Remove stale label from issues/PRs on updates/comments
          remove-stale-when-updated: true
          # Add specified labels to issues/PRs when they become unstale
          labels-to-add-when-unstale: stale::recovered
          # Remove specified labels to issues/PRs when they become unstale
          labels-to-remove-when-unstale: stale,stale::closed

          # Max number of operations per run
          operations-per-run: ${{ secrets.STALE_OPERATIONS_PER_RUN || 100 }}
          # Dry-run
          debug-only: ${{ github.event.inputs.dryrun || false }}
          # Order to get issues/PRs
          ascending: true
          # Delete branch after closing a stale PR
          delete-branch: false

          # Issues with these labels will never be considered stale
          exempt-issue-labels: stale::recovered,epic
          # Issues with these labels will never be considered stale
          exempt-pr-labels: stale::recovered,epic
          # Exempt all issues/PRs with milestones from stale
          exempt-all-milestones: true
          # Assignees on issues/PRs exempted from stale
          exempt-assignees: mingwandroid

      - name: Print outputs
        run: echo ${{ join(steps.stale.outputs.*, ',') }}
