name: Needs Triage

on:
  issues:
    types:
      - opened
      - reopened
  schedule:
    - cron: '0 8 * * 1-5' # Every Mon-Fri at 8am UTC
  workflow_dispatch: # Manually trigger a run

run-name: ${{ github.event_name == 'issues' && format('Label issue \#{0} as needs-triage', github.event.issue.number) || 'Report old un-triaged issues' }}

jobs:
  add-label:
    name: Label Issue
    if: github.event_name == 'issues'
    runs-on: ubuntu-latest
    permissions:
      issues: write
    steps:
      - name: Generate a GitHub token
        id: generate-token
        uses: actions/create-github-app-token@7e473efe3cb98aa54f8d4bac15400b15fad77d94 # v2.2.0
        with:
          app-id: ${{ vars.DD_K9_LIBRARY_GO_APP_ID }}
          private-key: ${{ secrets.DD_K9_LIBRARY_GO_APP_PRIVATE_KEY }}
      - name: Create needs-triage label if needed
        run: |-
          gh label create                                                       \
            'needs-triage'                                                      \
            --repo='${{ github.repository }}'                                   \
            --color='#0E8A16'                                                   \
            --description='New issues that have not yet been triaged'           \
            --force
        env:
          GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
      - name: Is author a guild member?
        id: is-author-guild-member
        run: |-
          members=$(gh api /orgs/DataDog/teams/dd-trace-go-guild/members --jq='.[].id')
          result='false'
          for member in ${members}; do
            if [ "${member}" = "${{ github.event.issue.user.id }}" ]; then
              result='true'
              break
            fi
          done
          echo "result=${result}" >> "${GITHUB_OUTPUT}"
        env:
          GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
      - name: Add needs-triage label
        if: steps.is-author-guild-member.outputs.result == 'false'
        run: |-
          gh issue edit ${{ github.event.issue.number }}                        \
            --repo='${{ github.repository }}'                                   \
            --add-label='needs-triage'
        env:
          GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}

  find-old-issues:
    name: Find issues in need of attention
    # Schedule or workflow dispatch
    if: github.event_name != 'issues'
    runs-on: ubuntu-latest
    permissions:
      issues: read
    outputs:
      count: ${{ steps.find-issues.outputs.count }}
      json: ${{ steps.find-issues.outputs.json }}
    steps:
      - name: Find un-triaged issues older than 3 days
        id: find-issues
        run: |-
          gh issue list                                                         \
            --repo='${{ github.repository }}'                                   \
            --state=open                                                        \
            --label='needs-triage'                                              \
            --search="created:<$(date --date='today - 3 days' +'%Y-%m-%d')"     \
            --limit=10                                                          \
            --json='number,title,body,createdAt'                                \
            | tee issues.json

          {
            echo "count=$(jq -r '. | length' < issues.json)"
            echo "json<<EOJSON"
            cat issues.json
            echo "EOJSON"
          } >> "${GITHUB_OUTPUT}"
        env:
          GITHUB_TOKEN: ${{ github.token }}

  notify-slack:
    needs: find-old-issues
    # Schedule or workflow dispatch
    if: github.event_name != 'issues' && fromJson(needs.find-old-issues.outputs.count) > 0
    runs-on: ubuntu-latest

    strategy:
      matrix:
        include: ${{ fromJson(needs.find-old-issues.outputs.json) }}

    name: 'Notify Slack about #${{ matrix.number }}'

    steps:
      - name: Notify about ${{ matrix.number }}
        uses: slackapi/slack-github-action@91efab103c0de0a537f72a35f6b8cda0ee76bf0a # v2.1.1
        with:
          webhook: ${{ secrets.NEEDS_TRIAGE_SLACK_WEBHOOK_URL }}
          webhook-type: incoming-webhook
          payload: |-
            {
              "issue_number": ${{ toJson(matrix.number) }},
              "issue_title": ${{ toJson(matrix.title) }},
              "issue_url": ${{ toJson(format('github.com/{0}/issues/{1}', github.repository, matrix.number)) }},
              "created_at": ${{ toJson(matrix.createdAt) }}
            }
