added more stuff

This commit is contained in:
Parth Patel 2021-10-07 10:59:08 -04:00
parent 2d6c1e21d7
commit 5ff7ae89b6
5 changed files with 51 additions and 17 deletions

View file

@ -230,6 +230,25 @@ func (r Repository) ViewerCanTriage() bool {
}
}
func FetchRepository(client *Client, repo ghrepo.Interface, fields []string) (*Repository, error) {
query := fmt.Sprintf(`query RepositoryInfo($owner: String!, $name: String!) {
repository(owner: $owner, name: $name) {%s}
}`, RepositoryGraphQL(fields))
variables := map[string]interface{}{
"owner": repo.RepoOwner(),
"name": repo.RepoName(),
}
var result struct {
Repository Repository
}
if err := client.GraphQL(repo.RepoHost(), query, variables, &result); err != nil {
return nil, err
}
return InitRepoHostname(&result.Repository, repo.RepoHost()), nil
}
func GitHubRepo(client *Client, repo ghrepo.Interface) (*Repository, error) {
query := `
fragment repo on Repository {

2
go.mod
View file

@ -47,3 +47,5 @@ require (
replace github.com/shurcooL/graphql => github.com/cli/shurcooL-graphql v0.0.0-20200707151639-0f7232a2bf7e
replace golang.org/x/crypto => github.com/cli/crypto v0.0.0-20210929142629-6be313f59b03
replace github.com/cli/cli/v2/pkg/cmd/repo/rename => /cli/cli/v2/pkg/cmd/repo/rename

View file

@ -0,0 +1,2 @@
package rename

View file

@ -3,10 +3,15 @@ package rename
import (
"fmt"
"net/http"
"strings"
// "strings"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/internal/ghinstance"
// "github.com/cli/cli/v2/internal/ghinstance"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
@ -16,20 +21,23 @@ import (
type RenameOptions struct{
HttpClient func() (*http.Client, error)
IO *iostreams.IOStreams
Config func() (config.Config, error)
RepoName string
}
func MewCmcRename(f *cmdutil.Factory, runf func(*RenameOptions) error) *cobra.Command {
func NewCmdRename(f *cmdutil.Factory, runf func(*RenameOptions) error) *cobra.Command {
opts:= &RenameOptions {
IO: f.IOStreams,
HttpClient: f.HttpClient,
}
cmd := &cobra.Command{
Use: "rename <user/repo> <user/repo_change",
DisableFlagsInUseLine: true,
Use: "rename <user/repo_name>",
Short: "Rename a repository",
Long: "Rename a GitHub repository",
Args: cmdutil.ExactArgs(2, "cannot rename: repository argument required"),
Args: cmdutil.ExactArgs(1, "cannot rename: repository argument required"),
RunE: func (cmd *cobra.Command, args []string) error {
opts.RepoName = args[0]
if runf != nil {
@ -43,31 +51,32 @@ func MewCmcRename(f *cmdutil.Factory, runf func(*RenameOptions) error) *cobra.Co
func renameRun(opts *RenameOptions) error {
cs := opts.IO.ColorScheme()
// cs := opts.IO.ColorScheme()
httpClient, err := opts.HttpClient()
if err != nil {
return err
}
apiClient := api.NewClientFromHTTP(httpClient);
var toRename ghrepo.Interface
repoURL := opts.RepoName
if !strings.Contains(repoURL, "/") {
currentUser, err := api.CurrentLoginName(apiClient, ghinstance.Default())
if err != nil {
return err
}
repoURL = currentUser + "/" + repoURL
username, err := api.CurrentLoginName(apiClient, ghinstance.Default())
if err != nil {
return err
}
toRename, err = ghrepo.FromFullName(repoURL)
toRename, err := ghrepo.FromFullName(opts.RepoName)
if err != nil {
return fmt.Errorf("argument error: %w", err)
}
fields := []string{"name", "owner", "id"}
repo, err := api.FetchRepository(apiClient, toRename, fields)
if err != nil {
return err
}
repo, err :=
if username != repo.Owner.Login {
return fmt.Errorf("you do not own this repository");
}
return nil
}

View file

@ -10,6 +10,7 @@ import (
repoListCmd "github.com/cli/cli/v2/pkg/cmd/repo/list"
repoSyncCmd "github.com/cli/cli/v2/pkg/cmd/repo/sync"
repoViewCmd "github.com/cli/cli/v2/pkg/cmd/repo/view"
repoRenameCmd "github.com/cli/cli/v2/pkg/cmd/repo/rename"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/spf13/cobra"
)
@ -42,6 +43,7 @@ func NewCmdRepo(f *cmdutil.Factory) *cobra.Command {
cmd.AddCommand(repoSyncCmd.NewCmdSync(f, nil))
cmd.AddCommand(creditsCmd.NewCmdRepoCredits(f, nil))
cmd.AddCommand(gardenCmd.NewCmdGarden(f, nil))
cmd.AddCommand(repoRenameCmd.NewCmdRename(f, nil))
return cmd
}