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

Commit 69fb077

Browse files
committed
Better desing
1 parent 6071607 commit 69fb077

File tree

6 files changed

+319
-245
lines changed

6 files changed

+319
-245
lines changed

cmd/create_repo.go

Lines changed: 79 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,125 @@
11
package cmd
22

33
import (
4-
"errors"
5-
"os"
6-
7-
"github.com/deiwin/interact"
4+
"github.com/fatih/color"
85
"github.com/hellofresh/github-cli/pkg/pullapprove"
96
"github.com/hellofresh/github-cli/pkg/repo"
10-
log "github.com/sirupsen/logrus"
7+
"github.com/pkg/errors"
118
"github.com/spf13/cobra"
129
)
1310

1411
type (
1512
// CreateRepoFlags are the flags for the create repository command
1613
CreateRepoFlags struct {
14+
Name string
15+
Description string
16+
Org string
1717
Private bool
1818
HasPullApprove bool
1919
HasTeams bool
20+
HasCollaborators bool
2021
HasLabels bool
2122
HasDefaultLabels bool
2223
HasWebhooks bool
2324
HasBranchProtections bool
2425
}
2526
)
2627

27-
var (
28-
createRepoFlags CreateRepoFlags
29-
checkNotEmpty = func(input string) error {
30-
// note that the inputs provided to these checks are already trimmed
31-
if input == "" {
32-
return errors.New("input should not be empty")
33-
}
34-
return nil
35-
}
36-
)
28+
var createRepoFlags CreateRepoFlags
29+
30+
func init() {
31+
repoCmd.AddCommand(createRepoCmd)
32+
33+
createRepoCmd.Flags().StringVarP(&createRepoFlags.Name, "name", "n", "", "The name of the repository")
34+
createRepoCmd.Flags().StringVarP(&createRepoFlags.Description, "description", "d", "", "The repository's description")
35+
createRepoCmd.Flags().StringVarP(&createRepoFlags.Org, "organization", "o", "", "Github's organization")
36+
createRepoCmd.Flags().BoolVar(&createRepoFlags.Private, "private", true, "Is the repository private?")
37+
createRepoCmd.Flags().BoolVar(&createRepoFlags.HasPullApprove, "add-pullapprove", true, "Enables pull approve")
38+
createRepoCmd.Flags().BoolVar(&createRepoFlags.HasTeams, "add-teams", true, "Enable teams")
39+
createRepoCmd.Flags().BoolVar(&createRepoFlags.HasLabels, "add-labels", true, "Enable labels")
40+
createRepoCmd.Flags().BoolVar(&createRepoFlags.HasDefaultLabels, "add-default-labels", true, "Removes the default github labels")
41+
createRepoCmd.Flags().BoolVar(&createRepoFlags.HasWebhooks, "add-webhooks", true, "Enables webhooks configurations")
42+
createRepoCmd.Flags().BoolVar(&createRepoFlags.HasBranchProtections, "add-branch-protections", true, "Enables branch protections")
43+
}
3744

