-
-
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 code climate (GitLab) formatter
Add code climate formatter Update documentation
- Loading branch information
1 parent
9e906ea
commit a64cf88
Showing
10 changed files
with
189 additions
and
20 deletions.
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
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 |
---|---|---|
|
@@ -34,3 +34,5 @@ console.error(output); | |
- pretty | ||
- github-actions | ||
- sarif | ||
- gitlab | ||
- code-climate |
89 changes: 89 additions & 0 deletions
89
packages/formatters/src/__tests__/code-climate.jest.test.ts
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,89 @@ | ||
import { DiagnosticSeverity } from '@stoplight/types'; | ||
import type { IRuleResult } from '@stoplight/spectral-core'; | ||
import { codeClimate } from '../code-climate'; | ||
|
||
const cwd = process.cwd(); | ||
const results: IRuleResult[] = [ | ||
{ | ||
code: 'operation-description', | ||
message: 'paths./pets.get.description is not truthy', | ||
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('Code climate formatter', () => { | ||
test('should include ranges', () => { | ||
expect(JSON.parse(codeClimate(results, { failSeverity: DiagnosticSeverity.Error }))).toEqual([ | ||
expect.objectContaining({ | ||
location: { | ||
path: '__tests__/fixtures/petstore.oas2.yaml', | ||
positions: { | ||
begin: { | ||
line: 60, | ||
column: 8, | ||
}, | ||
end: { | ||
line: 71, | ||
column: 60, | ||
}, | ||
}, | ||
}, | ||
}), | ||
expect.objectContaining({ | ||
location: { | ||
path: '__tests__/fixtures/petstore.oas2.yaml', | ||
positions: { | ||
begin: { | ||
line: 60, | ||
column: 8, | ||
}, | ||
end: { | ||
line: 71, | ||
column: 60, | ||
}, | ||
}, | ||
}, | ||
}), | ||
]); | ||
}); | ||
|
||
test('should include description', () => { | ||
expect(JSON.parse(codeClimate(results, { failSeverity: DiagnosticSeverity.Error }))).toEqual([ | ||
expect.objectContaining({ | ||
description: 'paths./pets.get.description is not truthy', | ||
}), | ||
expect.objectContaining({ | ||
description: '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,69 @@ | ||
import { DiagnosticSeverity } from '@stoplight/types'; | ||
import { Formatter } from './types'; | ||
import { relative } from '@stoplight/path'; | ||
|
||
/** | ||
* @see https://github.com/codeclimate/platform/blob/690633cb2a08839a5bfa350ed925ddb6de55bbdc/spec/analyzers/SPEC.md#data-types | ||
*/ | ||
interface CodeClimateIssue { | ||
type: 'issue'; | ||
check_name: string; | ||
description: string; | ||
categories: CodeClimateIssueCategory[]; | ||
location: CodeClimateIssueLocation; | ||
content?: { body: string }; | ||
trace?: CodeClimateIssueTrace; | ||
remediation_points?: number; | ||
severity?: CodeClimateIssueSeverity; | ||
fingerprint?: string; | ||
} | ||
type CodeClimateIssueCategory = | ||
| 'Bug Risk' | ||
| 'Clarity' | ||
| 'Compatibility' | ||
| 'Complexity' | ||
| 'Duplication' | ||
| 'Performance' | ||
| 'Security' | ||
| 'Style'; | ||
interface CodeClimateIssueLocation { | ||
path: string; | ||
positions: { | ||
begin: { line: number; column: number }; | ||
end: { line: number; column: number }; | ||
}; | ||
} | ||
interface CodeClimateIssueTrace { | ||
locations: CodeClimateIssueLocation[]; | ||
stackTrace: boolean; | ||
} | ||
type CodeClimateIssueSeverity = 'info' | 'minor' | 'major' | 'critical' | 'blocker'; | ||
const severityMap: Record<DiagnosticSeverity, CodeClimateIssueSeverity> = { | ||
[DiagnosticSeverity.Error]: 'critical', | ||
[DiagnosticSeverity.Warning]: 'major', | ||
[DiagnosticSeverity.Information]: 'minor', | ||
[DiagnosticSeverity.Hint]: 'info', | ||
}; | ||
|
||
export const codeClimate: Formatter = results => { | ||
const outputJson: CodeClimateIssue[] = results.map(result => { | ||
const relPath = relative(process.cwd(), result.source ?? '').replace(/\\/g, '/'); | ||
const fingerprint = `${relPath}:${result.path.join('.')}:${result.code}`; | ||
return { | ||
type: 'issue' as const, | ||
check_name: result.code.toString(), | ||
description: result.message, | ||
categories: ['Style'], | ||
location: { | ||
path: relPath, | ||
positions: { | ||
begin: { line: result.range.start.line, column: result.range.start.character }, | ||
end: { line: result.range.end.line, column: result.range.end.character }, | ||
}, | ||
}, | ||
severity: severityMap[result.severity], | ||
fingerprint, | ||
}; | ||
}); | ||
return JSON.stringify(outputJson, null, '\t'); | ||
}; |
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
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