-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathgithub-issues-search.service.js
116 lines (103 loc) · 3.09 KB
/
github-issues-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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import gql from 'graphql-tag'
import Joi from 'joi'
import { pathParam, queryParam } from '../index.js'
import { metric } from '../text-formatters.js'
import { nonNegativeInteger } from '../validators.js'
import { GithubAuthV4Service } from './github-auth-service.js'
import { documentation, transformErrors } from './github-helpers.js'
const issuesSearchDocs = `
For a full list of available filters and allowed values,
see GitHub's documentation on
[Searching issues and pull requests](https://docs.github.com/en/search-github/searching-on-github/searching-issues-and-pull-requests)
`
const issueCountSchema = Joi.object({
data: Joi.object({
search: Joi.object({
issueCount: nonNegativeInteger,
}).required(),
}).required(),
}).required()
const queryParamSchema = Joi.object({
query: Joi.string().required(),
}).required()
class BaseGithubIssuesSearch extends GithubAuthV4Service {
static category = 'issue-tracking'
static defaultBadgeData = { label: 'query', color: 'informational' }
static render({ issueCount }) {
return { message: metric(issueCount) }
}
async fetch({ query }) {
const data = await this._requestGraphql({
query: gql`
query ($query: String!) {
search(query: $query, type: ISSUE) {
issueCount
}
}
`,
variables: { query },
schema: issueCountSchema,
transformErrors,
})
return data.data.search.issueCount
}
}
class GithubIssuesSearch extends BaseGithubIssuesSearch {
static route = {
base: 'github',
pattern: 'issues-search',
queryParamSchema,
}
static openApi = {
'/github/issues-search': {
get: {
summary: 'GitHub issue custom search',
description: documentation,
parameters: [
queryParam({
name: 'query',
description: issuesSearchDocs,
example:
'repo:badges/shields is:closed label:bug author:app/sentry-io',
required: true,
}),
],
},
},
}
async handle(namedParams, { query }) {
const issueCount = await this.fetch({ query })
return this.constructor.render({ issueCount })
}
}
class GithubRepoIssuesSearch extends BaseGithubIssuesSearch {
static route = {
base: 'github',
pattern: 'issues-search/:user/:repo',
queryParamSchema,
}
static openApi = {
'/github/issues-search/{user}/{repo}': {
get: {
summary: 'GitHub issue custom search in repo',
description: documentation,
parameters: [
pathParam({ name: 'user', example: 'badges' }),
pathParam({ name: 'repo', example: 'shields' }),
queryParam({
name: 'query',
description: issuesSearchDocs,
example: 'is:closed label:bug author:app/sentry-io',
required: true,
}),
],
},
},
}
async handle({ user, repo }, { query }) {
query = `repo:${user}/${repo} ${query}`
const issueCount = await this.fetch({ query })
return this.constructor.render({ issueCount })
}
}
export { GithubIssuesSearch, GithubRepoIssuesSearch }