Skip to content

Commit

Permalink
provide a non-repository scoped version of [githubcodesearch]
Browse files Browse the repository at this point in the history
and redirect /search/user/repo/q
to /search?query=q%20repo:user/repo
  • Loading branch information
ccoVeille authored and chris48s committed Dec 11, 2024
1 parent 5cdef88 commit 0009ecd
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 28 deletions.
64 changes: 40 additions & 24 deletions services/github/github-search.service.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
import Joi from 'joi'
import { pathParams } from '../index.js'
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()

export default class GithubSearch extends GithubAuthV3Service {
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/search',
pattern: ':user/:repo/:query+',
base: 'github',
pattern: 'search',
queryParamSchema,
}

static openApi = {
'/github/search/{user}/{repo}/{query}': {
'/github/search': {
get: {
summary: 'GitHub search hit counter',
summary: 'GitHub code search count',
description: documentation,
parameters: pathParams(
{
name: 'user',
example: 'torvalds',
},
{
name: 'repo',
example: 'linux',
},
{
name: 'query',
example: 'goto',
},
),
parameters: queryParams({
name: 'query',
description: codeSearchDocs,
example: 'goto language:javascript NOT is:fork NOT is:archived',
required: true,
}),
},
},
}
Expand All @@ -50,21 +52,35 @@ export default class GithubSearch extends GithubAuthV3Service {
}
}

async handle({ user, repo, query }) {
async handle(_routeParams, { query }) {
const { total_count: totalCount } = await this._requestJson({
url: '/search/code',
options: {
searchParams: {
q: `${query} repo:${user}/${repo}`,
q: query,
},
},
schema,
httpErrors: {
401: 'auth required for search api',
404: 'repo not found',
422: 'repo not found',
},
})

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 }
17 changes: 13 additions & 4 deletions services/github/github-search.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@ import { createServiceTester } from '../tester.js'
export const t = await createServiceTester()

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

t.create('hit counter for nonexistent repo')
.get('/badges/puppets/async%20handle.json')
.expectBadge({ label: 'async handle counter', message: '0' })
t.create('hit counter, zero results')
.get('/search.json?query=async%20handle%20repo%3Abadges%2Fpuppets')
.expectBadge({
label: 'async handle repo:badges/puppets counter',
message: '0',
})

t.create('legacy redirect')
.get('/search/badges/shields/async%20handle.svg')
.expectRedirect(
'/github/search.svg?query=async%20handle%20repo%3Abadges%2Fshields',
)

0 comments on commit 0009ecd

Please sign in to comment.