name: Codeowners

on:
  pull_request:

jobs:
  report_codeowners:
    name: "Report codeowners"
    runs-on: ubuntu-latest
    permissions:
       pull-requests: write
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          persist-credentials: false
          fetch-depth: 0
      - name: Get changed files
        id: changed-files
        uses: tj-actions/changed-files@ed68ef82c095e0d48ec87eccea555d944a631a4c # v46.0.5
      - name: Setup go
        uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
      - name: Install codeowners
        run: go install github.com/hmarr/codeowners/cmd/codeowners@latest
      - name: List owners of all changed files
        id: codeowners_resolved
        run: |
          codeowners ${{ steps.changed-files.outputs.all_modified_files	 }}
          echo "results<<EOF" >> "$GITHUB_OUTPUT"
          echo "$(codeowners ${{ steps.changed-files.outputs.all_modified_files	 }})" >> "$GITHUB_OUTPUT"
          echo "EOF" >> "$GITHUB_OUTPUT"
      - name: Comment PR
        uses: thollander/actions-comment-pull-request@24bffb9b452ba05a4f3f77933840a6a841d1b32b # v3.0.1
        with:
          file-path: resolved.txt
          comment-tag: codeowners_resolved
          mode: upsert
          message: |
            `CODEOWNERS` have been resolved as:

            ```
            ${{ steps.codeowners_resolved.outputs.results }}
            ```
