@@ -29,7 +29,7 @@ import (
2929 "github.com/cloudbase/garm/params"
3030)
3131
32- func (s * sqlDatabase ) CreateOrganization (ctx context.Context , name , credentialsName , webhookSecret string , poolBalancerType params.PoolBalancerType ) (org params.Organization , err error ) {
32+ func (s * sqlDatabase ) CreateOrganization (ctx context.Context , name string , credentials params. ForgeCredentials , webhookSecret string , poolBalancerType params.PoolBalancerType ) (param params.Organization , err error ) {
3333 if webhookSecret == "" {
3434 return params.Organization {}, errors .New ("creating org: missing secret" )
3535 }
@@ -40,7 +40,7 @@ func (s *sqlDatabase) CreateOrganization(ctx context.Context, name, credentialsN
4040
4141 defer func () {
4242 if err == nil {
43- s .sendNotify (common .OrganizationEntityType , common .CreateOperation , org )
43+ s .sendNotify (common .OrganizationEntityType , common .CreateOperation , param )
4444 }
4545 }()
4646 newOrg := Organization {
@@ -50,37 +50,37 @@ func (s *sqlDatabase) CreateOrganization(ctx context.Context, name, credentialsN
5050 }
5151
5252 err = s .conn .Transaction (func (tx * gorm.DB ) error {
53- creds , err := s .getGithubCredentialsByName (ctx , tx , credentialsName , false )
54- if err != nil {
55- return errors .Wrap (err , "creating org" )
56- }
57- if creds .EndpointName == nil {
58- return errors .Wrap (runnerErrors .ErrUnprocessable , "credentials have no endpoint" )
53+ switch credentials .ForgeType {
54+ case params .GithubEndpointType :
55+ newOrg .CredentialsID = & credentials .ID
56+ case params .GiteaEndpointType :
57+ newOrg .GiteaCredentialsID = & credentials .ID
58+ default :
59+ return errors .Wrap (runnerErrors .ErrBadRequest , "unsupported credentials type" )
5960 }
60- newOrg .CredentialsID = & creds .ID
61- newOrg .EndpointName = creds .EndpointName
6261
62+ newOrg .EndpointName = & credentials .Endpoint .Name
6363 q := tx .Create (& newOrg )
6464 if q .Error != nil {
6565 return errors .Wrap (q .Error , "creating org" )
6666 }
67-
68- newOrg .Credentials = creds
69- newOrg .Endpoint = creds .Endpoint
70-
7167 return nil
7268 })
7369 if err != nil {
7470 return params.Organization {}, errors .Wrap (err , "creating org" )
7571 }
7672
77- org , err = s .sqlToCommonOrganization ( newOrg , true )
73+ org , err : = s .getOrgByID ( ctx , s . conn , newOrg . ID . String (), "Pools" , "Endpoint" , "Credentials" , "GiteaCredentials" , "Credentials.Endpoint" , "GiteaCredentials.Endpoint" )
7874 if err != nil {
7975 return params.Organization {}, errors .Wrap (err , "creating org" )
8076 }
81- org .WebhookSecret = webhookSecret
8277
83- return org , nil
78+ param , err = s .sqlToCommonOrganization (org , true )
79+ if err != nil {
80+ return params.Organization {}, errors .Wrap (err , "creating org" )
81+ }
82+
83+ return param , nil
8484}
8585
8686func (s * sqlDatabase ) GetOrganization (ctx context.Context , name , endpointName string ) (params.Organization , error ) {
@@ -101,7 +101,9 @@ func (s *sqlDatabase) ListOrganizations(_ context.Context) ([]params.Organizatio
101101 var orgs []Organization
102102 q := s .conn .
103103 Preload ("Credentials" ).
104+ Preload ("GiteaCredentials" ).
104105 Preload ("Credentials.Endpoint" ).
106+ Preload ("GiteaCredentials.Endpoint" ).
105107 Preload ("Endpoint" ).
106108 Find (& orgs )
107109 if q .Error != nil {
@@ -121,7 +123,7 @@ func (s *sqlDatabase) ListOrganizations(_ context.Context) ([]params.Organizatio
121123}
122124
123125func (s * sqlDatabase ) DeleteOrganization (ctx context.Context , orgID string ) (err error ) {
124- org , err := s .getOrgByID (ctx , s .conn , orgID , "Endpoint" , "Credentials" , "Credentials.Endpoint" )
126+ org , err := s .getOrgByID (ctx , s .conn , orgID , "Endpoint" , "Credentials" , "Credentials.Endpoint" , "GiteaCredentials" , "GiteaCredentials.Endpoint" )
125127 if err != nil {
126128 return errors .Wrap (err , "fetching org" )
127129 }
@@ -201,7 +203,7 @@ func (s *sqlDatabase) UpdateOrganization(ctx context.Context, orgID string, para
201203 return params.Organization {}, errors .Wrap (err , "saving org" )
202204 }
203205
204- org , err = s .getOrgByID (ctx , s .conn , orgID , "Endpoint" , "Credentials" , "Credentials.Endpoint" )
206+ org , err = s .getOrgByID (ctx , s .conn , orgID , "Endpoint" , "Credentials" , "Credentials.Endpoint" , "GiteaCredentials" , "GiteaCredentials.Endpoint" )
205207 if err != nil {
206208 return params.Organization {}, errors .Wrap (err , "updating enterprise" )
207209 }
@@ -213,7 +215,7 @@ func (s *sqlDatabase) UpdateOrganization(ctx context.Context, orgID string, para
213215}
214216
215217func (s * sqlDatabase ) GetOrganizationByID (ctx context.Context , orgID string ) (params.Organization , error ) {
216- org , err := s .getOrgByID (ctx , s .conn , orgID , "Pools" , "Credentials" , "Endpoint" , "Credentials.Endpoint" )
218+ org , err := s .getOrgByID (ctx , s .conn , orgID , "Pools" , "Credentials" , "Endpoint" , "Credentials.Endpoint" , "GiteaCredentials" , "GiteaCredentials.Endpoint" )
217219 if err != nil {
218220 return params.Organization {}, errors .Wrap (err , "fetching org" )
219221 }
@@ -254,7 +256,9 @@ func (s *sqlDatabase) getOrg(_ context.Context, name, endpointName string) (Orga
254256
255257 q := s .conn .Where ("name = ? COLLATE NOCASE and endpoint_name = ? COLLATE NOCASE" , name , endpointName ).
256258 Preload ("Credentials" ).
259+ Preload ("GiteaCredentials" ).
257260 Preload ("Credentials.Endpoint" ).
261+ Preload ("GiteaCredentials.Endpoint" ).
258262 Preload ("Endpoint" ).
259263 First (& org )
260264 if q .Error != nil {
0 commit comments