Skip to content

Commit

Permalink
feat: add transfer command (profclems#929)
Browse files Browse the repository at this point in the history
* Add command for transferring repositories

* minor fixes

Co-authored-by: Clement Sam <clementsam75@gmail.com>
  • Loading branch information
x3ro and profclems committed Jan 29, 2022
1 parent 2ccdff9 commit 127e4df
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
24 changes: 24 additions & 0 deletions commands/cmdutils/cmdutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,3 +530,27 @@ func (ua *UserAssignments) UsersFromAddRemove(
}
return assignedIDs, actions, nil
}

func ConfirmTransfer() error {
const (
performTransferLabel = "Confirm repository transfer"
abortTransferLabel = "Abort repository transfer"
)

options := []string{abortTransferLabel, performTransferLabel}

var confirmTransfer string
err := prompt.Select(&confirmTransfer, "confirmation", "Do you wish to proceed with the repository transfer?", options)
if err != nil {
return fmt.Errorf("could not prompt: %w", err)
}

switch confirmTransfer {
case performTransferLabel:
return nil
case abortTransferLabel:
return fmt.Errorf("user aborted operation")
default:
return fmt.Errorf("invalid value: %s", confirmTransfer)
}
}
2 changes: 2 additions & 0 deletions commands/project/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
repoCmdList "github.com/profclems/glab/commands/project/list"
repoCmdMirror "github.com/profclems/glab/commands/project/mirror"
repoCmdSearch "github.com/profclems/glab/commands/project/search"
repoCmdTransfer "github.com/profclems/glab/commands/project/transfer"
repoCmdView "github.com/profclems/glab/commands/project/view"

"github.com/spf13/cobra"
Expand All @@ -32,6 +33,7 @@ func NewCmdRepo(f *cmdutils.Factory) *cobra.Command {
repoCmd.AddCommand(repoCmdDelete.NewCmdDelete(f))
repoCmd.AddCommand(repoCmdFork.NewCmdFork(f, nil))
repoCmd.AddCommand(repoCmdSearch.NewCmdSearch(f))
repoCmd.AddCommand(repoCmdTransfer.NewCmdTransfer(f))
repoCmd.AddCommand(repoCmdView.NewCmdView(f))
repoCmd.AddCommand(repoCmdMirror.NewCmdMirror(f))

Expand Down
93 changes: 93 additions & 0 deletions commands/project/transfer/project_transfer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package transfer

import (
"fmt"

"github.com/MakeNowJust/heredoc"
"github.com/profclems/glab/commands/cmdutils"
"github.com/spf13/cobra"
"github.com/xanzy/go-gitlab"
)

func NewCmdTransfer(f *cmdutils.Factory) *cobra.Command {
var repoTransferCmd = &cobra.Command{
Use: "transfer [repo] [flags]",
Short: `Transfer a repository to a new namespace.`,
Example: heredoc.Doc(`
$ glab repo transfer profclems/glab --target-namespace notprofclems
`),
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var err error

if len(args) != 0 {
err = f.RepoOverride(args[0])
if err != nil {
return err
}
}

apiClient, err := f.HttpClient()
if err != nil {
return err
}

repo, err := f.BaseRepo()
if err != nil {
return err
}

dontPromptForConfirmation, err := cmd.Flags().GetBool("yes")
if err != nil {
return err
}

targetNamespace, err := cmd.Flags().GetString("target-namespace")
if err != nil {
return err
}

c := f.IO.Color()
fmt.Printf(heredoc.Doc(`
🔴 Danger 🔴
The operation you are about to perform is potentially irreversible.
You will lose control of the repository you are transferring in case you do not
have access to the target namespace. In addition, you won't be able to transfer
the repository back to the original namespace unless you have administrative access
to the target namespace.
Source repository: %s
Target namespace: %s
`), c.Yellow(repo.FullName()), c.Yellow(targetNamespace))

if !dontPromptForConfirmation {
err = cmdutils.ConfirmTransfer()
if err != nil {
return fmt.Errorf("unable to confirm: %w", err)
}
}

opt := &gitlab.TransferProjectOptions{}
opt.Namespace = targetNamespace

project, _, err := apiClient.Projects.TransferProject(repo.FullName(), opt)
if err != nil {
return err
}

fmt.Fprintf(f.IO.StdOut, "%s Successfully transferred repository %s to %s\n",
c.GreenCheck(), c.Yellow(repo.FullName()), c.Yellow(project.PathWithNamespace))

return nil
},
}

repoTransferCmd.Flags().BoolP("yes", "y", false, "Danger: Skip confirmation prompt and force transfer operation. Transfer cannot be undone.")
repoTransferCmd.Flags().StringP("target-namespace", "t", "", "The namespace where your project should be transferred to")

_ = repoTransferCmd.MarkFlagRequired("target-namespace")

return repoTransferCmd
}

0 comments on commit 127e4df

Please sign in to comment.