Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add [GitHubDiscussionsSearch] and GitHubRepoDiscussionsSearch service #9340

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions services/github/github-discussions-custom-search.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import gql from 'graphql-tag'
import Joi from 'joi'
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 discussionsSearchDocs = `
For a full list of available filters and allowed values that can be used in the <code>query</code>,
see GitHub's documentation on
[Searching discussions](https://docs.github.com/en/search-github/searching-on-github/searching-discussions).
${documentation}
`

const discussionCountSchema = Joi.object({
data: Joi.object({
search: Joi.object({
discussionCount: nonNegativeInteger,
}).required(),
}).required(),
}).required()

const queryParamSchema = Joi.object({
query: Joi.string().required(),
}).required()

class BaseGithubDiscussionsSearch extends GithubAuthV4Service {
static category = 'other'
static defaultBadgeData = { label: 'query', color: 'informational' }

static render({ discussionCount }) {
return { message: metric(discussionCount) }
}

async fetch({ query }) {
const data = await this._requestGraphql({
query: gql`
query ($query: String!) {
search(query: $query, type: DISCUSSION) {
discussionCount
}
}
`,
variables: { query },
schema: discussionCountSchema,
transformErrors,
})
return data.data.search.discussionCount
}
}

class GithubDiscussionsSearch extends BaseGithubDiscussionsSearch {
static route = {
base: 'github',
pattern: 'discussions-search',
queryParamSchema,
}

static examples = [
{
title: 'GitHub discussions custom search',
namedParams: {},
queryParams: {
query: 'repo:badges/shields is:answered answered-by:chris48s',
},
staticPreview: {
label: 'query',
message: '2',
color: 'blue',
},
documentation: discussionsSearchDocs,
},
]

async handle(namedParams, { query }) {
const discussionCount = await this.fetch({ query })
return this.constructor.render({ discussionCount })
}
}

class GithubRepoDiscussionsSearch extends BaseGithubDiscussionsSearch {
static route = {
base: 'github',
pattern: 'discussions-search/:user/:repo',
queryParamSchema,
}

static examples = [
{
title: 'GitHub discussions custom search in repo',
namedParams: {
user: 'badges',
repo: 'shields',
},
queryParams: {
query: 'is:answered answered-by:chris48s',
},
staticPreview: {
label: 'query',
message: '2',
color: 'blue',
},
documentation: discussionsSearchDocs,
},
]

async handle({ user, repo }, { query }) {
query = `repo:${user}/${repo} ${query}`
const discussionCount = await this.fetch({ query })
return this.constructor.render({ discussionCount })
}
}

export { GithubDiscussionsSearch, GithubRepoDiscussionsSearch }
48 changes: 48 additions & 0 deletions services/github/github-discussions-custom-search.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { isMetric } from '../test-validators.js'
import { ServiceTester } from '../tester.js'
export const t = new ServiceTester({
id: 'GithubDiscussionsSearch',
title: 'Github Discussions Search',
pathPrefix: '/github',
})

t.create('GitHub discussions search (valid query string)')
.get(
'/discussions-search.json?query=repo%3Abadges%2Fshields%20is%3Aanswered%20author%3Achris48s'
)
.expectBadge({
label: 'query',
message: isMetric,
})

t.create('GitHub discussions search (invalid query string)')
.get('/discussions-search.json?query=')
.expectBadge({
label: 'query',
message: 'invalid query parameter: query',
})

t.create('GitHub Repo discussions search (valid query string)')
.get(
'/discussions-search/badges/shields.json?query=is%3Aanswered%20author%3Achris48s'
)
.expectBadge({
label: 'query',
message: isMetric,
})

t.create('GitHub Repo discussions search (invalid query string)')
.get('/discussions-search/badges/shields.json?query=')
.expectBadge({
label: 'query',
message: 'invalid query parameter: query',
})

t.create('GitHub Repo discussions search (invalid repo)')
.get(
'/discussions-search/badges/helmets.json?query=is%3Aanswered%20author%3Achris48s'
)
.expectBadge({
label: 'query',
message: '0',
})