ForkRepo forks the repository on GitHub and returns the new repository
(client *Client, repo ghrepo.Interface, org, newName string, defaultBranchOnly bool)
| 589 | |
| 590 | // ForkRepo forks the repository on GitHub and returns the new repository |
| 591 | func 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 |
| 638 | func RenameRepo(client *Client, repo ghrepo.Interface, newRepoName string) (*Repository, error) { |