Skip to content

Commit 5ff7ae8

Browse files
committed
added more stuff
1 parent 2d6c1e2 commit 5ff7ae8

File tree

5 files changed

+51
-17
lines changed

5 files changed

+51
-17
lines changed

api/queries_repo.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,25 @@ func (r Repository) ViewerCanTriage() bool {
230230
}
231231
}
232232

233+
func FetchRepository(client *Client, repo ghrepo.Interface, fields []string) (*Repository, error) {
234+
query := fmt.Sprintf(`query RepositoryInfo($owner: String!, $name: String!) {
235+
repository(owner: $owner, name: $name) {%s}
236+
}`, RepositoryGraphQL(fields))
237+
238+
variables := map[string]interface{}{
239+
"owner": repo.RepoOwner(),
240+
"name": repo.RepoName(),
241+
}
242+
243+
var result struct {
244+
Repository Repository
245+
}
246+
if err := client.GraphQL(repo.RepoHost(), query, variables, &result); err != nil {
247+
return nil, err
248+
}
249+
return InitRepoHostname(&result.Repository, repo.RepoHost()), nil
250+
}
251+
233252
func GitHubRepo(client *Client, repo ghrepo.Interface) (*Repository, error) {
234253
query := `
235254
fragment repo on Repository {

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,5 @@ require (
4747
replace github.com/shurcooL/graphql => github.com/cli/shurcooL-graphql v0.0.0-20200707151639-0f7232a2bf7e
4848

4949
replace golang.org/x/crypto => github.com/cli/crypto v0.0.0-20210929142629-6be313f59b03
50+
51+
replace github.com/cli/cli/v2/pkg/cmd/repo/rename => /cli/cli/v2/pkg/cmd/repo/rename

pkg/cmd/repo/rename/http.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package rename
2+

pkg/cmd/repo/rename/rename.go

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ package rename
33
import (
44
"fmt"
55
"net/http"
6-
"strings"
6+
7+
// "strings"
78

89
"github.com/cli/cli/v2/api"
10+
"github.com/cli/cli/v2/internal/config"
911
"github.com/cli/cli/v2/internal/ghinstance"
12+
13+
// "github.com/cli/cli/v2/internal/ghinstance"
14+
1015
"github.com/cli/cli/v2/internal/ghrepo"
1116
"github.com/cli/cli/v2/pkg/cmdutil"
1217
"github.com/cli/cli/v2/pkg/iostreams"
@@ -16,20 +21,23 @@ import (
1621
type RenameOptions struct{
1722
HttpClient func() (*http.Client, error)
1823
IO *iostreams.IOStreams
24+
Config func() (config.Config, error)
1925
RepoName string
2026
}
2127

22-
func MewCmcRename(f *cmdutil.Factory, runf func(*RenameOptions) error) *cobra.Command {
28+
func NewCmdRename(f *cmdutil.Factory, runf func(*RenameOptions) error) *cobra.Command {
2329
opts:= &RenameOptions {
2430
IO: f.IOStreams,
2531
HttpClient: f.HttpClient,
2632
}
2733

2834
cmd := &cobra.Command{
29-
Use: "rename <user/repo> <user/repo_change",
35+
DisableFlagsInUseLine: true,
36+
37+
Use: "rename <user/repo_name>",
3038
Short: "Rename a repository",
3139
Long: "Rename a GitHub repository",
32-
Args: cmdutil.ExactArgs(2, "cannot rename: repository argument required"),
40+
Args: cmdutil.ExactArgs(1, "cannot rename: repository argument required"),
3341
RunE: func (cmd *cobra.Command, args []string) error {
3442
opts.RepoName = args[0]
3543
if runf != nil {
@@ -43,31 +51,32 @@ func MewCmcRename(f *cmdutil.Factory, runf func(*RenameOptions) error) *cobra.Co
4351

4452

4553
func renameRun(opts *RenameOptions) error {
46-
cs := opts.IO.ColorScheme()
54+
// cs := opts.IO.ColorScheme()
4755
httpClient, err := opts.HttpClient()
4856
if err != nil {
4957
return err
5058
}
5159
apiClient := api.NewClientFromHTTP(httpClient);
5260

53-
var toRename ghrepo.Interface
54-
55-
repoURL := opts.RepoName
56-
57-
if !strings.Contains(repoURL, "/") {
58-
currentUser, err := api.CurrentLoginName(apiClient, ghinstance.Default())
59-
if err != nil {
60-
return err
61-
}
62-
repoURL = currentUser + "/" + repoURL
61+
username, err := api.CurrentLoginName(apiClient, ghinstance.Default())
62+
if err != nil {
63+
return err
6364
}
6465

65-
toRename, err = ghrepo.FromFullName(repoURL)
66+
toRename, err := ghrepo.FromFullName(opts.RepoName)
6667
if err != nil {
6768
return fmt.Errorf("argument error: %w", err)
6869
}
6970

7071
fields := []string{"name", "owner", "id"}
72+
repo, err := api.FetchRepository(apiClient, toRename, fields)
73+
if err != nil {
74+
return err
75+
}
76+
77+
if username != repo.Owner.Login {
78+
return fmt.Errorf("you do not own this repository");
79+
}
7180

72-
repo, err :=
81+
return nil
7382
}

pkg/cmd/repo/repo.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
repoListCmd "github.com/cli/cli/v2/pkg/cmd/repo/list"
1111
repoSyncCmd "github.com/cli/cli/v2/pkg/cmd/repo/sync"
1212
repoViewCmd "github.com/cli/cli/v2/pkg/cmd/repo/view"
13+
repoRenameCmd "github.com/cli/cli/v2/pkg/cmd/repo/rename"
1314
"github.com/cli/cli/v2/pkg/cmdutil"
1415
"github.com/spf13/cobra"
1516
)
@@ -42,6 +43,7 @@ func NewCmdRepo(f *cmdutil.Factory) *cobra.Command {
4243
cmd.AddCommand(repoSyncCmd.NewCmdSync(f, nil))
4344
cmd.AddCommand(creditsCmd.NewCmdRepoCredits(f, nil))
4445
cmd.AddCommand(gardenCmd.NewCmdGarden(f, nil))
46+
cmd.AddCommand(repoRenameCmd.NewCmdRename(f, nil))
4547

4648
return cmd
4749
}

0 commit comments

Comments
 (0)