name: Protobuf Janitor

on:
  schedule:
    # Run daily at 10 AM UTC (2 AM PDT)
    - cron: 0 10 * * *
  workflow_dispatch:

permissions: {}

jobs:
  stale-prs:
    name: Close Stale Copybara PRs
    runs-on: ubuntu-latest
    permissions:
      contents: write # to allow deleting branches
      pull-requests: write # to allow closing the PR
    env:
      GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      GH_REPO: ${{ github.repository }}
    steps:
      - run: |
          set -ex
          STALE_PRS=$(gh pr list --author "app/copybara-service" --limit 500 \
              --json "number" --search "updated:<=$(date --date="-7 day" +%F)" \
            | jq ".[].number")
          for pr in $STALE_PRS; do
            echo "Closing #$pr..."
            gh pr close --comment "Auto-closing Copybara pull request" --delete-branch "$pr"
          done

  stale-others:
    name: Close stale non-copybara PRs and issues
    runs-on: ubuntu-latest
    permissions:
      issues: write # allow the action to comment on, add labels to, and close issues
      pull-requests: write # allow the action to comment on, add labels to, and close PRs
    steps:
      - uses: actions/stale@997185467fa4f803885201cee163a9f38240193d  # v10.1.1
        with:
          stale-issue-message: >
            We triage inactive PRs and issues in order to make it easier to find
            active work. If this issue should remain active or becomes active
            again, please add a comment.


            This issue is labeled `inactive` because the last activity was over
            90 days ago. This issue will be closed and archived after 14
            additional days without activity.
          close-issue-message: >
            We triage inactive PRs and issues in order to make it easier to find
            active work. If this issue should remain active or becomes active
            again, please reopen it.


            This issue was closed and archived because there has been no new
            activity in the 14 days since the `inactive` label was added.
          stale-pr-message: >
            We triage inactive PRs and issues in order to make it easier to find
            active work. If this PR should remain active, please add a comment.


            This PR is labeled `inactive` because the last activity was over 90
            days ago. This PR will be closed and archived after 14 additional
            days without activity.
          close-pr-message: >
            We triage inactive PRs and issues in order to make it easier to find
            active work. If this PR should remain active or becomes active
            again, please reopen it.


            This PR was closed and archived because there has been no new
            activity in the 14 days since the `inactive` label was added.
          stale-issue-label: 'inactive'
          stale-pr-label: 'inactive'
          exempt-issue-labels: 'help wanted,keep open'
          labels-to-add-when-unstale: 'untriaged'
          days-before-stale: 90
          days-before-close: 14
          operations-per-run: 100
      - uses: actions/stale@997185467fa4f803885201cee163a9f38240193d  # v10.1.1
        with:
          # Have any reply on a 'wait for user action' issue or PR to readd 'untriaged' label. This
          # uses the same action as our actual stale issue management, but sets
          # days-before-stale to -1 to have it never automatically add the 'wait for user action'
          # label. Instead this run will just only view cases that have the label manually set as
          # 'stale' for the purpose of a reply unstaling it by readding the 'untriaged' label.
          days-before-stale: -1
          days-before-close: -1
          stale-issue-label: 'wait for user action'
          stale-pr-label: 'wait for user action'
          labels-to-add-when-unstale: 'untriaged'
          operations-per-run: 20
          exempt-pr-labels: 'safe for tests'

