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