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