GetCodespaceRepoSuggestions searches for and returns repo names based on the provided search text.
(ctx context.Context, partialSearch string, parameters RepoSearchParameters)
| 752 | |
| 753 | // GetCodespaceRepoSuggestions searches for and returns repo names based on the provided search text. |
| 754 | func (a *API) GetCodespaceRepoSuggestions(ctx context.Context, partialSearch string, parameters RepoSearchParameters) ([]string, error) { |
| 755 | reqURL, err := safeurl.JoinPathWithHostPrefix(a.githubAPI, "search", "repositories") |
| 756 | if err != nil { |
| 757 | return nil, err |
| 758 | } |
| 759 | req, err := http.NewRequest(http.MethodGet, reqURL.String(), nil) |
| 760 | if err != nil { |
| 761 | return nil, fmt.Errorf("error creating request: %w", err) |
| 762 | } |
| 763 | |
| 764 | parts := strings.SplitN(partialSearch, "/", 2) |
| 765 | |
| 766 | var nameSearch string |
| 767 | if len(parts) == 2 { |
| 768 | user := parts[0] |
| 769 | repo := parts[1] |
| 770 | nameSearch = fmt.Sprintf("%s user:%s", repo, user) |
| 771 | } else { |
| 772 | /* |
| 773 | * This results in searching for the text within the owner or the name. It's possible to |
| 774 | * do an owner search and then look up some repos for those owners, but that adds a |
| 775 | * good amount of latency to the fetch which slows down showing the suggestions. |
| 776 | */ |
| 777 | nameSearch = partialSearch |
| 778 | } |
| 779 | |
| 780 | queryStr := fmt.Sprintf("%s in:name", nameSearch) |
| 781 | |
| 782 | q := req.URL.Query() |
| 783 | q.Add("q", queryStr) |
| 784 | |
| 785 | if len(parameters.Sort) > 0 { |
| 786 | q.Add("sort", parameters.Sort) |
| 787 | } |
| 788 | |
| 789 | if parameters.MaxRepos > 0 { |
| 790 | q.Add("per_page", strconv.Itoa(parameters.MaxRepos)) |
| 791 | } |
| 792 | |
| 793 | req.URL.RawQuery = q.Encode() |
| 794 | |
| 795 | a.setHeaders(req) |
| 796 | resp, err := a.do(ctx, req, "/search/repositories/*") |
| 797 | if err != nil { |
| 798 | return nil, fmt.Errorf("error searching repositories: %w", err) |
| 799 | } |
| 800 | defer resp.Body.Close() |
| 801 | |
| 802 | if resp.StatusCode != http.StatusOK { |
| 803 | return nil, api.HandleHTTPError(resp) |
| 804 | } |
| 805 | |
| 806 | b, err := io.ReadAll(resp.Body) |
| 807 | if err != nil { |
| 808 | return nil, fmt.Errorf("error reading response body: %w", err) |
| 809 | } |
| 810 | |
| 811 | var response struct { |