Add github credentials API and cli code

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
This commit is contained in:
Gabriel Adrian Samfira 2024-04-19 08:47:44 +00:00
parent 77ecb16166
commit eadbe784b9
28 changed files with 2364 additions and 175 deletions

View file

@ -29,7 +29,7 @@ type GithubEndpointStore interface {
}
type GithubCredentialsStore interface {
CreateGithubCredentials(ctx context.Context, endpointName string, param params.CreateGithubCredentialsParams) (params.GithubCredentials, error)
CreateGithubCredentials(ctx context.Context, param params.CreateGithubCredentialsParams) (params.GithubCredentials, error)
GetGithubCredentials(ctx context.Context, id uint, detailed bool) (params.GithubCredentials, error)
GetGithubCredentialsByName(ctx context.Context, name string, detailed bool) (params.GithubCredentials, error)
ListGithubCredentials(ctx context.Context) ([]params.GithubCredentials, error)

View file

@ -134,9 +134,9 @@ func (_m *Store) CreateEntityPool(ctx context.Context, entity params.GithubEntit
return r0, r1
}
// CreateGithubCredentials provides a mock function with given fields: ctx, endpointName, param
func (_m *Store) CreateGithubCredentials(ctx context.Context, endpointName string, param params.CreateGithubCredentialsParams) (params.GithubCredentials, error) {
ret := _m.Called(ctx, endpointName, param)
// CreateGithubCredentials provides a mock function with given fields: ctx, param
func (_m *Store) CreateGithubCredentials(ctx context.Context, param params.CreateGithubCredentialsParams) (params.GithubCredentials, error) {
ret := _m.Called(ctx, param)
if len(ret) == 0 {
panic("no return value specified for CreateGithubCredentials")
@ -144,17 +144,17 @@ func (_m *Store) CreateGithubCredentials(ctx context.Context, endpointName strin
var r0 params.GithubCredentials
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, string, params.CreateGithubCredentialsParams) (params.GithubCredentials, error)); ok {
return rf(ctx, endpointName, param)
if rf, ok := ret.Get(0).(func(context.Context, params.CreateGithubCredentialsParams) (params.GithubCredentials, error)); ok {
return rf(ctx, param)
}
if rf, ok := ret.Get(0).(func(context.Context, string, params.CreateGithubCredentialsParams) params.GithubCredentials); ok {
r0 = rf(ctx, endpointName, param)
if rf, ok := ret.Get(0).(func(context.Context, params.CreateGithubCredentialsParams) params.GithubCredentials); ok {
r0 = rf(ctx, param)
} else {
r0 = ret.Get(0).(params.GithubCredentials)
}
if rf, ok := ret.Get(1).(func(context.Context, string, params.CreateGithubCredentialsParams) error); ok {
r1 = rf(ctx, endpointName, param)
if rf, ok := ret.Get(1).(func(context.Context, params.CreateGithubCredentialsParams) error); ok {
r1 = rf(ctx, param)
} else {
r1 = ret.Error(1)
}

View file

@ -257,15 +257,18 @@ func (s *sqlDatabase) DeleteGithubEndpoint(_ context.Context, name string) error
return nil
}
func (s *sqlDatabase) CreateGithubCredentials(ctx context.Context, endpointName string, param params.CreateGithubCredentialsParams) (params.GithubCredentials, error) {
func (s *sqlDatabase) CreateGithubCredentials(ctx context.Context, param params.CreateGithubCredentialsParams) (params.GithubCredentials, error) {
userID, err := getUIDFromContext(ctx)
if err != nil {
return params.GithubCredentials{}, errors.Wrap(err, "creating github credentials")
}
if param.Endpoint == "" {
return params.GithubCredentials{}, errors.New("endpoint name is required")
}
var creds GithubCredentials
err = s.conn.Transaction(func(tx *gorm.DB) error {
var endpoint GithubEndpoint
if err := tx.Where("name = ?", endpointName).First(&endpoint).Error; err != nil {
if err := tx.Where("name = ?", param.Endpoint).First(&endpoint).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return errors.Wrap(runnerErrors.ErrNotFound, "github endpoint not found")
}

View file

@ -297,6 +297,7 @@ func (s *sqlDatabase) migrateCredentialsToDB() (err error) {
credParams := params.CreateGithubCredentialsParams{
Name: cred.Name,
Description: cred.Description,
Endpoint: endpoint.Name,
AuthType: params.GithubAuthType(cred.GetAuthType()),
}
switch credParams.AuthType {
@ -327,7 +328,7 @@ func (s *sqlDatabase) migrateCredentialsToDB() (err error) {
}
}
creds, err := s.CreateGithubCredentials(adminCtx, endpoint.Name, credParams)
creds, err := s.CreateGithubCredentials(adminCtx, credParams)
if err != nil {
return errors.Wrap(err, "creating github credentials")
}