github.com/google/go-github/v49@v49.1.0/github/repos_forks.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 "encoding/json" 11 "fmt" 12 ) 13 14 // RepositoryListForksOptions specifies the optional parameters to the 15 // RepositoriesService.ListForks method. 16 type RepositoryListForksOptions struct { 17 // How to sort the forks list. Possible values are: newest, oldest, 18 // watchers. Default is "newest". 19 Sort string `url:"sort,omitempty"` 20 21 ListOptions 22 } 23 24 // ListForks lists the forks of the specified repository. 25 // 26 // GitHub API docs: https://docs.github.com/en/rest/repos/forks#list-forks 27 func (s *RepositoriesService) ListForks(ctx context.Context, owner, repo string, opts *RepositoryListForksOptions) ([]*Repository, *Response, error) { 28 u := fmt.Sprintf("repos/%v/%v/forks", owner, repo) 29 u, err := addOptions(u, opts) 30 if err != nil { 31 return nil, nil, err 32 } 33 34 req, err := s.client.NewRequest("GET", u, nil) 35 if err != nil { 36 return nil, nil, err 37 } 38 39 // TODO: remove custom Accept header when topics API fully launches. 40 req.Header.Set("Accept", mediaTypeTopicsPreview) 41 42 var repos []*Repository 43 resp, err := s.client.Do(ctx, req, &repos) 44 if err != nil { 45 return nil, resp, err 46 } 47 48 return repos, resp, nil 49 } 50 51 // RepositoryCreateForkOptions specifies the optional parameters to the 52 // RepositoriesService.CreateFork method. 53 type RepositoryCreateForkOptions struct { 54 // The organization to fork the repository into. 55 Organization string `json:"organization,omitempty"` 56 Name string `json:"name,omitempty"` 57 DefaultBranchOnly bool `json:"default_branch_only,omitempty"` 58 } 59 60 // CreateFork creates a fork of the specified repository. 61 // 62 // This method might return an *AcceptedError and a status code of 63 // 202. This is because this is the status that GitHub returns to signify that 64 // it is now computing creating the fork in a background task. In this event, 65 // the Repository value will be returned, which includes the details about the pending fork. 66 // A follow up request, after a delay of a second or so, should result 67 // in a successful request. 68 // 69 // GitHub API docs: https://docs.github.com/en/rest/repos/forks#create-a-fork 70 func (s *RepositoriesService) CreateFork(ctx context.Context, owner, repo string, opts *RepositoryCreateForkOptions) (*Repository, *Response, error) { 71 u := fmt.Sprintf("repos/%v/%v/forks", owner, repo) 72 73 req, err := s.client.NewRequest("POST", u, opts) 74 if err != nil { 75 return nil, nil, err 76 } 77 78 fork := new(Repository) 79 resp, err := s.client.Do(ctx, req, fork) 80 if err != nil { 81 // Persist AcceptedError's metadata to the Repository object. 82 if aerr, ok := err.(*AcceptedError); ok { 83 if err := json.Unmarshal(aerr.Raw, fork); err != nil { 84 return fork, resp, err 85 } 86 87 return fork, resp, err 88 } 89 return nil, resp, err 90 } 91 92 return fork, resp, nil 93 }