github.com/nesv/gothub@v0.0.0-20140406181320-f0c824044d5f/search.go (about)

     1  package gothub
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"net/url"
     7  	"strings"
     8  	"time"
     9  )
    10  
    11  var (
    12  	ErrSearchInvalidParam = errors.New("Invalid parameter for search")
    13  	ErrSearchOmitParam    = errors.New("Omit mandatory paramter for search")
    14  )
    15  
    16  // Types which represents params and result for search issues
    17  // http://developer.github.com/v3/search/#search-issues
    18  type IssuesSearchParam struct {
    19  	Owner      string
    20  	Repository string
    21  	State      string // open or close
    22  	Keyword    string
    23  
    24  	result issuesSearchResult
    25  }
    26  
    27  func (p *IssuesSearchParam) addr() (string, error) {
    28  	if len(p.Owner) == 0 || len(p.Repository) == 0 ||
    29  		len(p.State) == 0 || len(p.Keyword) == 0 {
    30  		return "", ErrSearchOmitParam
    31  	}
    32  
    33  	r := "/legacy/issues/search/"
    34  	r += p.Owner
    35  	r += ("/" + p.Repository)
    36  	r += ("/" + p.State)
    37  	r += ("/" + url.QueryEscape(p.Keyword))
    38  	return r, nil
    39  }
    40  
    41  type issuesSearchResult struct {
    42  	Issues []*IssuesSearchResult
    43  }
    44  
    45  type IssuesSearchResult struct {
    46  	GravatarId string    `json:"gravatar_id"`
    47  	Position   int       `json:"position"`
    48  	Number     int       `json:"number"`
    49  	Votes      int       `json:"votes"`
    50  	CreatedAt  time.Time `json:"created_at"`
    51  	Comments   int       `json:"comments"`
    52  	Body       string    `json:"body"`
    53  	Title      string    `json:"title"`
    54  	UpdatedAt  time.Time `json:"updated_at"`
    55  	HtmlUrl    string    `json:"html_url"`
    56  	User       string    `json:"user"`
    57  	Labels     []string  `json:"labels"`
    58  	State      string    `json:"state"`
    59  }
    60  
    61  // Types which represents params and result for search repositories
    62  // http://developer.github.com/v3/search/#search-repositories
    63  type RepositoriesSearchParam struct {
    64  	Keyword   string
    65  	Language  string // optional:
    66  	StartPage uint   // optional:
    67  	Sort      string // optional: stars, forks or update
    68  	Order     string // optional: asc or desc
    69  
    70  	result repositoriesSearchResult
    71  }
    72  
    73  func (p *RepositoriesSearchParam) addr() (string, error) {
    74  	if len(p.Keyword) == 0 {
    75  		return "", ErrSearchOmitParam
    76  	}
    77  
    78  	r := "/legacy/repos/search/"
    79  	r += url.QueryEscape(p.Keyword)
    80  	if len(p.Language) != 0 {
    81  		r += ("/" + p.Language)
    82  	}
    83  	if p.StartPage != 0 {
    84  		r += fmt.Sprintf("/%d", p.StartPage)
    85  	}
    86  	if len(p.Sort) != 0 {
    87  		r += ("/" + p.Sort)
    88  	}
    89  	if len(p.Order) != 0 {
    90  		r += ("/" + p.Order)
    91  	}
    92  	return r, nil
    93  }
    94  
    95  type repositoriesSearchResult struct {
    96  	Repositories []*RepositoriesSearchResult
    97  }
    98  
    99  type RepositoriesSearchResult struct {
   100  	Type         string    `json:"type"`
   101  	Created      time.Time `json:"created"`
   102  	Watchers     int       `json:"watchers"`
   103  	HasDownloads bool      `json:"has_downloads"`
   104  	Username     string    `json:"username"`
   105  	Homepage     string    `json:"homepage"`
   106  	Url          string    `json:"url"`
   107  	Fork         bool      `json:"fork"`
   108  	HasIssues    bool      `json:"has_issues"`
   109  	HasWiki      bool      `json:"has_wiki"`
   110  	Forks        int       `json:"forks"`
   111  	Size         int       `json:"size"`
   112  	Private      bool      `json:"private"`
   113  	Followers    int       `json:"followers"`
   114  	Name         string    `json:"name"`
   115  	Owner        string    `json:"owner"`
   116  	OpenIssues   int       `json:"open_issues"`
   117  	PushedAt     time.Time `json:"pushed_at"`
   118  	Score        float32   `json:"score"`
   119  	Pushed       time.Time `json:"pushed"`
   120  	Description  string    `json:"description"`
   121  	Language     string    `json:"language"`
   122  	CreatedAt    time.Time `json:"created_at"`
   123  }
   124  
   125  // Types which represents params and result for search users
   126  // http://developer.github.com/v3/search/#search-users
   127  type UsersSearchParam struct {
   128  	Keyword   string
   129  	StartPage uint   // optional:
   130  	Sort      string // optional: start, forks or update
   131  	Order     string // optional: asc or desc
   132  
   133  	result usersSearchResult
   134  }
   135  
   136  func (p *UsersSearchParam) addr() (string, error) {
   137  	if len(p.Keyword) == 0 {
   138  		return "", ErrSearchOmitParam
   139  	}
   140  
   141  	r := "/legacy/user/search/"
   142  	r += url.QueryEscape(p.Keyword)
   143  	if p.StartPage != 0 {
   144  		r += fmt.Sprintf("/%d", p.StartPage)
   145  	}
   146  	if len(p.Sort) != 0 {
   147  		r += ("/" + p.Sort)
   148  	}
   149  	if len(p.Order) != 0 {
   150  		r += ("/" + p.Order)
   151  	}
   152  	return r, nil
   153  }
   154  
   155  type usersSearchResult struct {
   156  	Users []*UsersSearchResult
   157  }
   158  
   159  type UsersSearchResult struct {
   160  	GravatarId      string    `json:"gravatar_id"`
   161  	Name            string    `json:"name"`
   162  	CreatedAt       time.Time `json:"created_at"`
   163  	Location        string    `json:"location"`
   164  	PublicRepoCount int       `json:"public_repo_count"`
   165  	Followers       int       `json:"followers"`
   166  	Language        string    `json:"language"`
   167  	Fullname        string    `json:"fullname"`
   168  	Username        string    `json:"username"`
   169  	Id              string    `json:"id"`
   170  	Repos           int       `json:"repos"`
   171  	Type            string    `json:"type"`
   172  	FollowersCount  int       `json:"followers_count"`
   173  	Login           string    `json:"login"`
   174  	Score           float32   `json:"score"`
   175  	Created         time.Time `json:"created"`
   176  }
   177  
   178  // Types which represents result for search email
   179  // http://developer.github.com/v3/search/#email-users
   180  type EmailSearchParam struct {
   181  	Email string
   182  
   183  	result emailSearchResult
   184  }
   185  
   186  func (p *EmailSearchParam) addr() (string, error) {
   187  	if len(p.Email) == 0 {
   188  		return "", ErrSearchOmitParam
   189  	}
   190  	if !strings.Contains(p.Email, "@") {
   191  		return "", ErrSearchInvalidParam
   192  	}
   193  
   194  	return "/legacy/user/email/" + p.Email, nil
   195  }
   196  
   197  type emailSearchResult struct {
   198  	User *EmailSearchResult
   199  }
   200  
   201  type EmailSearchResult struct {
   202  	PublicRepoCount int       `json:"public_repo_count"`
   203  	PublicGistCount int       `json:"public_gist_count"`
   204  	FollowersCount  int       `json:"followers_count"`
   205  	FollowingCount  int       `json:"following_count"`
   206  	Created         time.Time `json:"created"`
   207  	CreatedAt       time.Time `json:"created_at"`
   208  	Name            string    `json:"name"`
   209  	Company         string    `json:"company"`
   210  	BLog            string    `json:"blog"`
   211  	Location        string    `json:"location"`
   212  	Email           string    `json:"email"`
   213  	Id              int       `json:"id"`
   214  	Login           string    `json:"login"`
   215  	Type            string    `json:"type"`
   216  	GravatarId      string    `json:"gravatar_id"`
   217  }
   218  
   219  // Find issues by state and keyword.
   220  func (g *GitHub) SearchIssues(p *IssuesSearchParam) ([]*IssuesSearchResult, error) {
   221  	addr, err := p.addr()
   222  	if err != nil {
   223  		return nil, err
   224  	}
   225  	if err := g.callGithubApi("GET", addr, &p.result); err != nil {
   226  		return nil, err
   227  	}
   228  	return p.result.Issues, nil
   229  }
   230  
   231  // Find repositories by keyword.
   232  // This method returns up to 100 results per page and
   233  // pages can be fetched using the start_page parameter.
   234  func (g *GitHub) SearchRepositories(p *RepositoriesSearchParam) ([]*RepositoriesSearchResult, error) {
   235  	addr, err := p.addr()
   236  	if err != nil {
   237  		return nil, err
   238  	}
   239  	if err := g.callGithubApi("GET", addr, &p.result); err != nil {
   240  		return nil, err
   241  	}
   242  	return p.result.Repositories, nil
   243  }
   244  
   245  // Find users by state and keyword.
   246  func (g *GitHub) SearchUsers(p *UsersSearchParam) ([]*UsersSearchResult, error) {
   247  	addr, err := p.addr()
   248  	if err != nil {
   249  		return nil, err
   250  	}
   251  	if err := g.callGithubApi("GET", addr, &p.result); err != nil {
   252  		return nil, err
   253  	}
   254  	return p.result.Users, nil
   255  }
   256  
   257  // Find user by email address.
   258  // This API call is added for compatibility reasons only.
   259  // There’s no guarantee that full email searches will always be available.
   260  func (g *GitHub) SearchEmail(p *EmailSearchParam) (*EmailSearchResult, error) {
   261  	addr, err := p.addr()
   262  	if err != nil {
   263  		return nil, err
   264  	}
   265  	if err := g.callGithubApi("GET", addr, &p.result); err != nil {
   266  		return nil, err
   267  	}
   268  	return p.result.User, nil
   269  }