Skip to content

Commit

Permalink
Feat: file filter (#6)
Browse files Browse the repository at this point in the history
* Feat: file filter

Signed-off-by: Piotr Karpala <[email protected]>
  • Loading branch information
karpikpl authored Apr 1, 2024
1 parent 2674011 commit e3a4625
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 27 deletions.
27 changes: 21 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Create a JavaScript Action
# List files changed in a Pull Request

[![GitHub Super-Linter](https://github.com/karpikpl/sample-action/actions/workflows/linter.yml/badge.svg)](https://github.com/super-linter/super-linter)
![CI](https://github.com/karpikpl/sample-action/actions/workflows/ci.yml/badge.svg)

GitHub action that lists all Python (.py) files changed (were added, modified)
in the provided pull request.
GitHub action that lists all files changed (were added, modified) in the
provided pull request.

## Usage

Expand All @@ -18,11 +18,24 @@ permissions:
```
```yaml
uses: karpikpl/sample-action@v1
uses: karpikpl/[email protected]
```
To get a list of Python files changed in a PR:
```yaml
uses: karpikpl/[email protected]
with:
file-filter: '*.py'
```
## Inputs
### `file-filter`

**Optional** [Glob](https://github.com/fitzgen/glob-to-regexp) file filter to
apply on the changed files in the pull request. E.g. `*.py`. Default: no filter.

### `pull-number`

**Optional** ID of the pull request. Action will try to use pull request number
Expand Down Expand Up @@ -51,8 +64,10 @@ here. If not set, this will use `${{ github.token }}`.

### `changed_files`

Space separated list of Python (\*.py) files changed (added, modified) in the
pull request.
Space separated list of files changed (added, modified) in the pull request that
match provided `file-filter`.

e.g. `'file1.py' 'file2.py'`

### `head_sha`

Expand Down
102 changes: 88 additions & 14 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,7 @@ const github = require('@actions/github')
const main = require('../src/main')

// Mock the GitHub Actions core library
const getInputMock = jest.spyOn(core, 'getInput').mockImplementation(name => {
switch (name) {
case 'pull-number':
return 123
case 'repo-token':
return '1234567890abcdef'
case 'repo-owner':
return 'repoBoss'
case 'repo-name':
return 'myRepo'
default:
throw new Error(`Unknown input: ${name}`)
}
})
const getInputMock = jest.spyOn(core, 'getInput').mockImplementation()
const setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation()
const setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation()

Expand Down Expand Up @@ -70,13 +57,39 @@ const getOctokitMock = jest
}
})

const mockInput = ({
file_filter = '*.py',
pull_number = 123,
repo_token = 'abcdef',
repo_owner = 'repoBoss',
repo_name = 'myRepo'
}) => {
getInputMock.mockImplementation(name => {
switch (name) {
case 'file-filter':
return file_filter
case 'pull-number':
return pull_number
case 'repo-token':
return repo_token
case 'repo-owner':
return repo_owner
case 'repo-name':
return repo_name
default:
throw new Error(`Unknown input: ${name}`)
}
})
}

describe('action', () => {
beforeEach(() => {
jest.clearAllMocks()
})

it('sets the output to empty string when no python files', async () => {
compareCommitsWithBaseheadMock.mockResolvedValue(commitsData)
mockInput({ file_filter: '*.py' })
pullGetMock.mockResolvedValue(prGetData)
github.context.payload = contextData

Expand Down Expand Up @@ -118,6 +131,7 @@ describe('action', () => {
]
}
})
mockInput({ file_filter: '*.py' })
pullGetMock.mockResolvedValue(prGetData)
github.context.payload = contextData

Expand All @@ -144,6 +158,64 @@ describe('action', () => {
expect(setOutputMock).toHaveBeenNthCalledWith(4, 'result', 'Success')
})

it('sets the output to empty string when no files matched glob', async () => {
compareCommitsWithBaseheadMock.mockResolvedValue({
status: 200,
url: 'https://api.github.com/repos/octocat/Hello-World/compare/master...topic',
headers: {},
data: {
files: [
{ filename: 'file1.md', status: 'added' },
{ filename: 'pyInTheSky', status: 'modified' },
{ filename: 'file3.pip', status: 'added' },
{ filename: 'file4.python', status: 'modifed' }
]
}
})
mockInput({ file_filter: '*.py' })
pullGetMock.mockResolvedValue(prGetData)
github.context.payload = contextData

await main.run()
expect(runMock).toHaveReturned()

// Verify that all of the core library functions were called correctly
expect(getOctokitMock).toHaveBeenCalled()
expect(setOutputMock).toHaveBeenNthCalledWith(3, 'changed_files', '')
expect(setOutputMock).toHaveBeenNthCalledWith(4, 'result', 'Success')
})

it('sets the output to all the files when glob provided', async () => {
compareCommitsWithBaseheadMock.mockResolvedValue({
status: 200,
url: 'https://api.github.com/repos/octocat/Hello-World/compare/master...topic',
headers: {},
data: {
files: [
{ filename: 'file1.md', status: 'added' },
{ filename: 'pyInTheSky', status: 'modified' },
{ filename: 'file3.pip', status: 'added' },
{ filename: 'file4.python', status: 'modifed' }
]
}
})
mockInput({ file_filter: '' })
pullGetMock.mockResolvedValue(prGetData)
github.context.payload = contextData

await main.run()
expect(runMock).toHaveReturned()

// Verify that all of the core library functions were called correctly
expect(getOctokitMock).toHaveBeenCalled()
expect(setOutputMock).toHaveBeenNthCalledWith(
3,
'changed_files',
"'file1.md' 'pyInTheSky' 'file3.pip' 'file4.python'"
)
expect(setOutputMock).toHaveBeenNthCalledWith(4, 'result', 'Success')
})

it('fails if no input is provided', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation(name => {
Expand Down Expand Up @@ -172,6 +244,8 @@ describe('action', () => {
switch (name) {
case 'pull-number':
return ''
case 'file-filter':
return ''
case 'repo-token':
return '1234567890abcdef'
case 'repo-owner':
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ author: 'Piotr Karpala'

# Define your inputs here.
inputs:
file-filter:
description: 'A space separated list of file extensions to filter.'
required: false
pull-number:
description: 'The pull request number.'
required: false
Expand Down
147 changes: 145 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions dist/licenses.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e3a4625

Please sign in to comment.