github.com/google/go-github/v70@v70.0.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/rest/repos/forks#list-forks 27 // 28 //meta:operation GET /repos/{owner}/{repo}/forks 29 func (s *RepositoriesService) ListForks(ctx context.Context, owner, repo string, opts *RepositoryListForksOptions) ([]*Repository, *Response, error) { 30 u := fmt.Sprintf("repos/%v/%v/forks", 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 topics API fully launches. 42 req.Header.Set("Accept", mediaTypeTopicsPreview) 43 44 var repos []*Repository 45 resp, err := s.client.Do(ctx, req, &repos) 46 if err != nil { 47 return nil, resp, err 48 } 49 50 return repos, resp, nil 51 } 52 53 // RepositoryCreateForkOptions specifies the optional parameters to the 54 // RepositoriesService.CreateFork method. 55 type RepositoryCreateForkOptions struct { 56 // The organization to fork the repository into. 57 Organization string `json:"organization,omitempty"` 58 Name string `json:"name,omitempty"` 59 DefaultBranchOnly bool `json:"default_branch_only,omitempty"` 60 } 61 62 // CreateFork creates a fork of the specified repository. 63 // 64 // This method might return an *AcceptedError and a status code of 65 // 202. This is because this is the status that GitHub returns to signify that 66 // it is now computing creating the fork in a background task. In this event, 67 // the Repository value will be returned, which includes the details about the pending fork. 68 // A follow up request, after a delay of a second or so, should result 69 // in a successful request. 70 // 71 // GitHub API docs: https://docs.github.com/rest/repos/forks#create-a-fork 72 // 73 //meta:operation POST /repos/{owner}/{repo}/forks 74 func (s *RepositoriesService) CreateFork(ctx context.Context, owner, repo string, opts *RepositoryCreateForkOptions) (*Repository, *Response, error) { 75 u := fmt.Sprintf("repos/%v/%v/forks", owner, repo) 76 77 req, err := s.client.NewRequest("POST", u, opts) 78 if err != nil { 79 return nil, nil, err 80 } 81 82 fork := new(Repository) 83 resp, err := s.client.Do(ctx, req, fork) 84 if err != nil { 85 // Persist AcceptedError's metadata to the Repository object. 86 if aerr, ok := err.(*AcceptedError); ok { 87 if err := json.Unmarshal(aerr.Raw, fork); err != nil { 88 return fork, resp, err 89 } 90 91 return fork, resp, err 92 } 93 return nil, resp, err 94 } 95 96 return fork, resp, nil 97 }