Files
shields/services/sonar/sonar-base.js
dependabot-preview[bot] 478d14300c Build(deps-dev): bump eslint-plugin-import from 2.20.1 to 2.20.2 (#4859)
* Build(deps-dev): bump eslint-plugin-import from 2.20.1 to 2.20.2

Bumps [eslint-plugin-import](https://github.com/benmosher/eslint-plugin-import) from 2.20.1 to 2.20.2.
- [Release notes](https://github.com/benmosher/eslint-plugin-import/releases)
- [Changelog](https://github.com/benmosher/eslint-plugin-import/blob/master/CHANGELOG.md)
- [Commits](https://github.com/benmosher/eslint-plugin-import/compare/v2.20.1...v2.20.2)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

* Fixes

* refactor: combine imports

* refactor: combine imports

* refactor: combine imports

* refactor: update import ordering

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
Co-authored-by: Paul Melnikow <email@paulmelnikow.com>
Co-authored-by: Caleb Cartwright <calebcartwright@users.noreply.github.com>
Co-authored-by: Caleb Cartwright <caleb.cartwright@outlook.com>
2020-04-16 18:39:13 -05:00

122 lines
3.3 KiB
JavaScript

'use strict'
const Joi = require('@hapi/joi')
const { BaseJsonService, NotFound } = require('..')
const { isLegacyVersion } = require('./sonar-helpers')
// It is possible to see HTTP 404 response codes and HTTP 200 responses
// with empty arrays of metric values, with both the legacy (pre v5.3) and modern APIs.
//
// 404 responses can occur with non-existent component keys, as well as unknown/unsupported metrics.
//
// 200 responses with empty arrays can occur when the metric key is valid, but the data
// is unavailable for the specified component, for example using the metric key `tests` with a
// component that is not capturing test results.
// It can also happen when using an older/deprecated
// metric key with a newer version of Sonar, for example using the metric key
// `public_documented_api_density` with SonarQube v7.x or higher
const modernSchema = Joi.object({
component: Joi.object({
measures: Joi.array()
.items(
Joi.object({
metric: Joi.string().required(),
value: Joi.alternatives(
Joi.number().min(0),
Joi.allow('OK', 'ERROR')
).required(),
})
)
.min(0)
.required(),
}).required(),
}).required()
const legacySchema = Joi.array()
.items(
Joi.object({
msr: Joi.array()
.items(
Joi.object({
key: Joi.string().required(),
val: Joi.alternatives(
Joi.number().min(0),
Joi.allow('OK', 'ERROR')
).required(),
})
)
.required(),
}).required()
)
.required()
module.exports = class SonarBase extends BaseJsonService {
static get auth() {
return { userKey: 'sonarqube_token', serviceKey: 'sonar' }
}
async fetch({ sonarVersion, server, component, metricName }) {
const useLegacyApi = isLegacyVersion({ sonarVersion })
let qs, url, schema
if (useLegacyApi) {
schema = legacySchema
url = `${server}/api/resources`
qs = {
resource: component,
depth: 0,
metrics: metricName,
includeTrends: true,
}
} else {
schema = modernSchema
url = `${server}/api/measures/component`
qs = {
componentKey: component,
metricKeys: metricName,
}
}
return this._requestJson(
this.authHelper.withBasicAuth({
schema,
url,
options: { qs },
errorMessages: {
404: 'component or metric not found, or legacy API not supported',
},
})
)
}
transform({ json, sonarVersion }) {
const useLegacyApi = isLegacyVersion({ sonarVersion })
const metrics = {}
if (useLegacyApi) {
const [{ msr: measures }] = json
if (!measures.length) {
throw new NotFound({ prettyMessage: 'metric not found' })
}
measures.forEach(measure => {
// Most values are numeric, but not all of them.
metrics[measure.key] = parseInt(measure.val) || measure.val
})
} else {
const {
component: { measures },
} = json
if (!measures.length) {
throw new NotFound({ prettyMessage: 'metric not found' })
}
measures.forEach(measure => {
// Most values are numeric, but not all of them.
metrics[measure.metric] = parseInt(measure.value) || measure.value
})
}
return metrics
}
}