Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter out shared from yaml anchor output #239

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions __tests__/export.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as core from '@actions/core'
import {Filter} from '../src/filter'
import {File, ChangeStatus} from '../src/file'
import {exportResults} from '../src/main'

jest.mock('@actions/core', () => ({
info: jest.fn(),
setFailed: jest.fn(),
startGroup: jest.fn(),
setOutput: jest.fn(),
endGroup: jest.fn()
}))

describe('set output post filtering', () => {
test('correctly sets output', () => {
const yaml = `
backend:
- '!(**/*.tsx|**/*.less)'
`
const filter = new Filter(yaml)
const files = modified(['config/settings.yml'])
const match = filter.match(files)
exportResults(match, 'none')

expect(core.setOutput).toHaveBeenCalledWith('changes', '["backend"]')
})
test('correctly filters out shared from output', () => {
const yaml = `
shared: &shared
- common/**/*
- config/**/*
src:
- *shared
- src/**/*
backend:
- '!(**/*.tsx|**/*.less)'
`
const filter = new Filter(yaml)
const files = modified(['config/settings.yml'])
const match = filter.match(files)
exportResults(match, 'none')

expect(core.setOutput).toHaveBeenCalledWith('changes', '["src","backend"]')
})
})

function modified(paths: string[]): File[] {
return paths.map(filename => {
return {filename, status: ChangeStatus.Modified}
})
}
5 changes: 3 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ async function getChangedFilesFromApi(token: string, pullRequest: PullRequestEve
}
}

function exportResults(results: FilterResults, format: ExportFormat): void {
export function exportResults(results: FilterResults, format: ExportFormat): void {
core.info('Results:')
const changes = []
for (const [key, files] of Object.entries(results)) {
Expand All @@ -254,7 +254,8 @@ function exportResults(results: FilterResults, format: ExportFormat): void {
}

if (results['changes'] === undefined) {
const changesJson = JSON.stringify(changes)
const filteredShared = changes.filter(change => change !== 'shared')
const changesJson = JSON.stringify(filteredShared)
core.info(`Changes output set to ${changesJson}`)
core.setOutput('changes', changesJson)
} else {
Expand Down