Skip to content
This repository was archived by the owner on Sep 6, 2025. It is now read-only.

Commit 92653d0

Browse files
committed
Added new command
1 parent ff8153e commit 92653d0

File tree

3 files changed

+142
-11
lines changed

3 files changed

+142
-11
lines changed

cmd/create_test_repo.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
}

cmd/root.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
package cmd
22

33
import (
4+
"context"
45
"os"
56

7+
"github.com/google/go-github/github"
68
"github.com/hellofresh/github-cli/pkg/config"
79
log "github.com/sirupsen/logrus"
810
"github.com/spf13/cobra"
11+
"golang.org/x/oauth2"
912
)
1013

1114
var (
1215
cfgFile string
1316
globalConfig *config.Spec
17+
githubClient *github.Client
1418
version string
1519
// RootCmd is our main command
1620
RootCmd = &cobra.Command{
@@ -41,14 +45,31 @@ var (
4145

4246
// Only log the warning severity or above.
4347
log.SetLevel(lvl)
48+
49+
if globalConfig.Github.Token == "" {
50+
log.Fatal("You must provide a github token")
51+
}
52+
53+
ts := oauth2.StaticTokenSource(
54+
&oauth2.Token{AccessToken: globalConfig.Github.Token},
55+
)
56+
tc := oauth2.NewClient(context.Background(), ts)
57+
githubClient = github.NewClient(tc)
4458
},
4559
}
4660

4761
createRepoCmd = &cobra.Command{
48-
Use: "create",
62+
Use: "create-repo",
4963
Short: "Creates a new github repository",
5064
Long: `Creates a new github repository based on the rules defined on your .github.toml`,
51-
Run: RunCreate,
65+
Run: RunCreateRepo,
66+
}
67+
68+
createTestCmd = &cobra.Command{
69+
Use: "create-test",
70+
Short: "Creates a new hellofresh hiring test",
71+
Long: `Creates a new hellofresh hiring test based on the rules defined on your .github.toml`,
72+
Run: RunCreateTestRepo,
5273
}
5374
)
5475

@@ -63,4 +84,5 @@ func init() {
6384
createRepoCmd.Flags().BoolVar(&createRepoFlags.HasWebhooks, "add-webhooks", true, "Enables webhooks configurations")
6485
createRepoCmd.Flags().BoolVar(&createRepoFlags.HasBranchProtections, "add-branch-protections", true, "Enables branch protections")
6586
RootCmd.AddCommand(createRepoCmd)
87+
RootCmd.AddCommand(createTestCmd)
6688
}

pkg/config/config.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import (
77
type (
88
// Spec represents the global app configuration
99
Spec struct {
10-
Github Github
11-
PullApprove PullApprove
12-
LogLevel string
10+
PublicKeyPath string
11+
Github Github
12+
PullApprove PullApprove
13+
LogLevel string
1314
}
1415

1516
// PullApprove represents teh Pull Approve configurations
@@ -21,12 +22,13 @@ type (
2122

2223
// Github represents the github configurations
2324
Github struct {
24-
Organization string
25-
Token string
26-
Teams []*Team
27-
Labels []*Label
28-
Webhooks []*Webhook
29-
Protections map[string][]string
25+
Organization string
26+
Token string
27+
Teams []*Team
28+
Collaborators []*Collaborator
29+
Labels []*Label
30+
Webhooks []*Webhook
31+
Protections map[string][]string
3032
// RemoveDefaultLabels Remove GitHub's default labels?
3133
RemoveDefaultLabels bool
3234
}
@@ -37,6 +39,12 @@ type (
3739
Permission string
3840
}
3941

42+
// Collaborator represents a github collaborator
43+
Collaborator struct {
44+
Username string
45+
Permission string
46+
}
47+
4048
// Label represents a github label
4149
Label struct {
4250
Name string

0 commit comments

Comments
 (0)