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