activity_watching.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2014 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. // Subscription identifies a repository or thread subscription.
  11. type Subscription struct {
  12. Subscribed *bool `json:"subscribed,omitempty"`
  13. Ignored *bool `json:"ignored,omitempty"`
  14. Reason *string `json:"reason,omitempty"`
  15. CreatedAt *Timestamp `json:"created_at,omitempty"`
  16. URL *string `json:"url,omitempty"`
  17. // only populated for repository subscriptions
  18. RepositoryURL *string `json:"repository_url,omitempty"`
  19. // only populated for thread subscriptions
  20. ThreadURL *string `json:"thread_url,omitempty"`
  21. }
  22. // ListWatchers lists watchers of a particular repo.
  23. //
  24. // GitHub API docs: https://developer.github.com/v3/activity/watching/#list-watchers
  25. func (s *ActivityService) ListWatchers(ctx context.Context, owner, repo string, opt *ListOptions) ([]*User, *Response, error) {
  26. u := fmt.Sprintf("repos/%s/%s/subscribers", owner, repo)
  27. u, err := addOptions(u, opt)
  28. if err != nil {
  29. return nil, nil, err
  30. }
  31. req, err := s.client.NewRequest("GET", u, nil)
  32. if err != nil {
  33. return nil, nil, err
  34. }
  35. var watchers []*User
  36. resp, err := s.client.Do(ctx, req, &watchers)
  37. if err != nil {
  38. return nil, resp, err
  39. }
  40. return watchers, resp, nil
  41. }
  42. // ListWatched lists the repositories the specified user is watching. Passing
  43. // the empty string will fetch watched repos for the authenticated user.
  44. //
  45. // GitHub API docs: https://developer.github.com/v3/activity/watching/#list-repositories-being-watched
  46. func (s *ActivityService) ListWatched(ctx context.Context, user string, opt *ListOptions) ([]*Repository, *Response, error) {
  47. var u string
  48. if user != "" {
  49. u = fmt.Sprintf("users/%v/subscriptions", user)
  50. } else {
  51. u = "user/subscriptions"
  52. }
  53. u, err := addOptions(u, opt)
  54. if err != nil {
  55. return nil, nil, err
  56. }
  57. req, err := s.client.NewRequest("GET", u, nil)
  58. if err != nil {
  59. return nil, nil, err
  60. }
  61. var watched []*Repository
  62. resp, err := s.client.Do(ctx, req, &watched)
  63. if err != nil {
  64. return nil, resp, err
  65. }
  66. return watched, resp, nil
  67. }
  68. // GetRepositorySubscription returns the subscription for the specified
  69. // repository for the authenticated user. If the authenticated user is not
  70. // watching the repository, a nil Subscription is returned.
  71. //
  72. // GitHub API docs: https://developer.github.com/v3/activity/watching/#get-a-repository-subscription
  73. func (s *ActivityService) GetRepositorySubscription(ctx context.Context, owner, repo string) (*Subscription, *Response, error) {
  74. u := fmt.Sprintf("repos/%s/%s/subscription", owner, repo)
  75. req, err := s.client.NewRequest("GET", u, nil)
  76. if err != nil {
  77. return nil, nil, err
  78. }
  79. sub := new(Subscription)
  80. resp, err := s.client.Do(ctx, req, sub)
  81. if err != nil {
  82. // if it's just a 404, don't return that as an error
  83. _, err = parseBoolResponse(err)
  84. return nil, resp, err
  85. }
  86. return sub, resp, nil
  87. }
  88. // SetRepositorySubscription sets the subscription for the specified repository
  89. // for the authenticated user.
  90. //
  91. // To watch a repository, set subscription.Subscribed to true.
  92. // To ignore notifications made within a repository, set subscription.Ignored to true.
  93. // To stop watching a repository, use DeleteRepositorySubscription.
  94. //
  95. // GitHub API docs: https://developer.github.com/v3/activity/watching/#set-a-repository-subscription
  96. func (s *ActivityService) SetRepositorySubscription(ctx context.Context, owner, repo string, subscription *Subscription) (*Subscription, *Response, error) {
  97. u := fmt.Sprintf("repos/%s/%s/subscription", owner, repo)
  98. req, err := s.client.NewRequest("PUT", u, subscription)
  99. if err != nil {
  100. return nil, nil, err
  101. }
  102. sub := new(Subscription)
  103. resp, err := s.client.Do(ctx, req, sub)
  104. if err != nil {
  105. return nil, resp, err
  106. }
  107. return sub, resp, nil
  108. }
  109. // DeleteRepositorySubscription deletes the subscription for the specified
  110. // repository for the authenticated user.
  111. //
  112. // This is used to stop watching a repository. To control whether or not to
  113. // receive notifications from a repository, use SetRepositorySubscription.
  114. //
  115. // GitHub API docs: https://developer.github.com/v3/activity/watching/#delete-a-repository-subscription
  116. func (s *ActivityService) DeleteRepositorySubscription(ctx context.Context, owner, repo string) (*Response, error) {
  117. u := fmt.Sprintf("repos/%s/%s/subscription", owner, repo)
  118. req, err := s.client.NewRequest("DELETE", u, nil)
  119. if err != nil {
  120. return nil, err
  121. }
  122. return s.client.Do(ctx, req, nil)
  123. }