Skip to content

Commit

Permalink
Rewrite [GithubSearch] (#3099)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmelnikow committed Feb 25, 2019
1 parent 92039e4 commit e4fe8c0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 52 deletions.
86 changes: 35 additions & 51 deletions services/github/github-search.service.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
'use strict'

const LegacyService = require('../legacy-service')
const { makeBadgeData: getBadgeData } = require('../../lib/badge-data')
const Joi = require('joi')
const { metric } = require('../../lib/text-formatters')
const {
documentation,
checkErrorResponse: githubCheckErrorResponse,
} = require('./github-helpers')
const { nonNegativeInteger } = require('../validators')
const { GithubAuthService } = require('./github-auth-service')
const { errorMessagesFor, documentation } = require('./github-helpers')

// This legacy service should be rewritten to use e.g. BaseJsonService.
//
// Tips for rewriting:
// https://github.com/badges/shields/blob/master/doc/rewriting-services.md
//
// Do not base new services on this code.
module.exports = class GithubSearch extends LegacyService {
const schema = Joi.object({ total_count: nonNegativeInteger }).required()

module.exports = class GithubSearch extends GithubAuthService {
static get category() {
return 'analysis'
}

static get route() {
return {
base: 'github/search',
pattern: ':user/:repo/:query*',
pattern: ':user/:repo/:query+',
}
}

Expand All @@ -36,47 +30,37 @@ module.exports = class GithubSearch extends LegacyService {
repo: 'linux',
query: 'goto',
},
staticPreview: {
label: 'goto counter',
message: '14k',
color: 'blue',
},
staticPreview: this.render({ query: 'goto', totalCount: 14000 }),
documentation,
},
]
}

static registerLegacyRouteHandler({ camp, cache, githubApiProvider }) {
camp.route(
/^\/github\/search\/([^/]+)\/([^/]+)\/(.*)\.(svg|png|gif|jpg|json)$/,
cache((data, match, sendBadge, request) => {
const user = match[1]
const repo = match[2]
const search = match[3]
const format = match[4]
const query = { q: `${search} repo:${user}/${repo}` }
const badgeData = getBadgeData(`${search} counter`, data)
githubApiProvider.request(
request,
'/search/code',
query,
(err, res, buffer) => {
if (githubCheckErrorResponse(badgeData, err, res)) {
sendBadge(format, badgeData)
return
}
try {
const body = JSON.parse(buffer)
badgeData.text[1] = metric(body.total_count)
badgeData.colorscheme = 'blue'
sendBadge(format, badgeData)
} catch (e) {
badgeData.text[1] = 'invalid'
sendBadge(format, badgeData)
}
}
)
})
)
static get defaultBadgeData() {
return {
label: 'counter',
}
}

static render({ query, totalCount }) {
return {
label: `${query} counter`,
message: metric(totalCount),
color: 'blue',
}
}

async handle({ user, repo, query }) {
const { total_count: totalCount } = await this._requestJson({
url: '/search/code',
options: {
qs: {
q: `${query} repo:${user}/${repo}`,
},
},
schema,
errorMessages: errorMessagesFor('repo not found'),
})
return this.constructor.render({ query, totalCount })
}
}
2 changes: 1 addition & 1 deletion services/github/github-search.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ t.create('hit counter')

t.create('hit counter for nonexistent repo')
.get('/badges/puppets/async%20handle.json')
.expectJSON({ name: 'async handle counter', value: 'repo not found' })
.expectJSON({ name: 'counter', value: 'repo not found' })

0 comments on commit e4fe8c0

Please sign in to comment.