Files
shields/services/github/github-search.service.js
chris48s c586960ce8 allow calling [github] without auth (#9427)
* allow calling [github] without auth

* set a default for authType, fix broken tests
2023-08-07 14:51:18 +00:00

61 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 { 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,
httpErrors: {
401: 'auth required for search api',
404: 'repo not found',
422: 'repo not found',
},
})
return this.constructor.render({ query, totalCount })
}
}