Skip to content

Commit

Permalink
V2: Add file match information to output
Browse files Browse the repository at this point in the history
  • Loading branch information
SvanBoxel committed Oct 11, 2020
1 parent f435a78 commit 8016424
Show file tree
Hide file tree
Showing 16 changed files with 984 additions and 103 deletions.
45 changes: 43 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# CODEOWNERS Action
Use this action to extract your CODEOWNER file information. It parses all the information and exports it as JSON. This output is available via the `steps` output context. The output key is `codeowners`
Use this action to extract your CODEOWNER file information and check how individual files match CODEOWNER rules. It parses all the information and exports it as JSON. This output is available via the `steps` output context.

## Usage
Use this Action in your Actions workflow:
This Actions outputs CODEOWNER file information, and optionally how individual files match specific rules.

### Basic mode
In basic mode (default) it will only extract the information that can be found in the CODEOWNERS file and returns this information as an object. Use this Action in basic mode in your Actions workflow as follows:

```yml
- id: codeowner
Expand All @@ -11,6 +14,44 @@ Use this Action in your Actions workflow:
echo ${{ steps.codeowners.outputs.codeowners }};
```
Example `codeowners` object:
```json
{
"dist/index.js": ["@foo-bot"],
"lib/*": ["@hubot"],
"src/main.ts": ["@hubot", "@svanboxel", "@foo-bot"]
}
```

### File match option
When enabled, this Action checks how every file in your repository matches a specific CODEOWNER rule. The order of the CODEOWNERS rules defines (last matching pattern takes the most precedence), which rule matches which file.

```yml
- id: codeowner
uses: SvanBoxel/codeowners-action@v1
with:
file_match_info: 'true'
```

This will output the following (example) JSON format to the `filematches` output variable:

```json
{
"./bar/foo.cp": {
"rule_match": "*",
"owners": [ "@test" ]
},
"./lib/foo/bar.whop": {
"rule_match": "lib/foo/",
"owners": [ "@not-hubot" ]
},
"./src/main.ts": {
"rule_match": "src/main.ts",
"owners": [ "@hubot", "@svanboxel", "@foo-bot" ]
}
}
```
### Custom path
If your CODEOWNERS file isn't in the root of your repository, but for instead in the `.github` directory, you can change the path by using the path parameter:

```yml
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import extractInfo from '../src/extract-info'
import extractCodeOwners from '../src/extract-codeowners'

test('should return object if valid path is given', async () => {
expect(await extractInfo(`${__dirname}/CODEOWNERS`)).toMatchObject({
expect(await extractCodeOwners(`${__dirname}/CODEOWNERS`)).toMatchObject({
'dist/index.js': ['@foo-bot'],
'lib/*': ['@hubot'],
'src/main.ts': ['@hubot', '@svanboxel', '@foo-bot']
Expand All @@ -10,7 +10,7 @@ test('should return object if valid path is given', async () => {

test('should throw error if invalid path is given', async () => {
try {
await extractInfo(`invalid`)
await extractCodeOwners(`invalid`)
} catch (e) {
expect(e.code).toEqual('ENOENT');
}
Expand Down
53 changes: 53 additions & 0 deletions __tests__/extract-filematches.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import extractFileMatches from '../src/extract-filematches'

const codeOwnersInfo = {
'dist/index.js': ['@foo-bot'],
'lib/*': ['@hubot'],
'src/main.ts': ['@hubot', '@svanboxel', '@foo-bot']
}

const files = [
'./lib/foo/bar.whop',
'./src/main.ts',
'./bar/foo.cp'
]

test('should return which rules match a file', async () => {
expect(await extractFileMatches(files, codeOwnersInfo)).toMatchObject({
'./lib/foo/bar.whop': {
rule_match: 'lib/*',
owners: [ '@hubot' ]
},
'./src/main.ts':
{
rule_match: 'src/main.ts',
owners: [ '@hubot', '@svanboxel', '@foo-bot' ]
}
}
)
})

test('should give most precendence to last matching pattern', async () => {
const codeOwnersInfoTwo = {
'*': ["@test"],
...codeOwnersInfo,
'lib/foo/': ["@not-hubot"]
}

expect(await extractFileMatches(files, codeOwnersInfoTwo)).toMatchObject({
'./bar/foo.cp': {
rule_match: '*',
owners: [ '@test' ]
},
'./lib/foo/bar.whop': {
rule_match: 'lib/foo/',
owners: [ '@not-hubot' ]
},
'./src/main.ts':
{
rule_match: 'src/main.ts',
owners: [ '@hubot', '@svanboxel', '@foo-bot' ]
}
}
)
})
8 changes: 7 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ inputs:
required: false
description: 'Location of codeowners file'
default: './CODEOWNERS'
file_match_info:
required: false
description: Output how version controlled files match CODEOWNERS rules
default: 'false'
output:
codeowners:
description: "JSON string containing all CODEOWNER information"
description: "JSON string containing all CODEOWNERS information"
filematches:
description: "JSON string contain how files match CODEOWNERS rules"
runs:
using: 'node12'
main: 'dist/index.js'
Expand Down
Loading

0 comments on commit 8016424

Please sign in to comment.