1- package github
1+ package repo
22
33import (
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
1413type (
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
6971var 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