...

Source file src/github.com/google/go-github/v47/github/repos_forks.go

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

View as plain text