provide a non-repository scoped version of [githubcodesearch] (#10733)
and redirect /search/user/repo/q to /search?query=q%20repo:user/repo Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import Joi from 'joi'
|
import Joi from 'joi'
|
||||||
import { pathParams } from '../index.js'
|
import { queryParams, redirector } from '../index.js'
|
||||||
import { metric } from '../text-formatters.js'
|
import { metric } from '../text-formatters.js'
|
||||||
import { nonNegativeInteger } from '../validators.js'
|
import { nonNegativeInteger } from '../validators.js'
|
||||||
import { GithubAuthV3Service } from './github-auth-service.js'
|
import { GithubAuthV3Service } from './github-auth-service.js'
|
||||||
@@ -7,33 +7,35 @@ import { documentation } from './github-helpers.js'
|
|||||||
|
|
||||||
const schema = Joi.object({ total_count: nonNegativeInteger }).required()
|
const schema = Joi.object({ total_count: nonNegativeInteger }).required()
|
||||||
|
|
||||||
export default class GithubSearch extends GithubAuthV3Service {
|
const queryParamSchema = Joi.object({
|
||||||
|
query: Joi.string().required(),
|
||||||
|
}).required()
|
||||||
|
|
||||||
|
const codeSearchDocs = `
|
||||||
|
For a full list of available filters and allowed values,
|
||||||
|
see GitHub's documentation on
|
||||||
|
[Searching code](https://docs.github.com/en/search-github/github-code-search/understanding-github-code-search-syntax)`
|
||||||
|
|
||||||
|
class GitHubCodeSearch extends GithubAuthV3Service {
|
||||||
static category = 'analysis'
|
static category = 'analysis'
|
||||||
|
|
||||||
static route = {
|
static route = {
|
||||||
base: 'github/search',
|
base: 'github',
|
||||||
pattern: ':user/:repo/:query+',
|
pattern: 'search',
|
||||||
|
queryParamSchema,
|
||||||
}
|
}
|
||||||
|
|
||||||
static openApi = {
|
static openApi = {
|
||||||
'/github/search/{user}/{repo}/{query}': {
|
'/github/search': {
|
||||||
get: {
|
get: {
|
||||||
summary: 'GitHub search hit counter',
|
summary: 'GitHub code search count',
|
||||||
description: documentation,
|
description: documentation,
|
||||||
parameters: pathParams(
|
parameters: queryParams({
|
||||||
{
|
name: 'query',
|
||||||
name: 'user',
|
description: codeSearchDocs,
|
||||||
example: 'torvalds',
|
example: 'goto language:javascript NOT is:fork NOT is:archived',
|
||||||
},
|
required: true,
|
||||||
{
|
}),
|
||||||
name: 'repo',
|
|
||||||
example: 'linux',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'query',
|
|
||||||
example: 'goto',
|
|
||||||
},
|
|
||||||
),
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -50,21 +52,35 @@ export default class GithubSearch extends GithubAuthV3Service {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async handle({ user, repo, query }) {
|
async handle(_routeParams, { query }) {
|
||||||
const { total_count: totalCount } = await this._requestJson({
|
const { total_count: totalCount } = await this._requestJson({
|
||||||
url: '/search/code',
|
url: '/search/code',
|
||||||
options: {
|
options: {
|
||||||
searchParams: {
|
searchParams: {
|
||||||
q: `${query} repo:${user}/${repo}`,
|
q: query,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
schema,
|
schema,
|
||||||
httpErrors: {
|
httpErrors: {
|
||||||
401: 'auth required for search api',
|
401: 'auth required for search api',
|
||||||
404: 'repo not found',
|
|
||||||
422: 'repo not found',
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
return this.constructor.render({ query, totalCount })
|
return this.constructor.render({ query, totalCount })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const GitHubCodeSearchRedirect = redirector({
|
||||||
|
category: 'analysis',
|
||||||
|
route: {
|
||||||
|
base: 'github/search',
|
||||||
|
pattern: ':user/:repo/:query+',
|
||||||
|
},
|
||||||
|
transformPath: () => '/github/search',
|
||||||
|
transformQueryParams: ({ query, user, repo }) => ({
|
||||||
|
query: `${query} repo:${user}/${repo}`,
|
||||||
|
}),
|
||||||
|
dateAdded: new Date('2024-11-29'),
|
||||||
|
})
|
||||||
|
|
||||||
|
export { GitHubCodeSearch, GitHubCodeSearchRedirect }
|
||||||
|
|||||||
@@ -3,9 +3,18 @@ import { createServiceTester } from '../tester.js'
|
|||||||
export const t = await createServiceTester()
|
export const t = await createServiceTester()
|
||||||
|
|
||||||
t.create('hit counter')
|
t.create('hit counter')
|
||||||
.get('/badges/shields/async%20handle.json')
|
.get('/search.json?query=async%20handle')
|
||||||
.expectBadge({ label: 'async handle counter', message: isMetric })
|
.expectBadge({ label: 'async handle counter', message: isMetric })
|
||||||
|
|
||||||
t.create('hit counter for nonexistent repo')
|
t.create('hit counter, zero results')
|
||||||
.get('/badges/puppets/async%20handle.json')
|
.get('/search.json?query=async%20handle%20repo%3Abadges%2Fpuppets')
|
||||||
.expectBadge({ label: 'async handle counter', message: '0' })
|
.expectBadge({
|
||||||
|
label: 'async handle repo:badges/puppets counter',
|
||||||
|
message: '0',
|
||||||
|
})
|
||||||
|
|
||||||
|
t.create('legacy redirect')
|
||||||
|
.get('/search/badges/shields/async%20handle.svg')
|
||||||
|
.expectRedirect(
|
||||||
|
'/github/search.svg?query=async%20handle%20repo%3Abadges%2Fshields',
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user