GetCodespaceRepoSuggestions searches for and returns repo names based on the provided search text.
(ctx context.Context, partialSearch string, parameters RepoSearchParameters)
| 692 | |
| 693 | // GetCodespaceRepoSuggestions searches for and returns repo names based on the provided search text. |
| 694 | func (a *API) GetCodespaceRepoSuggestions(ctx context.Context, partialSearch string, parameters RepoSearchParameters) ([]string, error) { |
| 695 | reqURL := fmt.Sprintf("%s/search/repositories", a.githubAPI) |
| 696 | req, err := http.NewRequest(http.MethodGet, reqURL, nil) |
| 697 | if err != nil { |
| 698 | return nil, fmt.Errorf("error creating request: %w", err) |
| 699 | } |
| 700 | |
| 701 | parts := strings.SplitN(partialSearch, "/", 2) |
| 702 | |
| 703 | var nameSearch string |
| 704 | if len(parts) == 2 { |
| 705 | user := parts[0] |
| 706 | repo := parts[1] |
| 707 | nameSearch = fmt.Sprintf("%s user:%s", repo, user) |
| 708 | } else { |
| 709 | /* |
| 710 | * This results in searching for the text within the owner or the name. It's possible to |
| 711 | * do an owner search and then look up some repos for those owners, but that adds a |
| 712 | * good amount of latency to the fetch which slows down showing the suggestions. |
| 713 | */ |
| 714 | nameSearch = partialSearch |
| 715 | } |
| 716 | |
| 717 | queryStr := fmt.Sprintf("%s in:name", nameSearch) |
| 718 | |
| 719 | q := req.URL.Query() |
| 720 | q.Add("q", queryStr) |
| 721 | |
| 722 | if len(parameters.Sort) > 0 { |
| 723 | q.Add("sort", parameters.Sort) |
| 724 | } |
| 725 | |
| 726 | if parameters.MaxRepos > 0 { |
| 727 | q.Add("per_page", strconv.Itoa(parameters.MaxRepos)) |
| 728 | } |
| 729 | |
| 730 | req.URL.RawQuery = q.Encode() |
| 731 | |
| 732 | a.setHeaders(req) |
| 733 | resp, err := a.do(ctx, req, "/search/repositories/*") |
| 734 | if err != nil { |
| 735 | return nil, fmt.Errorf("error searching repositories: %w", err) |
| 736 | } |
| 737 | defer resp.Body.Close() |
| 738 | |
| 739 | if resp.StatusCode != http.StatusOK { |
| 740 | return nil, api.HandleHTTPError(resp) |
| 741 | } |
| 742 | |
| 743 | b, err := io.ReadAll(resp.Body) |
| 744 | if err != nil { |
| 745 | return nil, fmt.Errorf("error reading response body: %w", err) |
| 746 | } |
| 747 | |
| 748 | var response struct { |
| 749 | Items []*Repository `json:"items"` |
| 750 | } |
| 751 | if err := json.Unmarshal(b, &response); err != nil { |