|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "os/user" |
| 8 | + |
| 9 | + "github.com/deiwin/interact" |
| 10 | + "github.com/hellofresh/github-cli/pkg/config" |
| 11 | + "github.com/hellofresh/github-cli/pkg/repo" |
| 12 | + log "github.com/sirupsen/logrus" |
| 13 | + "github.com/spf13/cobra" |
| 14 | + "gopkg.in/src-d/go-git.v4" |
| 15 | + "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh" |
| 16 | + "gopkg.in/src-d/go-git.v4/storage/memory" |
| 17 | +) |
| 18 | + |
| 19 | +// RunCreateTestRepo runs the command to create a new hiring test repository |
| 20 | +func RunCreateTestRepo(cmd *cobra.Command, args []string) { |
| 21 | + var err error |
| 22 | + actor := interact.NewActor(os.Stdin, os.Stdout) |
| 23 | + org, err := actor.PromptOptional("Please enter the org name", globalConfig.Github.Organization, checkNotEmpty) |
| 24 | + if err != nil { |
| 25 | + log.Fatal(err) |
| 26 | + } |
| 27 | + |
| 28 | + candidate, err := actor.Prompt("GitHub username of the candidate", checkNotEmpty) |
| 29 | + if err != nil { |
| 30 | + log.Fatal(err) |
| 31 | + } |
| 32 | + |
| 33 | + testRepo, err := actor.Prompt("Name of the repo with the test", checkNotEmpty) |
| 34 | + if err != nil { |
| 35 | + log.Fatal(err) |
| 36 | + } |
| 37 | + |
| 38 | + target := fmt.Sprintf("%s-%s", candidate, testRepo) |
| 39 | + opts := &repo.HelloFreshRepoOpt{ |
| 40 | + Name: target, |
| 41 | + Org: org, |
| 42 | + Private: true, |
| 43 | + Collaborators: &repo.CollaboratorsRule{ |
| 44 | + Enabled: true, |
| 45 | + Collaborators: []*config.Collaborator{ |
| 46 | + &config.Collaborator{ |
| 47 | + Username: candidate, |
| 48 | + Permission: "push", |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + } |
| 53 | + creator := repo.NewGithub(githubClient) |
| 54 | + err = creator.Create(opts) |
| 55 | + if err != nil { |
| 56 | + log.WithError(err).Fatal("Could not create github repo for candidate") |
| 57 | + } |
| 58 | + |
| 59 | + if globalConfig.PublicKeyPath == "" { |
| 60 | + user, _ := user.Current() |
| 61 | + globalConfig.PublicKeyPath = fmt.Sprintf("%s/.ssh/id_rsa", user.HomeDir) |
| 62 | + } |
| 63 | + |
| 64 | + sshKey, err := ioutil.ReadFile(globalConfig.PublicKeyPath) |
| 65 | + if err != nil { |
| 66 | + log.WithError(err).Fatal("Error reading public key") |
| 67 | + } |
| 68 | + |
| 69 | + authMethod, err := ssh.NewPublicKeys("git", []byte(sshKey), "") |
| 70 | + if err != nil { |
| 71 | + log.WithError(err).Fatal("Error when creating public keys") |
| 72 | + } |
| 73 | + |
| 74 | + log.Info("Cloning repository...") |
| 75 | + r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{ |
| 76 | + Auth: authMethod, |
| 77 | + Progress: os.Stdout, |
| 78 | + URL: fmt.Sprintf("git@github.com:%s/%s", org, testRepo), |
| 79 | + }) |
| 80 | + if err != nil { |
| 81 | + log.WithError(err).Fatal("Error cloning to repository") |
| 82 | + } |
| 83 | + |
| 84 | + log.Info("Changing remote...") |
| 85 | + remote, err := r.Remote(git.DefaultRemoteName) |
| 86 | + if err != nil { |
| 87 | + log.WithError(err).Fatal("Error changing remote for repository") |
| 88 | + } |
| 89 | + |
| 90 | + log.Info("Pushing changes...") |
| 91 | + remote.Config().URLs = []string{fmt.Sprintf("git@github.com:%s/%s", org, target)} |
| 92 | + err = remote.Push(&git.PushOptions{ |
| 93 | + RemoteName: git.DefaultRemoteName, |
| 94 | + Progress: os.Stdout, |
| 95 | + }) |
| 96 | + if err != nil { |
| 97 | + log.WithError(err).Fatal("Error pushing to repository") |
| 98 | + } |
| 99 | + |
| 100 | + log.Infof("Done! Test for %s is created", candidate) |
| 101 | +} |
0 commit comments