3845
// RunCreateRepo runs the command to create a new repository
3946
func RunCreateRepo(cmd *cobra.Command, args []string) {
4047
var err error
4148

42-
actor := interact.NewActor(os.Stdin, os.Stdout)
43-
repoName, err := actor.Prompt("Please enter the repository name", checkNotEmpty)
44-
if err != nil {
45-
log.Fatal(err)
46-
}
49+
name := createRepoFlags.Name
50+
checkEmpty(name, "Please provide a repository name")
51+
52+
description := createRepoFlags.Description
4753

48-
org, err := actor.PromptOptional("Please enter the org name", globalConfig.Github.Organization, checkNotEmpty)
49-
if err != nil {
50-
log.Fatal(err)
54+
org := createRepoFlags.Org
55+
if org == "" {
56+
org = globalConfig.Github.Organization
5157
}
58+
checkEmpty(org, "Please provide an organization")
5259

53-
opts := &repo.HelloFreshRepoOpt{
54-
Name: repoName,
55-
Org: org,
56-
Private: createRepoFlags.Private,
57-
PullApprove: &repo.PullApproveRule{
58-
Enabled: createRepoFlags.HasPullApprove,
60+
opts := &repo.GithubRepoOpts{
61+
PullApprove: &repo.PullApproveOpts{
5962
Filename: globalConfig.PullApprove.Filename,
6063
ProtectedBranchName: globalConfig.PullApprove.ProtectedBranchName,
6164
Client: pullapprove.New(globalConfig.PullApprove.Token),
6265
},
63-
Labels: &repo.LabelsRule{
64-
Enabled: createRepoFlags.HasLabels,
66+
Labels: &repo.LabelsOpts{
6567
RemoveDefaultLabels: globalConfig.Github.RemoveDefaultLabels,
6668
Labels: globalConfig.Github.Labels,
6769
},
68-
Teams: &repo.TeamsRule{
69-
Enabled: createRepoFlags.HasTeams,
70-
Teams: globalConfig.Github.Teams,
70+
Teams: &repo.TeamsOpts{
71+
Teams: globalConfig.Github.Teams,
7172
},
72-
Webhooks: &repo.WebhooksRule{
73-
Enabled: createRepoFlags.HasWebhooks,
73+
Webhooks: &repo.WebhooksOpts{
7474
Webhooks: globalConfig.Github.Webhooks,
7575
},
76-
BranchProtections: &repo.BranchProtectionsRule{
77-
Enabled: createRepoFlags.HasBranchProtections,
76+
BranchProtections: &repo.BranchProtectionsOpts{
7877
Protections: globalConfig.Github.Protections,
7978
},
8079
}
8180

8281
creator := repo.NewGithub(githubClient)
83-
err = creator.Create(opts)
84-
if err != nil {
85-
log.WithError(err).Fatal("Could not create github repo")
82+
83+
color.White("Creating repository...")
84+
err = creator.CreateRepo(name, description, org, createRepoFlags.Private)
85+
checkEmpty(errors.Wrap(err, "could not create repository"), "")
86+
87+
if createRepoFlags.HasPullApprove {
88+
color.White("Adding pull approve...")
89+
err = creator.AddPullApprove(name, org, opts.PullApprove)
90+
checkEmpty(errors.Wrap(err, "could not add pull approve"), "")
8691
}
92+
93+
if createRepoFlags.HasTeams {
94+
color.White("Adding teams to repository...")
95+
err = creator.AddTeamsToRepo(name, org, opts.Teams)
96+
checkEmpty(errors.Wrap(err, "could add teams to repository"), "")
97+
}
98+
99+
if createRepoFlags.HasCollaborators {
100+
color.White("Adding collaborators to repository...")
101+
err = creator.AddCollaborators(name, org, opts.Collaborators)
102+
checkEmpty(errors.Wrap(err, "could not add collaborators to repository"), "")
103+
}
104+
105+
if createRepoFlags.HasLabels {
106+
color.White("Adding labels to repository...")
107+
err = creator.AddLabelsToRepo(name, org, opts.Labels)
108+
checkEmpty(errors.Wrap(err, "could add labels to repository"), "")
109+
}
110+
111+
if createRepoFlags.HasWebhooks {
112+
color.White("Adding webhooks to repository...")
113+
err = creator.AddWebhooksToRepo(name, org, opts.Webhooks)
114+
checkEmpty(errors.Wrap(err, "could add webhooks to repository"), "")
115+
}
116+
117+
if createRepoFlags.HasBranchProtections {
118+
color.White("Adding branch protections to repository...")
119+
err = creator.AddBranchProtections(name, org, opts.BranchProtections)
120+
checkEmpty(errors.Wrap(err, "could add branch protections to repository"), "")
121+
}
122+
123+
color.Green("Repository created!")
124+
checkEmpty(err, "Could not create github repo")
87125
}

cmd/create_test_repo.go

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,96 +6,101 @@ import (
66
"os"
77
"os/user"
88

9-
"github.com/deiwin/interact"
9+
"github.com/fatih/color"
1010
"github.com/hellofresh/github-cli/pkg/config"
1111
"github.com/hellofresh/github-cli/pkg/repo"
12-
log "github.com/sirupsen/logrus"
12+
"github.com/pkg/errors"
1313
"github.com/spf13/cobra"
1414
"gopkg.in/src-d/go-git.v4"
1515
"gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
1616
"gopkg.in/src-d/go-git.v4/storage/memory"
1717
)
1818

19+
type (
20+
// CreateTestRepoFlags are the flags for the create a hiring test repository command
21+
CreateTestRepoFlags struct {
22+
Org string
23+
Candidate string
24+
TestRepo string
25+
}
26+
)
27+
28+
var createTestRepoFlags CreateTestRepoFlags
29+
30+
func init() {
31+
testsCmd.AddCommand(createTestCmd)
32+
33+
createTestCmd.Flags().StringVarP(&createTestRepoFlags.TestRepo, "test-repo", "r", "", "The name of the test repository to clone from")
34+
createTestCmd.Flags().StringVarP(&createTestRepoFlags.Candidate, "username", "u", "", "The github's name of the candidate")
35+
createTestCmd.Flags().StringVarP(&createTestRepoFlags.Org, "organization", "o", "", "Github's organization")
36+
}
37+
1938
// RunCreateTestRepo runs the command to create a new hiring test repository
2039
func RunCreateTestRepo(cmd *cobra.Command, args []string) {
2140
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-
}
2741

28-
candidate, err := actor.Prompt("GitHub username of the candidate", checkNotEmpty)
29-
if err != nil {
30-
log.Fatal(err)
42+
org := createTestRepoFlags.Org
43+
if org == "" {
44+
org = globalConfig.Github.Organization
3145
}
46+
checkEmpty(org, "Please provide an organization")
3247

33-
testRepo, err := actor.Prompt("Name of the repo with the test", checkNotEmpty)
34-
if err != nil {
35-
log.Fatal(err)
36-
}
48+
candidate := createTestRepoFlags.Candidate
49+
checkEmpty(org, "Please provide a candidate username")
50+
51+
testRepo := createTestRepoFlags.TestRepo
52+
checkEmpty(org, "Please provide a test repository")
3753

3854
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-
},
55+
56+
creator := repo.NewGithub(githubClient)
57+
58+
color.White("Creating repository...")
59+
err = creator.CreateRepo(target, "", org, true)
60+
checkEmpty(err, "Could not create github repo for candidate")
61+
62+
color.White("Adding collaborators to repository...")
63+
opts := &repo.CollaboratorsOpts{
64+
Collaborators: []*config.Collaborator{
65+
&config.Collaborator{
66+
Username: candidate,
67+
Permission: "push",
5068
},
5169
},
5270
}
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-
}
71+
err = creator.AddCollaborators(target, org, opts)
72+
checkEmpty(errors.Wrap(err, "could not add collaborators to repository"), "")
5873

5974
if globalConfig.PublicKeyPath == "" {
6075
user, _ := user.Current()
6176
globalConfig.PublicKeyPath = fmt.Sprintf("%s/.ssh/id_rsa", user.HomeDir)
6277
}
6378

6479
sshKey, err := ioutil.ReadFile(globalConfig.PublicKeyPath)
65-
if err != nil {
66-
log.WithError(err).Fatal("Error reading public key")
67-
}
80+
checkEmpty(err, "Error reading public key")
6881

6982
authMethod, err := ssh.NewPublicKeys("git", []byte(sshKey), "")
70-
if err != nil {
71-
log.WithError(err).Fatal("Error when creating public keys")
72-
}
83+
checkEmpty(err, "Error when creating public keys")
7384

74-
log.Info("Cloning repository...")
85+
color.White("Cloning repository...")
7586
r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
7687
Auth: authMethod,
7788
Progress: os.Stdout,
7889
URL: fmt.Sprintf("git@github.com:%s/%s", org, testRepo),
7990
})
80-
if err != nil {
81-
log.WithError(err).Fatal("Error cloning to repository")
82-
}
91+
checkEmpty(err, "Error cloning to repository")
8392

84-
log.Info("Changing remote...")
93+
color.White("Changing remote...")
8594
remote, err := r.Remote(git.DefaultRemoteName)
86-
if err != nil {
87-
log.WithError(err).Fatal("Error changing remote for repository")
88-
}
95+
checkEmpty(err, "Error changing remote for repository")
8996

90-
log.Info("Pushing changes...")
97+
color.White("Pushing changes...")
9198
remote.Config().URLs = []string{fmt.Sprintf("git@github.com:%s/%s", org, target)}
9299
err = remote.Push(&git.PushOptions{
93100
RemoteName: git.DefaultRemoteName,
94101
Progress: os.Stdout,
95102
})
96-
if err != nil {
97-
log.WithError(err).Fatal("Error pushing to repository")
98-
}
103+
checkEmpty(err, "Error pushing to repository")
99104

100-
log.Infof("Done! Test for %s is created", candidate)
105+
color.Green("Done! Hiring test for %s is created", candidate)
101106
}

0 commit comments

Comments
 (0)