Skip to content

Commit

Permalink
Fix TextMatch Option doesn't work when search type is commits topicx …
Browse files Browse the repository at this point in the history
…repositories or issues (#2385)
  • Loading branch information
DarkMatterV committed Jun 17, 2022
1 parent 697804f commit 647e6ac
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
8 changes: 5 additions & 3 deletions github/search.go
Expand Up @@ -317,9 +317,11 @@ func (s *SearchService) search(ctx context.Context, searchType string, parameter
// Accept header for search issues based on reactions preview endpoint
// TODO: remove custom Accept header when this API fully launches.
req.Header.Set("Accept", mediaTypeReactionsPreview)
case opts != nil && opts.TextMatch:
// Accept header defaults to "application/vnd.github.v3+json"
// We change it here to fetch back text-match metadata
}
// https://docs.github.com/en/rest/search#search-repositories
// Accept header defaults to "application/vnd.github.v3+json"
// We change it here to fetch back text-match metadata
if opts != nil && opts.TextMatch {
req.Header.Set("Accept", "application/vnd.github.v3.text-match+json")
}

Expand Down
78 changes: 78 additions & 0 deletions github/search_test.go
Expand Up @@ -61,6 +61,84 @@ func TestSearchService_Repositories_coverage(t *testing.T) {
})
}

func TestSearchService_RepositoriesTextMatch(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/search/repositories", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
textMatchResponse := ""
switch r.Header.Get("Accept") {
case "application/vnd.github.v3.text-match+json":
textMatchResponse = `
{
"total_count": 1,
"incomplete_results": false,
"items": [
{
"name":"gopher1",
"text_matches": [
{
"fragment": "I'm afraid my friend what you have found\nIs a gopher who lives to feed",
"matches": [
{
"text": "gopher",
"indices": [
14,
21
]
}
]
}
]
}
]
}
`
default:
textMatchResponse = `
{
"total_count": 1,
"incomplete_results": false,
"items": [
{
"name":"gopher1"
}
]
}
`
}

fmt.Fprint(w, textMatchResponse)
})

opts := &SearchOptions{Sort: "forks", Order: "desc", ListOptions: ListOptions{Page: 2, PerPage: 2}, TextMatch: true}
ctx := context.Background()
result, _, err := client.Search.Repositories(ctx, "blah", opts)
if err != nil {
t.Errorf("Search.Code returned error: %v", err)
}

wantedRepoResult := &Repository{
Name: String("gopher1"),
TextMatches: []*TextMatch{{
Fragment: String("I'm afraid my friend what you have found\nIs a gopher who lives to feed"),
Matches: []*Match{{Text: String("gopher"), Indices: []int{14, 21}}},
},
},
}


want := &RepositoriesSearchResult{
Total: Int(1),
IncompleteResults: Bool(false),
Repositories: []*Repository{wantedRepoResult},
}
if !cmp.Equal(result, want) {
t.Errorf("Search.Repo returned %+v, want %+v", result, want)
}
}

func TestSearchService_Topics(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
Expand Down

0 comments on commit 647e6ac

Please sign in to comment.