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

Commit c625d65

Browse files
authored
Merge pull request #3 from hellofresh/feature/improvements
Added Improvements
2 parents 9ba97c1 + 69fb077 commit c625d65

File tree

10 files changed

+355
-252
lines changed

10 files changed

+355
-252
lines changed

.github.sample.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ LogLevel="debug"
2020
# Defines the github token
2121
Token=""
2222

23-
# Defines permission specifies the permission to grant the user on this repository.
23+
# Defines permission specifies the permission to grant the team on this repository.
2424
# Possible values are:
2525
# pull - team members can pull, but not push to or administer this repository
2626
# push - team members can pull and push, but not administer this repository
@@ -30,6 +30,12 @@ LogLevel="debug"
3030
{ID=1234, Permission='pull'},
3131
]
3232

33+
# Defines permission specifies the permission to grant the user on this repository.
34+
Collaborators=[
35+
#Example
36+
{Username="example", Permission='push'},
37+
]
38+
3339
# Adds a set of labels when creating the repo
3440
Labels=[
3541
{Name='custom', Color='ad974f'},

Gopkg.lock

Lines changed: 16 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@
3535
[[constraint]]
3636
name = "gopkg.in/src-d/go-git.v4"
3737
revision = "f9879dd043f84936a1f8acb8a53b74332a7ae135"
38+
39+
[[constraint]]
40+
name = "github.com/fatih/color"
41+
version = "1.5.0"

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,19 @@ After you have *github-cli* up and running we can create our first repository.
2121
First of all we have to create a configuration file that will customize how our repositories will be created. You can have a look on our [example](.github.sample.toml).
2222
This file can be placed on the same folder as your binary is, or in your home directory and it should be named `.github.toml` (You can also use it as `yaml` or `json`).
2323

24+
## Usage
25+
26+
```
27+
github-cli [command] [--flags]
28+
```
29+
2430
### Commands
2531

2632
| Command | Description |
2733
|--------------------------|--------------------------------------|
28-
| `github-cli create-repo` | Creates a new github repository |
29-
| `github-cli create-test` | Creates a new hellofresh hiring test |
34+
| `github-cli create repo [--flags]` | Creates a new github repository |
35+
| `github-cli create test [--flags]` | Creates a new hellofresh hiring test |
36+
| `github-cli unseat [--flags]` | Removes external collaborators from repositories |
3037

3138
## Contributing
3239

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
}

0 commit comments

Comments
 (0)