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

Rewrite [GithubSearch] #3099

Merged
merged 4 commits into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
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' })