Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate [Coverity] to new service model #2550

Merged
merged 4 commits into from
Dec 18, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/deprecated-services.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const deprecatedServices = {
libscore: new Date('2018-09-22'),
imagelayers: new Date('2018-11-18'),
nsp: new Date('2018-12-13'),
'coverity-on-demand': new Date('2018-12-18'),
}

module.exports = {
Expand Down
86 changes: 11 additions & 75 deletions services/coverity/coverity-on-demand.service.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,13 @@
'use strict'

const LegacyService = require('../legacy-service')
const { makeBadgeData: getBadgeData } = require('../../lib/badge-data')

// For Coverity Code Advisor On Demand.
module.exports = class CoverityOnDemand extends LegacyService {
static get category() {
return 'quality'
}

static get route() {
return {
base: 'coverity/ondemand',
}
}

static get examples() {
return [
{
title: 'Coverity Code Advisor On Demand Stream',
previewUrl: 'streams/STREAM',
},
{
title: 'Coverity Code Advisor On Demand Job',
previewUrl: 'jobs/JOB',
},
]
}

static registerLegacyRouteHandler({ camp, cache }) {
camp.route(
/^\/coverity\/ondemand\/(.+)\/(.+)\.(svg|png|gif|jpg|json)$/,
cache((data, match, sendBadge, request) => {
const badgeType = match[1] // One of the strings "streams" or "jobs"
const badgeTypeId = match[2] // streamId or jobId
const format = match[3]

const badgeData = getBadgeData('coverity', data)
if (
(badgeType === 'jobs' && badgeTypeId === 'JOB') ||
(badgeType === 'streams' && badgeTypeId === 'STREAM')
) {
// Request is for a static demo badge
badgeData.text[1] = 'clean'
badgeData.colorscheme = 'green'
sendBadge(format, badgeData)
} else {
//
// Request is for a real badge; send request to Coverity On Demand API
// server to get the badge
//
// Example URLs for requests sent to Coverity On Demand are:
//
// https://api.ondemand.coverity.com/streams/44b25sjc9l3ntc2ngfi29tngro/badge
// https://api.ondemand.coverity.com/jobs/p4tmm8031t4i971r0im4s7lckk/badge
//
const url = `https://api.ondemand.coverity.com/${badgeType}/${badgeTypeId}/badge`
request(url, (err, res, buffer) => {
if (err != null) {
badgeData.text[1] = 'inaccessible'
sendBadge(format, badgeData)
return
}
try {
const data = JSON.parse(buffer)
sendBadge(format, data)
} catch (e) {
badgeData.text[1] = 'invalid'
sendBadge(format, badgeData)
}
})
}
})
)
}
}
const deprecatedService = require('../deprecated-service')

// coverity on demand integration - deprecated as of December 2018.
module.exports = deprecatedService({
url: {
base: 'coverity/ondemand',
format: '(?:.+)',
},
label: 'coverity',
category: 'quality',
})
23 changes: 23 additions & 0 deletions services/coverity/coverity-on-demand.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

const ServiceTester = require('../service-tester')

const t = (module.exports = new ServiceTester({
id: 'CoverityOnDemand',
title: 'Coverity On Demand',
pathPrefix: '/coverity/ondemand',
}))

t.create('no longer available (streams)')
.get('/streams/44b25sjc9l3ntc2ngfi29tngro.json')
.expectJSON({
name: 'coverity',
value: 'no longer available',
})

t.create('no longer available (jobs)')
.get('/jobs/p4tmm8031t4i971r0im4s7lckk.json')
.expectJSON({
name: 'coverity',
value: 'no longer available',
})
96 changes: 56 additions & 40 deletions services/coverity/coverity-scan.service.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,79 @@
'use strict'

const LegacyService = require('../legacy-service')
const { makeBadgeData: getBadgeData } = require('../../lib/badge-data')
const Joi = require('joi')
const BaseJsonService = require('../base-json')

const messageRegex = /passed|passed .* new defects|pending|failed/
const schema = Joi.object({
message: Joi.string()
.regex(messageRegex)
.required(),
}).required()

module.exports = class CoverityScan extends BaseJsonService {
static render({ message }) {
let color
if (message === 'passed') {
color = 'brightgreen'
message = 'passing'
} else if (/^passed .* new defects$/.test(message)) {
color = 'yellow'
} else if (message === 'pending') {
color = 'orange'
} else {
color = 'red'
}

return {
message,
color,
}
}

module.exports = class CoverityScan extends LegacyService {
static get category() {
return 'quality'
}

static get defaultBadgeData() {
return {
label: 'coverity',
}
}

static get route() {
return {
base: 'coverity/scan',
pattern: ':projectId',
}
}

static get examples() {
return [
{
title: 'Coverity Scan',
previewUrl: '3997',
pattern: ':projectId',
namedParams: {
projectId: '3997',
},
staticPreview: this.render({
message: 'passed',
}),
keywords: ['coverity', 'scan'],
},
]
}

static registerLegacyRouteHandler({ camp, cache }) {
camp.route(
/^\/coverity\/scan\/(.+)\.(svg|png|gif|jpg|json)$/,
cache((data, match, sendBadge, request) => {
const projectId = match[1] // eg, `3997`
const format = match[2]
const url = `https://scan.coverity.com/projects/${projectId}/badge.json`
const badgeData = getBadgeData('coverity', data)
request(url, (err, res, buffer) => {
if (err != null) {
badgeData.text[1] = 'inaccessible'
sendBadge(format, badgeData)
return
}
try {
const data = JSON.parse(buffer)
badgeData.text[1] = data.message

if (data.message === 'passed') {
badgeData.colorscheme = 'brightgreen'
badgeData.text[1] = 'passing'
} else if (/^passed .* new defects$/.test(data.message)) {
badgeData.colorscheme = 'yellow'
} else if (data.message === 'pending') {
badgeData.colorscheme = 'orange'
} else if (data.message === 'failed') {
badgeData.colorscheme = 'red'
}
sendBadge(format, badgeData)
} catch (e) {
badgeData.text[1] = 'invalid'
sendBadge(format, badgeData)
}
})
})
)
async handle({ projectId }) {
const url = `https://scan.coverity.com/projects/${projectId}/badge.json`
const json = await this._requestJson({
url,
schema,
errorMessages: {
// At the moment Coverity returns an HTTP 200 with an HTML page
// displaying the text 404 when project is not found.
404: 'project not found',
},
})
return this.constructor.render({ message: json.message })
}
}
71 changes: 71 additions & 0 deletions services/coverity/coverity-scan.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'use strict'

const Joi = require('joi')
const t = (module.exports = require('../create-service-tester')())

t.create('live: known project id')
.get('/3997.json')
.expectJSONTypes(
Joi.object().keys({
name: 'coverity',
value: Joi.string().regex(/passing|passed .* new defects|pending|failed/),
})
)

t.create('live: unknown project id')
.get('/abc.json')
// Coverity actually returns an HTTP 200 status with an HTML page when the project is not found.
.expectJSON({ name: 'coverity', value: 'unparseable json response' })

t.create('404 response')
.get('/1.json')
.intercept(nock =>
nock('https://scan.coverity.com/projects/1')
.get('/badge.json')
.reply(404)
)
.expectJSON({ name: 'coverity', value: 'project not found' })

t.create('passed')
.get('/2.json')
.intercept(nock =>
nock('https://scan.coverity.com/projects/2')
.get('/badge.json')
.reply(200, {
message: 'passed',
})
)
.expectJSON({ name: 'coverity', value: 'passing' })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to mock a response for each case, we could usefully make the calls with the ?style=_shields_test param here and test the colouring logic too. There's an example of this in the docs https://github.com/badges/shields/blob/master/doc/service-tests.md#5-mocking-responses

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent! I was wondering about color validation. Will add this (I should probably update some other service tests to include this as well)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another approach which can be taken here is a Sazerac-based test of render in a .spec.js file.

Copy link
Member Author

@calebcartwright calebcartwright Dec 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added color validation via ?style=shields_test, will keep in mind the Sazerac-based tests too going forward


t.create('passed with defects')
.get('/2.json')
.intercept(nock =>
nock('https://scan.coverity.com/projects/2')
.get('/badge.json')
.reply(200, {
message: 'passed 51 new defects',
})
)
.expectJSON({ name: 'coverity', value: 'passed 51 new defects' })

t.create('pending')
.get('/2.json')
.intercept(nock =>
nock('https://scan.coverity.com/projects/2')
.get('/badge.json')
.reply(200, {
message: 'pending',
})
)
.expectJSON({ name: 'coverity', value: 'pending' })

t.create('failed')
.get('/2.json')
.intercept(nock =>
nock('https://scan.coverity.com/projects/2')
.get('/badge.json')
.reply(200, {
message: 'failed',
})
)
.expectJSON({ name: 'coverity', value: 'failed' })