-
Notifications
You must be signed in to change notification settings - Fork 280
Upgrade Dredd Transactions and refactor how annotations are processed #1351
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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.
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); } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand this is copy-pasted, but you can also do a slightly shorter syntax: const segments = [
origin.apiName,
origin.resourceGroupName,
origin.resourceName,
origin.actionName,
origin.exampleName,
].filter(Boolean).join(' > ') Apart from shorter declaration it also reduces the function's complexity. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good suggestion. I'll file it as a DT issue and fix it there. This wile stays copy-pasted and then deleted once DT provides the name directly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return segments.join(' > '); | ||
}; |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can reference #1225 here to keep track of the changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added