This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1351 from apiaryio/honzajavorek/upgrade-dt
Upgrade Dredd Transactions and refactor how annotations are processed
- Loading branch information
Showing
12 changed files
with
305 additions
and
499 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
const compileTransactionName = require('./compileTransactionName'); | ||
|
||
|
||
/** | ||
* Turns annotation type into a log level | ||
*/ | ||
function typeToLogLevel(annotationType) { | ||
const level = { error: 'error', warning: 'warn' }[annotationType]; | ||
if (!level) { | ||
throw new Error(`Invalid annotation type: '${annotationType}'`); | ||
} | ||
return level; | ||
} | ||
|
||
|
||
/** | ||
* Takes a component identifier and turns it into something user can understand | ||
* | ||
* @param {string} component | ||
*/ | ||
function formatComponent(component) { | ||
switch (component) { | ||
case 'apiDescriptionParser': | ||
return 'API description parser'; | ||
case 'parametersValidation': | ||
return 'API description URI parameters validation'; | ||
case 'uriTemplateExpansion': | ||
return 'API description URI template expansion'; | ||
default: | ||
return 'API description'; | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Formats given location data as something user can understand | ||
* | ||
* @param {string} apiDescriptionLocation API description location name | ||
* @param {array} annotationLocation See 'dredd-transactions' docs | ||
*/ | ||
function formatLocation(apiDescriptionLocation, annotationLocation) { | ||
if (!annotationLocation) { | ||
return apiDescriptionLocation; | ||
} | ||
|
||
const [[startLine, startColumn], [endLine, endColumn]] = annotationLocation; | ||
const editorLink = `${apiDescriptionLocation}:${startLine}`; | ||
const from = `line ${startLine} column ${startColumn}`; | ||
|
||
if (startLine === endLine && startColumn === endColumn) { | ||
return `${editorLink} (${from})`; | ||
} | ||
|
||
const to = startLine === endLine | ||
? `column ${endColumn}` | ||
: `line ${endLine} column ${endColumn}`; | ||
return `${editorLink} (from ${from} to ${to})`; | ||
} | ||
|
||
|
||
/** | ||
* @typedef {Object} LoggerInfo A plain object winston.log() accepts as input | ||
* @property {string} level | ||
* @property {string} message | ||
*/ | ||
|
||
/** | ||
* Takes API description parser or compiler annotation returned from | ||
* the 'dredd-transactions' library and transforms it into a message | ||
* Dredd can show to the user. Returns an object logger accepts as input. | ||
* | ||
* @param {string} apiDescriptionLocation API description location name | ||
* @param {Object} annotation the annotation object from Dredd Transactions | ||
* @return {LoggerInfo} | ||
*/ | ||
module.exports = function annotationToLoggerInfo(apiDescriptionLocation, annotation) { | ||
const level = typeToLogLevel(annotation.type); | ||
|
||
if (annotation.component === 'apiDescriptionParser') { | ||
const message = ( | ||
`${formatComponent(annotation.component)} ${annotation.type}` | ||
+ ` in ${formatLocation(apiDescriptionLocation, annotation.location)}:` | ||
+ ` ${annotation.message}` | ||
); | ||
return { level, message }; | ||
} | ||
|
||
// See https://github.com/apiaryio/dredd-transactions/issues/275 why this | ||
// is handled in a different way than parser annotations | ||
const message = ( | ||
`${formatComponent(annotation.component)} ${annotation.type}` | ||
+ ` in ${apiDescriptionLocation} (${compileTransactionName(annotation.origin)}):` | ||
+ ` ${annotation.message}` | ||
); | ||
return { level, message }; | ||
}; |
This file was deleted.
Oops, something went wrong.
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,14 @@ | ||
// This file is copy-pasted "as is" from the Dredd Transactions library, where | ||
// it's also tested. This is a temporary solution, | ||
// see https://github.com/apiaryio/dredd-transactions/issues/276 | ||
|
||
|
||
module.exports = function compileTransactionName(origin) { | ||
const segments = []; | ||
if (origin.apiName) { segments.push(origin.apiName); } | ||
if (origin.resourceGroupName) { segments.push(origin.resourceGroupName); } | ||
if (origin.resourceName) { segments.push(origin.resourceName); } | ||
if (origin.actionName) { segments.push(origin.actionName); } | ||
if (origin.exampleName) { segments.push(origin.exampleName); } | ||
return segments.join(' > '); | ||
}; |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.