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