-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathgithub-search.service.js
86 lines (74 loc) · 2.16 KB
/
github-search.service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import Joi from 'joi'
import { queryParams, redirector } from '../index.js'
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()
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 route = {
base: 'github',
pattern: 'search',
queryParamSchema,
}
static openApi = {
'/github/search': {
get: {
summary: 'GitHub code search count',
description: documentation,
parameters: queryParams({
name: 'query',
description: codeSearchDocs,
example: 'goto language:javascript NOT is:fork NOT is:archived',
required: true,
}),
},
},
}
static defaultBadgeData = {
label: 'counter',
}
static render({ query, totalCount }) {
return {
label: `${query} counter`,
message: metric(totalCount),
color: 'blue',
}
}
async handle(_routeParams, { query }) {
const { total_count: totalCount } = await this._requestJson({
url: '/search/code',
options: {
searchParams: {
q: query,
},
},
schema,
httpErrors: {
401: 'auth required for search api',
},
})
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 }