Files
shields/services/gerrit/gerrit.service.js
chris48s 38cdc0033f adopt JSDoc, eslint-plugin-jsdoc (#3645)
eslint-plugin-jsdoc:
- install eslint-plugin-jsdoc
- config file
- fix lint/style errors

JSDoc:
- add JSDoc as a dev dependency
- get everything rendering nicely with JSDoc
- config, build command + ignores
2019-07-11 20:14:47 +01:00

97 lines
2.3 KiB
JavaScript

'use strict'
const Joi = require('@hapi/joi')
const { optionalUrl } = require('../validators')
const { BaseJsonService } = require('..')
const queryParamSchema = Joi.object({
baseUrl: optionalUrl.required(),
}).required()
const schema = Joi.object({
status: Joi.equal('NEW', 'MERGED', 'ABANDONED').required(),
}).required()
module.exports = class Gerrit extends BaseJsonService {
static get category() {
return 'issue-tracking'
}
static get route() {
return {
base: 'gerrit',
pattern: ':changeId',
queryParamSchema,
}
}
static get examples() {
return [
{
title: 'Gerrit change status',
namedParams: {
changeId: '1011478',
},
queryParams: { baseUrl: 'https://android-review.googlesource.com' },
staticPreview: this.render({
changeId: 1011478,
status: 'MERGED',
}),
},
]
}
static get defaultBadgeData() {
return { label: 'gerrit' }
}
static getColor({ displayStatus }) {
if (displayStatus === 'new') {
return '2cbe4e'
} else if (displayStatus === 'merged') {
return 'blueviolet'
} else if (displayStatus === 'abandoned') {
return 'red'
}
}
static render({ changeId, status }) {
const displayStatus = status.toLowerCase()
const color = this.getColor({ displayStatus })
return {
label: `change ${changeId}`,
message: displayStatus,
color,
}
}
/*
* To prevent against Cross Site Script Inclusion (XSSI) attacks, Gerrit's
* JSON response body starts with a magic prefix line that must be stripped
* before feeding the rest of the response body to a JSON parser.
* See https://gerrit-review.googlesource.com/Documentation/rest-api.html#output
*/
_parseJson(buffer) {
const bufferWithoutPrefixLine = buffer.substring(buffer.indexOf('\n') + 1)
return super._parseJson(bufferWithoutPrefixLine)
}
async fetch({ changeId, baseUrl }) {
return this._requestJson({
schema,
url: `${baseUrl}/changes/${changeId}`,
errorMessages: {
404: 'change not found',
},
})
}
async handle({ changeId }, { baseUrl }) {
const data = await this.fetch({ changeId, baseUrl })
return this.constructor.render({
changeId,
status: data.status,
})
}
}