hmarr / codeowners

🔒 Command line tool and Go library for CODEOWNERS files
MIT License
170 stars 20 forks source link

Example use case: `on: pullrequest` github action #22

Closed hoshinotsuyoshi closed 10 months ago

hoshinotsuyoshi commented 1 year ago

(This issue is not a problem report.)

Thank you for publishing this tool! We use this very conveniently.

I'd like to share an example that demonstrates how to report the precise changes made to a CODEOWNERS file within a pull request, which could potentially be useful to others. As you may already be aware, interpreting .github/CODEOWNERS can be quite intricate.

To simplify this process, I've crafted a GitHub Action that provides detailed insights into which files are specifically affected by modifications to .github/CODEOWNERS.

# .github/workflows/post-codeowners-result-diff.yml
name: post-codeowners-result-diff

on:
  pull_request:
    paths:
      - .github/CODEOWNERS
      - .github/workflows/post-codeowners-result-diff.yml

jobs:
  post-codeowners-result-diff:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write
    timeout-minutes: 10

    steps:
      - name: setup codeowners
        run: |
          mkdir /opt/bin
          curl -sL "https://github.com/hmarr/codeowners/releases/download/v${CODEOWNERS_VERSION}/codeowners_${CODEOWNERS_VERSION}_linux_amd64.tar.gz" -o /tmp/codeowners.tar.gz
          hash=$(sha256sum /tmp/codeowners.tar.gz  | awk '{ print $1 }')
          if [ "$hash" = "e69a42a4a28efd58a87a0dc0d8f32223c1f6ccda7954afd40108abf98517a13f" ]; then
              cat /tmp/codeowners.tar.gz | tar zfx - -C /opt/bin
          else
              echo $hash
              echo invalid
              exit 1
          fi
        env:
          CODEOWNERS_VERSION: 1.1.2

      - uses: actions/checkout@v3
        with:
          ref: ${{ github.base_ref }}

      - name: call codeowners command on github.base_ref
        run: |
          /opt/bin/codeowners > /tmp/result.1

      - uses: actions/checkout@v3
        with:
          ref: ${{ github.ref }}

      - name: call codeowners command on github.ref
        run: |
          /opt/bin/codeowners > /tmp/result.2

      - name: make report file
        run: |
          echo '<details><summary>codeowners diff report (.github/workflows/post-codeowners-result-diff.yml)</summary>' > /tmp/diffs
          echo ''        >> /tmp/diffs
          echo '```diff' >> /tmp/diffs
          diff -u /tmp/result.1 /tmp/result.2 >> /tmp/diffs | true
          echo '```' >> /tmp/diffs
          echo ''    >> /tmp/diffs
          echo '</details>' >> /tmp/diffs

      - name: post comment
        uses: marocchino/sticky-pull-request-comment@v2
        with:
          hide_and_recreate: true
          header: report
          path: /tmp/diffs

This action will add a report comment like the one below when a pullrequest is opened that modifies .github/CODEOWNERS.

image-image

Now, armed with this handy diff report, you can tweak .github/CODEOWNERS with confidence, no more guesswork involved!

Anyway, big thanks for making all of this possible!