Skip to content

Commit 21a059d

Browse files
chris48sccoVeille
andauthored
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>
1 parent 7c067fd commit 21a059d

File tree

2 files changed

+53
-28
lines changed

2 files changed

+53
-28
lines changed
+40-24
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,41 @@
11
import Joi from 'joi'
2-
import { pathParams } from '../index.js'
2+
import { queryParams, redirector } from '../index.js'
33
import { metric } from '../text-formatters.js'
44
import { nonNegativeInteger } from '../validators.js'
55
import { GithubAuthV3Service } from './github-auth-service.js'
66
import { documentation } from './github-helpers.js'
77

88
const schema = Joi.object({ total_count: nonNegativeInteger }).required()
99

10-
export default class GithubSearch extends GithubAuthV3Service {
10+
const queryParamSchema = Joi.object({
11+
query: Joi.string().required(),
12+
}).required()
13+
14+
const codeSearchDocs = `
15+
For a full list of available filters and allowed values,
16+
see GitHub's documentation on
17+
[Searching code](https://docs.github.com/en/search-github/github-code-search/understanding-github-code-search-syntax)`
18+
19+
class GitHubCodeSearch extends GithubAuthV3Service {
1120
static category = 'analysis'
1221

1322
static route = {
14-
base: 'github/search',
15-
pattern: ':user/:repo/:query+',
23+
base: 'github',
24+
pattern: 'search',
25+
queryParamSchema,
1626
}
1727

1828
static openApi = {
19-
'/github/search/{user}/{repo}/{query}': {
29+
'/github/search': {
2030
get: {
21-
summary: 'GitHub search hit counter',
31+
summary: 'GitHub code search count',
2232
description: documentation,
23-
parameters: pathParams(
24-
{
25-
name: 'user',
26-
example: 'torvalds',
27-
},
28-
{
29-
name: 'repo',
30-
example: 'linux',
31-
},
32-
{
33-
name: 'query',
34-
example: 'goto',
35-
},
36-
),
33+
parameters: queryParams({
34+
name: 'query',
35+
description: codeSearchDocs,
36+
example: 'goto language:javascript NOT is:fork NOT is:archived',
37+
required: true,
38+
}),
3739
},
3840
},
3941
}
@@ -50,21 +52,35 @@ export default class GithubSearch extends GithubAuthV3Service {
5052
}
5153
}
5254

53-
async handle({ user, repo, query }) {
55+
async handle(_routeParams, { query }) {
5456
const { total_count: totalCount } = await this._requestJson({
5557
url: '/search/code',
5658
options: {
5759
searchParams: {
58-
q: `${query} repo:${user}/${repo}`,
60+
q: query,
5961
},
6062
},
6163
schema,
6264
httpErrors: {
6365
401: 'auth required for search api',
64-
404: 'repo not found',
65-
422: 'repo not found',
6666
},
6767
})
68+
6869
return this.constructor.render({ query, totalCount })
6970
}
7071
}
72+
73+
const GitHubCodeSearchRedirect = redirector({
74+
category: 'analysis',
75+
route: {
76+
base: 'github/search',
77+
pattern: ':user/:repo/:query+',
78+
},
79+
transformPath: () => '/github/search',
80+
transformQueryParams: ({ query, user, repo }) => ({
81+
query: `${query} repo:${user}/${repo}`,
82+
}),
83+
dateAdded: new Date('2024-11-29'),
84+
})
85+
86+
export { GitHubCodeSearch, GitHubCodeSearchRedirect }

services/github/github-search.tester.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@ import { createServiceTester } from '../tester.js'
33
export const t = await createServiceTester()
44

55
t.create('hit counter')
6-
.get('/badges/shields/async%20handle.json')
6+
.get('/search.json?query=async%20handle')
77
.expectBadge({ label: 'async handle counter', message: isMetric })
88

9-
t.create('hit counter for nonexistent repo')
10-
.get('/badges/puppets/async%20handle.json')
11-
.expectBadge({ label: 'async handle counter', message: '0' })
9+
t.create('hit counter, zero results')
10+
.get('/search.json?query=async%20handle%20repo%3Abadges%2Fpuppets')
11+
.expectBadge({
12+
label: 'async handle repo:badges/puppets counter',
13+
message: '0',
14+
})
15+
16+
t.create('legacy redirect')
17+
.get('/search/badges/shields/async%20handle.svg')
18+
.expectRedirect(
19+
'/github/search.svg?query=async%20handle%20repo%3Abadges%2Fshields',
20+
)

0 commit comments

Comments
 (0)