Search: break dependencies on pre-formatted markdown (#32346)

Currently, for commit matches, we generate label and detail in the backend as markdown-formatted strings. This adds the information needed to generate these fields to the commit match stream type cuts markdown out of the loop.
This commit is contained in:
Camden Cheek 2022-03-09 19:08:45 -06:00 committed by GitHub
parent 7c8091d234
commit 154ca95e7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 62 additions and 34 deletions

View File

@ -83,3 +83,9 @@
flex-grow: 1;
min-width: 0;
}
.commit-oid {
background: var(--code-bg);
display: inline-block;
padding: 0.25rem;
}

View File

@ -4,15 +4,23 @@ import LockIcon from 'mdi-react/LockIcon'
import SourceForkIcon from 'mdi-react/SourceForkIcon'
import React from 'react'
import { renderMarkdown } from '@sourcegraph/common'
import { LastSyncedIcon } from '@sourcegraph/shared/src/components/LastSyncedIcon'
import { Markdown } from '@sourcegraph/shared/src/components/Markdown'
import { displayRepoName } from '@sourcegraph/shared/src/components/RepoFileLink'
import { RepoIcon } from '@sourcegraph/shared/src/components/RepoIcon'
import { ResultContainer } from '@sourcegraph/shared/src/components/ResultContainer'
import { SearchResultStar } from '@sourcegraph/shared/src/components/SearchResultStar'
import { PlatformContextProps } from '@sourcegraph/shared/src/platform/context'
import { CommitMatch, getMatchTitle, RepositoryMatch } from '@sourcegraph/shared/src/search/stream'
import {
CommitMatch,
getCommitMatchUrl,
getRepoMatchLabel,
getRepoMatchUrl,
getRepositoryUrl,
RepositoryMatch,
} from '@sourcegraph/shared/src/search/stream'
import { formatRepositoryStarCount } from '@sourcegraph/shared/src/util/stars'
import { Timestamp } from '@sourcegraph/web/src/components/time/Timestamp'
import { Link } from '@sourcegraph/wildcard'
import { CommitSearchResultMatch } from './CommitSearchResultMatch'
import styles from './SearchResult.module.scss'
@ -38,19 +46,28 @@ export const SearchResult: React.FunctionComponent<Props> = ({
return (
<div className={styles.title}>
<RepoIcon repoName={repoName} className="icon-inline text-muted flex-shrink-0" />
<Markdown
className="test-search-result-label ml-1 flex-shrink-past-contents text-truncate"
dangerousInnerHTML={renderMarkdown(getMatchTitle(result))}
/>
<span className="test-search-result-label ml-1 flex-shrink-past-contents text-truncate">
{result.type === 'commit' && (
<>
<Link to={getRepositoryUrl(result.repository)}>{displayRepoName(result.repository)}</Link>
{' '}
<Link to={getCommitMatchUrl(result)}>{result.authorName}</Link>
{': '}
<Link to={getCommitMatchUrl(result)}>{result.message.split('\n', 1)[0]}</Link>
</>
)}
{result.type === 'repo' && (
<Link to={getRepoMatchUrl(result)}>{displayRepoName(getRepoMatchLabel(result))}</Link>
)}
</span>
<span className={styles.spacer} />
{result.type === 'commit' && result.detail && (
<>
<Markdown className="flex-shrink-0" dangerousInnerHTML={renderMarkdown(result.detail)} />
</>
)}
{result.type === 'commit' && result.detail && formattedRepositoryStarCount && (
<div className={styles.divider} />
{result.type === 'commit' && (
<Link to={getCommitMatchUrl(result)}>
<code className={styles.commitOid}>{result.oid.slice(0, 7)}</code>{' '}
<Timestamp date={result.authorDate} noAbout={true} strict={true} />
</Link>
)}
{result.type === 'commit' && formattedRepositoryStarCount && <div className={styles.divider} />}
{formattedRepositoryStarCount && (
<>
<SearchResultStar />

View File

@ -5,7 +5,6 @@ import { AggregableBadge } from 'sourcegraph'
import { asError, ErrorLike, isErrorLike } from '@sourcegraph/common'
import { displayRepoName } from '../components/RepoFileLink'
import { SearchPatternType } from '../graphql-operations'
import { SymbolKind } from '../schema'
@ -102,10 +101,12 @@ type MarkdownText = string
*/
export interface CommitMatch {
type: 'commit'
label: MarkdownText
url: string
detail: MarkdownText
repository: string
oid: string
message: string
authorName: string
authorDate: string
repoStars?: number
repoLastFetched?: string
@ -513,6 +514,10 @@ export function getRepoMatchUrl(repoMatch: RepositoryMatch): string {
return '/' + encodeURI(label)
}
export function getCommitMatchUrl(commitMatch: CommitMatch): string {
return '/' + encodeURI(commitMatch.repository) + '/-/commit/' + commitMatch.oid
}
export function getMatchUrl(match: SearchMatch): string {
switch (match.type) {
case 'path':
@ -520,16 +525,8 @@ export function getMatchUrl(match: SearchMatch): string {
case 'symbol':
return getFileMatchUrl(match)
case 'commit':
return match.url
return getCommitMatchUrl(match)
case 'repo':
return getRepoMatchUrl(match)
}
}
export function getMatchTitle(match: RepositoryMatch | CommitMatch): MarkdownText {
if (match.type === 'commit') {
return match.label
}
return `[${displayRepoName(getRepoMatchLabel(match))}](${getRepoMatchUrl(match)})`
}

View File

@ -10,12 +10,12 @@ export const diffSearchStreamEvents: SearchEvent[] = [
data: [
{
type: 'commit',
label:
'[sourcegraph/sourcegraph-lightstep](/gitlab.sgdev.org/sourcegraph/sourcegraph-lightstep) [Quinn Slack](/gitlab.sgdev.org/sourcegraph/sourcegraph-lightstep/-/commit/65dba23797be9e0ce1941f92c5385a7856bc5a42): [build: set up test deps and scripts](/gitlab.sgdev.org/sourcegraph/sourcegraph-lightstep/-/commit/65dba23797be9e0ce1941f92c5385a7856bc5a42)',
oid: '65dba23797be9e0ce1941f92c5385a7856bc5a42',
message: 'build: set up test deps and scripts\n',
authorName: 'Quinn Slack',
authorDate: '2019-10-29T20:59:15Z',
url:
'/gitlab.sgdev.org/sourcegraph/sourcegraph-lightstep/-/commit/65dba23797be9e0ce1941f92c5385a7856bc5a42',
detail:
'[`65dba23` 2 years ago](/gitlab.sgdev.org/sourcegraph/sourcegraph-lightstep/-/commit/65dba23797be9e0ce1941f92c5385a7856bc5a42)',
repository: 'gitlab.sgdev.org/sourcegraph/sourcegraph-lightstep',
content:
'```diff\nmocha.opts mocha.opts\n@@ -0,0 +3,2 @@\n+--timeout 200\n+src/**/*.test.ts\n\\ No newline at end of file\npackage.json package.json\n@@ -50,0 +54,3 @@\n+ "exclude": [\n+ "**/*.test.ts"\n+ ],\n@@ -54,1 +64,2 @@\n- "serve": "parcel serve --no-hmr --out-file dist/extension.js src/extension.ts",\n+ "test": "TS_NODE_COMPILER_OPTIONS=\'{\\"module\\":\\"commonjs\\"}\' mocha --require ts-node/register --require source-map-support/register --opts mocha.opts",\n+ "cover": "TS_NODE_COMPILER_OPTIONS=\'{\\"module\\":\\"commonjs\\"}\' nyc --require ts-node/register --require source-map-support/register --all mocha --opts mocha.opts --timeout 10000",\n@@ -57,2 +70,2 @@\n- "sourcegraph:prepublish": "parcel build src/extension.ts"\n+ "sourcegraph:prepublish": "yarn typecheck && yarn test && yarn build"\n },\nyarn.lock yarn.lock\n@@ -3736,0 +4204,3 @@ number-is-nan@^1.0.0:\n+ spawn-wrap "^1.4.2"\n+ test-exclude "^5.1.0"\n+ uuid "^3.3.2"\n@@ -5550,1 +6166,5 @@ terser@^3.7.3, terser@^3.8.1:\n \n+test-exclude@^5.1.0:\n+ version "5.1.0"\n+ resolved "https://registry.yarnpkg.com/test-exclude/-/test-exclude-5.1.0.tgz#6ba6b25179d2d38724824661323b73e03c0c1de1"\n+ integrity sha512-gwf0S2fFsANC55fSeSqpb8BYk6w3FDvwZxfNjeF6FRgvFa43r+7wRiA/Q0IxoRU37wB/LE8IQ4221BsNucTaCA==\n```',
@ -48,12 +48,12 @@ export const commitSearchStreamEvents: SearchEvent[] = [
data: [
{
type: 'commit',
label:
'[sourcegraph/sourcegraph-sentry](/gitlab.sgdev.org/sourcegraph/sourcegraph-sentry) [Vanesa](/gitlab.sgdev.org/sourcegraph/sourcegraph-sentry/-/commit/7e69ceb49adc30cb46bbe50335e1a371a0f2f6b1): [add more tests, use the Sourcegraph stubs api and improve repo matching. (#13)](/gitlab.sgdev.org/sourcegraph/sourcegraph-sentry/-/commit/7e69ceb49adc30cb46bbe50335e1a371a0f2f6b1)',
oid: '7e69ceb49adc30cb46bbe50335e1a371a0f2f6b1',
message: 'add more tests, use the Sourcegraph stubs api and improve repo matching. (#13)',
authorName: 'Vanesa',
authorDate: '2019-10-29T20:59:15Z',
url:
'/gitlab.sgdev.org/sourcegraph/sourcegraph-sentry/-/commit/7e69ceb49adc30cb46bbe50335e1a371a0f2f6b1',
detail:
'[`7e69ceb` 2 years ago](/gitlab.sgdev.org/sourcegraph/sourcegraph-sentry/-/commit/7e69ceb49adc30cb46bbe50335e1a371a0f2f6b1)',
repository: 'gitlab.sgdev.org/sourcegraph/sourcegraph-sentry',
content:
'```COMMIT_EDITMSG\nadd more tests, use the Sourcegraph stubs api and improve repo matching. (#13)\n\n* add more tests, refactor to use extension api stubs\r\n* improve repo matching\r\nCo-Authored-By: Felix Becker <felix.b@outlook.com>\n```',

View File

@ -561,6 +561,10 @@ func fromCommit(commit *result.CommitMatch, repoCache map[api.RepoID]*types.Sear
URL: commit.URL().String(),
Detail: commit.Detail(),
Repository: string(commit.Repo.Name),
OID: string(commit.Commit.ID),
Message: string(commit.Commit.Message),
AuthorName: commit.Commit.Author.Name,
AuthorDate: commit.Commit.Author.Date,
Content: hls.Value,
Ranges: ranges,
}

View File

@ -137,6 +137,10 @@ type EventCommitMatch struct {
Detail string `json:"detail"`
RepositoryID int32 `json:"repositoryID"`
Repository string `json:"repository"`
OID string `json:"oid"`
Message string `json:"message"`
AuthorName string `json:"authorName"`
AuthorDate time.Time `json:"authorDate"`
RepoStars int `json:"repoStars,omitempty"`
RepoLastFetched *time.Time `json:"repoLastFetched,omitempty"`
Content string `json:"content"`