1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
// Copyright 2013 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"context"
"fmt"
)
// UsersService handles communication with the user related
// methods of the GitHub API.
//
// GitHub API docs: https://docs.github.com/rest/users/
type UsersService service
// User represents a GitHub user.
type User struct {
Login *string `json:"login,omitempty"`
ID *int64 `json:"id,omitempty"`
NodeID *string `json:"node_id,omitempty"`
AvatarURL *string `json:"avatar_url,omitempty"`
HTMLURL *string `json:"html_url,omitempty"`
GravatarID *string `json:"gravatar_id,omitempty"`
Name *string `json:"name,omitempty"`
Company *string `json:"company,omitempty"`
Blog *string `json:"blog,omitempty"`
Location *string `json:"location,omitempty"`
Email *string `json:"email,omitempty"`
Hireable *bool `json:"hireable,omitempty"`
Bio *string `json:"bio,omitempty"`
TwitterUsername *string `json:"twitter_username,omitempty"`
PublicRepos *int `json:"public_repos,omitempty"`
PublicGists *int `json:"public_gists,omitempty"`
Followers *int `json:"followers,omitempty"`
Following *int `json:"following,omitempty"`
CreatedAt *Timestamp `json:"created_at,omitempty"`
UpdatedAt *Timestamp `json:"updated_at,omitempty"`
SuspendedAt *Timestamp `json:"suspended_at,omitempty"`
Type *string `json:"type,omitempty"`
SiteAdmin *bool `json:"site_admin,omitempty"`
TotalPrivateRepos *int64 `json:"total_private_repos,omitempty"`
OwnedPrivateRepos *int64 `json:"owned_private_repos,omitempty"`
PrivateGists *int `json:"private_gists,omitempty"`
DiskUsage *int `json:"disk_usage,omitempty"`
Collaborators *int `json:"collaborators,omitempty"`
TwoFactorAuthentication *bool `json:"two_factor_authentication,omitempty"`
Plan *Plan `json:"plan,omitempty"`
LdapDn *string `json:"ldap_dn,omitempty"`
// API URLs
URL *string `json:"url,omitempty"`
EventsURL *string `json:"events_url,omitempty"`
FollowingURL *string `json:"following_url,omitempty"`
FollowersURL *string `json:"followers_url,omitempty"`
GistsURL *string `json:"gists_url,omitempty"`
OrganizationsURL *string `json:"organizations_url,omitempty"`
ReceivedEventsURL *string `json:"received_events_url,omitempty"`
ReposURL *string `json:"repos_url,omitempty"`
StarredURL *string `json:"starred_url,omitempty"`
SubscriptionsURL *string `json:"subscriptions_url,omitempty"`
// TextMatches is only populated from search results that request text matches
// See: search.go and https://docs.github.com/rest/search/#text-match-metadata
TextMatches []*TextMatch `json:"text_matches,omitempty"`
// Permissions and RoleName identify the permissions and role that a user has on a given
// repository. These are only populated when calling Repositories.ListCollaborators.
Permissions map[string]bool `json:"permissions,omitempty"`
RoleName *string `json:"role_name,omitempty"`
}
func (u User) String() string {
return Stringify(u)
}
// Get fetches a user. Passing the empty string will fetch the authenticated
// user.
//
// GitHub API docs: https://docs.github.com/rest/users/users#get-a-user
// GitHub API docs: https://docs.github.com/rest/users/users#get-the-authenticated-user
//
//meta:operation GET /user
//meta:operation GET /users/{username}
func (s *UsersService) Get(ctx context.Context, user string) (*User, *Response, error) {
var u string
if user != "" {
u = fmt.Sprintf("users/%v", user)
} else {
u = "user"
}
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
uResp := new(User)
resp, err := s.client.Do(ctx, req, uResp)
if err != nil {
return nil, resp, err
}
return uResp, resp, nil
}
// GetByID fetches a user.
//
// Note: GetByID uses the undocumented GitHub API endpoint "GET /user/{user_id}".
//
//meta:operation GET /user/{user_id}
func (s *UsersService) GetByID(ctx context.Context, id int64) (*User, *Response, error) {
u := fmt.Sprintf("user/%d", id)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
user := new(User)
resp, err := s.client.Do(ctx, req, user)
if err != nil {
return nil, resp, err
}
return user, resp, nil
}
// Edit the authenticated user.
//
// GitHub API docs: https://docs.github.com/rest/users/users#update-the-authenticated-user
//
//meta:operation PATCH /user
func (s *UsersService) Edit(ctx context.Context, user *User) (*User, *Response, error) {
u := "user"
req, err := s.client.NewRequest("PATCH", u, user)
if err != nil {
return nil, nil, err
}
uResp := new(User)
resp, err := s.client.Do(ctx, req, uResp)
if err != nil {
return nil, resp, err
}
return uResp, resp, nil
}
// HovercardOptions specifies optional parameters to the UsersService.GetHovercard
// method.
type HovercardOptions struct {
// SubjectType specifies the additional information to be received about the hovercard.
// Possible values are: organization, repository, issue, pull_request. (Required when using subject_id.)
SubjectType string `url:"subject_type"`
// SubjectID specifies the ID for the SubjectType. (Required when using subject_type.)
SubjectID string `url:"subject_id"`
}
// Hovercard represents hovercard information about a user.
type Hovercard struct {
Contexts []*UserContext `json:"contexts,omitempty"`
}
// UserContext represents the contextual information about user.
type UserContext struct {
Message *string `json:"message,omitempty"`
Octicon *string `json:"octicon,omitempty"`
}
// GetHovercard fetches contextual information about user. It requires authentication
// via Basic Auth or via OAuth with the repo scope.
//
// GitHub API docs: https://docs.github.com/rest/users/users#get-contextual-information-for-a-user
//
//meta:operation GET /users/{username}/hovercard
func (s *UsersService) GetHovercard(ctx context.Context, user string, opts *HovercardOptions) (*Hovercard, *Response, error) {
u := fmt.Sprintf("users/%v/hovercard", user)
u, err := addOptions(u, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
hc := new(Hovercard)
resp, err := s.client.Do(ctx, req, hc)
if err != nil {
return nil, resp, err
}
return hc, resp, nil
}
// UserListOptions specifies optional parameters to the UsersService.ListAll
// method.
type UserListOptions struct {
// ID of the last user seen
Since int64 `url:"since,omitempty"`
// Note: Pagination is powered exclusively by the Since parameter,
// ListOptions.Page has no effect.
// ListOptions.PerPage controls an undocumented GitHub API parameter.
ListOptions
}
// ListAll lists all GitHub users.
//
// To paginate through all users, populate 'Since' with the ID of the last user.
//
// GitHub API docs: https://docs.github.com/rest/users/users#list-users
//
//meta:operation GET /users
func (s *UsersService) ListAll(ctx context.Context, opts *UserListOptions) ([]*User, *Response, error) {
u, err := addOptions("users", opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
var users []*User
resp, err := s.client.Do(ctx, req, &users)
if err != nil {
return nil, resp, err
}
return users, resp, nil
}
// ListInvitations lists all currently-open repository invitations for the
// authenticated user.
//
// GitHub API docs: https://docs.github.com/rest/collaborators/invitations#list-repository-invitations-for-the-authenticated-user
//
//meta:operation GET /user/repository_invitations
func (s *UsersService) ListInvitations(ctx context.Context, opts *ListOptions) ([]*RepositoryInvitation, *Response, error) {
u, err := addOptions("user/repository_invitations", opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
invites := []*RepositoryInvitation{}
resp, err := s.client.Do(ctx, req, &invites)
if err != nil {
return nil, resp, err
}
return invites, resp, nil
}
// AcceptInvitation accepts the currently-open repository invitation for the
// authenticated user.
//
// GitHub API docs: https://docs.github.com/rest/collaborators/invitations#accept-a-repository-invitation
//
//meta:operation PATCH /user/repository_invitations/{invitation_id}
func (s *UsersService) AcceptInvitation(ctx context.Context, invitationID int64) (*Response, error) {
u := fmt.Sprintf("user/repository_invitations/%v", invitationID)
req, err := s.client.NewRequest("PATCH", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// DeclineInvitation declines the currently-open repository invitation for the
// authenticated user.
//
// GitHub API docs: https://docs.github.com/rest/collaborators/invitations#decline-a-repository-invitation
//
//meta:operation DELETE /user/repository_invitations/{invitation_id}
func (s *UsersService) DeclineInvitation(ctx context.Context, invitationID int64) (*Response, error) {
u := fmt.Sprintf("user/repository_invitations/%v", invitationID)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
|