MCPcopy Create free account
hub / github.com/cli/cli / ForkRepo

Function ForkRepo

api/queries_repo.go:592–639  ·  view source on GitHub ↗

ForkRepo forks the repository on GitHub and returns the new repository

(client *Client, repo ghrepo.Interface, org, newName string, defaultBranchOnly bool)

Source from the content-addressed store, hash-verified

590
591// ForkRepo forks the repository on GitHub and returns the new repository
592func ForkRepo(client *Client, repo ghrepo.Interface, org, newName string, defaultBranchOnly bool) (*Repository, error) {
593 path, err := safeurl.JoinPath("repos", repo.RepoOwner(), repo.RepoName(), "forks")
594 if err != nil {
595 return nil, err
596 }
597
598 params := map[string]interface{}{}
599 if org != "" {
600 params["organization"] = org
601 }
602 if newName != "" {
603 params["name"] = newName
604 }
605 if defaultBranchOnly {
606 params["default_branch_only"] = true
607 }
608
609 body := &bytes.Buffer{}
610 enc := json.NewEncoder(body)
611 if err := enc.Encode(params); err != nil {
612 return nil, err
613 }
614
615 result := repositoryV3{}
616 err = client.REST(repo.RepoHost(), "POST", path.String(), body, &result)
617 if err != nil {
618 return nil, err
619 }
620
621 newRepo := &Repository{
622 ID: result.NodeID,
623 Name: result.Name,
624 CreatedAt: result.CreatedAt,
625 Owner: RepositoryOwner{
626 Login: result.Owner.Login,
627 },
628 ViewerPermission: "WRITE",
629 hostname: repo.RepoHost(),
630 }
631
632 // The GitHub API will happily return a HTTP 200 when attempting to fork own repo even though no forking
633 // actually took place. Ensure that we raise an error instead.
634 if ghrepo.IsSame(repo, newRepo) {
635 return newRepo, fmt.Errorf("%s cannot be forked. A single user account cannot own both a parent and fork.", ghrepo.FullName(repo))
636 }
637
638 return newRepo, nil
639}
640
641// RenameRepo renames the repository on GitHub and returns the renamed repository
642func RenameRepo(client *Client, repo ghrepo.Interface, newRepoName string) (*Repository, error) {

Callers 3

forkRunFunction · 0.92
handlePushFunction · 0.92

Calls 9

JoinPathFunction · 0.92
IsSameFunction · 0.92
FullNameFunction · 0.92
RepoOwnerMethod · 0.65
RepoNameMethod · 0.65
RESTMethod · 0.65
RepoHostMethod · 0.65
StringMethod · 0.65
ErrorfMethod · 0.65