From 65720e498e623795b6d44904f302f04a987a1f77 Mon Sep 17 00:00:00 2001 From: Josh Kraft Date: Fri, 21 Apr 2023 09:47:04 -0600 Subject: [PATCH] implement code search updates --- pkg/cmd/search/code/code.go | 218 +++++++++++++++++++ pkg/cmd/search/code/code_test.go | 347 +++++++++++++++++++++++++++++++ pkg/cmd/search/search.go | 2 + pkg/iostreams/color.go | 34 ++- pkg/search/query.go | 4 + pkg/search/query_test.go | 8 +- pkg/search/result.go | 52 +++++ pkg/search/result_test.go | 45 ++++ pkg/search/searcher.go | 29 +++ pkg/search/searcher_mock.go | 44 ++++ pkg/search/searcher_test.go | 151 ++++++++++++++ 11 files changed, 924 insertions(+), 10 deletions(-) create mode 100644 pkg/cmd/search/code/code.go create mode 100644 pkg/cmd/search/code/code_test.go diff --git a/pkg/cmd/search/code/code.go b/pkg/cmd/search/code/code.go new file mode 100644 index 000000000..e8d4a6405 --- /dev/null +++ b/pkg/cmd/search/code/code.go @@ -0,0 +1,218 @@ +package code + +import ( + "fmt" + "strconv" + "strings" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/internal/browser" + "github.com/cli/cli/v2/internal/tableprinter" + "github.com/cli/cli/v2/internal/text" + "github.com/cli/cli/v2/pkg/cmd/search/shared" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/cli/cli/v2/pkg/search" + "github.com/spf13/cobra" +) + +type CodeOptions struct { + Browser browser.Browser + Exporter cmdutil.Exporter + IO *iostreams.IOStreams + Query search.Query + Searcher search.Searcher + WebMode bool +} + +func NewCmdCode(f *cmdutil.Factory, runF func(*CodeOptions) error) *cobra.Command { + opts := &CodeOptions{ + Browser: f.Browser, + IO: f.IOStreams, + Query: search.Query{Kind: search.KindCode, TextMatch: true}, + } + + cmd := &cobra.Command{ + Use: "code []", + Short: "Search for code", + Long: heredoc.Doc(` + Search for code on GitHub. + + The command supports constructing queries using the GitHub search syntax, + using the parameter and qualifier flags, or a combination of the two. + + At least one search term is required when searching code. + + GitHub search syntax is documented at: + + `), + Example: heredoc.Doc(` + # search code matching "react" and "lifecycle" + $ gh search code react lifecycle + + # search code matching "error handling" + $ gh search code "error handling" + + # search code matching "deque" in Python files + $ gh search code deque --language=python + + # search code matching "cli" in repositories owned by microsoft organization + $ gh search code cli --owner=microsoft + + # search code matching "html" in repositories owned by octocat + $ gh search code html --owner=octocat + + # search code matching "panic" in cli repository + $ gh search code panic --repo cli/cli + + # search code matching keyword "lint" in package.json files + $ gh search code lint --filename package.json + `), + RunE: func(c *cobra.Command, args []string) error { + if len(args) == 0 && c.Flags().NFlag() == 0 { + return cmdutil.FlagErrorf("specify search keywords or flags") + } + if opts.Query.Limit < 1 || opts.Query.Limit > shared.SearchMaxResults { + return cmdutil.FlagErrorf("`--limit` must be between 1 and 1000") + } + opts.Query.Keywords = args + if runF != nil { + return runF(opts) + } + var err error + opts.Searcher, err = shared.Searcher(f) + if err != nil { + return err + } + return codeRun(opts) + }, + } + + // Output flags + cmdutil.AddJSONFlags(cmd, &opts.Exporter, search.CodeFields) + cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "Open the search query in the web browser") + + // Query parameter flags + cmd.Flags().IntVarP(&opts.Query.Limit, "limit", "L", 30, "Maximum number of code results to fetch") + + // Query qualifier flags + cmd.Flags().StringVar(&opts.Query.Qualifiers.Extension, "extension", "", "Filter on file extension") + cmd.Flags().StringVar(&opts.Query.Qualifiers.Filename, "filename", "", "Filter on filename") + cmdutil.StringSliceEnumFlag(cmd, &opts.Query.Qualifiers.In, "match", "", nil, []string{"file", "path"}, "Restrict search to file contents or file path") + cmd.Flags().StringVarP(&opts.Query.Qualifiers.Language, "language", "l", "", "Filter results by language") + cmd.Flags().StringSliceVar(&opts.Query.Qualifiers.Repo, "repo", nil, "Filter on repository") + cmd.Flags().StringVar(&opts.Query.Qualifiers.Size, "size", "", "Filter on size range, in kilobytes") + cmd.Flags().StringVar(&opts.Query.Qualifiers.User, "owner", "", "Filter on owner") + + return cmd +} + +func codeRun(opts *CodeOptions) error { + io := opts.IO + if opts.WebMode { + url := opts.Searcher.URL(opts.Query) + if io.IsStdoutTTY() { + fmt.Fprintf(io.ErrOut, "Opening %s in your browser.\n", text.DisplayURL(url)) + } + return opts.Browser.Browse(url) + } + io.StartProgressIndicator() + results, err := opts.Searcher.Code(opts.Query) + io.StopProgressIndicator() + if err != nil { + return err + } + if len(results.Items) == 0 && opts.Exporter == nil { + return cmdutil.NewNoResultsError("no code results matched your search") + } + if err := io.StartPager(); err == nil { + defer io.StopPager() + } else { + fmt.Fprintf(io.ErrOut, "failed to start pager: %v\n", err) + } + if opts.Exporter != nil { + return opts.Exporter.Write(io, results) + } + + return displayResults(io, results, opts.Query.Limit) +} + +func displayResults(io *iostreams.IOStreams, results search.CodeResult, limit int) error { + cs := io.ColorScheme() + tp := tableprinter.New(io) + displayed := 0 + for _, code := range results.Items { + if displayed == limit { + break + } + if io.IsStdoutTTY() { + header := fmt.Sprintf("%s %s", cs.GreenBold(code.Repo.FullName), cs.GreenBold(code.Path)) + tp.AddField(header) + tp.EndRow() + } + row := 0 + for _, textMatch := range code.TextMatches { + out, shouldPrint := buildOutput(textMatch, cs) + + for i, line := range strings.Split(out, "\n") { + if !shouldPrint[i] { + continue + } + if displayed == limit { + break + } + if io.IsStdoutTTY() { + tp.AddField(fmt.Sprintf("%s: %s", cs.YellowBold(strconv.Itoa(row+1)), line)) + } else { + tp.AddField(fmt.Sprintf("%s %s: %s", code.Repo.FullName, code.Path, line)) + } + tp.EndRow() + row++ + displayed++ + } + } + } + if io.IsStdoutTTY() { + header := fmt.Sprintf("Showing %d of %d results\n\n", displayed, max(displayed, results.Total)) + fmt.Fprintf(io.Out, "\n%s", header) + } + return tp.Render() +} + +func buildOutput(tm search.TextMatch, cs *iostreams.ColorScheme) (string, map[int]bool) { + shouldHighlight := getHighlightIndices(tm.Matches) + linesToPrint := make(map[int]bool) + line := 0 + var out strings.Builder + for i, c := range tm.Fragment { + if shouldHighlight[i] { + out.WriteString(cs.Yellow(string(c))) + linesToPrint[line] = true + } else { + out.WriteRune(c) + } + if c == '\n' { + line++ + } + } + return out.String(), linesToPrint +} + +func getHighlightIndices(matches []search.Match) map[int]bool { + m := make(map[int]bool) + for _, match := range matches { + start := match.Indices[0] + stop := match.Indices[1] + for i := start; i < stop; i++ { + m[i] = true + } + } + return m +} + +func max(a, b int) int { + if b < a { + return a + } + return b +} diff --git a/pkg/cmd/search/code/code_test.go b/pkg/cmd/search/code/code_test.go new file mode 100644 index 000000000..6017996ba --- /dev/null +++ b/pkg/cmd/search/code/code_test.go @@ -0,0 +1,347 @@ +package code + +import ( + "bytes" + "fmt" + "testing" + + "github.com/cli/cli/v2/internal/browser" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/cli/cli/v2/pkg/search" + "github.com/google/shlex" + "github.com/stretchr/testify/assert" +) + +func TestNewCmdCode(t *testing.T) { + tests := []struct { + name string + input string + output CodeOptions + wantErr bool + errMsg string + }{ + { + name: "no arguments", + input: "", + wantErr: true, + errMsg: "specify search keywords or flags", + }, + { + name: "keyword arguments", + input: "react lifecycle", + output: CodeOptions{ + Query: search.Query{ + Keywords: []string{"react", "lifecycle"}, + Kind: "code", + Limit: 30, + TextMatch: true, + }, + }, + }, + { + name: "web flag", + input: "--web", + output: CodeOptions{ + Query: search.Query{ + Keywords: []string{}, + Kind: "code", + Limit: 30, + TextMatch: true, + }, + WebMode: true, + }, + }, + { + name: "limit flag", + input: "--limit 10", + output: CodeOptions{ + Query: search.Query{ + Keywords: []string{}, + Kind: "code", + Limit: 10, + TextMatch: true, + }, + }, + }, + { + name: "invalid limit flag", + input: "--limit 1001", + wantErr: true, + errMsg: "`--limit` must be between 1 and 1000", + }, + { + name: "qualifier flags", + input: ` + --extension=extension + --filename=filename + --match=path,file + --language=language + --repo=owner/repo + --size=5 + --owner=owner + `, + output: CodeOptions{ + Query: search.Query{ + Keywords: []string{}, + Kind: "code", + Limit: 30, + TextMatch: true, + Qualifiers: search.Qualifiers{ + Extension: "extension", + Filename: "filename", + In: []string{"path", "file"}, + Language: "language", + Repo: []string{"owner/repo"}, + Size: "5", + User: "owner", + }, + }, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, _, _ := iostreams.Test() + f := &cmdutil.Factory{ + IOStreams: ios, + } + argv, err := shlex.Split(tt.input) + assert.NoError(t, err) + var gotOpts *CodeOptions + cmd := NewCmdCode(f, func(opts *CodeOptions) error { + gotOpts = opts + return nil + }) + cmd.SetArgs(argv) + cmd.SetIn(&bytes.Buffer{}) + cmd.SetOut(&bytes.Buffer{}) + cmd.SetErr(&bytes.Buffer{}) + + _, err = cmd.ExecuteC() + if tt.wantErr { + assert.EqualError(t, err, tt.errMsg) + return + } + + assert.NoError(t, err) + assert.Equal(t, tt.output.Query, gotOpts.Query) + assert.Equal(t, tt.output.WebMode, gotOpts.WebMode) + }) + } +} + +func TestCodeRun(t *testing.T) { + var query = search.Query{ + Keywords: []string{"map"}, + Kind: "code", + Limit: 30, + Qualifiers: search.Qualifiers{ + Language: "go", + Repo: []string{"cli/cli"}, + }, + TextMatch: true, + } + tests := []struct { + errMsg string + name string + opts *CodeOptions + tty bool + wantErr bool + wantStderr string + wantStdout string + }{ + { + name: "displays results tty", + opts: &CodeOptions{ + Query: query, + Searcher: &search.SearcherMock{ + CodeFunc: func(query search.Query) (search.CodeResult, error) { + return search.CodeResult{ + IncompleteResults: false, + Items: []search.Code{ + { + Name: "context.go", + Path: "context/context.go", + Repo: search.Repository{ + FullName: "cli/cli", + }, + TextMatches: []search.TextMatch{ + { + Fragment: "\t}\n\n\tvar repos []*api.Repository\n\trepoMap := map[string]bool{}\n\n\tadd := func(r *api.Repository) {\n\t\tfn := ghrepo.FullName(r)", + Matches: []search.Match{ + { + Text: "Map", + Indices: []int{38, 41}, + }, + { + Text: "map", + Indices: []int{45, 48}, + }, + }, + }, + }, + }, + { + Name: "pr.go", + Path: "pkg/cmd/pr/pr.go", + Repo: search.Repository{ + FullName: "cli/cli", + }, + TextMatches: []search.TextMatch{ + { + + Fragment: "\t\t\t$ gh pr create --fill\n\t\t\t$ gh pr view --web\n\t\t`),\n\t\tAnnotations: map[string]string{\n\t\t\t\"help:arguments\": heredoc.Doc(`\n\t\t\t\tA pull request can be supplied as argument in any of the following formats:\n\t\t\t\t- by number, e.g. \"123\";", + Matches: []search.Match{ + { + Text: "map", + Indices: []int{68, 71}, + }, + }, + }, + }, + }, + }, + Total: 2, + }, nil + }, + }, + }, + tty: true, + wantStdout: "\nShowing 2 of 2 results\n\ncli/cli context/context.go\n1: \trepoMap := map[string]bool{}\ncli/cli pkg/cmd/pr/pr.go\n1: \t\tAnnotations: map[string]string{\n", + }, + { + name: "displays results notty", + opts: &CodeOptions{ + Query: query, + Searcher: &search.SearcherMock{ + CodeFunc: func(query search.Query) (search.CodeResult, error) { + return search.CodeResult{ + IncompleteResults: false, + Items: []search.Code{ + { + Name: "context.go", + Path: "context/context.go", + Repo: search.Repository{ + FullName: "cli/cli", + }, + TextMatches: []search.TextMatch{ + { + Fragment: "\t}\n\n\tvar repos []*api.Repository\n\trepoMap := map[string]bool{}\n\n\tadd := func(r *api.Repository) {\n\t\tfn := ghrepo.FullName(r)", + Matches: []search.Match{ + { + Text: "Map", + Indices: []int{38, 41}, + }, + { + Text: "map", + Indices: []int{45, 48}, + }, + }, + }, + }, + }, + { + Name: "pr.go", + Path: "pkg/cmd/pr/pr.go", + Repo: search.Repository{ + FullName: "cli/cli", + }, + TextMatches: []search.TextMatch{ + { + + Fragment: "\t\t\t$ gh pr create --fill\n\t\t\t$ gh pr view --web\n\t\t`),\n\t\tAnnotations: map[string]string{\n\t\t\t\"help:arguments\": heredoc.Doc(`\n\t\t\t\tA pull request can be supplied as argument in any of the following formats:\n\t\t\t\t- by number, e.g. \"123\";", + Matches: []search.Match{ + { + Text: "map", + Indices: []int{68, 71}, + }, + }, + }, + }, + }, + }, + Total: 2, + }, nil + }, + }, + }, + tty: false, + wantStdout: "cli/cli context/context.go: \trepoMap := map[string]bool{}\ncli/cli pkg/cmd/pr/pr.go: \t\tAnnotations: map[string]string{\n", + }, + { + name: "displays no results", + opts: &CodeOptions{ + Query: query, + Searcher: &search.SearcherMock{ + CodeFunc: func(query search.Query) (search.CodeResult, error) { + return search.CodeResult{}, nil + }, + }, + }, + wantErr: true, + errMsg: "no code results matched your search", + }, + { + name: "displays search error", + opts: &CodeOptions{ + Query: query, + Searcher: &search.SearcherMock{ + CodeFunc: func(query search.Query) (search.CodeResult, error) { + return search.CodeResult{}, fmt.Errorf("error with query") + }, + }, + }, + wantErr: true, + errMsg: "error with query", + }, + { + name: "opens browser for web mode tty", + opts: &CodeOptions{ + Browser: &browser.Stub{}, + Query: query, + Searcher: &search.SearcherMock{ + URLFunc: func(query search.Query) string { + return "https://github.com/search?type=code&q=map+repo%3Acli%2Fcli" + }, + }, + WebMode: true, + }, + tty: true, + wantStderr: "Opening github.com/search in your browser.\n", + }, + { + name: "opens browser for web mode notty", + opts: &CodeOptions{ + Browser: &browser.Stub{}, + Query: query, + Searcher: &search.SearcherMock{ + URLFunc: func(query search.Query) string { + return "https://github.com/search?type=code&q=map+repo%3Acli%2Fcli" + }, + }, + WebMode: true, + }, + }, + } + + for _, tt := range tests { + ios, _, stdout, stderr := iostreams.Test() + ios.SetStdinTTY(tt.tty) + ios.SetStdoutTTY(tt.tty) + ios.SetStderrTTY(tt.tty) + tt.opts.IO = ios + t.Run(tt.name, func(t *testing.T) { + err := codeRun(tt.opts) + if tt.wantErr { + assert.EqualError(t, err, tt.errMsg) + return + } else if err != nil { + t.Fatalf("codeRun unexpected error: %v", err) + } + assert.Equal(t, tt.wantStdout, stdout.String()) + assert.Equal(t, tt.wantStderr, stderr.String()) + }) + } +} diff --git a/pkg/cmd/search/search.go b/pkg/cmd/search/search.go index 7b9a4a653..e8714065b 100644 --- a/pkg/cmd/search/search.go +++ b/pkg/cmd/search/search.go @@ -4,6 +4,7 @@ import ( "github.com/cli/cli/v2/pkg/cmdutil" "github.com/spf13/cobra" + searchCodeCmd "github.com/cli/cli/v2/pkg/cmd/search/code" searchCommitsCmd "github.com/cli/cli/v2/pkg/cmd/search/commits" searchIssuesCmd "github.com/cli/cli/v2/pkg/cmd/search/issues" searchPrsCmd "github.com/cli/cli/v2/pkg/cmd/search/prs" @@ -17,6 +18,7 @@ func NewCmdSearch(f *cmdutil.Factory) *cobra.Command { Long: "Search across all of GitHub.", } + cmd.AddCommand(searchCodeCmd.NewCmdCode(f, nil)) cmd.AddCommand(searchCommitsCmd.NewCmdCommits(f, nil)) cmd.AddCommand(searchIssuesCmd.NewCmdIssues(f, nil)) cmd.AddCommand(searchPrsCmd.NewCmdPrs(f, nil)) diff --git a/pkg/iostreams/color.go b/pkg/iostreams/color.go index 1d35c05d9..12ff49c54 100644 --- a/pkg/iostreams/color.go +++ b/pkg/iostreams/color.go @@ -9,15 +9,17 @@ import ( ) var ( - magenta = ansi.ColorFunc("magenta") - cyan = ansi.ColorFunc("cyan") - red = ansi.ColorFunc("red") - yellow = ansi.ColorFunc("yellow") - blue = ansi.ColorFunc("blue") - green = ansi.ColorFunc("green") - gray = ansi.ColorFunc("black+h") - bold = ansi.ColorFunc("default+b") - cyanBold = ansi.ColorFunc("cyan+b") + magenta = ansi.ColorFunc("magenta") + cyan = ansi.ColorFunc("cyan") + red = ansi.ColorFunc("red") + yellow = ansi.ColorFunc("yellow") + blue = ansi.ColorFunc("blue") + green = ansi.ColorFunc("green") + gray = ansi.ColorFunc("black+h") + bold = ansi.ColorFunc("default+b") + cyanBold = ansi.ColorFunc("cyan+b") + greenBold = ansi.ColorFunc("green+b") + yellowBold = ansi.ColorFunc("yellow+b") gray256 = func(t string) string { return fmt.Sprintf("\x1b[%d;5;%dm%s\x1b[m", 38, 242, t) @@ -71,6 +73,13 @@ func (c *ColorScheme) Yellowf(t string, args ...interface{}) string { return c.Yellow(fmt.Sprintf(t, args...)) } +func (c *ColorScheme) YellowBold(t string) string { + if !c.enabled { + return t + } + return yellowBold(t) +} + func (c *ColorScheme) Green(t string) string { if !c.enabled { return t @@ -82,6 +91,13 @@ func (c *ColorScheme) Greenf(t string, args ...interface{}) string { return c.Green(fmt.Sprintf(t, args...)) } +func (c *ColorScheme) GreenBold(t string) string { + if !c.enabled { + return t + } + return greenBold(t) +} + func (c *ColorScheme) Gray(t string) string { if !c.enabled { return t diff --git a/pkg/search/query.go b/pkg/search/query.go index fab6305f6..cba333984 100644 --- a/pkg/search/query.go +++ b/pkg/search/query.go @@ -10,6 +10,7 @@ import ( const ( KindRepositories = "repositories" + KindCode = "code" KindIssues = "issues" KindCommits = "commits" ) @@ -22,6 +23,7 @@ type Query struct { Page int Qualifiers Qualifiers Sort string + TextMatch bool } type Qualifiers struct { @@ -41,6 +43,8 @@ type Qualifiers struct { CommitterName string Created string Draft *bool + Extension string + Filename string Followers string Fork string Forks string diff --git a/pkg/search/query_test.go b/pkg/search/query_test.go index dacc033f7..58992ba92 100644 --- a/pkg/search/query_test.go +++ b/pkg/search/query_test.go @@ -23,6 +23,8 @@ func TestQueryString(t *testing.T) { AuthorEmail: "foo@example.com", CommitterDate: "2021-02-28", Created: "created", + Extension: "go", + Filename: ".vimrc", Followers: "1", Fork: "true", Forks: "2", @@ -40,7 +42,7 @@ func TestQueryString(t *testing.T) { Is: []string{"public"}, }, }, - out: "some keywords archived:true author-email:foo@example.com committer-date:2021-02-28 created:created followers:1 fork:true forks:2 good-first-issues:3 help-wanted-issues:4 in:description in:readme is:public language:language license:license pushed:updated size:5 stars:6 topic:topic topics:7 user:user", + out: "some keywords archived:true author-email:foo@example.com committer-date:2021-02-28 created:created extension:go filename:.vimrc followers:1 fork:true forks:2 good-first-issues:3 help-wanted-issues:4 in:description in:readme is:public language:language license:license pushed:updated size:5 stars:6 topic:topic topics:7 user:user", }, { name: "quotes keywords", @@ -86,6 +88,8 @@ func TestQualifiersMap(t *testing.T) { AuthorEmail: "foo@example.com", CommitterDate: "2021-02-28", Created: "created", + Extension: "go", + Filename: ".vimrc", Followers: "1", Fork: "true", Forks: "2", @@ -107,6 +111,8 @@ func TestQualifiersMap(t *testing.T) { "author-email": {"foo@example.com"}, "committer-date": {"2021-02-28"}, "created": {"created"}, + "extension": {"go"}, + "filename": {".vimrc"}, "followers": {"1"}, "fork": {"true"}, "forks": {"2"}, diff --git a/pkg/search/result.go b/pkg/search/result.go index d7113bfda..beac37e4f 100644 --- a/pkg/search/result.go +++ b/pkg/search/result.go @@ -6,6 +6,15 @@ import ( "time" ) +var CodeFields = []string{ + "name", + "path", + "repository", + "sha", + "textMatches", + "url", +} + var CommitFields = []string{ "author", "commit", @@ -72,6 +81,12 @@ var PullRequestFields = append(IssueFields, "isDraft", ) +type CodeResult struct { + IncompleteResults bool `json:"incomplete_results"` + Items []Code `json:"items"` + Total int `json:"total_count"` +} + type CommitsResult struct { IncompleteResults bool `json:"incomplete_results"` Items []Commit `json:"items"` @@ -90,6 +105,28 @@ type IssuesResult struct { Total int `json:"total_count"` } +type Code struct { + Name string `json:"name"` + Path string `json:"path"` + Repo Repository `json:"repository"` + Sha string `json:"sha"` + TextMatches []TextMatch `json:"text_matches"` + URL string `json:"html_url"` +} + +type TextMatch struct { + Fragment string `json:"fragment"` + Matches []Match `json:"matches"` + ObjectURL string `json:"object_url"` + ObjectType string `json:"object_type"` + Property string `json:"property"` +} + +type Match struct { + Indices []int `json:"indices"` + Text string `json:"text"` +} + type Commit struct { Author User `json:"author"` Committer User `json:"committer"` @@ -230,6 +267,21 @@ func (u User) ExportData() map[string]interface{} { } } +func (code Code) ExportData(fields []string) map[string]interface{} { + v := reflect.ValueOf(code) + data := map[string]interface{}{} + for _, f := range fields { + switch f { + case "repository": + data[f] = code.Repo.ExportData(RepositoryFields) + default: + sf := fieldByName(v, f) + data[f] = sf.Interface() + } + } + return data +} + func (commit Commit) ExportData(fields []string) map[string]interface{} { v := reflect.ValueOf(commit) data := map[string]interface{}{} diff --git a/pkg/search/result_test.go b/pkg/search/result_test.go index cdf424b0b..376980844 100644 --- a/pkg/search/result_test.go +++ b/pkg/search/result_test.go @@ -11,6 +11,51 @@ import ( "github.com/stretchr/testify/require" ) +func TestCodeExportData(t *testing.T) { + tests := []struct { + name string + fields []string + code Code + output string + }{ + { + name: "exports requested fields", + fields: []string{"name", "path", "textMatches"}, + code: Code{ + Repo: Repository{ + Name: "repo", + }, + Path: "path", + Name: "name", + TextMatches: []TextMatch{ + { + Fragment: "fragment", + Matches: []Match{ + { + Text: "fr", + Indices: []int{ + 0, + 1, + }, + }, + }, + }, + }, + }, + output: `{"name":"name","path":"path","textMatches":[{"object_url":"","object_type":"","property":"","fragment":"fragment","matches":[{"text":"fr","indices":[0,1]}]}]}`, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + exported := tt.code.ExportData(tt.fields) + buf := bytes.Buffer{} + enc := json.NewEncoder(&buf) + require.NoError(t, enc.Encode(exported)) + assert.Equal(t, tt.output, strings.TrimSpace(buf.String())) + }) + } +} + func TestCommitExportData(t *testing.T) { var authoredAt = time.Date(2021, 2, 27, 11, 30, 0, 0, time.UTC) var committedAt = time.Date(2021, 2, 28, 12, 30, 0, 0, time.UTC) diff --git a/pkg/search/searcher.go b/pkg/search/searcher.go index baa84c03c..889a69487 100644 --- a/pkg/search/searcher.go +++ b/pkg/search/searcher.go @@ -25,6 +25,7 @@ var jsonTypeRE = regexp.MustCompile(`[/+]json($|;)`) //go:generate moq -rm -out searcher_mock.go . Searcher type Searcher interface { + Code(Query) (CodeResult, error) Commits(Query) (CommitsResult, error) Repositories(Query) (RepositoriesResult, error) Issues(Query) (IssuesResult, error) @@ -57,6 +58,30 @@ func NewSearcher(client *http.Client, host string) Searcher { } } +func (s searcher) Code(query Query) (CodeResult, error) { + result := CodeResult{} + toRetrieve := query.Limit + var resp *http.Response + var err error + for toRetrieve > 0 { + query.Limit = min(toRetrieve, maxPerPage) + query.Page = nextPage(resp) + if query.Page == 0 { + break + } + page := CodeResult{} + resp, err = s.search(query, &page) + if err != nil { + return result, err + } + result.IncompleteResults = page.IncompleteResults + result.Total = page.Total + result.Items = append(result.Items, page.Items...) + toRetrieve = toRetrieve - len(page.Items) + } + return result, nil +} + func (s searcher) Commits(query Query) (CommitsResult, error) { result := CommitsResult{} toRetrieve := query.Limit @@ -148,6 +173,10 @@ func (s searcher) search(query Query, result interface{}) (*http.Response, error } req.Header.Set("Content-Type", "application/json; charset=utf-8") req.Header.Set("Accept", "application/vnd.github.v3+json") + if query.TextMatch { + req.Header.Set("Accept", "application/vnd.github.text-match+json") + } + resp, err := s.client.Do(req) if err != nil { return nil, err diff --git a/pkg/search/searcher_mock.go b/pkg/search/searcher_mock.go index c1eecdaf4..332539121 100644 --- a/pkg/search/searcher_mock.go +++ b/pkg/search/searcher_mock.go @@ -17,6 +17,9 @@ var _ Searcher = &SearcherMock{} // // // make and configure a mocked Searcher // mockedSearcher := &SearcherMock{ +// CodeFunc: func(query Query) (CodeResult, error) { +// panic("mock out the Code method") +// }, // CommitsFunc: func(query Query) (CommitsResult, error) { // panic("mock out the Commits method") // }, @@ -36,6 +39,9 @@ var _ Searcher = &SearcherMock{} // // } type SearcherMock struct { + // CodeFunc mocks the Code method. + CodeFunc func(query Query) (CodeResult, error) + // CommitsFunc mocks the Commits method. CommitsFunc func(query Query) (CommitsResult, error) @@ -50,6 +56,11 @@ type SearcherMock struct { // calls tracks calls to the methods. calls struct { + // Code holds details about calls to the Code method. + Code []struct { + // Query is the query argument value. + Query Query + } // Commits holds details about calls to the Commits method. Commits []struct { // Query is the query argument value. @@ -71,12 +82,45 @@ type SearcherMock struct { Query Query } } + lockCode sync.RWMutex lockCommits sync.RWMutex lockIssues sync.RWMutex lockRepositories sync.RWMutex lockURL sync.RWMutex } +// Code calls CodeFunc. +func (mock *SearcherMock) Code(query Query) (CodeResult, error) { + if mock.CodeFunc == nil { + panic("SearcherMock.CodeFunc: method is nil but Searcher.Code was just called") + } + callInfo := struct { + Query Query + }{ + Query: query, + } + mock.lockCode.Lock() + mock.calls.Code = append(mock.calls.Code, callInfo) + mock.lockCode.Unlock() + return mock.CodeFunc(query) +} + +// CodeCalls gets all the calls that were made to Code. +// Check the length with: +// +// len(mockedSearcher.CodeCalls()) +func (mock *SearcherMock) CodeCalls() []struct { + Query Query +} { + var calls []struct { + Query Query + } + mock.lockCode.RLock() + calls = mock.calls.Code + mock.lockCode.RUnlock() + return calls +} + // Commits calls CommitsFunc. func (mock *SearcherMock) Commits(query Query) (CommitsResult, error) { if mock.CommitsFunc == nil { diff --git a/pkg/search/searcher_test.go b/pkg/search/searcher_test.go index 8cc90c533..b6a8ae873 100644 --- a/pkg/search/searcher_test.go +++ b/pkg/search/searcher_test.go @@ -10,6 +10,157 @@ import ( "github.com/stretchr/testify/assert" ) +func TestSearcherCode(t *testing.T) { + query := Query{ + Keywords: []string{"keyword"}, + Kind: "code", + Limit: 30, + TextMatch: true, + Qualifiers: Qualifiers{ + Language: "go", + }, + } + + values := url.Values{ + "page": []string{"1"}, + "per_page": []string{"30"}, + "q": []string{"keyword language:go"}, + } + + tests := []struct { + name string + host string + query Query + result CodeResult + wantErr bool + errMsg string + httpStubs func(reg *httpmock.Registry) + }{ + { + name: "searches code", + query: query, + result: CodeResult{ + IncompleteResults: false, + Items: []Code{{Name: "file.go"}}, + Total: 1, + }, + httpStubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.QueryMatcher("GET", "search/code", values), + httpmock.JSONResponse(CodeResult{ + IncompleteResults: false, + Items: []Code{{Name: "file.go"}}, + Total: 1, + }), + ) + }, + }, + { + name: "searches code for enterprise host", + host: "enterprise.com", + query: query, + result: CodeResult{ + IncompleteResults: false, + Items: []Code{{Name: "file.go"}}, + Total: 1, + }, + httpStubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.QueryMatcher("GET", "api/v3/search/code", values), + httpmock.JSONResponse(CodeResult{ + IncompleteResults: false, + Items: []Code{{Name: "file.go"}}, + Total: 1, + }), + ) + }, + }, + { + name: "paginates results", + query: query, + result: CodeResult{ + IncompleteResults: false, + Items: []Code{{Name: "file.go"}, {Name: "file2.go"}}, + Total: 2, + }, + httpStubs: func(reg *httpmock.Registry) { + firstReq := httpmock.QueryMatcher("GET", "search/code", values) + firstRes := httpmock.JSONResponse(CodeResult{ + IncompleteResults: false, + Items: []Code{{Name: "file.go"}}, + Total: 2, + }, + ) + firstRes = httpmock.WithHeader(firstRes, "Link", `; rel="next"`) + secondReq := httpmock.QueryMatcher("GET", "search/code", url.Values{ + "page": []string{"2"}, + "per_page": []string{"29"}, + "q": []string{"keyword language:go"}, + }, + ) + secondRes := httpmock.JSONResponse(CodeResult{ + IncompleteResults: false, + Items: []Code{{Name: "file2.go"}}, + Total: 2, + }, + ) + reg.Register(firstReq, firstRes) + reg.Register(secondReq, secondRes) + }, + }, + { + name: "handles search errors", + query: query, + wantErr: true, + errMsg: heredoc.Doc(` + Invalid search query "keyword language:go". + "blah" is not a recognized date/time format. Please provide an ISO 8601 date/time value, such as YYYY-MM-DD.`), + httpStubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.QueryMatcher("GET", "search/code", values), + httpmock.WithHeader( + httpmock.StatusStringResponse(422, + `{ + "message": "Validation Failed", + "errors": [ + { + "message":"\"blah\" is not a recognized date/time format. Please provide an ISO 8601 date/time value, such as YYYY-MM-DD.", + "resource":"Search", + "field":"q", + "code":"invalid" + } + ], + "documentation_url":"https://developer.github.com/v3/search/" + }`, + ), "Content-Type", "application/json"), + ) + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + reg := &httpmock.Registry{} + defer reg.Verify(t) + if tt.httpStubs != nil { + tt.httpStubs(reg) + } + client := &http.Client{Transport: reg} + if tt.host == "" { + tt.host = "github.com" + } + searcher := NewSearcher(client, tt.host) + result, err := searcher.Code(tt.query) + if tt.wantErr { + assert.EqualError(t, err, tt.errMsg) + return + } + assert.NoError(t, err) + assert.Equal(t, tt.result, result) + }) + } +} + func TestSearcherCommits(t *testing.T) { query := Query{ Keywords: []string{"keyword"},