Skip to content

Commit

Permalink
adding composite action to use codeowners-generator in a workflow (#337)
Browse files Browse the repository at this point in the history
This PR will add the the capability for the project to be an action in
GitHub.

I will fast follow this PR with a release so I can publish it on the
GitHub market.

This PR should close #335
  • Loading branch information
gagoar authored Mar 19, 2023
1 parent e911037 commit 4bc8d89
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
- [Built With](#built-with)
- [Installation](#installation)
- [Usage](#usage)
- [Action](#action)
- [Contributing](#contributing)
- [License](#license)

Expand Down Expand Up @@ -112,6 +113,74 @@ codeowners-generator generate --use-maintainers
codeowners-generator generate --includes '**/CODEOWNERS'
```

## Action

Now you can use `codeowners-generator` to validate if the CODEOWNERS file has been updated during a Pull Request.

```yml
name: Lint CODEOWNERS

on:
pull_request:

jobs:
codeowners:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2 # to checkout the code of the repo you want to check the CODEOWNERS from.
- name: check codeowners
uses: gagoar/codeowners-generator@master
with:
use-maintainers: true
check: true
```
You can also use it to update the Pull Request. For that, you will need a GitHub App or Personal Token with the necessary permissions (code content). The code for that will look roughly like this:
```yml
name: update CODEOWNERS

on:
pull_request:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: gagoar/codeowners-generator@master
with:
use-maintainers: true
- run: |
STATUS=$(git diff --quiet && echo clean || echo modified)
echo "status=$(echo $STATUS)" >> $GITHUB_OUTPUT
id: gitStatus
- run: |
echo ${{ steps.gitStatus.outputs.status }}
echo ${{ contains(steps.gitStatus.outputs.status, 'modified') }}
- name: Commit CODEOWNERS
if: contains(steps.gitStatus.outputs.status, 'modified')
run: |
set -x
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add CODEOWNERS
git commit -m "update CODEOWNERS"
- id: auth
if: contains(steps.gitStatus.outputs.status, 'modified')
uses: jnwng/github-app-installation-token-action@v2
with:
appId: ${{ secrets.YOUR_APP_ID }}
installationId: ${{ secrets.YOUR_APP_INSTALLATION_ID }}
privateKey: ${{ secrets.YOUR_APP_PRIVATE_KEY }}
- name: Push changes
if: contains(steps.gitStatus.outputs.status, 'modified')
uses: ad-m/github-push-action@master
with:
github_token: ${{ steps.auth.outputs.token }}
branch: ${{github.head_ref}}
```
<!-- CONFIGURATION -->
## Configuration
Expand Down
67 changes: 67 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: 'codeowners-generator'
description: 'CODEOWNERS generator for mono-repos. This action will run codeowners-generator on your project and apply changes'
inputs:
use-maintainers:
description: 'For every package.json found, generate a CODEOWNERS entry using the maintainers field'
required: false
default: 'false'
group-source-comments:
description: 'Instead of generating one comment per rule, enabling this flag will group them, reducing comments to one per source file. Useful if your codeowners file gets too noisy'
required: false
default: 'false'
custom-regeneration-command:
description: 'Specify a custom regeneration command to be printed in the generated CODEOWNERS file, it should be mapped to run codeowners-generator'
required: false
default: 'false'
check:
description: It will fail if the CODEOWNERS generated does not match the current (or missing) CODEOWNERS. Useful for validating that the CODEOWNERS file is up to date during CI.'
required: false
default: 'false'
output:
description: 'The output path and name of the file, (default: CODEOWNERS)'
required: false
default: 'CODEOWNERS'
version:
description: codeowners-generator version. It will default to the latest in npm otherwise'
required: false
runs:
using: 'composite'
steps:
- id: get-input
shell: bash
run: |
ARGS_INPUT=("--output ${{inputs.output}}")
VERSION='latest'
if [ "${{inputs.use-maintainers}}" = "true" ]; then
ARGS_INPUT+=("--use-maintainers")
fi
if [ "${{inputs.use-maintainers}}" = "true" ]; then
ARGS_INPUT+=("--use-root-maintainers")
fi
if [ "${{inputs.group-source-comments}}" = "true" ]; then
ARGS_INPUT+=("--group-source-comments")
fi
if [ "${{inputs.custom-regeneration-command}}" = "true" ]; then
ARGS_INPUT+=("--custom-regeneration-command")
fi
if [ "${{inputs.check}}" = "true" ]; then
ARGS_INPUT+=("--check")
fi
if [ ! -z "${{inputs.version}}" ]; then
VERSION="${{inputs.version}}"
fi
echo "Arguments we will use: ${ARGS_INPUT[@]}"
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "args-input=${ARGS_INPUT[@]}" >> $GITHUB_OUTPUT
- shell: bash
run: |
npx codeowners-generator@${{steps.get-input.outputs.version}} generate ${{steps.get-input.outputs.args-input}}

0 comments on commit 4bc8d89

Please sign in to comment.