Skip to content

Commit

Permalink
redirect /search/user/repo/q to /search?query=q%20repo:user/repo
Browse files Browse the repository at this point in the history
  • Loading branch information
chris48s committed Dec 11, 2024
1 parent ab1b57d commit a99c11b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 42 deletions.
7 changes: 0 additions & 7 deletions services/github/github-code-search.tester.js

This file was deleted.

14 changes: 0 additions & 14 deletions services/github/github-search-redirect.service.js

This file was deleted.

8 changes: 0 additions & 8 deletions services/github/github-search-redirect.tester.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Joi from 'joi'
import { queryParams } 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'
Expand All @@ -14,26 +14,25 @@ const queryParamSchema = Joi.object({
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)
[Searching code](https://docs.github.com/en/search-github/github-code-search/understanding-github-code-search-syntax)`

${documentation}`

export class GitHubCodeSearch extends GithubAuthV3Service {
class GitHubCodeSearch extends GithubAuthV3Service {
static category = 'analysis'

static route = {
base: 'github',
pattern: 'code-search',
pattern: 'search',
queryParamSchema,
}

static openApi = {
'/github/code-search': {
'/github/search': {
get: {
summary: 'GitHub code search count',
description: codeSearchDocs,
description: documentation,
parameters: queryParams({
name: 'query',
description: codeSearchDocs,
example: 'goto language:javascript NOT is:fork NOT is:archived',
required: true,
}),
Expand All @@ -45,6 +44,14 @@ export class GitHubCodeSearch extends GithubAuthV3Service {
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',
Expand All @@ -59,10 +66,21 @@ export class GitHubCodeSearch extends GithubAuthV3Service {
},
})

return {
label: `${query} counter`,
message: metric(totalCount),
color: 'blue',
}
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 }
20 changes: 20 additions & 0 deletions services/github/github-search.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { isMetric } from '../test-validators.js'
import { createServiceTester } from '../tester.js'
export const t = await createServiceTester()

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

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 a99c11b

Please sign in to comment.