...

Source file src/github.com/google/go-github/v33/github/activity_star.go

Documentation: github.com/google/go-github/v33/github

     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/free-pro-team@latest/rest/reference/activity/#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/free-pro-team@latest/rest/reference/activity/#list-repositories-starred-by-the-authenticated-user
    71  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/activity/#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/free-pro-team@latest/rest/reference/activity/#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  	resp, err := s.client.Do(ctx, req, nil)
   112  	starred, err := parseBoolResponse(err)
   113  	return starred, resp, err
   114  }
   115  
   116  // Star a repository as the authenticated user.
   117  //
   118  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/activity/#star-a-repository-for-the-authenticated-user
   119  func (s *ActivityService) Star(ctx context.Context, owner, repo string) (*Response, error) {
   120  	u := fmt.Sprintf("user/starred/%v/%v", owner, repo)
   121  	req, err := s.client.NewRequest("PUT", u, nil)
   122  	if err != nil {
   123  		return nil, err
   124  	}
   125  	return s.client.Do(ctx, req, nil)
   126  }
   127  
   128  // Unstar a repository as the authenticated user.
   129  //
   130  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/activity/#unstar-a-repository-for-the-authenticated-user
   131  func (s *ActivityService) Unstar(ctx context.Context, owner, repo string) (*Response, error) {
   132  	u := fmt.Sprintf("user/starred/%v/%v", owner, repo)
   133  	req, err := s.client.NewRequest("DELETE", u, nil)
   134  	if err != nil {
   135  		return nil, err
   136  	}
   137  	return s.client.Do(ctx, req, nil)
   138  }
   139  

View as plain text