本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.2KB

  1. // Copyright 2013 The go-github AUTHORS. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package github
  6. import (
  7. "context"
  8. "fmt"
  9. "strings"
  10. )
  11. // StarredRepository is returned by ListStarred.
  12. type StarredRepository struct {
  13. StarredAt *Timestamp `json:"starred_at,omitempty"`
  14. Repository *Repository `json:"repo,omitempty"`
  15. }
  16. // Stargazer represents a user that has starred a repository.
  17. type Stargazer struct {
  18. StarredAt *Timestamp `json:"starred_at,omitempty"`
  19. User *User `json:"user,omitempty"`
  20. }
  21. // ListStargazers lists people who have starred the specified repo.
  22. //
  23. // GitHub API docs: https://developer.github.com/v3/activity/starring/#list-stargazers
  24. func (s *ActivityService) ListStargazers(ctx context.Context, owner, repo string, opt *ListOptions) ([]*Stargazer, *Response, error) {
  25. u := fmt.Sprintf("repos/%s/%s/stargazers", owner, repo)
  26. u, err := addOptions(u, opt)
  27. if err != nil {
  28. return nil, nil, err
  29. }
  30. req, err := s.client.NewRequest("GET", u, nil)
  31. if err != nil {
  32. return nil, nil, err
  33. }
  34. // TODO: remove custom Accept header when this API fully launches
  35. req.Header.Set("Accept", mediaTypeStarringPreview)
  36. var stargazers []*Stargazer
  37. resp, err := s.client.Do(ctx, req, &stargazers)
  38. if err != nil {
  39. return nil, resp, err
  40. }
  41. return stargazers, resp, nil
  42. }
  43. // ActivityListStarredOptions specifies the optional parameters to the
  44. // ActivityService.ListStarred method.
  45. type ActivityListStarredOptions struct {
  46. // How to sort the repository list. Possible values are: created, updated,
  47. // pushed, full_name. Default is "full_name".
  48. Sort string `url:"sort,omitempty"`
  49. // Direction in which to sort repositories. Possible values are: asc, desc.
  50. // Default is "asc" when sort is "full_name", otherwise default is "desc".
  51. Direction string `url:"direction,omitempty"`
  52. ListOptions
  53. }
  54. // ListStarred lists all the repos starred by a user. Passing the empty string
  55. // will list the starred repositories for the authenticated user.
  56. //
  57. // GitHub API docs: https://developer.github.com/v3/activity/starring/#list-repositories-being-starred
  58. func (s *ActivityService) ListStarred(ctx context.Context, user string, opt *ActivityListStarredOptions) ([]*StarredRepository, *Response, error) {
  59. var u string
  60. if user != "" {
  61. u = fmt.Sprintf("users/%v/starred", user)
  62. } else {
  63. u = "user/starred"
  64. }
  65. u, err := addOptions(u, opt)
  66. if err != nil {
  67. return nil, nil, err
  68. }
  69. req, err := s.client.NewRequest("GET", u, nil)
  70. if err != nil {
  71. return nil, nil, err
  72. }
  73. // TODO: remove custom Accept header when APIs fully launch
  74. acceptHeaders := []string{mediaTypeStarringPreview, mediaTypeTopicsPreview}
  75. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  76. var repos []*StarredRepository
  77. resp, err := s.client.Do(ctx, req, &repos)
  78. if err != nil {
  79. return nil, resp, err
  80. }
  81. return repos, resp, nil
  82. }
  83. // IsStarred checks if a repository is starred by authenticated user.
  84. //
  85. // GitHub API docs: https://developer.github.com/v3/activity/starring/#check-if-you-are-starring-a-repository
  86. func (s *ActivityService) IsStarred(ctx context.Context, owner, repo string) (bool, *Response, error) {
  87. u := fmt.Sprintf("user/starred/%v/%v", owner, repo)
  88. req, err := s.client.NewRequest("GET", u, nil)
  89. if err != nil {
  90. return false, nil, err
  91. }
  92. resp, err := s.client.Do(ctx, req, nil)
  93. starred, err := parseBoolResponse(err)
  94. return starred, resp, err
  95. }
  96. // Star a repository as the authenticated user.
  97. //
  98. // GitHub API docs: https://developer.github.com/v3/activity/starring/#star-a-repository
  99. func (s *ActivityService) Star(ctx context.Context, owner, repo string) (*Response, error) {
  100. u := fmt.Sprintf("user/starred/%v/%v", owner, repo)
  101. req, err := s.client.NewRequest("PUT", u, nil)
  102. if err != nil {
  103. return nil, err
  104. }
  105. return s.client.Do(ctx, req, nil)
  106. }
  107. // Unstar a repository as the authenticated user.
  108. //
  109. // GitHub API docs: https://developer.github.com/v3/activity/starring/#unstar-a-repository
  110. func (s *ActivityService) Unstar(ctx context.Context, owner, repo string) (*Response, error) {
  111. u := fmt.Sprintf("user/starred/%v/%v", owner, repo)
  112. req, err := s.client.NewRequest("DELETE", u, nil)
  113. if err != nil {
  114. return nil, err
  115. }
  116. return s.client.Do(ctx, req, nil)
  117. }
上海开阖软件有限公司 沪ICP备12045867号-1