activity_star.go 4.1 KB

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