Files
shields/services/github/github-search.service.js
chris48s c73072deed Remove requestOptions2GotOptions compatibility layer (#7270)
* gzip --> decompress

* strictSSL --> https.rejectUnauthorized

* auth --> username/password

* qs --> searchParams

* fix base service auth docs

* completely remove requestOptions2GotOptions layer

* update the docs

Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
2021-11-15 19:56:08 +00:00

57 lines
1.4 KiB
JavaScript

import Joi from 'joi'
import { metric } from '../text-formatters.js'
import { nonNegativeInteger } from '../validators.js'
import { GithubAuthV3Service } from './github-auth-service.js'
import { errorMessagesFor, documentation } from './github-helpers.js'
const schema = Joi.object({ total_count: nonNegativeInteger }).required()
export default class GithubSearch extends GithubAuthV3Service {
static category = 'analysis'
static route = {
base: 'github/search',
pattern: ':user/:repo/:query+',
}
static examples = [
{
title: 'GitHub search hit counter',
pattern: ':user/:repo/:query',
namedParams: {
user: 'torvalds',
repo: 'linux',
query: 'goto',
},
staticPreview: this.render({ query: 'goto', totalCount: 14000 }),
documentation,
},
]
static defaultBadgeData = {
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: {
searchParams: {
q: `${query} repo:${user}/${repo}`,
},
},
schema,
errorMessages: errorMessagesFor('repo not found'),
})
return this.constructor.render({ query, totalCount })
}
}