@@ -9,16 +9,16 @@ import (
99 v1 "k8s.io/api/core/v1"
1010 "k8s.io/apimachinery/pkg/types"
1111 "sigs.k8s.io/controller-runtime/pkg/client"
12- "sigs.k8s.io/controller-runtime/pkg/log"
1312
1413 mpasv1alpha1 "github.com/open-component-model/git-controller/apis/mpas/v1alpha1"
1514 "github.com/open-component-model/git-controller/pkg/providers"
15+ "github.com/open-component-model/git-controller/pkg/providers/gogit"
1616)
1717
1818const (
1919 tokenKey = "token"
2020 providerType = "github"
21- defaultDomain = " github.com"
21+ defaultDomain = github .DefaultDomain
2222)
2323
2424// Client github.
@@ -27,13 +27,6 @@ type Client struct {
2727 next providers.Provider
2828}
2929
30- // TODO: Use this instead and somehow abstract the two clients.
31- type RepositoryOpts struct {
32- Owner string
33- Domain string
34- Visibility gitprovider.RepositoryVisibility
35- }
36-
3730// NewClient creates a new GitHub client.
3831func NewClient (client client.Client , next providers.Provider ) * Client {
3932 return & Client {
@@ -58,27 +51,21 @@ func (c *Client) CreateRepository(ctx context.Context, obj mpasv1alpha1.Reposito
5851 return err
5952 }
6053
61- gc , err := github .NewClient (authenticationOption )
62- if err != nil {
63- return fmt .Errorf ("failed to create github client: %w" , err )
64- }
65-
66- visibility := gitprovider .RepositoryVisibility (obj .Spec .Visibility )
67-
68- if err := gitprovider .ValidateRepositoryVisibility (visibility ); err != nil {
69- return fmt .Errorf ("failed to validate visibility: %w" , err )
70- }
71-
7254 domain := defaultDomain
7355 if obj .Spec .Domain != "" {
7456 domain = obj .Spec .Domain
7557 }
7658
59+ gc , err := github .NewClient (authenticationOption , gitprovider .WithDomain (domain ))
60+ if err != nil {
61+ return fmt .Errorf ("failed to create github client: %w" , err )
62+ }
63+
7764 if obj .Spec .IsOrganization {
78- return c . createOrganizationRepository (ctx , gc , domain , visibility , obj .Spec )
65+ return gogit . CreateOrganizationRepository (ctx , gc , domain , obj .Spec )
7966 }
8067
81- return c . createUserRepository (ctx , gc , domain , visibility , obj .Spec )
68+ return gogit . CreateUserRepository (ctx , gc , domain , obj .Spec )
8269}
8370
8471// constructAuthenticationOption will take the object and construct an authentication option.
@@ -100,86 +87,6 @@ func (c *Client) constructAuthenticationOption(ctx context.Context, obj mpasv1al
10087 return gitprovider .WithOAuth2Token (string (token )), nil
10188}
10289
103- func (c * Client ) createOrganizationRepository (ctx context.Context , gc gitprovider.Client , domain string , visibility gitprovider.RepositoryVisibility , spec mpasv1alpha1.RepositorySpec ) error {
104- logger := log .FromContext (ctx )
105-
106- ref := gitprovider.OrgRepositoryRef {
107- OrganizationRef : gitprovider.OrganizationRef {
108- Domain : domain ,
109- Organization : spec .Owner ,
110- },
111- RepositoryName : spec .RepositoryName ,
112- }
113- info := gitprovider.RepositoryInfo {
114- DefaultBranch : gitprovider .StringVar ("main" ),
115- Visibility : & visibility ,
116- }
117-
118- switch spec .ExistingRepositoryPolicy {
119- case mpasv1alpha1 .ExistingRepositoryPolicyFail :
120- if _ , err := gc .OrgRepositories ().Create (ctx , ref , info ); err != nil {
121- return fmt .Errorf ("failed to create repository: %w" , err )
122- }
123-
124- logger .Info ("successfully created organization repository" , "domain" , domain , "repository" , spec .RepositoryName )
125- case mpasv1alpha1 .ExistingRepositoryPolicyAdopt :
126- _ , created , err := gc .OrgRepositories ().Reconcile (ctx , ref , info )
127- if err != nil {
128- return fmt .Errorf ("failed to reconcile repository: %w" , err )
129- }
130-
131- if ! created {
132- logger .Info ("using existing repository" , "domain" , domain , "repository" , spec .RepositoryName )
133- } else {
134- logger .Info ("successfully created organization repository" , "domain" , domain , "repository" , spec .RepositoryName )
135- }
136- default :
137- return fmt .Errorf ("unknown repository policy '%s'" , spec .ExistingRepositoryPolicy )
138- }
139-
140- return nil
141- }
142-
143- func (c * Client ) createUserRepository (ctx context.Context , gc gitprovider.Client , domain string , visibility gitprovider.RepositoryVisibility , spec mpasv1alpha1.RepositorySpec ) error {
144- logger := log .FromContext (ctx )
145-
146- ref := gitprovider.UserRepositoryRef {
147- UserRef : gitprovider.UserRef {
148- Domain : domain ,
149- UserLogin : spec .Owner ,
150- },
151- RepositoryName : spec .RepositoryName ,
152- }
153- info := gitprovider.RepositoryInfo {
154- DefaultBranch : gitprovider .StringVar ("main" ),
155- Visibility : & visibility ,
156- }
157-
158- switch spec .ExistingRepositoryPolicy {
159- case mpasv1alpha1 .ExistingRepositoryPolicyFail :
160- if _ , err := gc .UserRepositories ().Create (ctx , ref , info ); err != nil {
161- return fmt .Errorf ("failed to create repository: %w" , err )
162- }
163-
164- logger .Info ("successfully created user repository" , "domain" , domain , "repository" , spec .RepositoryName )
165- case mpasv1alpha1 .ExistingRepositoryPolicyAdopt :
166- _ , created , err := gc .UserRepositories ().Reconcile (ctx , ref , info )
167- if err != nil {
168- return fmt .Errorf ("failed to reconcile repository: %w" , err )
169- }
170-
171- if ! created {
172- logger .Info ("using existing repository" , "domain" , domain , "repository" , spec .RepositoryName )
173- } else {
174- logger .Info ("successfully created user repository" , "domain" , domain , "repository" , spec .RepositoryName )
175- }
176- default :
177- return fmt .Errorf ("unknown repository policy '%s'" , spec .ExistingRepositoryPolicy )
178- }
179-
180- return nil
181- }
182-
18390func (c * Client ) CreatePullRequest (ctx context.Context , owner , repo , title , branch , description string ) error {
18491 return nil
18592}
0 commit comments