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