Debian Codegraph @

github/activity_star.go

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