Files
shields/services/bitbucket/bitbucket-issues.service.js
Paul Melnikow 226fa67a02 Create shortcut for BaseService-related imports (#2809)
Continue to implement #2698:

- Add `core/base-service/index.js` (but hold off on moving the things it imports)
- Add shortcuts in `services/index.js` for Base*Service, errors, and deprecatedService. This file will be streamlined later to avoid cluttering it with rarely used bits.
- Apply consistent ordering of imports and use of `module.exports` in testers.
- Remove some renaming of imports.
- Remove obsolete tests here and there.
2019-01-21 15:41:24 -05:00

75 lines
1.8 KiB
JavaScript

'use strict'
const Joi = require('joi')
const { metric } = require('../../lib/text-formatters')
const { BaseJsonService } = require('..')
const { nonNegativeInteger } = require('../validators')
const bitbucketIssuesSchema = Joi.object({
count: nonNegativeInteger,
}).required()
function issueClassGenerator(raw) {
const routePrefix = raw ? 'issues-raw' : 'issues'
const badgeSuffix = raw ? '' : ' open'
return class BitbucketIssues extends BaseJsonService {
async fetch({ user, repo }) {
const url = `https://bitbucket.org/api/1.0/repositories/${user}/${repo}/issues/`
return this._requestJson({
url,
schema: bitbucketIssuesSchema,
options: {
qs: { limit: 0, status: ['new', 'open'] },
useQuerystring: true,
},
errorMessages: { 403: 'private repo' },
})
}
static render({ issues }) {
return {
message: `${metric(issues)}${badgeSuffix}`,
color: issues ? 'yellow' : 'brightgreen',
}
}
async handle({ user, repo }) {
const data = await this.fetch({ user, repo })
return this.constructor.render({ issues: data.count })
}
static get category() {
return 'issue-tracking'
}
static get defaultBadgeData() {
return { label: 'issues' }
}
static get route() {
return {
base: `bitbucket/${routePrefix}`,
format: '([^/]+)/([^/]+)',
capture: ['user', 'repo'],
}
}
static get examples() {
return [
{
title: 'Bitbucket open issues',
pattern: ':user/:repo',
namedParams: {
user: 'atlassian',
repo: 'python-bitbucket',
},
staticPreview: this.render({ issues: 33 }),
},
]
}
}
}
module.exports = [true, false].map(issueClassGenerator)