1
0
mirror of https://github.com/StackExchange/dnscontrol.git synced 2024-05-11 05:55:12 +00:00

Switch from govendor to go modules. (#587)

Thanks to @BenoitKnecht for leading the way on this.
This commit is contained in:
Tom Limoncelli
2020-01-18 14:40:28 -05:00
committed by GitHub
parent 31188c3a70
commit 16d0043cce
1554 changed files with 400867 additions and 98222 deletions

View File

@ -8,6 +8,9 @@ package github
import (
"context"
"fmt"
"net/url"
"strconv"
"strings"
qs "github.com/google/go-querystring/query"
)
@ -48,6 +51,12 @@ type SearchOptions struct {
ListOptions
}
// Common search parameters.
type searchParameters struct {
Query string
RepositoryID *int64 // Sent if non-nil.
}
// RepositoriesSearchResult represents the result of a repositories search.
type RepositoriesSearchResult struct {
Total *int `json:"total_count,omitempty"`
@ -60,7 +69,7 @@ type RepositoriesSearchResult struct {
// GitHub API docs: https://developer.github.com/v3/search/#search-repositories
func (s *SearchService) Repositories(ctx context.Context, query string, opt *SearchOptions) (*RepositoriesSearchResult, *Response, error) {
result := new(RepositoriesSearchResult)
resp, err := s.search(ctx, "repositories", query, opt, result)
resp, err := s.search(ctx, "repositories", &searchParameters{Query: query}, opt, result)
return result, resp, err
}
@ -91,7 +100,7 @@ type CommitResult struct {
// GitHub API docs: https://developer.github.com/v3/search/#search-commits
func (s *SearchService) Commits(ctx context.Context, query string, opt *SearchOptions) (*CommitsSearchResult, *Response, error) {
result := new(CommitsSearchResult)
resp, err := s.search(ctx, "commits", query, opt, result)
resp, err := s.search(ctx, "commits", &searchParameters{Query: query}, opt, result)
return result, resp, err
}
@ -107,7 +116,7 @@ type IssuesSearchResult struct {
// GitHub API docs: https://developer.github.com/v3/search/#search-issues
func (s *SearchService) Issues(ctx context.Context, query string, opt *SearchOptions) (*IssuesSearchResult, *Response, error) {
result := new(IssuesSearchResult)
resp, err := s.search(ctx, "issues", query, opt, result)
resp, err := s.search(ctx, "issues", &searchParameters{Query: query}, opt, result)
return result, resp, err
}
@ -123,7 +132,7 @@ type UsersSearchResult struct {
// GitHub API docs: https://developer.github.com/v3/search/#search-users
func (s *SearchService) Users(ctx context.Context, query string, opt *SearchOptions) (*UsersSearchResult, *Response, error) {
result := new(UsersSearchResult)
resp, err := s.search(ctx, "users", query, opt, result)
resp, err := s.search(ctx, "users", &searchParameters{Query: query}, opt, result)
return result, resp, err
}
@ -172,19 +181,57 @@ func (c CodeResult) String() string {
// GitHub API docs: https://developer.github.com/v3/search/#search-code
func (s *SearchService) Code(ctx context.Context, query string, opt *SearchOptions) (*CodeSearchResult, *Response, error) {
result := new(CodeSearchResult)
resp, err := s.search(ctx, "code", query, opt, result)
resp, err := s.search(ctx, "code", &searchParameters{Query: query}, opt, result)
return result, resp, err
}
// LabelsSearchResult represents the result of a code search.
type LabelsSearchResult struct {
Total *int `json:"total_count,omitempty"`
IncompleteResults *bool `json:"incomplete_results,omitempty"`
Labels []*LabelResult `json:"items,omitempty"`
}
// LabelResult represents a single search result.
type LabelResult struct {
ID *int64 `json:"id,omitempty"`
URL *string `json:"url,omitempty"`
Name *string `json:"name,omitempty"`
Color *string `json:"color,omitempty"`
Default *bool `json:"default,omitempty"`
Description *string `json:"description,omitempty"`
Score *float64 `json:"score,omitempty"`
}
func (l LabelResult) String() string {
return Stringify(l)
}
// Labels searches labels in the repository with ID repoID via various criteria.
//
// GitHub API docs: https://developer.github.com/v3/search/#search-labels
func (s *SearchService) Labels(ctx context.Context, repoID int64, query string, opt *SearchOptions) (*LabelsSearchResult, *Response, error) {
result := new(LabelsSearchResult)
resp, err := s.search(ctx, "labels", &searchParameters{RepositoryID: &repoID, Query: query}, opt, result)
return result, resp, err
}
// Helper function that executes search queries against different
// GitHub search types (repositories, commits, code, issues, users)
func (s *SearchService) search(ctx context.Context, searchType string, query string, opt *SearchOptions, result interface{}) (*Response, error) {
// GitHub search types (repositories, commits, code, issues, users, labels)
func (s *SearchService) search(ctx context.Context, searchType string, parameters *searchParameters, opt *SearchOptions, result interface{}) (*Response, error) {
params, err := qs.Values(opt)
if err != nil {
return nil, err
}
params.Set("q", query)
u := fmt.Sprintf("search/%s?%s", searchType, params.Encode())
q := strings.Replace(parameters.Query, " ", "+", -1)
if parameters.RepositoryID != nil {
params.Set("repository_id", strconv.FormatInt(*parameters.RepositoryID, 10))
}
query := "q=" + url.PathEscape(q)
if v := params.Encode(); v != "" {
query = query + "&" + v
}
u := fmt.Sprintf("search/%s?%s", searchType, query)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
@ -200,6 +247,10 @@ func (s *SearchService) search(ctx context.Context, searchType string, query str
// Accept header for search repositories based on topics preview endpoint
// TODO: remove custom Accept header when this API fully launches.
req.Header.Set("Accept", mediaTypeTopicsPreview)
case searchType == "labels":
// Accept header for search labels based on label description preview endpoint.
// TODO: remove custom Accept header when this API fully launches.
req.Header.Set("Accept", mediaTypeLabelDescriptionSearchPreview)
case opt != nil && opt.TextMatch:
// Accept header defaults to "application/vnd.github.v3+json"
// We change it here to fetch back text-match metadata