MCPcopy
hub / github.com/cli/cli / ForkRepo

Function ForkRepo

api/queries_repo.go:591–635  ·  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

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

Callers 3

forkRunFunction · 0.92
handlePushFunction · 0.92

Calls 5

FullNameFunction · 0.92
IsSameFunction · 0.92
RESTMethod · 0.65
RepoHostMethod · 0.65
ErrorfMethod · 0.65