Skip to content

Commit

Permalink
Refactor GitHub Code Search, and provide a non-repository scoped version
Browse files Browse the repository at this point in the history
  • Loading branch information
ccoVeille committed Nov 21, 2024
1 parent 5cdef88 commit 0cf4a48
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 16 deletions.
77 changes: 62 additions & 15 deletions services/github/github-search.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,66 @@ import { documentation } from './github-helpers.js'

const schema = Joi.object({ total_count: nonNegativeInteger }).required()

export default class GithubSearch extends GithubAuthV3Service {
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)
${documentation}`

class BaseGithubCodeSearch extends GithubAuthV3Service {
static category = 'analysis'

static defaultBadgeData = {
label: 'counter',
}

static render({ query, totalCount }) {
return {
label: `${query} counter`,
message: metric(totalCount),
color: 'blue',
}
}
}

class GitHubCodeSearch extends BaseGithubCodeSearch {
static route = {
base: 'github/search',
pattern: ':query+',
}

static openApi = {
'/github/search/{query}': {
get: {
summary: 'GitHub search hit counter',
description: codeSearchDocs,
parameters: pathParams({
name: 'query',
example: 'goto language:javascript NOT is:fork NOT is:archived',
}),
},
},
}

async handle({ user, repo, 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 })
}
}

class GitHubRepoCodeSearch extends BaseGithubCodeSearch {
static route = {
base: 'github/search',
pattern: ':user/:repo/:query+',
Expand All @@ -18,8 +75,8 @@ export default class GithubSearch extends GithubAuthV3Service {
static openApi = {
'/github/search/{user}/{repo}/{query}': {
get: {
summary: 'GitHub search hit counter',
description: documentation,
summary: 'GitHub repo search hit counter',
description: codeSearchDocs,
parameters: pathParams(
{
name: 'user',
Expand All @@ -38,18 +95,6 @@ export default class GithubSearch extends GithubAuthV3Service {
},
}

static defaultBadgeData = {
label: 'counter',
}

static render({ query, totalCount }) {
return {
label: `${query} counter`,
message: metric(totalCount),
color: 'blue',
}
}

async handle({ user, repo, query }) {
const { total_count: totalCount } = await this._requestJson({
url: '/search/code',
Expand All @@ -68,3 +113,5 @@ export default class GithubSearch extends GithubAuthV3Service {
return this.constructor.render({ query, totalCount })
}
}

export { GitHubCodeSearch, GitHubRepoCodeSearch }
6 changes: 5 additions & 1 deletion services/github/github-search.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import { isMetric } from '../test-validators.js'
import { createServiceTester } from '../tester.js'
export const t = await createServiceTester()

t.create('hit counter')
t.create('hit counter on repo')
.get('/badges/shields/async%20handle.json')
.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')
.get('/async%20handle.json')
.expectBadge({ label: 'async handle counter', message: isMetric })

0 comments on commit 0cf4a48

Please sign in to comment.