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

Commit ff8153e

Browse files
committed
Renaming to smth that makes more sense
1 parent ac34c8a commit ff8153e

File tree

2 files changed

+87
-81
lines changed

2 files changed

+87
-81
lines changed

cmd/create.go renamed to cmd/create_repo.go

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import (
66

77
"github.com/deiwin/interact"
88
"github.com/hellofresh/github-cli/pkg/pullapprove"
9-
10-
"github.com/hellofresh/github-cli/pkg/github"
9+
"github.com/hellofresh/github-cli/pkg/repo"
1110
log "github.com/sirupsen/logrus"
1211
"github.com/spf13/cobra"
1312
)
@@ -36,7 +35,8 @@ var (
3635
}
3736
)
3837

39-
func RunCreate(cmd *cobra.Command, args []string) {
38+
// RunCreateRepo runs the command to create a new repository
39+
func RunCreateRepo(cmd *cobra.Command, args []string) {
4040
var err error
4141

4242
actor := interact.NewActor(os.Stdin, os.Stdout)
@@ -50,43 +50,38 @@ func RunCreate(cmd *cobra.Command, args []string) {
5050
log.Fatal(err)
5151
}
5252

53-
opts := &github.HelloFreshRepoOpt{
54-
Token: globalConfig.Github.Token,
53+
opts := &repo.HelloFreshRepoOpt{
54+
Name: repoName,
55+
Org: org,
5556
Private: createRepoFlags.Private,
56-
PullApprove: &github.PullApproveRule{
57+
PullApprove: &repo.PullApproveRule{
5758
Enabled: createRepoFlags.HasPullApprove,
5859
Filename: globalConfig.PullApprove.Filename,
5960
ProtectedBranchName: globalConfig.PullApprove.ProtectedBranchName,
6061
Client: pullapprove.New(globalConfig.PullApprove.Token),
6162
},
62-
Labels: &github.LabelsRule{
63+
Labels: &repo.LabelsRule{
6364
Enabled: createRepoFlags.HasLabels,
6465
RemoveDefaultLabels: globalConfig.Github.RemoveDefaultLabels,
6566
Labels: globalConfig.Github.Labels,
6667
},
67-
Teams: &github.TeamsRule{
68+
Teams: &repo.TeamsRule{
6869
Enabled: createRepoFlags.HasTeams,
6970
Teams: globalConfig.Github.Teams,
7071
},
71-
Webhooks: &github.WebhooksRule{
72+
Webhooks: &repo.WebhooksRule{
7273
Enabled: createRepoFlags.HasWebhooks,
7374
Webhooks: globalConfig.Github.Webhooks,
7475
},
75-
BranchProtections: &github.BranchProtectionsRule{
76+
BranchProtections: &repo.BranchProtectionsRule{
7677
Enabled: createRepoFlags.HasBranchProtections,
7778
Protections: globalConfig.Github.Protections,
7879
},
7980
}
8081

81-
creator, err := github.NewHelloFreshRepoCreator(repoName, org, opts)
82-
if err != nil {
83-
log.WithError(err).Error("Could not create github client")
84-
os.Exit(1)
85-
}
86-
87-
err = creator.Create()
82+
creator := repo.NewGithub(githubClient)
83+
err = creator.Create(opts)
8884
if err != nil {
89-
log.WithError(err).Error("Could not create github repo")
90-
os.Exit(1)
85+
log.WithError(err).Fatal("Could not create github repo")
9186
}
9287
}
Lines changed: 73 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package github
1+
package repo
22

33
import (
44
"context"
@@ -8,30 +8,27 @@ import (
88
"github.com/hellofresh/github-cli/pkg/pullapprove"
99
"github.com/pkg/errors"
1010
log "github.com/sirupsen/logrus"
11-
"golang.org/x/oauth2"
1211
)
1312

1413
type (
1514
// RepoCreateor is used as an aggregator of rules to setup your repository
1615
RepoCreateor interface {
17-
Create() error
16+
Create(opts *HelloFreshRepoOpt) error
1817
}
1918

20-
// HelloFreshRepoCreator contains all the hellofresh repository creation rules
21-
HelloFreshRepoCreator struct {
19+
// GithubRepo contains all the hellofresh repository creation rules for github
20+
GithubRepo struct {
2221
GithubClient *github.Client
23-
Name string
24-
Org string
25-
Private bool
26-
opts *HelloFreshRepoOpt
2722
}
2823

2924
// HelloFreshRepoOpt represents the repo creation options
3025
HelloFreshRepoOpt struct {
31-
Token string
26+
Name string
27+
Org string
3228
Private bool
3329
PullApprove *PullApproveRule
3430
Teams *TeamsRule
31+
Collaborators *CollaboratorsRule
3532
Labels *LabelsRule
3633
Webhooks *WebhooksRule
3734
BranchProtections *BranchProtectionsRule
@@ -49,6 +46,11 @@ type (
4946
Teams []*config.Team
5047
}
5148

49+
CollaboratorsRule struct {
50+
Enabled bool
51+
Collaborators []*config.Collaborator
52+
}
53+
5254
LabelsRule struct {
5355
Enabled bool
5456
RemoveDefaultLabels bool
@@ -68,69 +70,64 @@ type (
6870

6971
var ctx = context.Background()
7072

71-
// NewHelloFreshRepoCreator creates a new instance of Client
72-
func NewHelloFreshRepoCreator(name string, org string, opts *HelloFreshRepoOpt) (*HelloFreshRepoCreator, error) {
73-
if opts.Token == "" {
74-
return nil, errors.New("You must provide a github token")
73+
// NewGithub creates a new instance of Client
74+
func NewGithub(githubClient *github.Client) *GithubRepo {
75+
return &GithubRepo{
76+
GithubClient: githubClient,
7577
}
76-
77-
ts := oauth2.StaticTokenSource(
78-
&oauth2.Token{AccessToken: opts.Token},
79-
)
80-
tc := oauth2.NewClient(ctx, ts)
81-
82-
return &HelloFreshRepoCreator{
83-
Name: name,
84-
Org: org,
85-
Private: opts.Private,
86-
GithubClient: github.NewClient(tc),
87-
opts: opts,
88-
}, nil
8978
}
9079

9180
// Create aggregates all rules to create a repository with all necessary requirements
92-
func (c *HelloFreshRepoCreator) Create() error {
81+
func (c *GithubRepo) Create(opts *HelloFreshRepoOpt) error {
9382
log.Info("Creating repository...")
94-
err := c.createRepo()
83+
err := c.createRepo(opts.Name, opts.Org, opts.Private)
9584
if err != nil {
9685
return errors.Wrap(err, "could not create repository")
9786
}
9887

99-
if c.opts.PullApprove.Enabled {
88+
if opts.PullApprove != nil && opts.PullApprove.Enabled {
10089
log.Info("Adding pull approve...")
101-
err = c.addPullApprove()
90+
err = c.addPullApprove(opts.Name, opts.Org, opts.PullApprove)
10291
if err != nil {
10392
return errors.Wrap(err, "could not add pull approve")
10493
}
10594
}
10695

107-
if c.opts.Teams.Enabled {
96+
if opts.Teams != nil && opts.Teams.Enabled {
10897
log.Info("Adding teams to repository...")
109-
err = c.addTeamsToRepo()
98+
err = c.addTeamsToRepo(opts.Name, opts.Org, opts.Teams)
11099
if err != nil {
111100
return errors.Wrap(err, "could add teams to repository")
112101
}
113102
}
114103

115-
if c.opts.Labels.Enabled {
104+
if opts.Collaborators != nil && opts.Collaborators.Enabled {
105+
log.Info("Adding collaborators to repository...")
106+
err = c.addCollaborators(opts.Name, opts.Org, opts.Collaborators)
107+
if err != nil {
108+
return errors.Wrap(err, "could not add collaborators to repository")
109+
}
110+
}
111+
112+
if opts.Labels != nil && opts.Labels.Enabled {
116113
log.Info("Adding labels to repository...")
117-
err = c.addLabelsToRepo()
114+
err = c.addLabelsToRepo(opts.Name, opts.Org, opts.Labels)
118115
if err != nil {
119116
return errors.Wrap(err, "could add labels to repository")
120117
}
121118
}
122119

123-
if c.opts.Webhooks.Enabled {
120+
if opts.Webhooks != nil && opts.Webhooks.Enabled {
124121
log.Info("Adding webhooks to repository...")
125-
err = c.addWebhooksToRepo()
122+
err = c.addWebhooksToRepo(opts.Name, opts.Org, opts.Webhooks)
126123
if err != nil {
127124
return errors.Wrap(err, "could add webhooks to repository")
128125
}
129126
}
130127

131-
if c.opts.BranchProtections.Enabled {
128+
if opts.BranchProtections != nil && opts.BranchProtections.Enabled {
132129
log.Info("Adding branch protections to repository...")
133-
err = c.addBranchProtections()
130+
err = c.addBranchProtections(opts.Name, opts.Org, opts.BranchProtections)
134131
if err != nil {
135132
return errors.Wrap(err, "could add branch protections to repository")
136133
}
@@ -140,100 +137,114 @@ func (c *HelloFreshRepoCreator) Create() error {
140137
return nil
141138
}
142139

143-
func (c *HelloFreshRepoCreator) createRepo() error {
140+
func (c *GithubRepo) createRepo(name string, org string, private bool) error {
144141
repo := &github.Repository{
145-
Name: github.String(c.Name),
146-
Private: github.Bool(c.Private),
142+
Name: github.String(name),
143+
Private: github.Bool(private),
147144
HasIssues: github.Bool(true),
148145
}
149146

150-
_, _, err := c.GithubClient.Repositories.Create(ctx, c.Org, repo)
147+
_, _, err := c.GithubClient.Repositories.Create(ctx, org, repo)
151148
return err
152149
}
153150

154-
func (c *HelloFreshRepoCreator) addPullApprove() error {
155-
if c.opts.PullApprove.Client == nil {
151+
func (c *GithubRepo) addPullApprove(repo string, org string, opts *PullApproveRule) error {
152+
if opts.Client == nil {
156153
return errors.New("Cannot add pull approve, since the client is nil")
157154
}
158155

159156
fileOpt := &github.RepositoryContentFileOptions{
160157
Message: github.String("Initialize repository :tada:"),
161158
Content: []byte("extends: hellofresh"),
162-
Branch: github.String(c.opts.PullApprove.ProtectedBranchName),
159+
Branch: github.String(opts.ProtectedBranchName),
163160
}
164-
_, _, err := c.GithubClient.Repositories.CreateFile(ctx, c.Org, c.Name, c.opts.PullApprove.Filename, fileOpt)
161+
_, _, err := c.GithubClient.Repositories.CreateFile(ctx, org, repo, opts.Filename, fileOpt)
165162
if err != nil {
166163
return err
167164
}
168165

169-
err = c.opts.PullApprove.Client.Create(c.Name, c.Org)
166+
err = opts.Client.Create(repo, org)
170167
if err != nil {
171168
return err
172169
}
173170

174171
return nil
175172
}
176173

177-
func (c *HelloFreshRepoCreator) addTeamsToRepo() error {
174+
func (c *GithubRepo) addTeamsToRepo(repo string, org string, opts *TeamsRule) error {
178175
var err error
179176

180-
for _, team := range c.opts.Teams.Teams {
177+
for _, team := range opts.Teams {
181178
opt := &github.OrganizationAddTeamRepoOptions{
182179
Permission: team.Permission,
183180
}
184181

185-
_, err = c.GithubClient.Organizations.AddTeamRepo(ctx, team.ID, c.Org, c.Name, opt)
182+
_, err = c.GithubClient.Organizations.AddTeamRepo(ctx, team.ID, org, repo, opt)
186183
}
187184

188185
return err
189186
}
190187

191-
func (c *HelloFreshRepoCreator) addLabelsToRepo() error {
188+
func (c *GithubRepo) addLabelsToRepo(repo string, org string, opts *LabelsRule) error {
192189
var err error
193190
defaultLabels := []string{"bug", "duplicate", "enhancement", "help wanted", "invalid", "question", "wontfix", "good first issue"}
194191

195-
for _, label := range c.opts.Labels.Labels {
192+
for _, label := range opts.Labels {
196193
githubLabel := &github.Label{
197194
Name: github.String(label.Name),
198195
Color: github.String(label.Color),
199196
}
200197

201-
_, _, err = c.GithubClient.Issues.CreateLabel(ctx, c.Org, c.Name, githubLabel)
198+
_, _, err = c.GithubClient.Issues.CreateLabel(ctx, org, repo, githubLabel)
202199
}
203200

204-
if c.opts.Labels.RemoveDefaultLabels {
201+
if opts.RemoveDefaultLabels {
205202
for _, label := range defaultLabels {
206-
_, err = c.GithubClient.Issues.DeleteLabel(ctx, c.Org, c.Name, label)
203+
_, err = c.GithubClient.Issues.DeleteLabel(ctx, org, repo, label)
207204
}
208205
}
209206

210207
return err
211208
}
212209

213-
func (c *HelloFreshRepoCreator) addWebhooksToRepo() error {
210+
func (c *GithubRepo) addWebhooksToRepo(repo string, org string, opts *WebhooksRule) error {
214211
var err error
215212

216-
for _, webhook := range c.opts.Webhooks.Webhooks {
213+
for _, webhook := range opts.Webhooks {
217214
hook := &github.Hook{
218215
Name: github.String(webhook.Type),
219216
Config: webhook.Config,
220217
}
221-
_, _, err = c.GithubClient.Repositories.CreateHook(ctx, c.Org, c.Name, hook)
218+
_, _, err = c.GithubClient.Repositories.CreateHook(ctx, org, repo, hook)
222219
}
223220

224221
return err
225222
}
226223

227-
func (c *HelloFreshRepoCreator) addBranchProtections() error {
224+
func (c *GithubRepo) addBranchProtections(repo string, org string, opts *BranchProtectionsRule) error {
228225
var err error
229226

230-
for branch, contexts := range c.opts.BranchProtections.Protections {
227+
for branch, contexts := range opts.Protections {
231228
pr := &github.ProtectionRequest{
232229
RequiredStatusChecks: &github.RequiredStatusChecks{
233230
Contexts: contexts,
234231
},
235232
}
236-
_, _, err = c.GithubClient.Repositories.UpdateBranchProtection(ctx, c.Org, c.Name, branch, pr)
233+
_, _, err = c.GithubClient.Repositories.UpdateBranchProtection(ctx, org, repo, branch, pr)
234+
}
235+
236+
return err
237+
}
238+
239+
func (c *GithubRepo) addCollaborators(repo string, org string, opts *CollaboratorsRule) error {
240+
var err error
241+
242+
for _, collaborator := range opts.Collaborators {
243+
opt := &github.RepositoryAddCollaboratorOptions{
244+
Permission: collaborator.Permission,
245+
}
246+
247+
_, err = c.GithubClient.Repositories.AddCollaborator(ctx, org, repo, collaborator.Username, opt)
237248
}
238249

239250
return err

0 commit comments

Comments
 (0)