Skip to content

Commit 4bc8d89

Browse files
authored
adding composite action to use codeowners-generator in a workflow (#337)
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
1 parent e911037 commit 4bc8d89

2 files changed

Lines changed: 136 additions & 0 deletions

File tree

README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
- [Built With](#built-with)
5050
- [Installation](#installation)
5151
- [Usage](#usage)
52+
- [Action](#action)
5253
- [Contributing](#contributing)
5354
- [License](#license)
5455

@@ -112,6 +113,74 @@ codeowners-generator generate --use-maintainers
112113
codeowners-generator generate --includes '**/CODEOWNERS'
113114
```
114115

116+
## Action
117+
118+
Now you can use `codeowners-generator` to validate if the CODEOWNERS file has been updated during a Pull Request.
119+
120+
```yml
121+
name: Lint CODEOWNERS
122+
123+
on:
124+
pull_request:
125+
126+
jobs:
127+
codeowners:
128+
runs-on: ubuntu-latest
129+
steps:
130+
- uses: actions/checkout@v2 # to checkout the code of the repo you want to check the CODEOWNERS from.
131+
- name: check codeowners
132+
uses: gagoar/codeowners-generator@master
133+
with:
134+
use-maintainers: true
135+
check: true
136+
```
137+
138+
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:
139+
140+
```yml
141+
name: update CODEOWNERS
142+
143+
on:
144+
pull_request:
145+
146+
jobs:
147+
build:
148+
runs-on: ubuntu-latest
149+
steps:
150+
- uses: actions/checkout@v3
151+
- uses: gagoar/codeowners-generator@master
152+
with:
153+
use-maintainers: true
154+
- run: |
155+
STATUS=$(git diff --quiet && echo clean || echo modified)
156+
echo "status=$(echo $STATUS)" >> $GITHUB_OUTPUT
157+
id: gitStatus
158+
- run: |
159+
echo ${{ steps.gitStatus.outputs.status }}
160+
echo ${{ contains(steps.gitStatus.outputs.status, 'modified') }}
161+
- name: Commit CODEOWNERS
162+
if: contains(steps.gitStatus.outputs.status, 'modified')
163+
run: |
164+
set -x
165+
git config --local user.email "action@github.com"
166+
git config --local user.name "GitHub Action"
167+
git add CODEOWNERS
168+
git commit -m "update CODEOWNERS"
169+
- id: auth
170+
if: contains(steps.gitStatus.outputs.status, 'modified')
171+
uses: jnwng/github-app-installation-token-action@v2
172+
with:
173+
appId: ${{ secrets.YOUR_APP_ID }}
174+
installationId: ${{ secrets.YOUR_APP_INSTALLATION_ID }}
175+
privateKey: ${{ secrets.YOUR_APP_PRIVATE_KEY }}
176+
- name: Push changes
177+
if: contains(steps.gitStatus.outputs.status, 'modified')
178+
uses: ad-m/github-push-action@master
179+
with:
180+
github_token: ${{ steps.auth.outputs.token }}
181+
branch: ${{github.head_ref}}
182+
```
183+
115184
<!-- CONFIGURATION -->
116185
117186
## Configuration

action.yml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
name: 'codeowners-generator'
2+
description: 'CODEOWNERS generator for mono-repos. This action will run codeowners-generator on your project and apply changes'
3+
inputs:
4+
use-maintainers:
5+
description: 'For every package.json found, generate a CODEOWNERS entry using the maintainers field'
6+
required: false
7+
default: 'false'
8+
group-source-comments:
9+
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'
10+
required: false
11+
default: 'false'
12+
custom-regeneration-command:
13+
description: 'Specify a custom regeneration command to be printed in the generated CODEOWNERS file, it should be mapped to run codeowners-generator'
14+
required: false
15+
default: 'false'
16+
check:
17+
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.'
18+
required: false
19+
default: 'false'
20+
output:
21+
description: 'The output path and name of the file, (default: CODEOWNERS)'
22+
required: false
23+
default: 'CODEOWNERS'
24+
version:
25+
description: codeowners-generator version. It will default to the latest in npm otherwise'
26+
required: false
27+
runs:
28+
using: 'composite'
29+
steps:
30+
- id: get-input
31+
shell: bash
32+
run: |
33+
ARGS_INPUT=("--output ${{inputs.output}}")
34+
VERSION='latest'
35+
36+
if [ "${{inputs.use-maintainers}}" = "true" ]; then
37+
ARGS_INPUT+=("--use-maintainers")
38+
fi
39+
40+
if [ "${{inputs.use-maintainers}}" = "true" ]; then
41+
ARGS_INPUT+=("--use-root-maintainers")
42+
fi
43+
44+
if [ "${{inputs.group-source-comments}}" = "true" ]; then
45+
ARGS_INPUT+=("--group-source-comments")
46+
fi
47+
48+
if [ "${{inputs.custom-regeneration-command}}" = "true" ]; then
49+
ARGS_INPUT+=("--custom-regeneration-command")
50+
fi
51+
52+
if [ "${{inputs.check}}" = "true" ]; then
53+
ARGS_INPUT+=("--check")
54+
fi
55+
56+
if [ ! -z "${{inputs.version}}" ]; then
57+
VERSION="${{inputs.version}}"
58+
fi
59+
60+
echo "Arguments we will use: ${ARGS_INPUT[@]}"
61+
62+
echo "version=$VERSION" >> $GITHUB_OUTPUT
63+
echo "args-input=${ARGS_INPUT[@]}" >> $GITHUB_OUTPUT
64+
65+
- shell: bash
66+
run: |
67+
npx codeowners-generator@${{steps.get-input.outputs.version}} generate ${{steps.get-input.outputs.args-input}}

0 commit comments

Comments
 (0)