@@ -6,19 +6,18 @@ import (
66
77 "github.com/fluxcd/go-git-providers/github"
88 "github.com/fluxcd/go-git-providers/gitprovider"
9+ mpasv1alpha1 "github.com/open-component-model/git-controller/apis/mpas/v1alpha1"
10+ "github.com/open-component-model/git-controller/pkg/providers"
11+ "github.com/open-component-model/git-controller/pkg/providers/gogit"
912 v1 "k8s.io/api/core/v1"
1013 "k8s.io/apimachinery/pkg/types"
1114 "sigs.k8s.io/controller-runtime/pkg/client"
12- "sigs.k8s.io/controller-runtime/pkg/log"
13-
14- mpasv1alpha1 "github.com/open-component-model/git-controller/apis/mpas/v1alpha1"
15- "github.com/open-component-model/git-controller/pkg/providers"
1615)
1716
1817const (
1918 tokenKey = "token"
2019 providerType = "github"
21- defaultDomain = " github.com"
20+ defaultDomain = github .DefaultDomain
2221)
2322
2423// Client github.
@@ -27,13 +26,6 @@ type Client struct {
2726 next providers.Provider
2827}
2928
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-
3729// NewClient creates a new GitHub client.
3830func NewClient (client client.Client , next providers.Provider ) * Client {
3931 return & Client {
@@ -58,27 +50,21 @@ func (c *Client) CreateRepository(ctx context.Context, obj mpasv1alpha1.Reposito
5850 return err
5951 }
6052
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-
7253 domain := defaultDomain
7354 if obj .Spec .Domain != "" {
7455 domain = obj .Spec .Domain
7556 }
7657
58+ gc , err := github .NewClient (authenticationOption , gitprovider .WithDomain (domain ))
59+ if err != nil {
60+ return fmt .Errorf ("failed to create github client: %w" , err )
61+ }
62+
7763 if obj .Spec .IsOrganization {
78- return c . createOrganizationRepository (ctx , gc , domain , visibility , obj .Spec )
64+ return gogit . CreateOrganizationRepository (ctx , gc , domain , obj .Spec )
7965 }
8066
81- return c . createUserRepository (ctx , gc , domain , visibility , obj .Spec )
67+ return gogit . CreateUserRepository (ctx , gc , domain , obj .Spec )
8268}
8369
8470// constructAuthenticationOption will take the object and construct an authentication option.
@@ -100,86 +86,6 @@ func (c *Client) constructAuthenticationOption(ctx context.Context, obj mpasv1al
10086 return gitprovider .WithOAuth2Token (string (token )), nil
10187}
10288
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-
18389func (c * Client ) CreatePullRequest (ctx context.Context , owner , repo , title , branch , description string ) error {
18490 return nil
18591}
0 commit comments