1
1
import Joi from 'joi'
2
- import { pathParams } from '../index.js'
2
+ import { queryParams , redirector } from '../index.js'
3
3
import { metric } from '../text-formatters.js'
4
4
import { nonNegativeInteger } from '../validators.js'
5
5
import { GithubAuthV3Service } from './github-auth-service.js'
6
6
import { documentation } from './github-helpers.js'
7
7
8
8
const schema = Joi . object ( { total_count : nonNegativeInteger } ) . required ( )
9
9
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 {
11
20
static category = 'analysis'
12
21
13
22
static route = {
14
- base : 'github/search' ,
15
- pattern : ':user/:repo/:query+' ,
23
+ base : 'github' ,
24
+ pattern : 'search' ,
25
+ queryParamSchema,
16
26
}
17
27
18
28
static openApi = {
19
- '/github/search/{user}/{repo}/{query} ' : {
29
+ '/github/search' : {
20
30
get : {
21
- summary : 'GitHub search hit counter ' ,
31
+ summary : 'GitHub code search count ' ,
22
32
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
+ } ) ,
37
39
} ,
38
40
} ,
39
41
}
@@ -50,21 +52,35 @@ export default class GithubSearch extends GithubAuthV3Service {
50
52
}
51
53
}
52
54
53
- async handle ( { user , repo , query } ) {
55
+ async handle ( _routeParams , { query } ) {
54
56
const { total_count : totalCount } = await this . _requestJson ( {
55
57
url : '/search/code' ,
56
58
options : {
57
59
searchParams : {
58
- q : ` ${ query } repo: ${ user } / ${ repo } ` ,
60
+ q : query ,
59
61
} ,
60
62
} ,
61
63
schema,
62
64
httpErrors : {
63
65
401 : 'auth required for search api' ,
64
- 404 : 'repo not found' ,
65
- 422 : 'repo not found' ,
66
66
} ,
67
67
} )
68
+
68
69
return this . constructor . render ( { query, totalCount } )
69
70
}
70
71
}
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 }
0 commit comments