add flag to set fork-name during repo fork. Tests WIP
This commit is contained in:
parent
29816b63c3
commit
54d92facbf
5 changed files with 49 additions and 62 deletions
|
|
@ -5,6 +5,7 @@ import (
|
|||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/cli/cli/v2/internal/ghinstance"
|
||||
"io"
|
||||
"net/http"
|
||||
"sort"
|
||||
|
|
@ -524,6 +525,37 @@ func ForkRepo(client *Client, repo ghrepo.Interface, org string) (*Repository, e
|
|||
}, nil
|
||||
}
|
||||
|
||||
// RenameRepo renames the repository on GitHub and returns the renamed repository
|
||||
func RenameRepo(client *Client, repo ghrepo.Interface, newRepoName string) (*Repository, error) {
|
||||
input := map[string]string{"name": newRepoName}
|
||||
body := &bytes.Buffer{}
|
||||
enc := json.NewEncoder(body)
|
||||
if err := enc.Encode(input); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("%srepos/%s",
|
||||
ghinstance.RESTPrefix(repo.RepoHost()),
|
||||
ghrepo.FullName(repo))
|
||||
|
||||
result := repositoryV3{}
|
||||
err := client.REST(repo.RepoHost(), "PATCH", path, body, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Repository{
|
||||
ID: result.NodeID,
|
||||
Name: result.Name,
|
||||
CreatedAt: result.CreatedAt,
|
||||
Owner: RepositoryOwner{
|
||||
Login: result.Owner.Login,
|
||||
},
|
||||
ViewerPermission: "WRITE",
|
||||
hostname: repo.RepoHost(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// RepoFindForks finds forks of the repo that are affiliated with the viewer
|
||||
func RepoFindForks(client *Client, repo ghrepo.Interface, limit int) ([]*Repository, error) {
|
||||
result := struct {
|
||||
|
|
|
|||
|
|
@ -153,6 +153,7 @@ func AddRemote(name, u string) (*Remote, error) {
|
|||
}
|
||||
|
||||
func UpdateRemoteURL(name, u string) error {
|
||||
fmt.Println(name, u, "=====================name u==============")
|
||||
addCmd, err := GitCommand("remote", "set-url", name, u)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ type ForkOptions struct {
|
|||
PromptRemote bool
|
||||
RemoteName string
|
||||
Organization string
|
||||
ForkName string
|
||||
Rename bool
|
||||
}
|
||||
|
||||
|
|
@ -115,6 +116,7 @@ Additional 'git clone' flags can be passed in by listing them after '--'.`,
|
|||
cmd.Flags().BoolVar(&opts.Remote, "remote", false, "Add remote for fork {true|false}")
|
||||
cmd.Flags().StringVar(&opts.RemoteName, "remote-name", defaultRemoteName, "Specify a name for a fork's new remote.")
|
||||
cmd.Flags().StringVar(&opts.Organization, "org", "", "Create the fork in an organization")
|
||||
cmd.Flags().StringVar(&opts.ForkName, "fork-name", "", "Specify a name for the forked repo")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
|
@ -180,6 +182,14 @@ func forkRun(opts *ForkOptions) error {
|
|||
return fmt.Errorf("failed to fork: %w", err)
|
||||
}
|
||||
|
||||
// Rename the forked repo if ForkName is specified in opts.
|
||||
if opts.ForkName != "" {
|
||||
forkedRepo, err = api.RenameRepo(apiClient, forkedRepo, opts.ForkName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// This is weird. There is not an efficient way to determine via the GitHub API whether or not a
|
||||
// given user has forked a given repo. We noticed, also, that the create fork API endpoint just
|
||||
// returns the fork repo data even if it already exists -- with no change in status code or
|
||||
|
|
|
|||
|
|
@ -1,61 +0,0 @@
|
|||
package rename
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/cli/cli/v2/api"
|
||||
"github.com/cli/cli/v2/internal/ghinstance"
|
||||
"github.com/cli/cli/v2/internal/ghrepo"
|
||||
)
|
||||
|
||||
func apiRename(client *http.Client, repo ghrepo.Interface, newRepoName string) (ghrepo.Interface, error) {
|
||||
input := map[string]string{"name": newRepoName}
|
||||
body, err := json.Marshal(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("%srepos/%s",
|
||||
ghinstance.RESTPrefix(repo.RepoHost()),
|
||||
ghrepo.FullName(repo))
|
||||
|
||||
request, err := http.NewRequest("PATCH", path, bytes.NewBuffer(body))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
request.Header.Set("Content-Type", "application/json; charset=utf-8")
|
||||
|
||||
resp, err := client.Do(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode > 299 {
|
||||
return nil, api.HandleHTTPError(resp)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := struct {
|
||||
Name string
|
||||
Owner struct {
|
||||
Login string
|
||||
}
|
||||
}{}
|
||||
if err := json.Unmarshal(b, &result); err != nil {
|
||||
return nil, fmt.Errorf("error unmarshaling response: %w", err)
|
||||
}
|
||||
|
||||
newRepo := ghrepo.NewWithHost(result.Owner.Login, result.Name, repo.RepoHost())
|
||||
|
||||
return newRepo, nil
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@ package rename
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/cli/cli/v2/api"
|
||||
"net/http"
|
||||
|
||||
"github.com/AlecAivazis/survey/v2"
|
||||
|
|
@ -114,7 +115,9 @@ func renameRun(opts *RenameOptions) error {
|
|||
}
|
||||
}
|
||||
|
||||
newRepo, err := apiRename(httpClient, currRepo, newRepoName)
|
||||
apiClient := api.NewClientFromHTTP(httpClient)
|
||||
|
||||
newRepo, err := api.RenameRepo(apiClient, currRepo, newRepoName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -124,6 +127,8 @@ func renameRun(opts *RenameOptions) error {
|
|||
fmt.Fprintf(opts.IO.Out, "%s Renamed repository %s\n", cs.SuccessIcon(), ghrepo.FullName(newRepo))
|
||||
}
|
||||
|
||||
fmt.Println(opts.HasRepoOverride, "=========erpo override=============")
|
||||
|
||||
if opts.HasRepoOverride {
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue