Files
Jam Balaya 40091e2115 refactor!: Rename RepositoryCreateForkOptions and RepositoryAddCollaboratorOptions to ...Request and pass them by value (#4607)
BREAKING CHANGE: `RepositoryCreateForkOptions` and `RepositoryAddCollaboratorOptions` are renamed to `CreateForkRequest` and `AddCollaboratorRequest`; `RepositoriesService.CreateFork` and `RepositoriesService.AddCollaborator` now take them by value instead of by pointer; `CreateForkRequest.Organization`, `CreateForkRequest.Name`, `CreateForkRequest.DefaultBranchOnly` and `AddCollaboratorRequest.Permission` are now pointers.
2026-10-09 11:48:30 -04:00

97 lines
2.9 KiB
Go

// Copyright 2013 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"context"
"encoding/json"
"errors"
"fmt"
)
// RepositoryListForksOptions specifies the optional parameters to the
// RepositoriesService.ListForks method.
type RepositoryListForksOptions struct {
// How to sort the forks list. Possible values are: newest, oldest,
// watchers. Default is "newest".
Sort string `url:"sort,omitempty"`
ListOptions
}
// ListForks lists the forks of the specified repository.
//
// GitHub API docs: https://docs.github.com/rest/repos/forks?apiVersion=2022-11-28#list-forks
//
//meta:operation GET /repos/{owner}/{repo}/forks
func (s *RepositoriesService) ListForks(ctx context.Context, owner, repo string, opts *RepositoryListForksOptions) ([]*Repository, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/forks", owner, repo)
u, err := addOptions(u, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(ctx, "GET", u, nil)
if err != nil {
return nil, nil, err
}
req.Header.Set("Accept", mediaTypeTopicsPreview)
var repos []*Repository
resp, err := s.client.Do(req, &repos)
if err != nil {
return nil, resp, err
}
return repos, resp, nil
}
// CreateForkRequest represents a request to create a fork of a repository.
type CreateForkRequest struct {
// The organization to fork the repository into.
Organization *string `json:"organization,omitempty"`
Name *string `json:"name,omitempty"`
DefaultBranchOnly *bool `json:"default_branch_only,omitempty"`
}
// CreateFork creates a fork of the specified repository.
//
// This method might return an *AcceptedError and a status code of
// 202. This is because this is the status that GitHub returns to signify that
// it is now computing creating the fork in a background task. In this event,
// the Repository value will be returned, which includes the details about the pending fork.
// A follow up request, after a delay of a second or so, should result
// in a successful request.
//
// GitHub API docs: https://docs.github.com/rest/repos/forks?apiVersion=2022-11-28#create-a-fork
//
//meta:operation POST /repos/{owner}/{repo}/forks
func (s *RepositoriesService) CreateFork(ctx context.Context, owner, repo string, body CreateForkRequest) (*Repository, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/forks", owner, repo)
req, err := s.client.NewRequest(ctx, "POST", u, body)
if err != nil {
return nil, nil, err
}
var fork *Repository
resp, err := s.client.Do(req, &fork)
if err != nil {
// Persist AcceptedError's metadata to the Repository object.
if aerr, ok := errors.AsType[*AcceptedError](err); ok {
if err := json.Unmarshal(aerr.Raw, &fork); err != nil {
return fork, resp, err
}
return fork, resp, err
}
return nil, resp, err
}
return fork, resp, nil
}