-
-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(formatters): add GitHub Actions formatter (#2508)
- Loading branch information
Showing
6 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,4 @@ console.error(output); | |
### Node.js only | ||
|
||
- pretty | ||
- github-actions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { DiagnosticSeverity } from '@stoplight/types'; | ||
import type { IRuleResult } from '@stoplight/spectral-core'; | ||
import { githubActions } from '../github-actions'; | ||
|
||
const cwd = process.cwd(); | ||
const results: IRuleResult[] = [ | ||
{ | ||
code: 'operation-description', | ||
message: 'paths./pets.get.description is not truthy\nMessage can have\nmultiple lines', | ||
path: ['paths', '/pets', 'get', 'description'], | ||
severity: 1, | ||
source: `${cwd}/__tests__/fixtures/petstore.oas2.yaml`, | ||
range: { | ||
start: { | ||
line: 60, | ||
character: 8, | ||
}, | ||
end: { | ||
line: 71, | ||
character: 60, | ||
}, | ||
}, | ||
}, | ||
{ | ||
code: 'operation-tags', | ||
message: 'paths./pets.get.tags is not truthy', | ||
path: ['paths', '/pets', 'get', 'tags'], | ||
severity: 1, | ||
source: `${cwd}/__tests__/fixtures/petstore.oas2.yaml`, | ||
range: { | ||
start: { | ||
line: 60, | ||
character: 8, | ||
}, | ||
end: { | ||
line: 71, | ||
character: 60, | ||
}, | ||
}, | ||
}, | ||
]; | ||
|
||
describe('GitHub Actions formatter', () => { | ||
test('should be formatted correctly', () => { | ||
expect(githubActions(results, { failSeverity: DiagnosticSeverity.Error }).split('\n')).toEqual([ | ||
'::warning title=operation-description,file=__tests__/fixtures/petstore.oas2.yaml,col=9,endColumn=61,line=61,endLine=72::paths./pets.get.description is not truthy%0AMessage can have%0Amultiple lines', | ||
'::warning title=operation-tags,file=__tests__/fixtures/petstore.oas2.yaml,col=9,endColumn=61,line=61,endLine=72::paths./pets.get.tags is not truthy', | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { relative } from '@stoplight/path'; | ||
import { DiagnosticSeverity, Dictionary } from '@stoplight/types'; | ||
import { Formatter } from './types'; | ||
|
||
const OUTPUT_TYPES: Dictionary<string, DiagnosticSeverity> = { | ||
[DiagnosticSeverity.Error]: 'error', | ||
[DiagnosticSeverity.Warning]: 'warning', | ||
[DiagnosticSeverity.Information]: 'notice', | ||
[DiagnosticSeverity.Hint]: 'notice', | ||
}; | ||
|
||
type OutputParams = { | ||
title?: string; | ||
file: string; | ||
col?: number; | ||
endColumn?: number; | ||
line?: number; | ||
endLine?: number; | ||
}; | ||
|
||
export const githubActions: Formatter = results => { | ||
return results | ||
.map(result => { | ||
// GitHub Actions requires relative path for annotations, determining from working directory here | ||
const file = relative(process.cwd(), result.source ?? ''); | ||
const params: OutputParams = { | ||
title: result.code.toString(), | ||
file, | ||
col: result.range.start.character + 1, | ||
endColumn: result.range.end.character + 1, | ||
line: result.range.start.line + 1, | ||
endLine: result.range.end.line + 1, | ||
}; | ||
|
||
const paramsString = Object.entries(params) | ||
.map(p => p.join('=')) | ||
.join(','); | ||
|
||
// As annotated messages must be one-line due to GitHub's limitation, replacing all LF to %0A here. | ||
// see: https://github.com/actions/toolkit/issues/193 | ||
// FIXME: Use replaceAll instead after removing Node.js 14 support. | ||
const message = result.message.replace(/\n/g, '%0A'); | ||
|
||
return `::${OUTPUT_TYPES[result.severity]} ${paramsString}::${message}`; | ||
}) | ||
.join('\n'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { html, json, junit, text, stylish, teamcity } from './index'; | ||
export type { Formatter, FormatterOptions } from './index'; | ||
export { pretty } from './pretty'; | ||
export { githubActions } from './github-actions'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters