@@ -7,6 +7,7 @@ package gogit
77import (
88 "context"
99 "fmt"
10+ "strings"
1011
1112 "github.com/fluxcd/go-git-providers/gitprovider"
1213 deliveryv1alpha1 "github.com/open-component-model/git-controller/apis/delivery/v1alpha1"
@@ -37,22 +38,36 @@ func CreateOrganizationRepository(ctx context.Context, gc gitprovider.Client, do
3738 Visibility : & visibility ,
3839 }
3940
41+ createOpts , err := gitprovider .MakeRepositoryCreateOptions (& gitprovider.RepositoryCreateOptions {AutoInit : gitprovider .BoolVar (true )})
42+ if err != nil {
43+ return fmt .Errorf ("failed to create _create_ options for repository: %w" , err )
44+ }
45+
4046 switch spec .ExistingRepositoryPolicy {
4147 case mpasv1alpha1 .ExistingRepositoryPolicyFail :
42- if _ , err := gc .OrgRepositories ().Create (ctx , ref , info ); err != nil {
48+ repo , err := gc .OrgRepositories ().Create (ctx , ref , info , & createOpts )
49+ if err != nil {
4350 return fmt .Errorf ("failed to create repository: %w" , err )
4451 }
4552
53+ if err := createCodeownersFile (ctx , repo , spec .Maintainers ); err != nil {
54+ return fmt .Errorf ("failed to add CODEOWNERS file: %w" , err )
55+ }
56+
4657 logger .Info ("successfully created organization repository" , "domain" , domain , "repository" , spec .RepositoryName )
4758 case mpasv1alpha1 .ExistingRepositoryPolicyAdopt :
48- _ , created , err := gc .OrgRepositories ().Reconcile (ctx , ref , info )
59+ repo , created , err := gc .OrgRepositories ().Reconcile (ctx , ref , info , & createOpts )
4960 if err != nil {
5061 return fmt .Errorf ("failed to reconcile repository: %w" , err )
5162 }
5263
5364 if ! created {
5465 logger .Info ("using existing repository" , "domain" , domain , "repository" , spec .RepositoryName )
5566 } else {
67+ if err := createCodeownersFile (ctx , repo , spec .Maintainers ); err != nil {
68+ return fmt .Errorf ("failed to add CODEOWNERS file: %w" , err )
69+ }
70+
5671 logger .Info ("successfully created organization repository" , "domain" , domain , "repository" , spec .RepositoryName )
5772 }
5873 default :
@@ -84,22 +99,36 @@ func CreateUserRepository(ctx context.Context, gc gitprovider.Client, domain str
8499 Visibility : & visibility ,
85100 }
86101
102+ createOpts , err := gitprovider .MakeRepositoryCreateOptions (& gitprovider.RepositoryCreateOptions {AutoInit : gitprovider .BoolVar (true )})
103+ if err != nil {
104+ return fmt .Errorf ("failed to create _create_ options for repository: %w" , err )
105+ }
106+
87107 switch spec .ExistingRepositoryPolicy {
88108 case mpasv1alpha1 .ExistingRepositoryPolicyFail :
89- if _ , err := gc .UserRepositories ().Create (ctx , ref , info ); err != nil {
109+ repo , err := gc .UserRepositories ().Create (ctx , ref , info , & createOpts )
110+ if err != nil {
90111 return fmt .Errorf ("failed to create repository: %w" , err )
91112 }
92113
114+ if err := createCodeownersFile (ctx , repo , spec .Maintainers ); err != nil {
115+ return fmt .Errorf ("failed to add CODEOWNERS file: %w" , err )
116+ }
117+
93118 logger .Info ("successfully created user repository" , "domain" , domain , "repository" , spec .RepositoryName )
94119 case mpasv1alpha1 .ExistingRepositoryPolicyAdopt :
95- _ , created , err := gc .UserRepositories ().Reconcile (ctx , ref , info )
120+ repo , created , err := gc .UserRepositories ().Reconcile (ctx , ref , info , & createOpts )
96121 if err != nil {
97122 return fmt .Errorf ("failed to reconcile repository: %w" , err )
98123 }
99124
100125 if ! created {
101126 logger .Info ("using existing repository" , "domain" , domain , "repository" , spec .RepositoryName )
102127 } else {
128+ if err := createCodeownersFile (ctx , repo , spec .Maintainers ); err != nil {
129+ return fmt .Errorf ("failed to add CODEOWNERS file: %w" , err )
130+ }
131+
103132 logger .Info ("successfully created user repository" , "domain" , domain , "repository" , spec .RepositoryName )
104133 }
105134 default :
@@ -194,3 +223,38 @@ func CreateUserPullRequest(ctx context.Context, gc gitprovider.Client, domain, b
194223
195224 return nil
196225}
226+
227+ // Repositories groups together a common functionality of both repository types.
228+ type Repositories interface {
229+ Commits () gitprovider.CommitClient
230+ }
231+
232+ func createCodeownersFile (ctx context.Context , repo Repositories , maintainers []string ) error {
233+ if len (maintainers ) == 0 {
234+ return nil
235+ }
236+
237+ logger := log .FromContext (ctx )
238+
239+ content := strings.Builder {}
240+
241+ for _ , m := range maintainers {
242+ _ , _ = content .WriteString (fmt .Sprintf ("%s\n " , m ))
243+ }
244+
245+ files := []gitprovider.CommitFile {
246+ {
247+ Path : gitprovider .StringVar ("." ),
248+ Content : gitprovider .StringVar (content .String ()),
249+ },
250+ }
251+
252+ commit , err := repo .Commits ().Create (ctx , "main" , "adding CODEOWNERS" , files )
253+ if err != nil {
254+ return fmt .Errorf ("failed to create CODEOWNERS file: %w" , err )
255+ }
256+
257+ logger .Info ("successfully added CODEOWNERS" , "url" , commit .Get ().URL )
258+
259+ return nil
260+ }
0 commit comments