search: json encode repo metadata for csv export (#59334)

We received the feedback that it is not always obvious that metadata is line-separated in the CSV export of search results. Depending on the tool used to display the CSV, only the first metadata value might be visible. Additionally the current format can be hard to parse. 

Here we add a new column "Repository metadata JSON" to the CSV export of search results, which encodes metadata into a single line JSON. We deprecate "Repository metadata" but keep it around for backward compatibility for short period of time. 

## Test plan
- updated unit test
- manual testing: 
  - validated the JSON encoding with a python script that reads the CSV and parses the JSON field.
This commit is contained in:
Stefan Hengl 2024-01-09 19:31:25 +01:00 committed by GitHub
parent b697ec03e4
commit a834619212
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 2 deletions

View File

@ -21,6 +21,7 @@ All notable changes to Sourcegraph are documented in this file.
- Batch Changes now allows changesets to be exported in CSV and JSON format. [#56721](https://github.com/sourcegraph/sourcegraph/pull/56721)
- Supports custom ChatCompletion models in Cody clients for dotcom users. [#58158](https://github.com/sourcegraph/sourcegraph/pull/58158)
- Topics synced from GitHub and GitLab are now displayed for repository matches in the search results and on the repository tree page. [#58927](https://github.com/sourcegraph/sourcegraph/pull/58927)
- Added a new column "Repository metadata JSON" to the CSV export of repository search results, which includes the JSON encoded object of metadata key-value pairs. [#59334](https://github.com/sourcegraph/sourcegraph/pull/59334)
### Changed
@ -57,6 +58,7 @@ All notable changes to Sourcegraph are documented in this file.
- The feature flag `search-ranking` is now completely removed. [#58156](https://github.com/sourcegraph/sourcegraph/pull/58156)
- The notepad UI, notebook creation feature. [#58217](https://github.com/sourcegraph/sourcegraph/pull/58217)
- The experimental `indexRepositoryName` option for the rust packages code host connection has been removed. [#59176](https://github.com/sourcegraph/sourcegraph/pull/59176)
- The column "Repository metadata" in the CSV export of repository search results is now deprecated and will be removed in a future release. Use "Repository metadata JSON" instead [#59334](https://github.com/sourcegraph/sourcegraph/pull/59334)
## Unreleased 5.2.6

View File

@ -1404,7 +1404,7 @@ describe('searchResultsToFileContent', () => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const [, results] = data.find(([searchType]) => searchType === 'repo')!
expect(searchResultsToFileContent(results, sourcegraphURL, true)).toEqual(
'Match type,Repository,Repository external URL,Repository metadata\nrepo,github.com/lorenzodifuccia/safaribooks,http://localhost:3443/github.com/lorenzodifuccia/safaribooks,"oss\ndeprecated\nsome"",non-standard-key:value"\nrepo,github.com/rfletcher/safari-json-formatter,http://localhost:3443/github.com/rfletcher/safari-json-formatter,\nrepo,github.com/AdguardTeam/AdGuardForSafari,http://localhost:3443/github.com/AdguardTeam/AdGuardForSafari,\nrepo,github.com/kishikawakatsumi/SourceKitForSafari,http://localhost:3443/github.com/kishikawakatsumi/SourceKitForSafari,\nrepo,github.com/shaojiankui/iOS-UDID-Safari,http://localhost:3443/github.com/shaojiankui/iOS-UDID-Safari,'
'Match type,Repository,Repository external URL,Repository metadata,Repository metadata JSON\nrepo,github.com/lorenzodifuccia/safaribooks,http://localhost:3443/github.com/lorenzodifuccia/safaribooks,"oss\ndeprecated\nsome"",non-standard-key:value","{""oss"":null,""deprecated"":null,""some\\"",non-standard-key"":""value""}"\nrepo,github.com/rfletcher/safari-json-formatter,http://localhost:3443/github.com/rfletcher/safari-json-formatter,,{}\nrepo,github.com/AdguardTeam/AdGuardForSafari,http://localhost:3443/github.com/AdguardTeam/AdGuardForSafari,,{}\nrepo,github.com/kishikawakatsumi/SourceKitForSafari,http://localhost:3443/github.com/kishikawakatsumi/SourceKitForSafari,,{}\nrepo,github.com/shaojiankui/iOS-UDID-Safari,http://localhost:3443/github.com/shaojiankui/iOS-UDID-Safari,,{}'
)
})
})

View File

@ -117,7 +117,7 @@ export const searchResultsToFileContent = (
case 'repo': {
content = [
enableRepositoryMetadata ? [...headers, 'Repository metadata'] : headers,
enableRepositoryMetadata ? [...headers, 'Repository metadata', 'Repository metadata JSON'] : headers,
...searchResults
.filter((result: SearchMatch): result is RepositoryMatch => result.type === 'repo')
.map(result => [
@ -131,6 +131,19 @@ export const searchResultsToFileContent = (
.join('\n'),
]
: []),
...(enableRepositoryMetadata
? [
JSON.stringify(
Object.entries(result.metadata ?? {}).reduce(
(obj: { [key: string]: string | null }, [key, value]) => {
obj[key] = value ?? null
return obj
},
{}
)
),
]
: []),
]),
]
break