Switch to fmt.Errorf
Replace all instances of errors.Wrap() with fmt.Errorf. Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
This commit is contained in:
parent
10dcbec954
commit
118319c7c1
88 changed files with 1007 additions and 4467 deletions
|
|
@ -15,10 +15,11 @@
|
|||
package sql
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
|
||||
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
|
||||
|
|
@ -30,7 +31,7 @@ import (
|
|||
func dbControllerToCommonController(dbInfo ControllerInfo) (params.ControllerInfo, error) {
|
||||
url, err := url.JoinPath(dbInfo.WebhookBaseURL, dbInfo.ControllerID.String())
|
||||
if err != nil {
|
||||
return params.ControllerInfo{}, errors.Wrap(err, "joining webhook URL")
|
||||
return params.ControllerInfo{}, fmt.Errorf("error joining webhook URL: %w", err)
|
||||
}
|
||||
|
||||
return params.ControllerInfo{
|
||||
|
|
@ -49,14 +50,14 @@ func (s *sqlDatabase) ControllerInfo() (params.ControllerInfo, error) {
|
|||
q := s.conn.Model(&ControllerInfo{}).First(&info)
|
||||
if q.Error != nil {
|
||||
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return params.ControllerInfo{}, errors.Wrap(runnerErrors.ErrNotFound, "fetching controller info")
|
||||
return params.ControllerInfo{}, fmt.Errorf("error fetching controller info: %w", runnerErrors.ErrNotFound)
|
||||
}
|
||||
return params.ControllerInfo{}, errors.Wrap(q.Error, "fetching controller info")
|
||||
return params.ControllerInfo{}, fmt.Errorf("error fetching controller info: %w", q.Error)
|
||||
}
|
||||
|
||||
paramInfo, err := dbControllerToCommonController(info)
|
||||
if err != nil {
|
||||
return params.ControllerInfo{}, errors.Wrap(err, "converting controller info")
|
||||
return params.ControllerInfo{}, fmt.Errorf("error converting controller info: %w", err)
|
||||
}
|
||||
|
||||
return paramInfo, nil
|
||||
|
|
@ -69,7 +70,7 @@ func (s *sqlDatabase) InitController() (params.ControllerInfo, error) {
|
|||
|
||||
newID, err := uuid.NewRandom()
|
||||
if err != nil {
|
||||
return params.ControllerInfo{}, errors.Wrap(err, "generating UUID")
|
||||
return params.ControllerInfo{}, fmt.Errorf("error generating UUID: %w", err)
|
||||
}
|
||||
|
||||
newInfo := ControllerInfo{
|
||||
|
|
@ -79,7 +80,7 @@ func (s *sqlDatabase) InitController() (params.ControllerInfo, error) {
|
|||
|
||||
q := s.conn.Save(&newInfo)
|
||||
if q.Error != nil {
|
||||
return params.ControllerInfo{}, errors.Wrap(q.Error, "saving controller info")
|
||||
return params.ControllerInfo{}, fmt.Errorf("error saving controller info: %w", q.Error)
|
||||
}
|
||||
|
||||
return params.ControllerInfo{
|
||||
|
|
@ -98,13 +99,13 @@ func (s *sqlDatabase) UpdateController(info params.UpdateControllerParams) (para
|
|||
q := tx.Model(&ControllerInfo{}).First(&dbInfo)
|
||||
if q.Error != nil {
|
||||
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return errors.Wrap(runnerErrors.ErrNotFound, "fetching controller info")
|
||||
return fmt.Errorf("error fetching controller info: %w", runnerErrors.ErrNotFound)
|
||||
}
|
||||
return errors.Wrap(q.Error, "fetching controller info")
|
||||
return fmt.Errorf("error fetching controller info: %w", q.Error)
|
||||
}
|
||||
|
||||
if err := info.Validate(); err != nil {
|
||||
return errors.Wrap(err, "validating controller info")
|
||||
return fmt.Errorf("error validating controller info: %w", err)
|
||||
}
|
||||
|
||||
if info.MetadataURL != nil {
|
||||
|
|
@ -125,17 +126,17 @@ func (s *sqlDatabase) UpdateController(info params.UpdateControllerParams) (para
|
|||
|
||||
q = tx.Save(&dbInfo)
|
||||
if q.Error != nil {
|
||||
return errors.Wrap(q.Error, "saving controller info")
|
||||
return fmt.Errorf("error saving controller info: %w", q.Error)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return params.ControllerInfo{}, errors.Wrap(err, "updating controller info")
|
||||
return params.ControllerInfo{}, fmt.Errorf("error updating controller info: %w", err)
|
||||
}
|
||||
|
||||
paramInfo, err = dbControllerToCommonController(dbInfo)
|
||||
if err != nil {
|
||||
return params.ControllerInfo{}, errors.Wrap(err, "converting controller info")
|
||||
return params.ControllerInfo{}, fmt.Errorf("error converting controller info: %w", err)
|
||||
}
|
||||
return paramInfo, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,10 +16,11 @@ package sql
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
|
||||
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
|
||||
|
|
@ -33,12 +34,12 @@ func (s *sqlDatabase) CreateEnterprise(ctx context.Context, name string, credent
|
|||
return params.Enterprise{}, errors.New("creating enterprise: missing secret")
|
||||
}
|
||||
if credentials.ForgeType != params.GithubEndpointType {
|
||||
return params.Enterprise{}, errors.Wrap(runnerErrors.ErrBadRequest, "enterprises are not supported on this forge type")
|
||||
return params.Enterprise{}, fmt.Errorf("enterprises are not supported on this forge type: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
|
||||
secret, err := util.Seal([]byte(webhookSecret), []byte(s.cfg.Passphrase))
|
||||
if err != nil {
|
||||
return params.Enterprise{}, errors.Wrap(err, "encoding secret")
|
||||
return params.Enterprise{}, fmt.Errorf("error encoding secret: %w", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
|
|
@ -57,22 +58,22 @@ func (s *sqlDatabase) CreateEnterprise(ctx context.Context, name string, credent
|
|||
|
||||
q := tx.Create(&newEnterprise)
|
||||
if q.Error != nil {
|
||||
return errors.Wrap(q.Error, "creating enterprise")
|
||||
return fmt.Errorf("error creating enterprise: %w", q.Error)
|
||||
}
|
||||
|
||||
newEnterprise, err = s.getEnterpriseByID(ctx, tx, newEnterprise.ID.String(), "Pools", "Credentials", "Endpoint", "Credentials.Endpoint")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "creating enterprise")
|
||||
return fmt.Errorf("error creating enterprise: %w", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return params.Enterprise{}, errors.Wrap(err, "creating enterprise")
|
||||
return params.Enterprise{}, fmt.Errorf("error creating enterprise: %w", err)
|
||||
}
|
||||
|
||||
ret, err := s.GetEnterpriseByID(ctx, newEnterprise.ID.String())
|
||||
if err != nil {
|
||||
return params.Enterprise{}, errors.Wrap(err, "creating enterprise")
|
||||
return params.Enterprise{}, fmt.Errorf("error creating enterprise: %w", err)
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
|
|
@ -81,12 +82,12 @@ func (s *sqlDatabase) CreateEnterprise(ctx context.Context, name string, credent
|
|||
func (s *sqlDatabase) GetEnterprise(ctx context.Context, name, endpointName string) (params.Enterprise, error) {
|
||||
enterprise, err := s.getEnterprise(ctx, name, endpointName)
|
||||
if err != nil {
|
||||
return params.Enterprise{}, errors.Wrap(err, "fetching enterprise")
|
||||
return params.Enterprise{}, fmt.Errorf("error fetching enterprise: %w", err)
|
||||
}
|
||||
|
||||
param, err := s.sqlToCommonEnterprise(enterprise, true)
|
||||
if err != nil {
|
||||
return params.Enterprise{}, errors.Wrap(err, "fetching enterprise")
|
||||
return params.Enterprise{}, fmt.Errorf("error fetching enterprise: %w", err)
|
||||
}
|
||||
return param, nil
|
||||
}
|
||||
|
|
@ -101,12 +102,12 @@ func (s *sqlDatabase) GetEnterpriseByID(ctx context.Context, enterpriseID string
|
|||
}
|
||||
enterprise, err := s.getEnterpriseByID(ctx, s.conn, enterpriseID, preloadList...)
|
||||
if err != nil {
|
||||
return params.Enterprise{}, errors.Wrap(err, "fetching enterprise")
|
||||
return params.Enterprise{}, fmt.Errorf("error fetching enterprise: %w", err)
|
||||
}
|
||||
|
||||
param, err := s.sqlToCommonEnterprise(enterprise, true)
|
||||
if err != nil {
|
||||
return params.Enterprise{}, errors.Wrap(err, "fetching enterprise")
|
||||
return params.Enterprise{}, fmt.Errorf("error fetching enterprise: %w", err)
|
||||
}
|
||||
return param, nil
|
||||
}
|
||||
|
|
@ -125,7 +126,7 @@ func (s *sqlDatabase) ListEnterprises(_ context.Context, filter params.Enterpris
|
|||
}
|
||||
q = q.Find(&enterprises)
|
||||
if q.Error != nil {
|
||||
return []params.Enterprise{}, errors.Wrap(q.Error, "fetching enterprises")
|
||||
return []params.Enterprise{}, fmt.Errorf("error fetching enterprises: %w", q.Error)
|
||||
}
|
||||
|
||||
ret := make([]params.Enterprise, len(enterprises))
|
||||
|
|
@ -133,7 +134,7 @@ func (s *sqlDatabase) ListEnterprises(_ context.Context, filter params.Enterpris
|
|||
var err error
|
||||
ret[idx], err = s.sqlToCommonEnterprise(val, true)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "fetching enterprises")
|
||||
return nil, fmt.Errorf("error fetching enterprises: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -143,7 +144,7 @@ func (s *sqlDatabase) ListEnterprises(_ context.Context, filter params.Enterpris
|
|||
func (s *sqlDatabase) DeleteEnterprise(ctx context.Context, enterpriseID string) error {
|
||||
enterprise, err := s.getEnterpriseByID(ctx, s.conn, enterpriseID, "Endpoint", "Credentials", "Credentials.Endpoint")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "fetching enterprise")
|
||||
return fmt.Errorf("error fetching enterprise: %w", err)
|
||||
}
|
||||
|
||||
defer func(ent Enterprise) {
|
||||
|
|
@ -159,7 +160,7 @@ func (s *sqlDatabase) DeleteEnterprise(ctx context.Context, enterpriseID string)
|
|||
|
||||
q := s.conn.Unscoped().Delete(&enterprise)
|
||||
if q.Error != nil && !errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return errors.Wrap(q.Error, "deleting enterprise")
|
||||
return fmt.Errorf("error deleting enterprise: %w", q.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
@ -177,31 +178,31 @@ func (s *sqlDatabase) UpdateEnterprise(ctx context.Context, enterpriseID string,
|
|||
var err error
|
||||
enterprise, err = s.getEnterpriseByID(ctx, tx, enterpriseID)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "fetching enterprise")
|
||||
return fmt.Errorf("error fetching enterprise: %w", err)
|
||||
}
|
||||
|
||||
if enterprise.EndpointName == nil {
|
||||
return errors.Wrap(runnerErrors.ErrUnprocessable, "enterprise has no endpoint")
|
||||
return fmt.Errorf("error enterprise has no endpoint: %w", runnerErrors.ErrUnprocessable)
|
||||
}
|
||||
|
||||
if param.CredentialsName != "" {
|
||||
creds, err = s.getGithubCredentialsByName(ctx, tx, param.CredentialsName, false)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "fetching credentials")
|
||||
return fmt.Errorf("error fetching credentials: %w", err)
|
||||
}
|
||||
if creds.EndpointName == nil {
|
||||
return errors.Wrap(runnerErrors.ErrUnprocessable, "credentials have no endpoint")
|
||||
return fmt.Errorf("error credentials have no endpoint: %w", runnerErrors.ErrUnprocessable)
|
||||
}
|
||||
|
||||
if *creds.EndpointName != *enterprise.EndpointName {
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "endpoint mismatch")
|
||||
return fmt.Errorf("error endpoint mismatch: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
enterprise.CredentialsID = &creds.ID
|
||||
}
|
||||
if param.WebhookSecret != "" {
|
||||
secret, err := util.Seal([]byte(param.WebhookSecret), []byte(s.cfg.Passphrase))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "encoding secret")
|
||||
return fmt.Errorf("error encoding secret: %w", err)
|
||||
}
|
||||
enterprise.WebhookSecret = secret
|
||||
}
|
||||
|
|
@ -212,22 +213,22 @@ func (s *sqlDatabase) UpdateEnterprise(ctx context.Context, enterpriseID string,
|
|||
|
||||
q := tx.Save(&enterprise)
|
||||
if q.Error != nil {
|
||||
return errors.Wrap(q.Error, "saving enterprise")
|
||||
return fmt.Errorf("error saving enterprise: %w", q.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return params.Enterprise{}, errors.Wrap(err, "updating enterprise")
|
||||
return params.Enterprise{}, fmt.Errorf("error updating enterprise: %w", err)
|
||||
}
|
||||
|
||||
enterprise, err = s.getEnterpriseByID(ctx, s.conn, enterpriseID, "Endpoint", "Credentials", "Credentials.Endpoint")
|
||||
if err != nil {
|
||||
return params.Enterprise{}, errors.Wrap(err, "updating enterprise")
|
||||
return params.Enterprise{}, fmt.Errorf("error updating enterprise: %w", err)
|
||||
}
|
||||
newParams, err = s.sqlToCommonEnterprise(enterprise, true)
|
||||
if err != nil {
|
||||
return params.Enterprise{}, errors.Wrap(err, "updating enterprise")
|
||||
return params.Enterprise{}, fmt.Errorf("error updating enterprise: %w", err)
|
||||
}
|
||||
return newParams, nil
|
||||
}
|
||||
|
|
@ -244,7 +245,7 @@ func (s *sqlDatabase) getEnterprise(_ context.Context, name, endpointName string
|
|||
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return Enterprise{}, runnerErrors.ErrNotFound
|
||||
}
|
||||
return Enterprise{}, errors.Wrap(q.Error, "fetching enterprise from database")
|
||||
return Enterprise{}, fmt.Errorf("error fetching enterprise from database: %w", q.Error)
|
||||
}
|
||||
return enterprise, nil
|
||||
}
|
||||
|
|
@ -252,7 +253,7 @@ func (s *sqlDatabase) getEnterprise(_ context.Context, name, endpointName string
|
|||
func (s *sqlDatabase) getEnterpriseByID(_ context.Context, tx *gorm.DB, id string, preload ...string) (Enterprise, error) {
|
||||
u, err := uuid.Parse(id)
|
||||
if err != nil {
|
||||
return Enterprise{}, errors.Wrap(runnerErrors.ErrBadRequest, "parsing id")
|
||||
return Enterprise{}, fmt.Errorf("error parsing id: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
var enterprise Enterprise
|
||||
|
||||
|
|
@ -268,7 +269,7 @@ func (s *sqlDatabase) getEnterpriseByID(_ context.Context, tx *gorm.DB, id strin
|
|||
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return Enterprise{}, runnerErrors.ErrNotFound
|
||||
}
|
||||
return Enterprise{}, errors.Wrap(q.Error, "fetching enterprise from database")
|
||||
return Enterprise{}, fmt.Errorf("error fetching enterprise from database: %w", q.Error)
|
||||
}
|
||||
return enterprise, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -218,7 +218,7 @@ func (s *EnterpriseTestSuite) TestCreateEnterpriseInvalidDBPassphrase() {
|
|||
params.PoolBalancerTypeRoundRobin)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("encoding secret: invalid passphrase length (expected length 32 characters)", err.Error())
|
||||
s.Require().Equal("error encoding secret: invalid passphrase length (expected length 32 characters)", err.Error())
|
||||
}
|
||||
|
||||
func (s *EnterpriseTestSuite) TestCreateEnterpriseDBCreateErr() {
|
||||
|
|
@ -236,7 +236,7 @@ func (s *EnterpriseTestSuite) TestCreateEnterpriseDBCreateErr() {
|
|||
params.PoolBalancerTypeRoundRobin)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("creating enterprise: creating enterprise: creating enterprise mock error", err.Error())
|
||||
s.Require().Equal("error creating enterprise: error creating enterprise: creating enterprise mock error", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -259,7 +259,7 @@ func (s *EnterpriseTestSuite) TestGetEnterpriseNotFound() {
|
|||
_, err := s.Store.GetEnterprise(s.adminCtx, "dummy-name", "github.com")
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching enterprise: not found", err.Error())
|
||||
s.Require().Equal("error fetching enterprise: not found", err.Error())
|
||||
}
|
||||
|
||||
func (s *EnterpriseTestSuite) TestGetEnterpriseDBDecryptingErr() {
|
||||
|
|
@ -271,7 +271,7 @@ func (s *EnterpriseTestSuite) TestGetEnterpriseDBDecryptingErr() {
|
|||
_, err := s.StoreSQLMocked.GetEnterprise(s.adminCtx, s.Fixtures.Enterprises[0].Name, s.Fixtures.Enterprises[0].Endpoint.Name)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching enterprise: missing secret", err.Error())
|
||||
s.Require().Equal("error fetching enterprise: missing secret", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -341,7 +341,7 @@ func (s *EnterpriseTestSuite) TestListEnterprisesDBFetchErr() {
|
|||
|
||||
s.assertSQLMockExpectations()
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching enterprises: fetching user from database mock error", err.Error())
|
||||
s.Require().Equal("error fetching enterprises: fetching user from database mock error", err.Error())
|
||||
}
|
||||
|
||||
func (s *EnterpriseTestSuite) TestDeleteEnterprise() {
|
||||
|
|
@ -350,14 +350,14 @@ func (s *EnterpriseTestSuite) TestDeleteEnterprise() {
|
|||
s.Require().Nil(err)
|
||||
_, err = s.Store.GetEnterpriseByID(s.adminCtx, s.Fixtures.Enterprises[0].ID)
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching enterprise: not found", err.Error())
|
||||
s.Require().Equal("error fetching enterprise: not found", err.Error())
|
||||
}
|
||||
|
||||
func (s *EnterpriseTestSuite) TestDeleteEnterpriseInvalidEnterpriseID() {
|
||||
err := s.Store.DeleteEnterprise(s.adminCtx, "dummy-enterprise-id")
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching enterprise: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error fetching enterprise: error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *EnterpriseTestSuite) TestDeleteEnterpriseDBDeleteErr() {
|
||||
|
|
@ -375,7 +375,7 @@ func (s *EnterpriseTestSuite) TestDeleteEnterpriseDBDeleteErr() {
|
|||
err := s.StoreSQLMocked.DeleteEnterprise(s.adminCtx, s.Fixtures.Enterprises[0].ID)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("deleting enterprise: mocked delete enterprise error", err.Error())
|
||||
s.Require().Equal("error deleting enterprise: mocked delete enterprise error", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -391,7 +391,7 @@ func (s *EnterpriseTestSuite) TestUpdateEnterpriseInvalidEnterpriseID() {
|
|||
_, err := s.Store.UpdateEnterprise(s.adminCtx, "dummy-enterprise-id", s.Fixtures.UpdateRepoParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("updating enterprise: fetching enterprise: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error updating enterprise: error fetching enterprise: error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *EnterpriseTestSuite) TestUpdateEnterpriseDBEncryptErr() {
|
||||
|
|
@ -416,7 +416,7 @@ func (s *EnterpriseTestSuite) TestUpdateEnterpriseDBEncryptErr() {
|
|||
_, err := s.StoreSQLMocked.UpdateEnterprise(s.adminCtx, s.Fixtures.Enterprises[0].ID, s.Fixtures.UpdateRepoParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("updating enterprise: encoding secret: invalid passphrase length (expected length 32 characters)", err.Error())
|
||||
s.Require().Equal("error updating enterprise: error encoding secret: invalid passphrase length (expected length 32 characters)", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -444,7 +444,7 @@ func (s *EnterpriseTestSuite) TestUpdateEnterpriseDBSaveErr() {
|
|||
_, err := s.StoreSQLMocked.UpdateEnterprise(s.adminCtx, s.Fixtures.Enterprises[0].ID, s.Fixtures.UpdateRepoParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("updating enterprise: saving enterprise: saving enterprise mock error", err.Error())
|
||||
s.Require().Equal("error updating enterprise: error saving enterprise: saving enterprise mock error", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -472,7 +472,7 @@ func (s *EnterpriseTestSuite) TestUpdateEnterpriseDBDecryptingErr() {
|
|||
_, err := s.StoreSQLMocked.UpdateEnterprise(s.adminCtx, s.Fixtures.Enterprises[0].ID, s.Fixtures.UpdateRepoParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("updating enterprise: encoding secret: invalid passphrase length (expected length 32 characters)", err.Error())
|
||||
s.Require().Equal("error updating enterprise: error encoding secret: invalid passphrase length (expected length 32 characters)", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -487,7 +487,7 @@ func (s *EnterpriseTestSuite) TestGetEnterpriseByIDInvalidEnterpriseID() {
|
|||
_, err := s.Store.GetEnterpriseByID(s.adminCtx, "dummy-enterprise-id")
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching enterprise: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error fetching enterprise: error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *EnterpriseTestSuite) TestGetEnterpriseByIDDBDecryptingErr() {
|
||||
|
|
@ -508,7 +508,7 @@ func (s *EnterpriseTestSuite) TestGetEnterpriseByIDDBDecryptingErr() {
|
|||
|
||||
s.assertSQLMockExpectations()
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching enterprise: missing secret", err.Error())
|
||||
s.Require().Equal("error fetching enterprise: missing secret", err.Error())
|
||||
}
|
||||
|
||||
func (s *EnterpriseTestSuite) TestCreateEnterprisePool() {
|
||||
|
|
@ -547,7 +547,7 @@ func (s *EnterpriseTestSuite) TestCreateEnterprisePoolInvalidEnterpriseID() {
|
|||
_, err := s.Store.CreateEntityPool(s.adminCtx, entity, s.Fixtures.CreatePoolParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *EnterpriseTestSuite) TestCreateEnterprisePoolDBFetchTagErr() {
|
||||
|
|
@ -565,7 +565,7 @@ func (s *EnterpriseTestSuite) TestCreateEnterprisePoolDBFetchTagErr() {
|
|||
_, err = s.StoreSQLMocked.CreateEntityPool(s.adminCtx, entity, s.Fixtures.CreatePoolParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("creating tag: fetching tag from database: mocked fetching tag error", err.Error())
|
||||
s.Require().Equal("error creating tag: error fetching tag from database: mocked fetching tag error", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -592,7 +592,7 @@ func (s *EnterpriseTestSuite) TestCreateEnterprisePoolDBAddingPoolErr() {
|
|||
_, err = s.StoreSQLMocked.CreateEntityPool(s.adminCtx, entity, s.Fixtures.CreatePoolParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("creating pool: mocked adding pool error", err.Error())
|
||||
s.Require().Equal("error creating pool: mocked adding pool error", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -623,7 +623,7 @@ func (s *EnterpriseTestSuite) TestCreateEnterprisePoolDBSaveTagErr() {
|
|||
_, err = s.StoreSQLMocked.CreateEntityPool(s.adminCtx, entity, s.Fixtures.CreatePoolParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("associating tags: mocked saving tag error", err.Error())
|
||||
s.Require().Equal("error associating tags: mocked saving tag error", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -663,7 +663,7 @@ func (s *EnterpriseTestSuite) TestCreateEnterprisePoolDBFetchPoolErr() {
|
|||
_, err = s.StoreSQLMocked.CreateEntityPool(s.adminCtx, entity, s.Fixtures.CreatePoolParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching pool: not found", err.Error())
|
||||
s.Require().Equal("error fetching pool: not found", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -694,7 +694,7 @@ func (s *EnterpriseTestSuite) TestListEnterprisePoolsInvalidEnterpriseID() {
|
|||
_, err := s.Store.ListEntityPools(s.adminCtx, entity)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching pools: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error fetching pools: error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *EnterpriseTestSuite) TestGetEnterprisePool() {
|
||||
|
|
@ -719,7 +719,7 @@ func (s *EnterpriseTestSuite) TestGetEnterprisePoolInvalidEnterpriseID() {
|
|||
_, err := s.Store.GetEntityPool(s.adminCtx, entity, "dummy-pool-id")
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching pool: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("fetching pool: error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *EnterpriseTestSuite) TestDeleteEnterprisePool() {
|
||||
|
|
@ -734,7 +734,7 @@ func (s *EnterpriseTestSuite) TestDeleteEnterprisePool() {
|
|||
|
||||
s.Require().Nil(err)
|
||||
_, err = s.Store.GetEntityPool(s.adminCtx, entity, pool.ID)
|
||||
s.Require().Equal("fetching pool: finding pool: not found", err.Error())
|
||||
s.Require().Equal("fetching pool: error finding pool: not found", err.Error())
|
||||
}
|
||||
|
||||
func (s *EnterpriseTestSuite) TestDeleteEnterprisePoolInvalidEnterpriseID() {
|
||||
|
|
@ -745,7 +745,7 @@ func (s *EnterpriseTestSuite) TestDeleteEnterprisePoolInvalidEnterpriseID() {
|
|||
err := s.Store.DeleteEntityPool(s.adminCtx, entity, "dummy-pool-id")
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *EnterpriseTestSuite) TestDeleteEnterprisePoolDBDeleteErr() {
|
||||
|
|
@ -765,7 +765,7 @@ func (s *EnterpriseTestSuite) TestDeleteEnterprisePoolDBDeleteErr() {
|
|||
|
||||
err = s.StoreSQLMocked.DeleteEntityPool(s.adminCtx, entity, pool.ID)
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("removing pool: mocked deleting pool error", err.Error())
|
||||
s.Require().Equal("error removing pool: mocked deleting pool error", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -800,7 +800,7 @@ func (s *EnterpriseTestSuite) TestListEnterpriseInstancesInvalidEnterpriseID() {
|
|||
_, err := s.Store.ListEntityInstances(s.adminCtx, entity)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching entity: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error fetching entity: error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *EnterpriseTestSuite) TestUpdateEnterprisePool() {
|
||||
|
|
@ -828,7 +828,7 @@ func (s *EnterpriseTestSuite) TestUpdateEnterprisePoolInvalidEnterpriseID() {
|
|||
_, err := s.Store.UpdateEntityPool(s.adminCtx, entity, "dummy-pool-id", s.Fixtures.UpdatePoolParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching pool: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error fetching pool: error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *EnterpriseTestSuite) TestAddRepoEntityEvent() {
|
||||
|
|
|
|||
|
|
@ -16,9 +16,10 @@ package sql
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
|
||||
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
|
||||
|
|
@ -36,7 +37,7 @@ func (s *sqlDatabase) CreateGiteaEndpoint(_ context.Context, param params.Create
|
|||
var endpoint GithubEndpoint
|
||||
err = s.conn.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Where("name = ?", param.Name).First(&endpoint).Error; err == nil {
|
||||
return errors.Wrap(runnerErrors.ErrDuplicateEntity, "gitea endpoint already exists")
|
||||
return fmt.Errorf("gitea endpoint already exists: %w", runnerErrors.ErrDuplicateEntity)
|
||||
}
|
||||
endpoint = GithubEndpoint{
|
||||
Name: param.Name,
|
||||
|
|
@ -48,16 +49,16 @@ func (s *sqlDatabase) CreateGiteaEndpoint(_ context.Context, param params.Create
|
|||
}
|
||||
|
||||
if err := tx.Create(&endpoint).Error; err != nil {
|
||||
return errors.Wrap(err, "creating gitea endpoint")
|
||||
return fmt.Errorf("error creating gitea endpoint: %w", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return params.ForgeEndpoint{}, errors.Wrap(err, "creating gitea endpoint")
|
||||
return params.ForgeEndpoint{}, fmt.Errorf("error creating gitea endpoint: %w", err)
|
||||
}
|
||||
ghEndpoint, err = s.sqlToCommonGithubEndpoint(endpoint)
|
||||
if err != nil {
|
||||
return params.ForgeEndpoint{}, errors.Wrap(err, "converting gitea endpoint")
|
||||
return params.ForgeEndpoint{}, fmt.Errorf("error converting gitea endpoint: %w", err)
|
||||
}
|
||||
return ghEndpoint, nil
|
||||
}
|
||||
|
|
@ -66,14 +67,14 @@ func (s *sqlDatabase) ListGiteaEndpoints(_ context.Context) ([]params.ForgeEndpo
|
|||
var endpoints []GithubEndpoint
|
||||
err := s.conn.Where("endpoint_type = ?", params.GiteaEndpointType).Find(&endpoints).Error
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "fetching gitea endpoints")
|
||||
return nil, fmt.Errorf("error fetching gitea endpoints: %w", err)
|
||||
}
|
||||
|
||||
var ret []params.ForgeEndpoint
|
||||
for _, ep := range endpoints {
|
||||
commonEp, err := s.sqlToCommonGithubEndpoint(ep)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "converting gitea endpoint")
|
||||
return nil, fmt.Errorf("error converting gitea endpoint: %w", err)
|
||||
}
|
||||
ret = append(ret, commonEp)
|
||||
}
|
||||
|
|
@ -90,19 +91,19 @@ func (s *sqlDatabase) UpdateGiteaEndpoint(_ context.Context, name string, param
|
|||
err = s.conn.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Where("name = ? and endpoint_type = ?", name, params.GiteaEndpointType).First(&endpoint).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.Wrap(runnerErrors.ErrNotFound, "gitea endpoint not found")
|
||||
return runnerErrors.NewNotFoundError("gitea endpoint %q not found", name)
|
||||
}
|
||||
return errors.Wrap(err, "fetching gitea endpoint")
|
||||
return fmt.Errorf("error fetching gitea endpoint: %w", err)
|
||||
}
|
||||
|
||||
var credsCount int64
|
||||
if err := tx.Model(&GiteaCredentials{}).Where("endpoint_name = ?", endpoint.Name).Count(&credsCount).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.Wrap(err, "fetching gitea credentials")
|
||||
return fmt.Errorf("error fetching gitea credentials: %w", err)
|
||||
}
|
||||
}
|
||||
if credsCount > 0 && (param.APIBaseURL != nil || param.BaseURL != nil) {
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "cannot update endpoint URLs with existing credentials")
|
||||
return runnerErrors.NewBadRequestError("cannot update endpoint URLs with existing credentials")
|
||||
}
|
||||
|
||||
if param.APIBaseURL != nil {
|
||||
|
|
@ -122,17 +123,17 @@ func (s *sqlDatabase) UpdateGiteaEndpoint(_ context.Context, name string, param
|
|||
}
|
||||
|
||||
if err := tx.Save(&endpoint).Error; err != nil {
|
||||
return errors.Wrap(err, "updating gitea endpoint")
|
||||
return fmt.Errorf("error updating gitea endpoint: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return params.ForgeEndpoint{}, errors.Wrap(err, "updating gitea endpoint")
|
||||
return params.ForgeEndpoint{}, fmt.Errorf("error updating gitea endpoint: %w", err)
|
||||
}
|
||||
ghEndpoint, err = s.sqlToCommonGithubEndpoint(endpoint)
|
||||
if err != nil {
|
||||
return params.ForgeEndpoint{}, errors.Wrap(err, "converting gitea endpoint")
|
||||
return params.ForgeEndpoint{}, fmt.Errorf("error converting gitea endpoint: %w", err)
|
||||
}
|
||||
return ghEndpoint, nil
|
||||
}
|
||||
|
|
@ -142,9 +143,9 @@ func (s *sqlDatabase) GetGiteaEndpoint(_ context.Context, name string) (params.F
|
|||
err := s.conn.Where("name = ? and endpoint_type = ?", name, params.GiteaEndpointType).First(&endpoint).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return params.ForgeEndpoint{}, errors.Wrap(runnerErrors.ErrNotFound, "gitea endpoint not found")
|
||||
return params.ForgeEndpoint{}, runnerErrors.NewNotFoundError("gitea endpoint %q not found", name)
|
||||
}
|
||||
return params.ForgeEndpoint{}, errors.Wrap(err, "fetching gitea endpoint")
|
||||
return params.ForgeEndpoint{}, fmt.Errorf("error fetching gitea endpoint: %w", err)
|
||||
}
|
||||
|
||||
return s.sqlToCommonGithubEndpoint(endpoint)
|
||||
|
|
@ -162,41 +163,41 @@ func (s *sqlDatabase) DeleteGiteaEndpoint(_ context.Context, name string) (err e
|
|||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil
|
||||
}
|
||||
return errors.Wrap(err, "fetching gitea endpoint")
|
||||
return fmt.Errorf("error fetching gitea endpoint: %w", err)
|
||||
}
|
||||
|
||||
var credsCount int64
|
||||
if err := tx.Model(&GiteaCredentials{}).Where("endpoint_name = ?", endpoint.Name).Count(&credsCount).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.Wrap(err, "fetching gitea credentials")
|
||||
return fmt.Errorf("error fetching gitea credentials: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
var repoCnt int64
|
||||
if err := tx.Model(&Repository{}).Where("endpoint_name = ?", endpoint.Name).Count(&repoCnt).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.Wrap(err, "fetching gitea repositories")
|
||||
return fmt.Errorf("error fetching gitea repositories: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
var orgCnt int64
|
||||
if err := tx.Model(&Organization{}).Where("endpoint_name = ?", endpoint.Name).Count(&orgCnt).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.Wrap(err, "fetching gitea organizations")
|
||||
return fmt.Errorf("error fetching gitea organizations: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if credsCount > 0 || repoCnt > 0 || orgCnt > 0 {
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "cannot delete endpoint with associated entities")
|
||||
return runnerErrors.NewBadRequestError("cannot delete endpoint with associated entities")
|
||||
}
|
||||
|
||||
if err := tx.Unscoped().Delete(&endpoint).Error; err != nil {
|
||||
return errors.Wrap(err, "deleting gitea endpoint")
|
||||
return fmt.Errorf("error deleting gitea endpoint: %w", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "deleting gitea endpoint")
|
||||
return fmt.Errorf("error deleting gitea endpoint: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -204,10 +205,10 @@ func (s *sqlDatabase) DeleteGiteaEndpoint(_ context.Context, name string) (err e
|
|||
func (s *sqlDatabase) CreateGiteaCredentials(ctx context.Context, param params.CreateGiteaCredentialsParams) (gtCreds params.ForgeCredentials, err error) {
|
||||
userID, err := getUIDFromContext(ctx)
|
||||
if err != nil {
|
||||
return params.ForgeCredentials{}, errors.Wrap(err, "creating gitea credentials")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("error creating gitea credentials: %w", err)
|
||||
}
|
||||
if param.Endpoint == "" {
|
||||
return params.ForgeCredentials{}, errors.Wrap(runnerErrors.ErrBadRequest, "endpoint name is required")
|
||||
return params.ForgeCredentials{}, runnerErrors.NewBadRequestError("endpoint name is required")
|
||||
}
|
||||
|
||||
defer func() {
|
||||
|
|
@ -220,13 +221,13 @@ func (s *sqlDatabase) CreateGiteaCredentials(ctx context.Context, param params.C
|
|||
var endpoint GithubEndpoint
|
||||
if err := tx.Where("name = ? and endpoint_type = ?", param.Endpoint, params.GiteaEndpointType).First(&endpoint).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.Wrap(runnerErrors.ErrNotFound, "gitea endpoint not found")
|
||||
return runnerErrors.NewNotFoundError("gitea endpoint %q not found", param.Endpoint)
|
||||
}
|
||||
return errors.Wrap(err, "fetching gitea endpoint")
|
||||
return fmt.Errorf("error fetching gitea endpoint: %w", err)
|
||||
}
|
||||
|
||||
if err := tx.Where("name = ? and user_id = ?", param.Name, userID).First(&creds).Error; err == nil {
|
||||
return errors.Wrap(runnerErrors.ErrDuplicateEntity, "gitea credentials already exists")
|
||||
return fmt.Errorf("gitea credentials already exists: %w", runnerErrors.ErrDuplicateEntity)
|
||||
}
|
||||
|
||||
var data []byte
|
||||
|
|
@ -235,10 +236,10 @@ func (s *sqlDatabase) CreateGiteaCredentials(ctx context.Context, param params.C
|
|||
case params.ForgeAuthTypePAT:
|
||||
data, err = s.marshalAndSeal(param.PAT)
|
||||
default:
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "invalid auth type")
|
||||
return runnerErrors.NewBadRequestError("invalid auth type %q", param.AuthType)
|
||||
}
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "marshaling and sealing credentials")
|
||||
return fmt.Errorf("error marshaling and sealing credentials: %w", err)
|
||||
}
|
||||
|
||||
creds = GiteaCredentials{
|
||||
|
|
@ -251,7 +252,7 @@ func (s *sqlDatabase) CreateGiteaCredentials(ctx context.Context, param params.C
|
|||
}
|
||||
|
||||
if err := tx.Create(&creds).Error; err != nil {
|
||||
return errors.Wrap(err, "creating gitea credentials")
|
||||
return fmt.Errorf("error creating gitea credentials: %w", err)
|
||||
}
|
||||
// Skip making an extra query.
|
||||
creds.Endpoint = endpoint
|
||||
|
|
@ -259,11 +260,11 @@ func (s *sqlDatabase) CreateGiteaCredentials(ctx context.Context, param params.C
|
|||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return params.ForgeCredentials{}, errors.Wrap(err, "creating gitea credentials")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("error creating gitea credentials: %w", err)
|
||||
}
|
||||
gtCreds, err = s.sqlGiteaToCommonForgeCredentials(creds)
|
||||
if err != nil {
|
||||
return params.ForgeCredentials{}, errors.Wrap(err, "converting gitea credentials")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("error converting gitea credentials: %w", err)
|
||||
}
|
||||
return gtCreds, nil
|
||||
}
|
||||
|
|
@ -284,16 +285,16 @@ func (s *sqlDatabase) getGiteaCredentialsByName(ctx context.Context, tx *gorm.DB
|
|||
|
||||
userID, err := getUIDFromContext(ctx)
|
||||
if err != nil {
|
||||
return GiteaCredentials{}, errors.Wrap(err, "fetching gitea credentials")
|
||||
return GiteaCredentials{}, fmt.Errorf("error fetching gitea credentials: %w", err)
|
||||
}
|
||||
q = q.Where("user_id = ?", userID)
|
||||
|
||||
err = q.Where("name = ?", name).First(&creds).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return GiteaCredentials{}, errors.Wrap(runnerErrors.ErrNotFound, "gitea credentials not found")
|
||||
return GiteaCredentials{}, runnerErrors.NewNotFoundError("gitea credentials %q not found", name)
|
||||
}
|
||||
return GiteaCredentials{}, errors.Wrap(err, "fetching gitea credentials")
|
||||
return GiteaCredentials{}, fmt.Errorf("error fetching gitea credentials: %w", err)
|
||||
}
|
||||
|
||||
return creds, nil
|
||||
|
|
@ -302,7 +303,7 @@ func (s *sqlDatabase) getGiteaCredentialsByName(ctx context.Context, tx *gorm.DB
|
|||
func (s *sqlDatabase) GetGiteaCredentialsByName(ctx context.Context, name string, detailed bool) (params.ForgeCredentials, error) {
|
||||
creds, err := s.getGiteaCredentialsByName(ctx, s.conn, name, detailed)
|
||||
if err != nil {
|
||||
return params.ForgeCredentials{}, errors.Wrap(err, "fetching gitea credentials")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("error fetching gitea credentials: %w", err)
|
||||
}
|
||||
|
||||
return s.sqlGiteaToCommonForgeCredentials(creds)
|
||||
|
|
@ -325,7 +326,7 @@ func (s *sqlDatabase) GetGiteaCredentials(ctx context.Context, id uint, detailed
|
|||
if !auth.IsAdmin(ctx) {
|
||||
userID, err := getUIDFromContext(ctx)
|
||||
if err != nil {
|
||||
return params.ForgeCredentials{}, errors.Wrap(err, "fetching gitea credentials")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("error fetching gitea credentials: %w", err)
|
||||
}
|
||||
q = q.Where("user_id = ?", userID)
|
||||
}
|
||||
|
|
@ -333,9 +334,9 @@ func (s *sqlDatabase) GetGiteaCredentials(ctx context.Context, id uint, detailed
|
|||
err := q.Where("id = ?", id).First(&creds).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return params.ForgeCredentials{}, errors.Wrap(runnerErrors.ErrNotFound, "gitea credentials not found")
|
||||
return params.ForgeCredentials{}, runnerErrors.NewNotFoundError("gitea credentials not found")
|
||||
}
|
||||
return params.ForgeCredentials{}, errors.Wrap(err, "fetching gitea credentials")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("error fetching gitea credentials: %w", err)
|
||||
}
|
||||
|
||||
return s.sqlGiteaToCommonForgeCredentials(creds)
|
||||
|
|
@ -346,7 +347,7 @@ func (s *sqlDatabase) ListGiteaCredentials(ctx context.Context) ([]params.ForgeC
|
|||
if !auth.IsAdmin(ctx) {
|
||||
userID, err := getUIDFromContext(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "fetching gitea credentials")
|
||||
return nil, fmt.Errorf("error fetching gitea credentials: %w", err)
|
||||
}
|
||||
q = q.Where("user_id = ?", userID)
|
||||
}
|
||||
|
|
@ -354,14 +355,14 @@ func (s *sqlDatabase) ListGiteaCredentials(ctx context.Context) ([]params.ForgeC
|
|||
var creds []GiteaCredentials
|
||||
err := q.Preload("Endpoint").Find(&creds).Error
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "fetching gitea credentials")
|
||||
return nil, fmt.Errorf("error fetching gitea credentials: %w", err)
|
||||
}
|
||||
|
||||
var ret []params.ForgeCredentials
|
||||
for _, c := range creds {
|
||||
commonCreds, err := s.sqlGiteaToCommonForgeCredentials(c)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "converting gitea credentials")
|
||||
return nil, fmt.Errorf("error converting gitea credentials: %w", err)
|
||||
}
|
||||
ret = append(ret, commonCreds)
|
||||
}
|
||||
|
|
@ -380,16 +381,16 @@ func (s *sqlDatabase) UpdateGiteaCredentials(ctx context.Context, id uint, param
|
|||
if !auth.IsAdmin(ctx) {
|
||||
userID, err := getUIDFromContext(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "updating gitea credentials")
|
||||
return fmt.Errorf("error updating gitea credentials: %w", err)
|
||||
}
|
||||
q = q.Where("user_id = ?", userID)
|
||||
}
|
||||
|
||||
if err := q.Where("id = ?", id).First(&creds).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.Wrap(runnerErrors.ErrNotFound, "gitea credentials not found")
|
||||
return runnerErrors.NewNotFoundError("gitea credentials not found")
|
||||
}
|
||||
return errors.Wrap(err, "fetching gitea credentials")
|
||||
return fmt.Errorf("error fetching gitea credentials: %w", err)
|
||||
}
|
||||
|
||||
if param.Name != nil {
|
||||
|
|
@ -407,28 +408,28 @@ func (s *sqlDatabase) UpdateGiteaCredentials(ctx context.Context, id uint, param
|
|||
data, err = s.marshalAndSeal(param.PAT)
|
||||
}
|
||||
default:
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "invalid auth type")
|
||||
return runnerErrors.NewBadRequestError("invalid auth type %q", creds.AuthType)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "marshaling and sealing credentials")
|
||||
return fmt.Errorf("error marshaling and sealing credentials: %w", err)
|
||||
}
|
||||
if len(data) > 0 {
|
||||
creds.Payload = data
|
||||
}
|
||||
|
||||
if err := tx.Save(&creds).Error; err != nil {
|
||||
return errors.Wrap(err, "updating gitea credentials")
|
||||
return fmt.Errorf("error updating gitea credentials: %w", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return params.ForgeCredentials{}, errors.Wrap(err, "updating gitea credentials")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("error updating gitea credentials: %w", err)
|
||||
}
|
||||
|
||||
gtCreds, err = s.sqlGiteaToCommonForgeCredentials(creds)
|
||||
if err != nil {
|
||||
return params.ForgeCredentials{}, errors.Wrap(err, "converting gitea credentials")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("error converting gitea credentials: %w", err)
|
||||
}
|
||||
return gtCreds, nil
|
||||
}
|
||||
|
|
@ -454,7 +455,7 @@ func (s *sqlDatabase) DeleteGiteaCredentials(ctx context.Context, id uint) (err
|
|||
if !auth.IsAdmin(ctx) {
|
||||
userID, err := getUIDFromContext(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "deleting gitea credentials")
|
||||
return fmt.Errorf("error deleting gitea credentials: %w", err)
|
||||
}
|
||||
q = q.Where("user_id = ?", userID)
|
||||
}
|
||||
|
|
@ -464,22 +465,22 @@ func (s *sqlDatabase) DeleteGiteaCredentials(ctx context.Context, id uint) (err
|
|||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil
|
||||
}
|
||||
return errors.Wrap(err, "fetching gitea credentials")
|
||||
return fmt.Errorf("error fetching gitea credentials: %w", err)
|
||||
}
|
||||
|
||||
if len(creds.Repositories) > 0 {
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "cannot delete credentials with repositories")
|
||||
return runnerErrors.NewBadRequestError("cannot delete credentials with repositories")
|
||||
}
|
||||
if len(creds.Organizations) > 0 {
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "cannot delete credentials with organizations")
|
||||
return runnerErrors.NewBadRequestError("cannot delete credentials with organizations")
|
||||
}
|
||||
if err := tx.Unscoped().Delete(&creds).Error; err != nil {
|
||||
return errors.Wrap(err, "deleting gitea credentials")
|
||||
return fmt.Errorf("error deleting gitea credentials: %w", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "deleting gitea credentials")
|
||||
return fmt.Errorf("error deleting gitea credentials: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -236,7 +236,7 @@ func (s *GiteaTestSuite) TestCreateCredentialsFailsWhenEndpointDoesNotExist() {
|
|||
_, err := s.db.CreateGiteaCredentials(ctx, params.CreateGiteaCredentialsParams{Endpoint: "non-existing"})
|
||||
s.Require().Error(err)
|
||||
s.Require().ErrorIs(err, runnerErrors.ErrNotFound)
|
||||
s.Require().Regexp("endpoint not found", err.Error())
|
||||
s.Require().Regexp("error creating gitea credentials: gitea endpoint \"non-existing\" not found", err.Error())
|
||||
}
|
||||
|
||||
func (s *GiteaTestSuite) TestCreateCredentialsFailsWhenAuthTypeIsInvalid() {
|
||||
|
|
@ -807,7 +807,7 @@ func (s *GiteaTestSuite) TestUpdateEndpointURLsFailsIfCredentialsAreAssociated()
|
|||
_, err = s.db.UpdateGiteaEndpoint(ctx, testEndpointName, updateEpParams)
|
||||
s.Require().Error(err)
|
||||
s.Require().ErrorIs(err, runnerErrors.ErrBadRequest)
|
||||
s.Require().EqualError(err, "updating gitea endpoint: cannot update endpoint URLs with existing credentials: invalid request")
|
||||
s.Require().EqualError(err, "error updating gitea endpoint: cannot update endpoint URLs with existing credentials")
|
||||
|
||||
updateEpParams = params.UpdateGiteaEndpointParams{
|
||||
APIBaseURL: &newAPIBaseURL,
|
||||
|
|
@ -815,7 +815,7 @@ func (s *GiteaTestSuite) TestUpdateEndpointURLsFailsIfCredentialsAreAssociated()
|
|||
_, err = s.db.UpdateGiteaEndpoint(ctx, testEndpointName, updateEpParams)
|
||||
s.Require().Error(err)
|
||||
s.Require().ErrorIs(err, runnerErrors.ErrBadRequest)
|
||||
s.Require().EqualError(err, "updating gitea endpoint: cannot update endpoint URLs with existing credentials: invalid request")
|
||||
s.Require().EqualError(err, "error updating gitea endpoint: cannot update endpoint URLs with existing credentials")
|
||||
|
||||
updateEpParams = params.UpdateGiteaEndpointParams{
|
||||
Description: &newDescription,
|
||||
|
|
|
|||
|
|
@ -16,8 +16,9 @@ package sql
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
|
||||
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
|
||||
|
|
@ -35,7 +36,7 @@ func (s *sqlDatabase) CreateGithubEndpoint(_ context.Context, param params.Creat
|
|||
var endpoint GithubEndpoint
|
||||
err = s.conn.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Where("name = ?", param.Name).First(&endpoint).Error; err == nil {
|
||||
return errors.Wrap(runnerErrors.ErrDuplicateEntity, "github endpoint already exists")
|
||||
return fmt.Errorf("error github endpoint already exists: %w", runnerErrors.ErrDuplicateEntity)
|
||||
}
|
||||
endpoint = GithubEndpoint{
|
||||
Name: param.Name,
|
||||
|
|
@ -48,16 +49,16 @@ func (s *sqlDatabase) CreateGithubEndpoint(_ context.Context, param params.Creat
|
|||
}
|
||||
|
||||
if err := tx.Create(&endpoint).Error; err != nil {
|
||||
return errors.Wrap(err, "creating github endpoint")
|
||||
return fmt.Errorf("error creating github endpoint: %w", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return params.ForgeEndpoint{}, errors.Wrap(err, "creating github endpoint")
|
||||
return params.ForgeEndpoint{}, fmt.Errorf("error creating github endpoint: %w", err)
|
||||
}
|
||||
ghEndpoint, err = s.sqlToCommonGithubEndpoint(endpoint)
|
||||
if err != nil {
|
||||
return params.ForgeEndpoint{}, errors.Wrap(err, "converting github endpoint")
|
||||
return params.ForgeEndpoint{}, fmt.Errorf("error converting github endpoint: %w", err)
|
||||
}
|
||||
return ghEndpoint, nil
|
||||
}
|
||||
|
|
@ -66,14 +67,14 @@ func (s *sqlDatabase) ListGithubEndpoints(_ context.Context) ([]params.ForgeEndp
|
|||
var endpoints []GithubEndpoint
|
||||
err := s.conn.Where("endpoint_type = ?", params.GithubEndpointType).Find(&endpoints).Error
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "fetching github endpoints")
|
||||
return nil, fmt.Errorf("error fetching github endpoints: %w", err)
|
||||
}
|
||||
|
||||
var ret []params.ForgeEndpoint
|
||||
for _, ep := range endpoints {
|
||||
commonEp, err := s.sqlToCommonGithubEndpoint(ep)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "converting github endpoint")
|
||||
return nil, fmt.Errorf("error converting github endpoint: %w", err)
|
||||
}
|
||||
ret = append(ret, commonEp)
|
||||
}
|
||||
|
|
@ -90,19 +91,19 @@ func (s *sqlDatabase) UpdateGithubEndpoint(_ context.Context, name string, param
|
|||
err = s.conn.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Where("name = ? and endpoint_type = ?", name, params.GithubEndpointType).First(&endpoint).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.Wrap(runnerErrors.ErrNotFound, "github endpoint not found")
|
||||
return fmt.Errorf("error github endpoint not found: %w", runnerErrors.ErrNotFound)
|
||||
}
|
||||
return errors.Wrap(err, "fetching github endpoint")
|
||||
return fmt.Errorf("error fetching github endpoint: %w", err)
|
||||
}
|
||||
|
||||
var credsCount int64
|
||||
if err := tx.Model(&GithubCredentials{}).Where("endpoint_name = ?", endpoint.Name).Count(&credsCount).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.Wrap(err, "fetching github credentials")
|
||||
return fmt.Errorf("error fetching github credentials: %w", err)
|
||||
}
|
||||
}
|
||||
if credsCount > 0 && (param.APIBaseURL != nil || param.BaseURL != nil || param.UploadBaseURL != nil) {
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "cannot update endpoint URLs with existing credentials")
|
||||
return fmt.Errorf("cannot update endpoint URLs with existing credentials: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
|
||||
if param.APIBaseURL != nil {
|
||||
|
|
@ -126,17 +127,17 @@ func (s *sqlDatabase) UpdateGithubEndpoint(_ context.Context, name string, param
|
|||
}
|
||||
|
||||
if err := tx.Save(&endpoint).Error; err != nil {
|
||||
return errors.Wrap(err, "updating github endpoint")
|
||||
return fmt.Errorf("error updating github endpoint: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return params.ForgeEndpoint{}, errors.Wrap(err, "updating github endpoint")
|
||||
return params.ForgeEndpoint{}, fmt.Errorf("error updating github endpoint: %w", err)
|
||||
}
|
||||
ghEndpoint, err = s.sqlToCommonGithubEndpoint(endpoint)
|
||||
if err != nil {
|
||||
return params.ForgeEndpoint{}, errors.Wrap(err, "converting github endpoint")
|
||||
return params.ForgeEndpoint{}, fmt.Errorf("error converting github endpoint: %w", err)
|
||||
}
|
||||
return ghEndpoint, nil
|
||||
}
|
||||
|
|
@ -147,9 +148,9 @@ func (s *sqlDatabase) GetGithubEndpoint(_ context.Context, name string) (params.
|
|||
err := s.conn.Where("name = ? and endpoint_type = ?", name, params.GithubEndpointType).First(&endpoint).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return params.ForgeEndpoint{}, errors.Wrap(runnerErrors.ErrNotFound, "github endpoint not found")
|
||||
return params.ForgeEndpoint{}, fmt.Errorf("github endpoint not found: %w", runnerErrors.ErrNotFound)
|
||||
}
|
||||
return params.ForgeEndpoint{}, errors.Wrap(err, "fetching github endpoint")
|
||||
return params.ForgeEndpoint{}, fmt.Errorf("error fetching github endpoint: %w", err)
|
||||
}
|
||||
|
||||
return s.sqlToCommonGithubEndpoint(endpoint)
|
||||
|
|
@ -167,48 +168,48 @@ func (s *sqlDatabase) DeleteGithubEndpoint(_ context.Context, name string) (err
|
|||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil
|
||||
}
|
||||
return errors.Wrap(err, "fetching github endpoint")
|
||||
return fmt.Errorf("error fetching github endpoint: %w", err)
|
||||
}
|
||||
|
||||
var credsCount int64
|
||||
if err := tx.Model(&GithubCredentials{}).Where("endpoint_name = ?", endpoint.Name).Count(&credsCount).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.Wrap(err, "fetching github credentials")
|
||||
return fmt.Errorf("error fetching github credentials: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
var repoCnt int64
|
||||
if err := tx.Model(&Repository{}).Where("endpoint_name = ?", endpoint.Name).Count(&repoCnt).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.Wrap(err, "fetching github repositories")
|
||||
return fmt.Errorf("error fetching github repositories: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
var orgCnt int64
|
||||
if err := tx.Model(&Organization{}).Where("endpoint_name = ?", endpoint.Name).Count(&orgCnt).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.Wrap(err, "fetching github organizations")
|
||||
return fmt.Errorf("error fetching github organizations: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
var entCnt int64
|
||||
if err := tx.Model(&Enterprise{}).Where("endpoint_name = ?", endpoint.Name).Count(&entCnt).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.Wrap(err, "fetching github enterprises")
|
||||
return fmt.Errorf("error fetching github enterprises: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if credsCount > 0 || repoCnt > 0 || orgCnt > 0 || entCnt > 0 {
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "cannot delete endpoint with associated entities")
|
||||
return fmt.Errorf("cannot delete endpoint with associated entities: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
|
||||
if err := tx.Unscoped().Delete(&endpoint).Error; err != nil {
|
||||
return errors.Wrap(err, "deleting github endpoint")
|
||||
return fmt.Errorf("error deleting github endpoint: %w", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "deleting github endpoint")
|
||||
return fmt.Errorf("error deleting github endpoint: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -216,10 +217,10 @@ func (s *sqlDatabase) DeleteGithubEndpoint(_ context.Context, name string) (err
|
|||
func (s *sqlDatabase) CreateGithubCredentials(ctx context.Context, param params.CreateGithubCredentialsParams) (ghCreds params.ForgeCredentials, err error) {
|
||||
userID, err := getUIDFromContext(ctx)
|
||||
if err != nil {
|
||||
return params.ForgeCredentials{}, errors.Wrap(err, "creating github credentials")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("error creating github credentials: %w", err)
|
||||
}
|
||||
if param.Endpoint == "" {
|
||||
return params.ForgeCredentials{}, errors.Wrap(runnerErrors.ErrBadRequest, "endpoint name is required")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("endpoint name is required: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
|
|
@ -232,13 +233,13 @@ func (s *sqlDatabase) CreateGithubCredentials(ctx context.Context, param params.
|
|||
var endpoint GithubEndpoint
|
||||
if err := tx.Where("name = ? and endpoint_type = ?", param.Endpoint, params.GithubEndpointType).First(&endpoint).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.Wrap(runnerErrors.ErrNotFound, "github endpoint not found")
|
||||
return fmt.Errorf("github endpoint not found: %w", runnerErrors.ErrNotFound)
|
||||
}
|
||||
return errors.Wrap(err, "fetching github endpoint")
|
||||
return fmt.Errorf("error fetching github endpoint: %w", err)
|
||||
}
|
||||
|
||||
if err := tx.Where("name = ? and user_id = ?", param.Name, userID).First(&creds).Error; err == nil {
|
||||
return errors.Wrap(runnerErrors.ErrDuplicateEntity, "github credentials already exists")
|
||||
return fmt.Errorf("github credentials already exists: %w", runnerErrors.ErrDuplicateEntity)
|
||||
}
|
||||
|
||||
var data []byte
|
||||
|
|
@ -249,10 +250,10 @@ func (s *sqlDatabase) CreateGithubCredentials(ctx context.Context, param params.
|
|||
case params.ForgeAuthTypeApp:
|
||||
data, err = s.marshalAndSeal(param.App)
|
||||
default:
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "invalid auth type")
|
||||
return fmt.Errorf("invalid auth type: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "marshaling and sealing credentials")
|
||||
return fmt.Errorf("error marshaling and sealing credentials: %w", err)
|
||||
}
|
||||
|
||||
creds = GithubCredentials{
|
||||
|
|
@ -265,7 +266,7 @@ func (s *sqlDatabase) CreateGithubCredentials(ctx context.Context, param params.
|
|||
}
|
||||
|
||||
if err := tx.Create(&creds).Error; err != nil {
|
||||
return errors.Wrap(err, "creating github credentials")
|
||||
return fmt.Errorf("error creating github credentials: %w", err)
|
||||
}
|
||||
// Skip making an extra query.
|
||||
creds.Endpoint = endpoint
|
||||
|
|
@ -273,11 +274,11 @@ func (s *sqlDatabase) CreateGithubCredentials(ctx context.Context, param params.
|
|||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return params.ForgeCredentials{}, errors.Wrap(err, "creating github credentials")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("error creating github credentials: %w", err)
|
||||
}
|
||||
ghCreds, err = s.sqlToCommonForgeCredentials(creds)
|
||||
if err != nil {
|
||||
return params.ForgeCredentials{}, errors.Wrap(err, "converting github credentials")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("error converting github credentials: %w", err)
|
||||
}
|
||||
return ghCreds, nil
|
||||
}
|
||||
|
|
@ -298,16 +299,16 @@ func (s *sqlDatabase) getGithubCredentialsByName(ctx context.Context, tx *gorm.D
|
|||
|
||||
userID, err := getUIDFromContext(ctx)
|
||||
if err != nil {
|
||||
return GithubCredentials{}, errors.Wrap(err, "fetching github credentials")
|
||||
return GithubCredentials{}, fmt.Errorf("error fetching github credentials: %w", err)
|
||||
}
|
||||
q = q.Where("user_id = ?", userID)
|
||||
|
||||
err = q.Where("name = ?", name).First(&creds).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return GithubCredentials{}, errors.Wrap(runnerErrors.ErrNotFound, "github credentials not found")
|
||||
return GithubCredentials{}, fmt.Errorf("github credentials not found: %w", runnerErrors.ErrNotFound)
|
||||
}
|
||||
return GithubCredentials{}, errors.Wrap(err, "fetching github credentials")
|
||||
return GithubCredentials{}, fmt.Errorf("error fetching github credentials: %w", err)
|
||||
}
|
||||
|
||||
return creds, nil
|
||||
|
|
@ -316,7 +317,7 @@ func (s *sqlDatabase) getGithubCredentialsByName(ctx context.Context, tx *gorm.D
|
|||
func (s *sqlDatabase) GetGithubCredentialsByName(ctx context.Context, name string, detailed bool) (params.ForgeCredentials, error) {
|
||||
creds, err := s.getGithubCredentialsByName(ctx, s.conn, name, detailed)
|
||||
if err != nil {
|
||||
return params.ForgeCredentials{}, errors.Wrap(err, "fetching github credentials")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("error fetching github credentials: %w", err)
|
||||
}
|
||||
return s.sqlToCommonForgeCredentials(creds)
|
||||
}
|
||||
|
|
@ -338,7 +339,7 @@ func (s *sqlDatabase) GetGithubCredentials(ctx context.Context, id uint, detaile
|
|||
if !auth.IsAdmin(ctx) {
|
||||
userID, err := getUIDFromContext(ctx)
|
||||
if err != nil {
|
||||
return params.ForgeCredentials{}, errors.Wrap(err, "fetching github credentials")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("error fetching github credentials: %w", err)
|
||||
}
|
||||
q = q.Where("user_id = ?", userID)
|
||||
}
|
||||
|
|
@ -346,9 +347,9 @@ func (s *sqlDatabase) GetGithubCredentials(ctx context.Context, id uint, detaile
|
|||
err := q.Where("id = ?", id).First(&creds).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return params.ForgeCredentials{}, errors.Wrap(runnerErrors.ErrNotFound, "github credentials not found")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("github credentials not found: %w", runnerErrors.ErrNotFound)
|
||||
}
|
||||
return params.ForgeCredentials{}, errors.Wrap(err, "fetching github credentials")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("error fetching github credentials: %w", err)
|
||||
}
|
||||
|
||||
return s.sqlToCommonForgeCredentials(creds)
|
||||
|
|
@ -359,7 +360,7 @@ func (s *sqlDatabase) ListGithubCredentials(ctx context.Context) ([]params.Forge
|
|||
if !auth.IsAdmin(ctx) {
|
||||
userID, err := getUIDFromContext(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "fetching github credentials")
|
||||
return nil, fmt.Errorf("error fetching github credentials: %w", err)
|
||||
}
|
||||
q = q.Where("user_id = ?", userID)
|
||||
}
|
||||
|
|
@ -367,14 +368,14 @@ func (s *sqlDatabase) ListGithubCredentials(ctx context.Context) ([]params.Forge
|
|||
var creds []GithubCredentials
|
||||
err := q.Preload("Endpoint").Find(&creds).Error
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "fetching github credentials")
|
||||
return nil, fmt.Errorf("error fetching github credentials: %w", err)
|
||||
}
|
||||
|
||||
var ret []params.ForgeCredentials
|
||||
for _, c := range creds {
|
||||
commonCreds, err := s.sqlToCommonForgeCredentials(c)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "converting github credentials")
|
||||
return nil, fmt.Errorf("error converting github credentials: %w", err)
|
||||
}
|
||||
ret = append(ret, commonCreds)
|
||||
}
|
||||
|
|
@ -393,16 +394,16 @@ func (s *sqlDatabase) UpdateGithubCredentials(ctx context.Context, id uint, para
|
|||
if !auth.IsAdmin(ctx) {
|
||||
userID, err := getUIDFromContext(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "updating github credentials")
|
||||
return fmt.Errorf("error updating github credentials: %w", err)
|
||||
}
|
||||
q = q.Where("user_id = ?", userID)
|
||||
}
|
||||
|
||||
if err := q.Where("id = ?", id).First(&creds).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.Wrap(runnerErrors.ErrNotFound, "github credentials not found")
|
||||
return fmt.Errorf("github credentials not found: %w", runnerErrors.ErrNotFound)
|
||||
}
|
||||
return errors.Wrap(err, "fetching github credentials")
|
||||
return fmt.Errorf("error fetching github credentials: %w", err)
|
||||
}
|
||||
|
||||
if param.Name != nil {
|
||||
|
|
@ -421,7 +422,7 @@ func (s *sqlDatabase) UpdateGithubCredentials(ctx context.Context, id uint, para
|
|||
}
|
||||
|
||||
if param.App != nil {
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "cannot update app credentials for PAT")
|
||||
return fmt.Errorf("cannot update app credentials for PAT: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
case params.ForgeAuthTypeApp:
|
||||
if param.App != nil {
|
||||
|
|
@ -429,33 +430,33 @@ func (s *sqlDatabase) UpdateGithubCredentials(ctx context.Context, id uint, para
|
|||
}
|
||||
|
||||
if param.PAT != nil {
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "cannot update PAT credentials for app")
|
||||
return fmt.Errorf("cannot update PAT credentials for app: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
default:
|
||||
// This should never happen, unless there was a bug in the DB migration code,
|
||||
// or the DB was manually modified.
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "invalid auth type")
|
||||
return fmt.Errorf("invalid auth type: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "marshaling and sealing credentials")
|
||||
return fmt.Errorf("error marshaling and sealing credentials: %w", err)
|
||||
}
|
||||
if len(data) > 0 {
|
||||
creds.Payload = data
|
||||
}
|
||||
|
||||
if err := tx.Save(&creds).Error; err != nil {
|
||||
return errors.Wrap(err, "updating github credentials")
|
||||
return fmt.Errorf("error updating github credentials: %w", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return params.ForgeCredentials{}, errors.Wrap(err, "updating github credentials")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("error updating github credentials: %w", err)
|
||||
}
|
||||
|
||||
ghCreds, err = s.sqlToCommonForgeCredentials(creds)
|
||||
if err != nil {
|
||||
return params.ForgeCredentials{}, errors.Wrap(err, "converting github credentials")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("error converting github credentials: %w", err)
|
||||
}
|
||||
return ghCreds, nil
|
||||
}
|
||||
|
|
@ -475,7 +476,7 @@ func (s *sqlDatabase) DeleteGithubCredentials(ctx context.Context, id uint) (err
|
|||
if !auth.IsAdmin(ctx) {
|
||||
userID, err := getUIDFromContext(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "deleting github credentials")
|
||||
return fmt.Errorf("error deleting github credentials: %w", err)
|
||||
}
|
||||
q = q.Where("user_id = ?", userID)
|
||||
}
|
||||
|
|
@ -486,27 +487,27 @@ func (s *sqlDatabase) DeleteGithubCredentials(ctx context.Context, id uint) (err
|
|||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil
|
||||
}
|
||||
return errors.Wrap(err, "fetching github credentials")
|
||||
return fmt.Errorf("error fetching github credentials: %w", err)
|
||||
}
|
||||
name = creds.Name
|
||||
|
||||
if len(creds.Repositories) > 0 {
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "cannot delete credentials with repositories")
|
||||
return fmt.Errorf("cannot delete credentials with repositories: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
if len(creds.Organizations) > 0 {
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "cannot delete credentials with organizations")
|
||||
return fmt.Errorf("cannot delete credentials with organizations: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
if len(creds.Enterprises) > 0 {
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "cannot delete credentials with enterprises")
|
||||
return fmt.Errorf("cannot delete credentials with enterprises: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
|
||||
if err := tx.Unscoped().Delete(&creds).Error; err != nil {
|
||||
return errors.Wrap(err, "deleting github credentials")
|
||||
return fmt.Errorf("error deleting github credentials: %w", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "deleting github credentials")
|
||||
return fmt.Errorf("error deleting github credentials: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -265,7 +265,7 @@ func (s *GithubTestSuite) TestUpdateEndpointURLsFailsIfCredentialsAreAssociated(
|
|||
_, err = s.db.UpdateGithubEndpoint(ctx, testEndpointName, updateEpParams)
|
||||
s.Require().Error(err)
|
||||
s.Require().ErrorIs(err, runnerErrors.ErrBadRequest)
|
||||
s.Require().EqualError(err, "updating github endpoint: cannot update endpoint URLs with existing credentials: invalid request")
|
||||
s.Require().EqualError(err, "error updating github endpoint: cannot update endpoint URLs with existing credentials: invalid request")
|
||||
|
||||
updateEpParams = params.UpdateGithubEndpointParams{
|
||||
UploadBaseURL: &newUploadBaseURL,
|
||||
|
|
@ -274,7 +274,7 @@ func (s *GithubTestSuite) TestUpdateEndpointURLsFailsIfCredentialsAreAssociated(
|
|||
_, err = s.db.UpdateGithubEndpoint(ctx, testEndpointName, updateEpParams)
|
||||
s.Require().Error(err)
|
||||
s.Require().ErrorIs(err, runnerErrors.ErrBadRequest)
|
||||
s.Require().EqualError(err, "updating github endpoint: cannot update endpoint URLs with existing credentials: invalid request")
|
||||
s.Require().EqualError(err, "error updating github endpoint: cannot update endpoint URLs with existing credentials: invalid request")
|
||||
|
||||
updateEpParams = params.UpdateGithubEndpointParams{
|
||||
APIBaseURL: &newAPIBaseURL,
|
||||
|
|
@ -282,7 +282,7 @@ func (s *GithubTestSuite) TestUpdateEndpointURLsFailsIfCredentialsAreAssociated(
|
|||
_, err = s.db.UpdateGithubEndpoint(ctx, testEndpointName, updateEpParams)
|
||||
s.Require().Error(err)
|
||||
s.Require().ErrorIs(err, runnerErrors.ErrBadRequest)
|
||||
s.Require().EqualError(err, "updating github endpoint: cannot update endpoint URLs with existing credentials: invalid request")
|
||||
s.Require().EqualError(err, "error updating github endpoint: cannot update endpoint URLs with existing credentials: invalid request")
|
||||
|
||||
updateEpParams = params.UpdateGithubEndpointParams{
|
||||
Description: &newDescription,
|
||||
|
|
@ -737,7 +737,7 @@ func (s *GithubTestSuite) TestUpdateGithubCredentialsFailIfWrongCredentialTypeIs
|
|||
_, err = s.db.UpdateGithubCredentials(ctx, creds.ID, updateCredParams)
|
||||
s.Require().Error(err)
|
||||
s.Require().ErrorIs(err, runnerErrors.ErrBadRequest)
|
||||
s.Require().EqualError(err, "updating github credentials: cannot update app credentials for PAT: invalid request")
|
||||
s.Require().EqualError(err, "error updating github credentials: cannot update app credentials for PAT: invalid request")
|
||||
|
||||
credParamsWithApp := params.CreateGithubCredentialsParams{
|
||||
Name: "test-credsApp",
|
||||
|
|
@ -764,7 +764,7 @@ func (s *GithubTestSuite) TestUpdateGithubCredentialsFailIfWrongCredentialTypeIs
|
|||
_, err = s.db.UpdateGithubCredentials(ctx, credsApp.ID, updateCredParams)
|
||||
s.Require().Error(err)
|
||||
s.Require().ErrorIs(err, runnerErrors.ErrBadRequest)
|
||||
s.Require().EqualError(err, "updating github credentials: cannot update PAT credentials for app: invalid request")
|
||||
s.Require().EqualError(err, "error updating github credentials: cannot update PAT credentials for app: invalid request")
|
||||
}
|
||||
|
||||
func (s *GithubTestSuite) TestUpdateCredentialsFailsForNonExistingCredentials() {
|
||||
|
|
|
|||
|
|
@ -17,10 +17,11 @@ package sql
|
|||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
|
|
@ -33,7 +34,7 @@ import (
|
|||
func (s *sqlDatabase) CreateInstance(_ context.Context, poolID string, param params.CreateInstanceParams) (instance params.Instance, err error) {
|
||||
pool, err := s.getPoolByID(s.conn, poolID)
|
||||
if err != nil {
|
||||
return params.Instance{}, errors.Wrap(err, "fetching pool")
|
||||
return params.Instance{}, fmt.Errorf("error fetching pool: %w", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
|
|
@ -46,7 +47,7 @@ func (s *sqlDatabase) CreateInstance(_ context.Context, poolID string, param par
|
|||
if len(param.AditionalLabels) > 0 {
|
||||
labels, err = json.Marshal(param.AditionalLabels)
|
||||
if err != nil {
|
||||
return params.Instance{}, errors.Wrap(err, "marshalling labels")
|
||||
return params.Instance{}, fmt.Errorf("error marshalling labels: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -54,7 +55,7 @@ func (s *sqlDatabase) CreateInstance(_ context.Context, poolID string, param par
|
|||
if len(param.JitConfiguration) > 0 {
|
||||
secret, err = s.marshalAndSeal(param.JitConfiguration)
|
||||
if err != nil {
|
||||
return params.Instance{}, errors.Wrap(err, "marshalling jit config")
|
||||
return params.Instance{}, fmt.Errorf("error marshalling jit config: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -74,7 +75,7 @@ func (s *sqlDatabase) CreateInstance(_ context.Context, poolID string, param par
|
|||
}
|
||||
q := s.conn.Create(&newInstance)
|
||||
if q.Error != nil {
|
||||
return params.Instance{}, errors.Wrap(q.Error, "creating instance")
|
||||
return params.Instance{}, fmt.Errorf("error creating instance: %w", q.Error)
|
||||
}
|
||||
|
||||
return s.sqlToParamsInstance(newInstance)
|
||||
|
|
@ -83,7 +84,7 @@ func (s *sqlDatabase) CreateInstance(_ context.Context, poolID string, param par
|
|||
func (s *sqlDatabase) getPoolInstanceByName(poolID string, instanceName string) (Instance, error) {
|
||||
pool, err := s.getPoolByID(s.conn, poolID)
|
||||
if err != nil {
|
||||
return Instance{}, errors.Wrap(err, "fetching pool")
|
||||
return Instance{}, fmt.Errorf("error fetching pool: %w", err)
|
||||
}
|
||||
|
||||
var instance Instance
|
||||
|
|
@ -93,9 +94,9 @@ func (s *sqlDatabase) getPoolInstanceByName(poolID string, instanceName string)
|
|||
First(&instance)
|
||||
if q.Error != nil {
|
||||
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return Instance{}, errors.Wrap(runnerErrors.ErrNotFound, "fetching pool instance by name")
|
||||
return Instance{}, fmt.Errorf("error fetching pool instance by name: %w", runnerErrors.ErrNotFound)
|
||||
}
|
||||
return Instance{}, errors.Wrap(q.Error, "fetching pool instance by name")
|
||||
return Instance{}, fmt.Errorf("error fetching pool instance by name: %w", q.Error)
|
||||
}
|
||||
|
||||
instance.Pool = pool
|
||||
|
|
@ -119,9 +120,9 @@ func (s *sqlDatabase) getInstanceByName(_ context.Context, instanceName string,
|
|||
First(&instance)
|
||||
if q.Error != nil {
|
||||
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return Instance{}, errors.Wrap(runnerErrors.ErrNotFound, "fetching instance by name")
|
||||
return Instance{}, fmt.Errorf("error fetching instance by name: %w", runnerErrors.ErrNotFound)
|
||||
}
|
||||
return Instance{}, errors.Wrap(q.Error, "fetching instance by name")
|
||||
return Instance{}, fmt.Errorf("error fetching instance by name: %w", q.Error)
|
||||
}
|
||||
return instance, nil
|
||||
}
|
||||
|
|
@ -129,7 +130,7 @@ func (s *sqlDatabase) getInstanceByName(_ context.Context, instanceName string,
|
|||
func (s *sqlDatabase) GetPoolInstanceByName(_ context.Context, poolID string, instanceName string) (params.Instance, error) {
|
||||
instance, err := s.getPoolInstanceByName(poolID, instanceName)
|
||||
if err != nil {
|
||||
return params.Instance{}, errors.Wrap(err, "fetching instance")
|
||||
return params.Instance{}, fmt.Errorf("error fetching instance: %w", err)
|
||||
}
|
||||
|
||||
return s.sqlToParamsInstance(instance)
|
||||
|
|
@ -138,7 +139,7 @@ func (s *sqlDatabase) GetPoolInstanceByName(_ context.Context, poolID string, in
|
|||
func (s *sqlDatabase) GetInstanceByName(ctx context.Context, instanceName string) (params.Instance, error) {
|
||||
instance, err := s.getInstanceByName(ctx, instanceName, "StatusMessages", "Pool", "ScaleSet")
|
||||
if err != nil {
|
||||
return params.Instance{}, errors.Wrap(err, "fetching instance")
|
||||
return params.Instance{}, fmt.Errorf("error fetching instance: %w", err)
|
||||
}
|
||||
|
||||
return s.sqlToParamsInstance(instance)
|
||||
|
|
@ -150,7 +151,7 @@ func (s *sqlDatabase) DeleteInstance(_ context.Context, poolID string, instanceN
|
|||
if errors.Is(err, runnerErrors.ErrNotFound) {
|
||||
return nil
|
||||
}
|
||||
return errors.Wrap(err, "deleting instance")
|
||||
return fmt.Errorf("error deleting instance: %w", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
|
|
@ -182,7 +183,7 @@ func (s *sqlDatabase) DeleteInstance(_ context.Context, poolID string, instanceN
|
|||
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return nil
|
||||
}
|
||||
return errors.Wrap(q.Error, "deleting instance")
|
||||
return fmt.Errorf("error deleting instance: %w", q.Error)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -193,7 +194,7 @@ func (s *sqlDatabase) DeleteInstanceByName(ctx context.Context, instanceName str
|
|||
if errors.Is(err, runnerErrors.ErrNotFound) {
|
||||
return nil
|
||||
}
|
||||
return errors.Wrap(err, "deleting instance")
|
||||
return fmt.Errorf("error deleting instance: %w", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
|
|
@ -224,7 +225,7 @@ func (s *sqlDatabase) DeleteInstanceByName(ctx context.Context, instanceName str
|
|||
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return nil
|
||||
}
|
||||
return errors.Wrap(q.Error, "deleting instance")
|
||||
return fmt.Errorf("error deleting instance: %w", q.Error)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -232,7 +233,7 @@ func (s *sqlDatabase) DeleteInstanceByName(ctx context.Context, instanceName str
|
|||
func (s *sqlDatabase) AddInstanceEvent(ctx context.Context, instanceName string, event params.EventType, eventLevel params.EventLevel, statusMessage string) error {
|
||||
instance, err := s.getInstanceByName(ctx, instanceName)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "updating instance")
|
||||
return fmt.Errorf("error updating instance: %w", err)
|
||||
}
|
||||
|
||||
msg := InstanceStatusUpdate{
|
||||
|
|
@ -242,7 +243,7 @@ func (s *sqlDatabase) AddInstanceEvent(ctx context.Context, instanceName string,
|
|||
}
|
||||
|
||||
if err := s.conn.Model(&instance).Association("StatusMessages").Append(&msg); err != nil {
|
||||
return errors.Wrap(err, "adding status message")
|
||||
return fmt.Errorf("error adding status message: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -250,7 +251,7 @@ func (s *sqlDatabase) AddInstanceEvent(ctx context.Context, instanceName string,
|
|||
func (s *sqlDatabase) UpdateInstance(ctx context.Context, instanceName string, param params.UpdateInstanceParams) (params.Instance, error) {
|
||||
instance, err := s.getInstanceByName(ctx, instanceName, "Pool", "ScaleSet")
|
||||
if err != nil {
|
||||
return params.Instance{}, errors.Wrap(err, "updating instance")
|
||||
return params.Instance{}, fmt.Errorf("error updating instance: %w", err)
|
||||
}
|
||||
|
||||
if param.AgentID != 0 {
|
||||
|
|
@ -287,7 +288,7 @@ func (s *sqlDatabase) UpdateInstance(ctx context.Context, instanceName string, p
|
|||
if param.JitConfiguration != nil {
|
||||
secret, err := s.marshalAndSeal(param.JitConfiguration)
|
||||
if err != nil {
|
||||
return params.Instance{}, errors.Wrap(err, "marshalling jit config")
|
||||
return params.Instance{}, fmt.Errorf("error marshalling jit config: %w", err)
|
||||
}
|
||||
instance.JitConfiguration = secret
|
||||
}
|
||||
|
|
@ -296,7 +297,7 @@ func (s *sqlDatabase) UpdateInstance(ctx context.Context, instanceName string, p
|
|||
|
||||
q := s.conn.Save(&instance)
|
||||
if q.Error != nil {
|
||||
return params.Instance{}, errors.Wrap(q.Error, "updating instance")
|
||||
return params.Instance{}, fmt.Errorf("error updating instance: %w", q.Error)
|
||||
}
|
||||
|
||||
if len(param.Addresses) > 0 {
|
||||
|
|
@ -308,12 +309,12 @@ func (s *sqlDatabase) UpdateInstance(ctx context.Context, instanceName string, p
|
|||
})
|
||||
}
|
||||
if err := s.conn.Model(&instance).Association("Addresses").Replace(addrs); err != nil {
|
||||
return params.Instance{}, errors.Wrap(err, "updating addresses")
|
||||
return params.Instance{}, fmt.Errorf("error updating addresses: %w", err)
|
||||
}
|
||||
}
|
||||
inst, err := s.sqlToParamsInstance(instance)
|
||||
if err != nil {
|
||||
return params.Instance{}, errors.Wrap(err, "converting instance")
|
||||
return params.Instance{}, fmt.Errorf("error converting instance: %w", err)
|
||||
}
|
||||
s.sendNotify(common.InstanceEntityType, common.UpdateOperation, inst)
|
||||
return inst, nil
|
||||
|
|
@ -322,7 +323,7 @@ func (s *sqlDatabase) UpdateInstance(ctx context.Context, instanceName string, p
|
|||
func (s *sqlDatabase) ListPoolInstances(_ context.Context, poolID string) ([]params.Instance, error) {
|
||||
u, err := uuid.Parse(poolID)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(runnerErrors.ErrBadRequest, "parsing id")
|
||||
return nil, fmt.Errorf("error parsing id: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
|
||||
var instances []Instance
|
||||
|
|
@ -332,14 +333,14 @@ func (s *sqlDatabase) ListPoolInstances(_ context.Context, poolID string) ([]par
|
|||
Where("pool_id = ?", u)
|
||||
|
||||
if err := query.Find(&instances); err.Error != nil {
|
||||
return nil, errors.Wrap(err.Error, "fetching instances")
|
||||
return nil, fmt.Errorf("error fetching instances: %w", err.Error)
|
||||
}
|
||||
|
||||
ret := make([]params.Instance, len(instances))
|
||||
for idx, inst := range instances {
|
||||
ret[idx], err = s.sqlToParamsInstance(inst)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "converting instance")
|
||||
return nil, fmt.Errorf("error converting instance: %w", err)
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
|
|
@ -354,14 +355,14 @@ func (s *sqlDatabase) ListAllInstances(_ context.Context) ([]params.Instance, er
|
|||
Preload("Job").
|
||||
Find(&instances)
|
||||
if q.Error != nil {
|
||||
return nil, errors.Wrap(q.Error, "fetching instances")
|
||||
return nil, fmt.Errorf("error fetching instances: %w", q.Error)
|
||||
}
|
||||
ret := make([]params.Instance, len(instances))
|
||||
var err error
|
||||
for idx, instance := range instances {
|
||||
ret[idx], err = s.sqlToParamsInstance(instance)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "converting instance")
|
||||
return nil, fmt.Errorf("error converting instance: %w", err)
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
|
|
@ -370,13 +371,13 @@ func (s *sqlDatabase) ListAllInstances(_ context.Context) ([]params.Instance, er
|
|||
func (s *sqlDatabase) PoolInstanceCount(_ context.Context, poolID string) (int64, error) {
|
||||
pool, err := s.getPoolByID(s.conn, poolID)
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "fetching pool")
|
||||
return 0, fmt.Errorf("error fetching pool: %w", err)
|
||||
}
|
||||
|
||||
var cnt int64
|
||||
q := s.conn.Model(&Instance{}).Where("pool_id = ?", pool.ID).Count(&cnt)
|
||||
if q.Error != nil {
|
||||
return 0, errors.Wrap(q.Error, "fetching instance count")
|
||||
return 0, fmt.Errorf("error fetching instance count: %w", q.Error)
|
||||
}
|
||||
return cnt, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -210,7 +210,7 @@ func (s *InstancesTestSuite) TestCreateInstance() {
|
|||
func (s *InstancesTestSuite) TestCreateInstanceInvalidPoolID() {
|
||||
_, err := s.Store.CreateInstance(s.adminCtx, "dummy-pool-id", params.CreateInstanceParams{})
|
||||
|
||||
s.Require().Equal("fetching pool: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error fetching pool: error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *InstancesTestSuite) TestCreateInstanceDBCreateErr() {
|
||||
|
|
@ -233,7 +233,7 @@ func (s *InstancesTestSuite) TestCreateInstanceDBCreateErr() {
|
|||
|
||||
s.assertSQLMockExpectations()
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("creating instance: mocked insert instance error", err.Error())
|
||||
s.Require().Equal("error creating instance: mocked insert instance error", err.Error())
|
||||
}
|
||||
|
||||
func (s *InstancesTestSuite) TestGetPoolInstanceByName() {
|
||||
|
|
@ -252,7 +252,7 @@ func (s *InstancesTestSuite) TestGetPoolInstanceByName() {
|
|||
func (s *InstancesTestSuite) TestGetPoolInstanceByNameNotFound() {
|
||||
_, err := s.Store.GetPoolInstanceByName(s.adminCtx, s.Fixtures.Pool.ID, "not-existent-instance-name")
|
||||
|
||||
s.Require().Equal("fetching instance: fetching pool instance by name: not found", err.Error())
|
||||
s.Require().Equal("error fetching instance: error fetching pool instance by name: not found", err.Error())
|
||||
}
|
||||
|
||||
func (s *InstancesTestSuite) TestGetInstanceByName() {
|
||||
|
|
@ -271,7 +271,7 @@ func (s *InstancesTestSuite) TestGetInstanceByName() {
|
|||
func (s *InstancesTestSuite) TestGetInstanceByNameFetchInstanceFailed() {
|
||||
_, err := s.Store.GetInstanceByName(s.adminCtx, "not-existent-instance-name")
|
||||
|
||||
s.Require().Equal("fetching instance: fetching instance by name: not found", err.Error())
|
||||
s.Require().Equal("error fetching instance: error fetching instance by name: not found", err.Error())
|
||||
}
|
||||
|
||||
func (s *InstancesTestSuite) TestDeleteInstance() {
|
||||
|
|
@ -282,7 +282,7 @@ func (s *InstancesTestSuite) TestDeleteInstance() {
|
|||
s.Require().Nil(err)
|
||||
|
||||
_, err = s.Store.GetPoolInstanceByName(s.adminCtx, s.Fixtures.Pool.ID, storeInstance.Name)
|
||||
s.Require().Equal("fetching instance: fetching pool instance by name: not found", err.Error())
|
||||
s.Require().Equal("error fetching instance: error fetching pool instance by name: not found", err.Error())
|
||||
|
||||
err = s.Store.DeleteInstance(s.adminCtx, s.Fixtures.Pool.ID, storeInstance.Name)
|
||||
s.Require().Nil(err)
|
||||
|
|
@ -296,7 +296,7 @@ func (s *InstancesTestSuite) TestDeleteInstanceByName() {
|
|||
s.Require().Nil(err)
|
||||
|
||||
_, err = s.Store.GetPoolInstanceByName(s.adminCtx, s.Fixtures.Pool.ID, storeInstance.Name)
|
||||
s.Require().Equal("fetching instance: fetching pool instance by name: not found", err.Error())
|
||||
s.Require().Equal("error fetching instance: error fetching pool instance by name: not found", err.Error())
|
||||
|
||||
err = s.Store.DeleteInstanceByName(s.adminCtx, storeInstance.Name)
|
||||
s.Require().Nil(err)
|
||||
|
|
@ -305,7 +305,7 @@ func (s *InstancesTestSuite) TestDeleteInstanceByName() {
|
|||
func (s *InstancesTestSuite) TestDeleteInstanceInvalidPoolID() {
|
||||
err := s.Store.DeleteInstance(s.adminCtx, "dummy-pool-id", "dummy-instance-name")
|
||||
|
||||
s.Require().Equal("deleting instance: fetching pool: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error deleting instance: error fetching pool: error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *InstancesTestSuite) TestDeleteInstanceDBRecordNotFoundErr() {
|
||||
|
|
@ -380,7 +380,7 @@ func (s *InstancesTestSuite) TestDeleteInstanceDBDeleteErr() {
|
|||
|
||||
s.assertSQLMockExpectations()
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("deleting instance: mocked delete instance error", err.Error())
|
||||
s.Require().Equal("error deleting instance: mocked delete instance error", err.Error())
|
||||
}
|
||||
|
||||
func (s *InstancesTestSuite) TestAddInstanceEvent() {
|
||||
|
|
@ -431,7 +431,7 @@ func (s *InstancesTestSuite) TestAddInstanceEventDBUpdateErr() {
|
|||
err := s.StoreSQLMocked.AddInstanceEvent(s.adminCtx, instance.Name, params.StatusEvent, params.EventInfo, statusMsg)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("adding status message: mocked add status message error", err.Error())
|
||||
s.Require().Equal("error adding status message: mocked add status message error", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -476,7 +476,7 @@ func (s *InstancesTestSuite) TestUpdateInstanceDBUpdateInstanceErr() {
|
|||
_, err := s.StoreSQLMocked.UpdateInstance(s.adminCtx, instance.Name, s.Fixtures.UpdateInstanceParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("updating instance: mocked update instance error", err.Error())
|
||||
s.Require().Equal("error updating instance: mocked update instance error", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -522,7 +522,7 @@ func (s *InstancesTestSuite) TestUpdateInstanceDBUpdateAddressErr() {
|
|||
_, err := s.StoreSQLMocked.UpdateInstance(s.adminCtx, instance.Name, s.Fixtures.UpdateInstanceParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("updating addresses: update addresses mock error", err.Error())
|
||||
s.Require().Equal("error updating addresses: update addresses mock error", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -536,7 +536,7 @@ func (s *InstancesTestSuite) TestListPoolInstances() {
|
|||
func (s *InstancesTestSuite) TestListPoolInstancesInvalidPoolID() {
|
||||
_, err := s.Store.ListPoolInstances(s.adminCtx, "dummy-pool-id")
|
||||
|
||||
s.Require().Equal("parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *InstancesTestSuite) TestListAllInstances() {
|
||||
|
|
@ -555,7 +555,7 @@ func (s *InstancesTestSuite) TestListAllInstancesDBFetchErr() {
|
|||
|
||||
s.assertSQLMockExpectations()
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching instances: fetch instances mock error", err.Error())
|
||||
s.Require().Equal("error fetching instances: fetch instances mock error", err.Error())
|
||||
}
|
||||
|
||||
func (s *InstancesTestSuite) TestPoolInstanceCount() {
|
||||
|
|
@ -568,7 +568,7 @@ func (s *InstancesTestSuite) TestPoolInstanceCount() {
|
|||
func (s *InstancesTestSuite) TestPoolInstanceCountInvalidPoolID() {
|
||||
_, err := s.Store.PoolInstanceCount(s.adminCtx, "dummy-pool-id")
|
||||
|
||||
s.Require().Equal("fetching pool: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error fetching pool: error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *InstancesTestSuite) TestPoolInstanceCountDBCountErr() {
|
||||
|
|
@ -587,7 +587,7 @@ func (s *InstancesTestSuite) TestPoolInstanceCountDBCountErr() {
|
|||
|
||||
s.assertSQLMockExpectations()
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching instance count: count mock error", err.Error())
|
||||
s.Require().Equal("error fetching instance count: count mock error", err.Error())
|
||||
}
|
||||
|
||||
func TestInstTestSuite(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -17,10 +17,11 @@ package sql
|
|||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
|
||||
|
|
@ -35,7 +36,7 @@ func sqlWorkflowJobToParamsJob(job WorkflowJob) (params.Job, error) {
|
|||
labels := []string{}
|
||||
if job.Labels != nil {
|
||||
if err := json.Unmarshal(job.Labels, &labels); err != nil {
|
||||
return params.Job{}, errors.Wrap(err, "unmarshaling labels")
|
||||
return params.Job{}, fmt.Errorf("error unmarshaling labels: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -73,7 +74,7 @@ func sqlWorkflowJobToParamsJob(job WorkflowJob) (params.Job, error) {
|
|||
func (s *sqlDatabase) paramsJobToWorkflowJob(ctx context.Context, job params.Job) (WorkflowJob, error) {
|
||||
asJSON, err := json.Marshal(job.Labels)
|
||||
if err != nil {
|
||||
return WorkflowJob{}, errors.Wrap(err, "marshaling labels")
|
||||
return WorkflowJob{}, fmt.Errorf("error marshaling labels: %w", err)
|
||||
}
|
||||
|
||||
workflofJob := WorkflowJob{
|
||||
|
|
@ -118,11 +119,11 @@ func (s *sqlDatabase) DeleteJob(_ context.Context, jobID int64) (err error) {
|
|||
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return nil
|
||||
}
|
||||
return errors.Wrap(q.Error, "fetching job")
|
||||
return fmt.Errorf("error fetching job: %w", q.Error)
|
||||
}
|
||||
removedJob, err := sqlWorkflowJobToParamsJob(workflowJob)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "converting job")
|
||||
return fmt.Errorf("error converting job: %w", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
|
|
@ -137,7 +138,7 @@ func (s *sqlDatabase) DeleteJob(_ context.Context, jobID int64) (err error) {
|
|||
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return nil
|
||||
}
|
||||
return errors.Wrap(q.Error, "deleting job")
|
||||
return fmt.Errorf("error deleting job: %w", q.Error)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -145,7 +146,7 @@ func (s *sqlDatabase) DeleteJob(_ context.Context, jobID int64) (err error) {
|
|||
func (s *sqlDatabase) LockJob(_ context.Context, jobID int64, entityID string) error {
|
||||
entityUUID, err := uuid.Parse(entityID)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "parsing entity id")
|
||||
return fmt.Errorf("error parsing entity id: %w", err)
|
||||
}
|
||||
var workflowJob WorkflowJob
|
||||
q := s.conn.Preload("Instance").Where("id = ?", jobID).First(&workflowJob)
|
||||
|
|
@ -154,7 +155,7 @@ func (s *sqlDatabase) LockJob(_ context.Context, jobID int64, entityID string) e
|
|||
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return runnerErrors.ErrNotFound
|
||||
}
|
||||
return errors.Wrap(q.Error, "fetching job")
|
||||
return fmt.Errorf("error fetching job: %w", q.Error)
|
||||
}
|
||||
|
||||
if workflowJob.LockedBy.String() == entityID {
|
||||
|
|
@ -169,12 +170,12 @@ func (s *sqlDatabase) LockJob(_ context.Context, jobID int64, entityID string) e
|
|||
workflowJob.LockedBy = entityUUID
|
||||
|
||||
if err := s.conn.Save(&workflowJob).Error; err != nil {
|
||||
return errors.Wrap(err, "saving job")
|
||||
return fmt.Errorf("error saving job: %w", err)
|
||||
}
|
||||
|
||||
asParams, err := sqlWorkflowJobToParamsJob(workflowJob)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "converting job")
|
||||
return fmt.Errorf("error converting job: %w", err)
|
||||
}
|
||||
s.sendNotify(common.JobEntityType, common.UpdateOperation, asParams)
|
||||
|
||||
|
|
@ -189,7 +190,7 @@ func (s *sqlDatabase) BreakLockJobIsQueued(_ context.Context, jobID int64) (err
|
|||
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return nil
|
||||
}
|
||||
return errors.Wrap(q.Error, "fetching job")
|
||||
return fmt.Errorf("error fetching job: %w", q.Error)
|
||||
}
|
||||
|
||||
if workflowJob.LockedBy == uuid.Nil {
|
||||
|
|
@ -199,11 +200,11 @@ func (s *sqlDatabase) BreakLockJobIsQueued(_ context.Context, jobID int64) (err
|
|||
|
||||
workflowJob.LockedBy = uuid.Nil
|
||||
if err := s.conn.Save(&workflowJob).Error; err != nil {
|
||||
return errors.Wrap(err, "saving job")
|
||||
return fmt.Errorf("error saving job: %w", err)
|
||||
}
|
||||
asParams, err := sqlWorkflowJobToParamsJob(workflowJob)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "converting job")
|
||||
return fmt.Errorf("error converting job: %w", err)
|
||||
}
|
||||
s.sendNotify(common.JobEntityType, common.UpdateOperation, asParams)
|
||||
return nil
|
||||
|
|
@ -217,7 +218,7 @@ func (s *sqlDatabase) UnlockJob(_ context.Context, jobID int64, entityID string)
|
|||
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return runnerErrors.ErrNotFound
|
||||
}
|
||||
return errors.Wrap(q.Error, "fetching job")
|
||||
return fmt.Errorf("error fetching job: %w", q.Error)
|
||||
}
|
||||
|
||||
if workflowJob.LockedBy == uuid.Nil {
|
||||
|
|
@ -231,12 +232,12 @@ func (s *sqlDatabase) UnlockJob(_ context.Context, jobID int64, entityID string)
|
|||
|
||||
workflowJob.LockedBy = uuid.Nil
|
||||
if err := s.conn.Save(&workflowJob).Error; err != nil {
|
||||
return errors.Wrap(err, "saving job")
|
||||
return fmt.Errorf("error saving job: %w", err)
|
||||
}
|
||||
|
||||
asParams, err := sqlWorkflowJobToParamsJob(workflowJob)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "converting job")
|
||||
return fmt.Errorf("error converting job: %w", err)
|
||||
}
|
||||
s.sendNotify(common.JobEntityType, common.UpdateOperation, asParams)
|
||||
return nil
|
||||
|
|
@ -256,7 +257,7 @@ func (s *sqlDatabase) CreateOrUpdateJob(ctx context.Context, job params.Job) (pa
|
|||
|
||||
if q.Error != nil {
|
||||
if !errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return params.Job{}, errors.Wrap(q.Error, "fetching job")
|
||||
return params.Job{}, fmt.Errorf("error fetching job: %w", q.Error)
|
||||
}
|
||||
}
|
||||
var operation common.OperationType
|
||||
|
|
@ -302,23 +303,23 @@ func (s *sqlDatabase) CreateOrUpdateJob(ctx context.Context, job params.Job) (pa
|
|||
workflowJob.EnterpriseID = job.EnterpriseID
|
||||
}
|
||||
if err := s.conn.Save(&workflowJob).Error; err != nil {
|
||||
return params.Job{}, errors.Wrap(err, "saving job")
|
||||
return params.Job{}, fmt.Errorf("error saving job: %w", err)
|
||||
}
|
||||
} else {
|
||||
operation = common.CreateOperation
|
||||
|
||||
workflowJob, err = s.paramsJobToWorkflowJob(ctx, job)
|
||||
if err != nil {
|
||||
return params.Job{}, errors.Wrap(err, "converting job")
|
||||
return params.Job{}, fmt.Errorf("error converting job: %w", err)
|
||||
}
|
||||
if err := s.conn.Create(&workflowJob).Error; err != nil {
|
||||
return params.Job{}, errors.Wrap(err, "creating job")
|
||||
return params.Job{}, fmt.Errorf("error creating job: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
asParams, err := sqlWorkflowJobToParamsJob(workflowJob)
|
||||
if err != nil {
|
||||
return params.Job{}, errors.Wrap(err, "converting job")
|
||||
return params.Job{}, fmt.Errorf("error converting job: %w", err)
|
||||
}
|
||||
s.sendNotify(common.JobEntityType, operation, asParams)
|
||||
|
||||
|
|
@ -338,7 +339,7 @@ func (s *sqlDatabase) ListJobsByStatus(_ context.Context, status params.JobStatu
|
|||
for idx, job := range jobs {
|
||||
jobParam, err := sqlWorkflowJobToParamsJob(job)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "converting job")
|
||||
return nil, fmt.Errorf("error converting job: %w", err)
|
||||
}
|
||||
ret[idx] = jobParam
|
||||
}
|
||||
|
|
@ -379,7 +380,7 @@ func (s *sqlDatabase) ListEntityJobsByStatus(_ context.Context, entityType param
|
|||
for idx, job := range jobs {
|
||||
jobParam, err := sqlWorkflowJobToParamsJob(job)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "converting job")
|
||||
return nil, fmt.Errorf("error converting job: %w", err)
|
||||
}
|
||||
ret[idx] = jobParam
|
||||
}
|
||||
|
|
@ -401,7 +402,7 @@ func (s *sqlDatabase) ListAllJobs(_ context.Context) ([]params.Job, error) {
|
|||
for idx, job := range jobs {
|
||||
jobParam, err := sqlWorkflowJobToParamsJob(job)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "converting job")
|
||||
return nil, fmt.Errorf("error converting job: %w", err)
|
||||
}
|
||||
ret[idx] = jobParam
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,10 +15,10 @@
|
|||
package sql
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
|
||||
|
|
@ -40,7 +40,7 @@ func (b *Base) BeforeCreate(_ *gorm.DB) error {
|
|||
}
|
||||
newID, err := uuid.NewRandom()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "generating id")
|
||||
return fmt.Errorf("error generating id: %w", err)
|
||||
}
|
||||
b.ID = newID
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -16,11 +16,11 @@ package sql
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
|
||||
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
|
||||
|
|
@ -35,7 +35,7 @@ func (s *sqlDatabase) CreateOrganization(ctx context.Context, name string, crede
|
|||
}
|
||||
secret, err := util.Seal([]byte(webhookSecret), []byte(s.cfg.Passphrase))
|
||||
if err != nil {
|
||||
return params.Organization{}, errors.Wrap(err, "encoding secret")
|
||||
return params.Organization{}, fmt.Errorf("error encoding secret: %w", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
|
|
@ -56,23 +56,23 @@ func (s *sqlDatabase) CreateOrganization(ctx context.Context, name string, crede
|
|||
case params.GiteaEndpointType:
|
||||
newOrg.GiteaCredentialsID = &credentials.ID
|
||||
default:
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "unsupported credentials type")
|
||||
return fmt.Errorf("unsupported credentials type: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
|
||||
newOrg.EndpointName = &credentials.Endpoint.Name
|
||||
q := tx.Create(&newOrg)
|
||||
if q.Error != nil {
|
||||
return errors.Wrap(q.Error, "creating org")
|
||||
return fmt.Errorf("error creating org: %w", q.Error)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return params.Organization{}, errors.Wrap(err, "creating org")
|
||||
return params.Organization{}, fmt.Errorf("error creating org: %w", err)
|
||||
}
|
||||
|
||||
ret, err := s.GetOrganizationByID(ctx, newOrg.ID.String())
|
||||
if err != nil {
|
||||
return params.Organization{}, errors.Wrap(err, "creating org")
|
||||
return params.Organization{}, fmt.Errorf("error creating org: %w", err)
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
|
|
@ -81,12 +81,12 @@ func (s *sqlDatabase) CreateOrganization(ctx context.Context, name string, crede
|
|||
func (s *sqlDatabase) GetOrganization(ctx context.Context, name, endpointName string) (params.Organization, error) {
|
||||
org, err := s.getOrg(ctx, name, endpointName)
|
||||
if err != nil {
|
||||
return params.Organization{}, errors.Wrap(err, "fetching org")
|
||||
return params.Organization{}, fmt.Errorf("error fetching org: %w", err)
|
||||
}
|
||||
|
||||
param, err := s.sqlToCommonOrganization(org, true)
|
||||
if err != nil {
|
||||
return params.Organization{}, errors.Wrap(err, "fetching org")
|
||||
return params.Organization{}, fmt.Errorf("error fetching org: %w", err)
|
||||
}
|
||||
|
||||
return param, nil
|
||||
|
|
@ -110,7 +110,7 @@ func (s *sqlDatabase) ListOrganizations(_ context.Context, filter params.Organiz
|
|||
}
|
||||
q = q.Find(&orgs)
|
||||
if q.Error != nil {
|
||||
return []params.Organization{}, errors.Wrap(q.Error, "fetching org from database")
|
||||
return []params.Organization{}, fmt.Errorf("error fetching org from database: %w", q.Error)
|
||||
}
|
||||
|
||||
ret := make([]params.Organization, len(orgs))
|
||||
|
|
@ -118,7 +118,7 @@ func (s *sqlDatabase) ListOrganizations(_ context.Context, filter params.Organiz
|
|||
var err error
|
||||
ret[idx], err = s.sqlToCommonOrganization(val, true)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "fetching org")
|
||||
return nil, fmt.Errorf("error fetching org: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -128,7 +128,7 @@ func (s *sqlDatabase) ListOrganizations(_ context.Context, filter params.Organiz
|
|||
func (s *sqlDatabase) DeleteOrganization(ctx context.Context, orgID string) (err error) {
|
||||
org, err := s.getOrgByID(ctx, s.conn, orgID, "Endpoint", "Credentials", "Credentials.Endpoint", "GiteaCredentials", "GiteaCredentials.Endpoint")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "fetching org")
|
||||
return fmt.Errorf("error fetching org: %w", err)
|
||||
}
|
||||
|
||||
defer func(org Organization) {
|
||||
|
|
@ -144,7 +144,7 @@ func (s *sqlDatabase) DeleteOrganization(ctx context.Context, orgID string) (err
|
|||
|
||||
q := s.conn.Unscoped().Delete(&org)
|
||||
if q.Error != nil && !errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return errors.Wrap(q.Error, "deleting org")
|
||||
return fmt.Errorf("error deleting org: %w", q.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
@ -162,23 +162,23 @@ func (s *sqlDatabase) UpdateOrganization(ctx context.Context, orgID string, para
|
|||
var err error
|
||||
org, err = s.getOrgByID(ctx, tx, orgID)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "fetching org")
|
||||
return fmt.Errorf("error fetching org: %w", err)
|
||||
}
|
||||
if org.EndpointName == nil {
|
||||
return errors.Wrap(runnerErrors.ErrUnprocessable, "org has no endpoint")
|
||||
return fmt.Errorf("error org has no endpoint: %w", runnerErrors.ErrUnprocessable)
|
||||
}
|
||||
|
||||
if param.CredentialsName != "" {
|
||||
creds, err = s.getGithubCredentialsByName(ctx, tx, param.CredentialsName, false)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "fetching credentials")
|
||||
return fmt.Errorf("error fetching credentials: %w", err)
|
||||
}
|
||||
if creds.EndpointName == nil {
|
||||
return errors.Wrap(runnerErrors.ErrUnprocessable, "credentials have no endpoint")
|
||||
return fmt.Errorf("error credentials have no endpoint: %w", runnerErrors.ErrUnprocessable)
|
||||
}
|
||||
|
||||
if *creds.EndpointName != *org.EndpointName {
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "endpoint mismatch")
|
||||
return fmt.Errorf("error endpoint mismatch: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
org.CredentialsID = &creds.ID
|
||||
}
|
||||
|
|
@ -197,22 +197,22 @@ func (s *sqlDatabase) UpdateOrganization(ctx context.Context, orgID string, para
|
|||
|
||||
q := tx.Save(&org)
|
||||
if q.Error != nil {
|
||||
return errors.Wrap(q.Error, "saving org")
|
||||
return fmt.Errorf("error saving org: %w", q.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return params.Organization{}, errors.Wrap(err, "saving org")
|
||||
return params.Organization{}, fmt.Errorf("error saving org: %w", err)
|
||||
}
|
||||
|
||||
org, err = s.getOrgByID(ctx, s.conn, orgID, "Endpoint", "Credentials", "Credentials.Endpoint", "GiteaCredentials", "GiteaCredentials.Endpoint")
|
||||
if err != nil {
|
||||
return params.Organization{}, errors.Wrap(err, "updating enterprise")
|
||||
return params.Organization{}, fmt.Errorf("error updating enterprise: %w", err)
|
||||
}
|
||||
paramOrg, err = s.sqlToCommonOrganization(org, true)
|
||||
if err != nil {
|
||||
return params.Organization{}, errors.Wrap(err, "saving org")
|
||||
return params.Organization{}, fmt.Errorf("error saving org: %w", err)
|
||||
}
|
||||
return paramOrg, nil
|
||||
}
|
||||
|
|
@ -229,12 +229,12 @@ func (s *sqlDatabase) GetOrganizationByID(ctx context.Context, orgID string) (pa
|
|||
}
|
||||
org, err := s.getOrgByID(ctx, s.conn, orgID, preloadList...)
|
||||
if err != nil {
|
||||
return params.Organization{}, errors.Wrap(err, "fetching org")
|
||||
return params.Organization{}, fmt.Errorf("error fetching org: %w", err)
|
||||
}
|
||||
|
||||
param, err := s.sqlToCommonOrganization(org, true)
|
||||
if err != nil {
|
||||
return params.Organization{}, errors.Wrap(err, "fetching org")
|
||||
return params.Organization{}, fmt.Errorf("error fetching org: %w", err)
|
||||
}
|
||||
return param, nil
|
||||
}
|
||||
|
|
@ -242,7 +242,7 @@ func (s *sqlDatabase) GetOrganizationByID(ctx context.Context, orgID string) (pa
|
|||
func (s *sqlDatabase) getOrgByID(_ context.Context, db *gorm.DB, id string, preload ...string) (Organization, error) {
|
||||
u, err := uuid.Parse(id)
|
||||
if err != nil {
|
||||
return Organization{}, errors.Wrap(runnerErrors.ErrBadRequest, "parsing id")
|
||||
return Organization{}, fmt.Errorf("error parsing id: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
var org Organization
|
||||
|
||||
|
|
@ -258,7 +258,7 @@ func (s *sqlDatabase) getOrgByID(_ context.Context, db *gorm.DB, id string, prel
|
|||
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return Organization{}, runnerErrors.ErrNotFound
|
||||
}
|
||||
return Organization{}, errors.Wrap(q.Error, "fetching org from database")
|
||||
return Organization{}, fmt.Errorf("error fetching org from database: %w", q.Error)
|
||||
}
|
||||
return org, nil
|
||||
}
|
||||
|
|
@ -277,7 +277,7 @@ func (s *sqlDatabase) getOrg(_ context.Context, name, endpointName string) (Orga
|
|||
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return Organization{}, runnerErrors.ErrNotFound
|
||||
}
|
||||
return Organization{}, errors.Wrap(q.Error, "fetching org from database")
|
||||
return Organization{}, fmt.Errorf("error fetching org from database: %w", q.Error)
|
||||
}
|
||||
return org, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -251,7 +251,7 @@ func (s *OrgTestSuite) TestCreateOrganizationInvalidForgeType() {
|
|||
s.Fixtures.CreateOrgParams.WebhookSecret,
|
||||
params.PoolBalancerTypeRoundRobin)
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("creating org: unsupported credentials type: invalid request", err.Error())
|
||||
s.Require().Equal("error creating org: unsupported credentials type: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *OrgTestSuite) TestCreateOrganizationInvalidDBPassphrase() {
|
||||
|
|
@ -275,7 +275,7 @@ func (s *OrgTestSuite) TestCreateOrganizationInvalidDBPassphrase() {
|
|||
params.PoolBalancerTypeRoundRobin)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("encoding secret: invalid passphrase length (expected length 32 characters)", err.Error())
|
||||
s.Require().Equal("error encoding secret: invalid passphrase length (expected length 32 characters)", err.Error())
|
||||
}
|
||||
|
||||
func (s *OrgTestSuite) TestCreateOrganizationDBCreateErr() {
|
||||
|
|
@ -293,7 +293,7 @@ func (s *OrgTestSuite) TestCreateOrganizationDBCreateErr() {
|
|||
params.PoolBalancerTypeRoundRobin)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("creating org: creating org: creating org mock error", err.Error())
|
||||
s.Require().Equal("error creating org: error creating org: creating org mock error", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -316,7 +316,7 @@ func (s *OrgTestSuite) TestGetOrganizationNotFound() {
|
|||
_, err := s.Store.GetOrganization(s.adminCtx, "dummy-name", "github.com")
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching org: not found", err.Error())
|
||||
s.Require().Equal("error fetching org: not found", err.Error())
|
||||
}
|
||||
|
||||
func (s *OrgTestSuite) TestGetOrganizationDBDecryptingErr() {
|
||||
|
|
@ -328,7 +328,7 @@ func (s *OrgTestSuite) TestGetOrganizationDBDecryptingErr() {
|
|||
_, err := s.StoreSQLMocked.GetOrganization(s.adminCtx, s.Fixtures.Orgs[0].Name, s.Fixtures.Orgs[0].Endpoint.Name)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching org: missing secret", err.Error())
|
||||
s.Require().Equal("error fetching org: missing secret", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -404,7 +404,7 @@ func (s *OrgTestSuite) TestListOrganizationsDBFetchErr() {
|
|||
|
||||
s.assertSQLMockExpectations()
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching org from database: fetching user from database mock error", err.Error())
|
||||
s.Require().Equal("error fetching org from database: fetching user from database mock error", err.Error())
|
||||
}
|
||||
|
||||
func (s *OrgTestSuite) TestDeleteOrganization() {
|
||||
|
|
@ -413,14 +413,14 @@ func (s *OrgTestSuite) TestDeleteOrganization() {
|
|||
s.Require().Nil(err)
|
||||
_, err = s.Store.GetOrganizationByID(s.adminCtx, s.Fixtures.Orgs[0].ID)
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching org: not found", err.Error())
|
||||
s.Require().Equal("error fetching org: not found", err.Error())
|
||||
}
|
||||
|
||||
func (s *OrgTestSuite) TestDeleteOrganizationInvalidOrgID() {
|
||||
err := s.Store.DeleteOrganization(s.adminCtx, "dummy-org-id")
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching org: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error fetching org: error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *OrgTestSuite) TestDeleteOrganizationDBDeleteErr() {
|
||||
|
|
@ -439,7 +439,7 @@ func (s *OrgTestSuite) TestDeleteOrganizationDBDeleteErr() {
|
|||
|
||||
s.assertSQLMockExpectations()
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("deleting org: mocked delete org error", err.Error())
|
||||
s.Require().Equal("error deleting org: mocked delete org error", err.Error())
|
||||
}
|
||||
|
||||
func (s *OrgTestSuite) TestUpdateOrganization() {
|
||||
|
|
@ -454,7 +454,7 @@ func (s *OrgTestSuite) TestUpdateOrganizationInvalidOrgID() {
|
|||
_, err := s.Store.UpdateOrganization(s.adminCtx, "dummy-org-id", s.Fixtures.UpdateRepoParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("saving org: fetching org: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error saving org: error fetching org: error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *OrgTestSuite) TestUpdateOrganizationDBEncryptErr() {
|
||||
|
|
@ -479,7 +479,7 @@ func (s *OrgTestSuite) TestUpdateOrganizationDBEncryptErr() {
|
|||
_, err := s.StoreSQLMocked.UpdateOrganization(s.adminCtx, s.Fixtures.Orgs[0].ID, s.Fixtures.UpdateRepoParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("saving org: saving org: failed to encrypt string: invalid passphrase length (expected length 32 characters)", err.Error())
|
||||
s.Require().Equal("error saving org: saving org: failed to encrypt string: invalid passphrase length (expected length 32 characters)", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -507,7 +507,7 @@ func (s *OrgTestSuite) TestUpdateOrganizationDBSaveErr() {
|
|||
_, err := s.StoreSQLMocked.UpdateOrganization(s.adminCtx, s.Fixtures.Orgs[0].ID, s.Fixtures.UpdateRepoParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("saving org: saving org: saving org mock error", err.Error())
|
||||
s.Require().Equal("error saving org: error saving org: saving org mock error", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -535,7 +535,7 @@ func (s *OrgTestSuite) TestUpdateOrganizationDBDecryptingErr() {
|
|||
_, err := s.StoreSQLMocked.UpdateOrganization(s.adminCtx, s.Fixtures.Orgs[0].ID, s.Fixtures.UpdateRepoParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("saving org: saving org: failed to encrypt string: invalid passphrase length (expected length 32 characters)", err.Error())
|
||||
s.Require().Equal("error saving org: saving org: failed to encrypt string: invalid passphrase length (expected length 32 characters)", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -550,7 +550,7 @@ func (s *OrgTestSuite) TestGetOrganizationByIDInvalidOrgID() {
|
|||
_, err := s.Store.GetOrganizationByID(s.adminCtx, "dummy-org-id")
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching org: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error fetching org: error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *OrgTestSuite) TestGetOrganizationByIDDBDecryptingErr() {
|
||||
|
|
@ -571,7 +571,7 @@ func (s *OrgTestSuite) TestGetOrganizationByIDDBDecryptingErr() {
|
|||
|
||||
s.assertSQLMockExpectations()
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching org: missing secret", err.Error())
|
||||
s.Require().Equal("error fetching org: missing secret", err.Error())
|
||||
}
|
||||
|
||||
func (s *OrgTestSuite) TestCreateOrganizationPool() {
|
||||
|
|
@ -610,7 +610,7 @@ func (s *OrgTestSuite) TestCreateOrganizationPoolInvalidOrgID() {
|
|||
_, err := s.Store.CreateEntityPool(s.adminCtx, entity, s.Fixtures.CreatePoolParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *OrgTestSuite) TestCreateOrganizationPoolDBFetchTagErr() {
|
||||
|
|
@ -628,7 +628,7 @@ func (s *OrgTestSuite) TestCreateOrganizationPoolDBFetchTagErr() {
|
|||
_, err = s.StoreSQLMocked.CreateEntityPool(s.adminCtx, entity, s.Fixtures.CreatePoolParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("creating tag: fetching tag from database: mocked fetching tag error", err.Error())
|
||||
s.Require().Equal("error creating tag: error fetching tag from database: mocked fetching tag error", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -656,7 +656,7 @@ func (s *OrgTestSuite) TestCreateOrganizationPoolDBAddingPoolErr() {
|
|||
_, err = s.StoreSQLMocked.CreateEntityPool(s.adminCtx, entity, s.Fixtures.CreatePoolParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("creating pool: mocked adding pool error", err.Error())
|
||||
s.Require().Equal("error creating pool: mocked adding pool error", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -687,7 +687,7 @@ func (s *OrgTestSuite) TestCreateOrganizationPoolDBSaveTagErr() {
|
|||
_, err = s.StoreSQLMocked.CreateEntityPool(s.adminCtx, entity, s.Fixtures.CreatePoolParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("associating tags: mocked saving tag error", err.Error())
|
||||
s.Require().Equal("error associating tags: mocked saving tag error", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -728,7 +728,7 @@ func (s *OrgTestSuite) TestCreateOrganizationPoolDBFetchPoolErr() {
|
|||
_, err = s.StoreSQLMocked.CreateEntityPool(s.adminCtx, entity, s.Fixtures.CreatePoolParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching pool: not found", err.Error())
|
||||
s.Require().Equal("error fetching pool: not found", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -758,7 +758,7 @@ func (s *OrgTestSuite) TestListOrgPoolsInvalidOrgID() {
|
|||
_, err := s.Store.ListEntityPools(s.adminCtx, entity)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching pools: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error fetching pools: error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *OrgTestSuite) TestGetOrganizationPool() {
|
||||
|
|
@ -783,7 +783,7 @@ func (s *OrgTestSuite) TestGetOrganizationPoolInvalidOrgID() {
|
|||
_, err := s.Store.GetEntityPool(s.adminCtx, entity, "dummy-pool-id")
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching pool: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("fetching pool: error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *OrgTestSuite) TestDeleteOrganizationPool() {
|
||||
|
|
@ -798,7 +798,7 @@ func (s *OrgTestSuite) TestDeleteOrganizationPool() {
|
|||
|
||||
s.Require().Nil(err)
|
||||
_, err = s.Store.GetEntityPool(s.adminCtx, entity, pool.ID)
|
||||
s.Require().Equal("fetching pool: finding pool: not found", err.Error())
|
||||
s.Require().Equal("fetching pool: error finding pool: not found", err.Error())
|
||||
}
|
||||
|
||||
func (s *OrgTestSuite) TestDeleteOrganizationPoolInvalidOrgID() {
|
||||
|
|
@ -809,7 +809,7 @@ func (s *OrgTestSuite) TestDeleteOrganizationPoolInvalidOrgID() {
|
|||
err := s.Store.DeleteEntityPool(s.adminCtx, entity, "dummy-pool-id")
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *OrgTestSuite) TestDeleteOrganizationPoolDBDeleteErr() {
|
||||
|
|
@ -831,7 +831,7 @@ func (s *OrgTestSuite) TestDeleteOrganizationPoolDBDeleteErr() {
|
|||
err = s.StoreSQLMocked.DeleteEntityPool(s.adminCtx, entity, pool.ID)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("removing pool: mocked deleting pool error", err.Error())
|
||||
s.Require().Equal("error removing pool: mocked deleting pool error", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -866,7 +866,7 @@ func (s *OrgTestSuite) TestListOrgInstancesInvalidOrgID() {
|
|||
_, err := s.Store.ListEntityInstances(s.adminCtx, entity)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching entity: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error fetching entity: error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *OrgTestSuite) TestUpdateOrganizationPool() {
|
||||
|
|
@ -916,7 +916,7 @@ func (s *OrgTestSuite) TestUpdateOrganizationPoolInvalidOrgID() {
|
|||
_, err := s.Store.UpdateEntityPool(s.adminCtx, entity, "dummy-pool-id", s.Fixtures.UpdatePoolParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching pool: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error fetching pool: error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func TestOrgTestSuite(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -16,10 +16,10 @@ package sql
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
|
||||
|
|
@ -48,7 +48,7 @@ func (s *sqlDatabase) ListAllPools(_ context.Context) ([]params.Pool, error) {
|
|||
Omit("extra_specs").
|
||||
Find(&pools)
|
||||
if q.Error != nil {
|
||||
return nil, errors.Wrap(q.Error, "fetching all pools")
|
||||
return nil, fmt.Errorf("error fetching all pools: %w", q.Error)
|
||||
}
|
||||
|
||||
ret := make([]params.Pool, len(pools))
|
||||
|
|
@ -56,7 +56,7 @@ func (s *sqlDatabase) ListAllPools(_ context.Context) ([]params.Pool, error) {
|
|||
for idx, val := range pools {
|
||||
ret[idx], err = s.sqlToCommonPool(val)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "converting pool")
|
||||
return nil, fmt.Errorf("error converting pool: %w", err)
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
|
|
@ -75,7 +75,7 @@ func (s *sqlDatabase) GetPoolByID(_ context.Context, poolID string) (params.Pool
|
|||
}
|
||||
pool, err := s.getPoolByID(s.conn, poolID, preloadList...)
|
||||
if err != nil {
|
||||
return params.Pool{}, errors.Wrap(err, "fetching pool by ID")
|
||||
return params.Pool{}, fmt.Errorf("error fetching pool by ID: %w", err)
|
||||
}
|
||||
return s.sqlToCommonPool(pool)
|
||||
}
|
||||
|
|
@ -83,7 +83,7 @@ func (s *sqlDatabase) GetPoolByID(_ context.Context, poolID string) (params.Pool
|
|||
func (s *sqlDatabase) DeletePoolByID(_ context.Context, poolID string) (err error) {
|
||||
pool, err := s.getPoolByID(s.conn, poolID)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "fetching pool by ID")
|
||||
return fmt.Errorf("error fetching pool by ID: %w", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
|
|
@ -93,7 +93,7 @@ func (s *sqlDatabase) DeletePoolByID(_ context.Context, poolID string) (err erro
|
|||
}()
|
||||
|
||||
if q := s.conn.Unscoped().Delete(&pool); q.Error != nil {
|
||||
return errors.Wrap(q.Error, "removing pool")
|
||||
return fmt.Errorf("error removing pool: %w", q.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
@ -101,12 +101,12 @@ func (s *sqlDatabase) DeletePoolByID(_ context.Context, poolID string) (err erro
|
|||
|
||||
func (s *sqlDatabase) getEntityPool(tx *gorm.DB, entityType params.ForgeEntityType, entityID, poolID string, preload ...string) (Pool, error) {
|
||||
if entityID == "" {
|
||||
return Pool{}, errors.Wrap(runnerErrors.ErrBadRequest, "missing entity id")
|
||||
return Pool{}, fmt.Errorf("error missing entity id: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
|
||||
u, err := uuid.Parse(poolID)
|
||||
if err != nil {
|
||||
return Pool{}, errors.Wrap(runnerErrors.ErrBadRequest, "parsing id")
|
||||
return Pool{}, fmt.Errorf("error parsing id: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
|
||||
var fieldName string
|
||||
|
|
@ -140,9 +140,9 @@ func (s *sqlDatabase) getEntityPool(tx *gorm.DB, entityType params.ForgeEntityTy
|
|||
First(&pool).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return Pool{}, errors.Wrap(runnerErrors.ErrNotFound, "finding pool")
|
||||
return Pool{}, fmt.Errorf("error finding pool: %w", runnerErrors.ErrNotFound)
|
||||
}
|
||||
return Pool{}, errors.Wrap(err, "fetching pool")
|
||||
return Pool{}, fmt.Errorf("error fetching pool: %w", err)
|
||||
}
|
||||
|
||||
return pool, nil
|
||||
|
|
@ -150,11 +150,11 @@ func (s *sqlDatabase) getEntityPool(tx *gorm.DB, entityType params.ForgeEntityTy
|
|||
|
||||
func (s *sqlDatabase) listEntityPools(tx *gorm.DB, entityType params.ForgeEntityType, entityID string, preload ...string) ([]Pool, error) {
|
||||
if _, err := uuid.Parse(entityID); err != nil {
|
||||
return nil, errors.Wrap(runnerErrors.ErrBadRequest, "parsing id")
|
||||
return nil, fmt.Errorf("error parsing id: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
|
||||
if err := s.hasGithubEntity(tx, entityType, entityID); err != nil {
|
||||
return nil, errors.Wrap(err, "checking entity existence")
|
||||
return nil, fmt.Errorf("error checking entity existence: %w", err)
|
||||
}
|
||||
|
||||
var preloadEntity string
|
||||
|
|
@ -191,7 +191,7 @@ func (s *sqlDatabase) listEntityPools(tx *gorm.DB, entityType params.ForgeEntity
|
|||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return []Pool{}, nil
|
||||
}
|
||||
return nil, errors.Wrap(err, "fetching pool")
|
||||
return nil, fmt.Errorf("error fetching pool: %w", err)
|
||||
}
|
||||
|
||||
return pools, nil
|
||||
|
|
@ -203,7 +203,7 @@ func (s *sqlDatabase) findPoolByTags(id string, poolType params.ForgeEntityType,
|
|||
}
|
||||
u, err := uuid.Parse(id)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(runnerErrors.ErrBadRequest, "parsing id")
|
||||
return nil, fmt.Errorf("error parsing id: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
|
||||
var fieldName string
|
||||
|
|
@ -233,7 +233,7 @@ func (s *sqlDatabase) findPoolByTags(id string, poolType params.ForgeEntityType,
|
|||
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return nil, runnerErrors.ErrNotFound
|
||||
}
|
||||
return nil, errors.Wrap(q.Error, "fetching pool")
|
||||
return nil, fmt.Errorf("error fetching pool: %w", q.Error)
|
||||
}
|
||||
|
||||
if len(pools) == 0 {
|
||||
|
|
@ -244,7 +244,7 @@ func (s *sqlDatabase) findPoolByTags(id string, poolType params.ForgeEntityType,
|
|||
for idx, val := range pools {
|
||||
ret[idx], err = s.sqlToCommonPool(val)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "converting pool")
|
||||
return nil, fmt.Errorf("error converting pool: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -261,7 +261,7 @@ func (s *sqlDatabase) FindPoolsMatchingAllTags(_ context.Context, entityType par
|
|||
if errors.Is(err, runnerErrors.ErrNotFound) {
|
||||
return []params.Pool{}, nil
|
||||
}
|
||||
return nil, errors.Wrap(err, "fetching pools")
|
||||
return nil, fmt.Errorf("error fetching pools: %w", err)
|
||||
}
|
||||
|
||||
return pools, nil
|
||||
|
|
@ -298,7 +298,7 @@ func (s *sqlDatabase) CreateEntityPool(_ context.Context, entity params.ForgeEnt
|
|||
|
||||
entityID, err := uuid.Parse(entity.ID)
|
||||
if err != nil {
|
||||
return params.Pool{}, errors.Wrap(runnerErrors.ErrBadRequest, "parsing id")
|
||||
return params.Pool{}, fmt.Errorf("error parsing id: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
|
||||
switch entity.EntityType {
|
||||
|
|
@ -311,26 +311,26 @@ func (s *sqlDatabase) CreateEntityPool(_ context.Context, entity params.ForgeEnt
|
|||
}
|
||||
err = s.conn.Transaction(func(tx *gorm.DB) error {
|
||||
if err := s.hasGithubEntity(tx, entity.EntityType, entity.ID); err != nil {
|
||||
return errors.Wrap(err, "checking entity existence")
|
||||
return fmt.Errorf("error checking entity existence: %w", err)
|
||||
}
|
||||
|
||||
tags := []Tag{}
|
||||
for _, val := range param.Tags {
|
||||
t, err := s.getOrCreateTag(tx, val)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "creating tag")
|
||||
return fmt.Errorf("error creating tag: %w", err)
|
||||
}
|
||||
tags = append(tags, t)
|
||||
}
|
||||
|
||||
q := tx.Create(&newPool)
|
||||
if q.Error != nil {
|
||||
return errors.Wrap(q.Error, "creating pool")
|
||||
return fmt.Errorf("error creating pool: %w", q.Error)
|
||||
}
|
||||
|
||||
for i := range tags {
|
||||
if err := tx.Model(&newPool).Association("Tags").Append(&tags[i]); err != nil {
|
||||
return errors.Wrap(err, "associating tags")
|
||||
return fmt.Errorf("error associating tags: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
|
@ -341,7 +341,7 @@ func (s *sqlDatabase) CreateEntityPool(_ context.Context, entity params.ForgeEnt
|
|||
|
||||
dbPool, err := s.getPoolByID(s.conn, newPool.ID.String(), "Tags", "Instances", "Enterprise", "Organization", "Repository")
|
||||
if err != nil {
|
||||
return params.Pool{}, errors.Wrap(err, "fetching pool")
|
||||
return params.Pool{}, fmt.Errorf("error fetching pool: %w", err)
|
||||
}
|
||||
|
||||
return s.sqlToCommonPool(dbPool)
|
||||
|
|
@ -358,7 +358,7 @@ func (s *sqlDatabase) GetEntityPool(_ context.Context, entity params.ForgeEntity
|
|||
func (s *sqlDatabase) DeleteEntityPool(_ context.Context, entity params.ForgeEntity, poolID string) (err error) {
|
||||
entityID, err := uuid.Parse(entity.ID)
|
||||
if err != nil {
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "parsing id")
|
||||
return fmt.Errorf("error parsing id: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
|
|
@ -372,7 +372,7 @@ func (s *sqlDatabase) DeleteEntityPool(_ context.Context, entity params.ForgeEnt
|
|||
|
||||
poolUUID, err := uuid.Parse(poolID)
|
||||
if err != nil {
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "parsing pool id")
|
||||
return fmt.Errorf("error parsing pool id: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
var fieldName string
|
||||
switch entity.EntityType {
|
||||
|
|
@ -387,7 +387,7 @@ func (s *sqlDatabase) DeleteEntityPool(_ context.Context, entity params.ForgeEnt
|
|||
}
|
||||
condition := fmt.Sprintf("id = ? and %s = ?", fieldName)
|
||||
if err := s.conn.Unscoped().Where(condition, poolUUID, entityID).Delete(&Pool{}).Error; err != nil {
|
||||
return errors.Wrap(err, "removing pool")
|
||||
return fmt.Errorf("error removing pool: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -401,12 +401,12 @@ func (s *sqlDatabase) UpdateEntityPool(ctx context.Context, entity params.ForgeE
|
|||
err = s.conn.Transaction(func(tx *gorm.DB) error {
|
||||
pool, err := s.getEntityPool(tx, entity.EntityType, entity.ID, poolID, "Tags", "Instances")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "fetching pool")
|
||||
return fmt.Errorf("error fetching pool: %w", err)
|
||||
}
|
||||
|
||||
updatedPool, err = s.updatePool(tx, pool, param)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "updating pool")
|
||||
return fmt.Errorf("error updating pool: %w", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
|
@ -424,14 +424,14 @@ func (s *sqlDatabase) UpdateEntityPool(ctx context.Context, entity params.ForgeE
|
|||
func (s *sqlDatabase) ListEntityPools(_ context.Context, entity params.ForgeEntity) ([]params.Pool, error) {
|
||||
pools, err := s.listEntityPools(s.conn, entity.EntityType, entity.ID, "Tags")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "fetching pools")
|
||||
return nil, fmt.Errorf("error fetching pools: %w", err)
|
||||
}
|
||||
|
||||
ret := make([]params.Pool, len(pools))
|
||||
for idx, pool := range pools {
|
||||
ret[idx], err = s.sqlToCommonPool(pool)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "fetching pool")
|
||||
return nil, fmt.Errorf("error fetching pool: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -441,7 +441,7 @@ func (s *sqlDatabase) ListEntityPools(_ context.Context, entity params.ForgeEnti
|
|||
func (s *sqlDatabase) ListEntityInstances(_ context.Context, entity params.ForgeEntity) ([]params.Instance, error) {
|
||||
pools, err := s.listEntityPools(s.conn, entity.EntityType, entity.ID, "Instances", "Instances.Job")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "fetching entity")
|
||||
return nil, fmt.Errorf("error fetching entity: %w", err)
|
||||
}
|
||||
ret := []params.Instance{}
|
||||
for _, pool := range pools {
|
||||
|
|
@ -451,7 +451,7 @@ func (s *sqlDatabase) ListEntityInstances(_ context.Context, entity params.Forge
|
|||
instance.Pool = pool
|
||||
paramsInstance, err := s.sqlToParamsInstance(instance)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "fetching instance")
|
||||
return nil, fmt.Errorf("error fetching instance: %w", err)
|
||||
}
|
||||
ret = append(ret, paramsInstance)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -157,7 +157,7 @@ func (s *PoolsTestSuite) TestListAllPoolsDBFetchErr() {
|
|||
|
||||
s.assertSQLMockExpectations()
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching all pools: mocked fetching all pools error", err.Error())
|
||||
s.Require().Equal("error fetching all pools: mocked fetching all pools error", err.Error())
|
||||
}
|
||||
|
||||
func (s *PoolsTestSuite) TestGetPoolByID() {
|
||||
|
|
@ -171,7 +171,7 @@ func (s *PoolsTestSuite) TestGetPoolByIDInvalidPoolID() {
|
|||
_, err := s.Store.GetPoolByID(s.adminCtx, "dummy-pool-id")
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching pool by ID: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error fetching pool by ID: error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *PoolsTestSuite) TestDeletePoolByID() {
|
||||
|
|
@ -179,14 +179,14 @@ func (s *PoolsTestSuite) TestDeletePoolByID() {
|
|||
|
||||
s.Require().Nil(err)
|
||||
_, err = s.Store.GetPoolByID(s.adminCtx, s.Fixtures.Pools[0].ID)
|
||||
s.Require().Equal("fetching pool by ID: not found", err.Error())
|
||||
s.Require().Equal("error fetching pool by ID: not found", err.Error())
|
||||
}
|
||||
|
||||
func (s *PoolsTestSuite) TestDeletePoolByIDInvalidPoolID() {
|
||||
err := s.Store.DeletePoolByID(s.adminCtx, "dummy-pool-id")
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching pool by ID: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error fetching pool by ID: error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *PoolsTestSuite) TestDeletePoolByIDDBRemoveErr() {
|
||||
|
|
@ -204,7 +204,7 @@ func (s *PoolsTestSuite) TestDeletePoolByIDDBRemoveErr() {
|
|||
|
||||
s.assertSQLMockExpectations()
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("removing pool: mocked removing pool error", err.Error())
|
||||
s.Require().Equal("error removing pool: mocked removing pool error", err.Error())
|
||||
}
|
||||
|
||||
func (s *PoolsTestSuite) TestEntityPoolOperations() {
|
||||
|
|
|
|||
|
|
@ -16,11 +16,11 @@ package sql
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
|
||||
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
|
||||
|
|
@ -57,23 +57,23 @@ func (s *sqlDatabase) CreateRepository(ctx context.Context, owner, name string,
|
|||
case params.GiteaEndpointType:
|
||||
newRepo.GiteaCredentialsID = &credentials.ID
|
||||
default:
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "unsupported credentials type")
|
||||
return runnerErrors.NewBadRequestError("unsupported credentials type")
|
||||
}
|
||||
|
||||
newRepo.EndpointName = &credentials.Endpoint.Name
|
||||
q := tx.Create(&newRepo)
|
||||
if q.Error != nil {
|
||||
return errors.Wrap(q.Error, "creating repository")
|
||||
return fmt.Errorf("error creating repository: %w", q.Error)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return params.Repository{}, errors.Wrap(err, "creating repository")
|
||||
return params.Repository{}, fmt.Errorf("error creating repository: %w", err)
|
||||
}
|
||||
|
||||
ret, err := s.GetRepositoryByID(ctx, newRepo.ID.String())
|
||||
if err != nil {
|
||||
return params.Repository{}, errors.Wrap(err, "creating repository")
|
||||
return params.Repository{}, fmt.Errorf("error creating repository: %w", err)
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
|
|
@ -82,12 +82,12 @@ func (s *sqlDatabase) CreateRepository(ctx context.Context, owner, name string,
|
|||
func (s *sqlDatabase) GetRepository(ctx context.Context, owner, name, endpointName string) (params.Repository, error) {
|
||||
repo, err := s.getRepo(ctx, owner, name, endpointName)
|
||||
if err != nil {
|
||||
return params.Repository{}, errors.Wrap(err, "fetching repo")
|
||||
return params.Repository{}, fmt.Errorf("error fetching repo: %w", err)
|
||||
}
|
||||
|
||||
param, err := s.sqlToCommonRepository(repo, true)
|
||||
if err != nil {
|
||||
return params.Repository{}, errors.Wrap(err, "fetching repo")
|
||||
return params.Repository{}, fmt.Errorf("error fetching repo: %w", err)
|
||||
}
|
||||
|
||||
return param, nil
|
||||
|
|
@ -112,7 +112,7 @@ func (s *sqlDatabase) ListRepositories(_ context.Context, filter params.Reposito
|
|||
}
|
||||
q = q.Find(&repos)
|
||||
if q.Error != nil {
|
||||
return []params.Repository{}, errors.Wrap(q.Error, "fetching user from database")
|
||||
return []params.Repository{}, fmt.Errorf("error fetching user from database: %w", q.Error)
|
||||
}
|
||||
|
||||
ret := make([]params.Repository, len(repos))
|
||||
|
|
@ -120,7 +120,7 @@ func (s *sqlDatabase) ListRepositories(_ context.Context, filter params.Reposito
|
|||
var err error
|
||||
ret[idx], err = s.sqlToCommonRepository(val, true)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "fetching repositories")
|
||||
return nil, fmt.Errorf("error fetching repositories: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -130,7 +130,7 @@ func (s *sqlDatabase) ListRepositories(_ context.Context, filter params.Reposito
|
|||
func (s *sqlDatabase) DeleteRepository(ctx context.Context, repoID string) (err error) {
|
||||
repo, err := s.getRepoByID(ctx, s.conn, repoID, "Endpoint", "Credentials", "Credentials.Endpoint", "GiteaCredentials", "GiteaCredentials.Endpoint")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "fetching repo")
|
||||
return fmt.Errorf("error fetching repo: %w", err)
|
||||
}
|
||||
|
||||
defer func(repo Repository) {
|
||||
|
|
@ -146,7 +146,7 @@ func (s *sqlDatabase) DeleteRepository(ctx context.Context, repoID string) (err
|
|||
|
||||
q := s.conn.Unscoped().Delete(&repo)
|
||||
if q.Error != nil && !errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return errors.Wrap(q.Error, "deleting repo")
|
||||
return fmt.Errorf("error deleting repo: %w", q.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
@ -164,23 +164,23 @@ func (s *sqlDatabase) UpdateRepository(ctx context.Context, repoID string, param
|
|||
var err error
|
||||
repo, err = s.getRepoByID(ctx, tx, repoID)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "fetching repo")
|
||||
return fmt.Errorf("error fetching repo: %w", err)
|
||||
}
|
||||
if repo.EndpointName == nil {
|
||||
return errors.Wrap(runnerErrors.ErrUnprocessable, "repository has no endpoint")
|
||||
return runnerErrors.NewUnprocessableError("repository has no endpoint")
|
||||
}
|
||||
|
||||
if param.CredentialsName != "" {
|
||||
creds, err = s.getGithubCredentialsByName(ctx, tx, param.CredentialsName, false)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "fetching credentials")
|
||||
return fmt.Errorf("error fetching credentials: %w", err)
|
||||
}
|
||||
if creds.EndpointName == nil {
|
||||
return errors.Wrap(runnerErrors.ErrUnprocessable, "credentials have no endpoint")
|
||||
return runnerErrors.NewUnprocessableError("credentials have no endpoint")
|
||||
}
|
||||
|
||||
if *creds.EndpointName != *repo.EndpointName {
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "endpoint mismatch")
|
||||
return runnerErrors.NewBadRequestError("endpoint mismatch")
|
||||
}
|
||||
repo.CredentialsID = &creds.ID
|
||||
}
|
||||
|
|
@ -199,23 +199,23 @@ func (s *sqlDatabase) UpdateRepository(ctx context.Context, repoID string, param
|
|||
|
||||
q := tx.Save(&repo)
|
||||
if q.Error != nil {
|
||||
return errors.Wrap(q.Error, "saving repo")
|
||||
return fmt.Errorf("error saving repo: %w", q.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return params.Repository{}, errors.Wrap(err, "saving repo")
|
||||
return params.Repository{}, fmt.Errorf("error saving repo: %w", err)
|
||||
}
|
||||
|
||||
repo, err = s.getRepoByID(ctx, s.conn, repoID, "Endpoint", "Credentials", "Credentials.Endpoint", "GiteaCredentials", "GiteaCredentials.Endpoint")
|
||||
if err != nil {
|
||||
return params.Repository{}, errors.Wrap(err, "updating enterprise")
|
||||
return params.Repository{}, fmt.Errorf("error updating enterprise: %w", err)
|
||||
}
|
||||
|
||||
newParams, err = s.sqlToCommonRepository(repo, true)
|
||||
if err != nil {
|
||||
return params.Repository{}, errors.Wrap(err, "saving repo")
|
||||
return params.Repository{}, fmt.Errorf("error saving repo: %w", err)
|
||||
}
|
||||
return newParams, nil
|
||||
}
|
||||
|
|
@ -232,12 +232,12 @@ func (s *sqlDatabase) GetRepositoryByID(ctx context.Context, repoID string) (par
|
|||
}
|
||||
repo, err := s.getRepoByID(ctx, s.conn, repoID, preloadList...)
|
||||
if err != nil {
|
||||
return params.Repository{}, errors.Wrap(err, "fetching repo")
|
||||
return params.Repository{}, fmt.Errorf("error fetching repo: %w", err)
|
||||
}
|
||||
|
||||
param, err := s.sqlToCommonRepository(repo, true)
|
||||
if err != nil {
|
||||
return params.Repository{}, errors.Wrap(err, "fetching repo")
|
||||
return params.Repository{}, fmt.Errorf("error fetching repo: %w", err)
|
||||
}
|
||||
return param, nil
|
||||
}
|
||||
|
|
@ -259,7 +259,7 @@ func (s *sqlDatabase) getRepo(_ context.Context, owner, name, endpointName strin
|
|||
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return Repository{}, runnerErrors.ErrNotFound
|
||||
}
|
||||
return Repository{}, errors.Wrap(q.Error, "fetching repository from database")
|
||||
return Repository{}, fmt.Errorf("error fetching repository from database: %w", q.Error)
|
||||
}
|
||||
return repo, nil
|
||||
}
|
||||
|
|
@ -267,7 +267,7 @@ func (s *sqlDatabase) getRepo(_ context.Context, owner, name, endpointName strin
|
|||
func (s *sqlDatabase) getRepoByID(_ context.Context, tx *gorm.DB, id string, preload ...string) (Repository, error) {
|
||||
u, err := uuid.Parse(id)
|
||||
if err != nil {
|
||||
return Repository{}, errors.Wrap(runnerErrors.ErrBadRequest, "parsing id")
|
||||
return Repository{}, runnerErrors.NewBadRequestError("error parsing id: %s", err)
|
||||
}
|
||||
var repo Repository
|
||||
|
||||
|
|
@ -283,7 +283,7 @@ func (s *sqlDatabase) getRepoByID(_ context.Context, tx *gorm.DB, id string, pre
|
|||
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return Repository{}, runnerErrors.ErrNotFound
|
||||
}
|
||||
return Repository{}, errors.Wrap(q.Error, "fetching repository from database")
|
||||
return Repository{}, fmt.Errorf("error fetching repository from database: %w", q.Error)
|
||||
}
|
||||
return repo, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -284,7 +284,7 @@ func (s *RepoTestSuite) TestCreateRepositoryInvalidForgeType() {
|
|||
)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("creating repository: unsupported credentials type: invalid request", err.Error())
|
||||
s.Require().Equal("error creating repository: unsupported credentials type", err.Error())
|
||||
}
|
||||
|
||||
func (s *RepoTestSuite) TestCreateRepositoryInvalidDBPassphrase() {
|
||||
|
|
@ -330,7 +330,7 @@ func (s *RepoTestSuite) TestCreateRepositoryInvalidDBCreateErr() {
|
|||
)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("creating repository: creating repository: creating repo mock error", err.Error())
|
||||
s.Require().Equal("error creating repository: error creating repository: creating repo mock error", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -355,7 +355,7 @@ func (s *RepoTestSuite) TestGetRepositoryNotFound() {
|
|||
_, err := s.Store.GetRepository(s.adminCtx, "dummy-owner", "dummy-name", "github.com")
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching repo: not found", err.Error())
|
||||
s.Require().Equal("error fetching repo: not found", err.Error())
|
||||
}
|
||||
|
||||
func (s *RepoTestSuite) TestGetRepositoryDBDecryptingErr() {
|
||||
|
|
@ -371,7 +371,7 @@ func (s *RepoTestSuite) TestGetRepositoryDBDecryptingErr() {
|
|||
_, err := s.StoreSQLMocked.GetRepository(s.adminCtx, s.Fixtures.Repos[0].Owner, s.Fixtures.Repos[0].Name, s.Fixtures.Repos[0].Endpoint.Name)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching repo: missing secret", err.Error())
|
||||
s.Require().Equal("error fetching repo: missing secret", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -471,7 +471,7 @@ func (s *RepoTestSuite) TestListRepositoriesDBFetchErr() {
|
|||
_, err := s.StoreSQLMocked.ListRepositories(s.adminCtx, params.RepositoryFilter{})
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching user from database: fetching user from database mock error", err.Error())
|
||||
s.Require().Equal("error fetching user from database: fetching user from database mock error", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -485,7 +485,7 @@ func (s *RepoTestSuite) TestListRepositoriesDBDecryptingErr() {
|
|||
_, err := s.StoreSQLMocked.ListRepositories(s.adminCtx, params.RepositoryFilter{})
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching repositories: decrypting secret: invalid passphrase length (expected length 32 characters)", err.Error())
|
||||
s.Require().Equal("error fetching repositories: error decrypting secret: invalid passphrase length (expected length 32 characters)", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -495,14 +495,14 @@ func (s *RepoTestSuite) TestDeleteRepository() {
|
|||
s.Require().Nil(err)
|
||||
_, err = s.Store.GetRepositoryByID(s.adminCtx, s.Fixtures.Repos[0].ID)
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching repo: not found", err.Error())
|
||||
s.Require().Equal("error fetching repo: not found", err.Error())
|
||||
}
|
||||
|
||||
func (s *RepoTestSuite) TestDeleteRepositoryInvalidRepoID() {
|
||||
err := s.Store.DeleteRepository(s.adminCtx, "dummy-repo-id")
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching repo: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error fetching repo: error parsing id: invalid UUID length: 13", err.Error())
|
||||
}
|
||||
|
||||
func (s *RepoTestSuite) TestDeleteRepositoryDBRemoveErr() {
|
||||
|
|
@ -520,7 +520,7 @@ func (s *RepoTestSuite) TestDeleteRepositoryDBRemoveErr() {
|
|||
err := s.StoreSQLMocked.DeleteRepository(s.adminCtx, s.Fixtures.Repos[0].ID)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("deleting repo: mocked deleting repo error", err.Error())
|
||||
s.Require().Equal("error deleting repo: mocked deleting repo error", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -536,7 +536,7 @@ func (s *RepoTestSuite) TestUpdateRepositoryInvalidRepoID() {
|
|||
_, err := s.Store.UpdateRepository(s.adminCtx, "dummy-repo-id", s.Fixtures.UpdateRepoParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("saving repo: fetching repo: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error saving repo: error fetching repo: error parsing id: invalid UUID length: 13", err.Error())
|
||||
}
|
||||
|
||||
func (s *RepoTestSuite) TestUpdateRepositoryDBEncryptErr() {
|
||||
|
|
@ -561,7 +561,7 @@ func (s *RepoTestSuite) TestUpdateRepositoryDBEncryptErr() {
|
|||
_, err := s.StoreSQLMocked.UpdateRepository(s.adminCtx, s.Fixtures.Repos[0].ID, s.Fixtures.UpdateRepoParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("saving repo: saving repo: failed to encrypt string: invalid passphrase length (expected length 32 characters)", err.Error())
|
||||
s.Require().Equal("error saving repo: saving repo: failed to encrypt string: invalid passphrase length (expected length 32 characters)", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -589,7 +589,7 @@ func (s *RepoTestSuite) TestUpdateRepositoryDBSaveErr() {
|
|||
_, err := s.StoreSQLMocked.UpdateRepository(s.adminCtx, s.Fixtures.Repos[0].ID, s.Fixtures.UpdateRepoParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("saving repo: saving repo: saving repo mock error", err.Error())
|
||||
s.Require().Equal("error saving repo: error saving repo: saving repo mock error", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -616,7 +616,7 @@ func (s *RepoTestSuite) TestUpdateRepositoryDBDecryptingErr() {
|
|||
_, err := s.StoreSQLMocked.UpdateRepository(s.adminCtx, s.Fixtures.Repos[0].ID, s.Fixtures.UpdateRepoParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("saving repo: saving repo: failed to encrypt string: invalid passphrase length (expected length 32 characters)", err.Error())
|
||||
s.Require().Equal("error saving repo: saving repo: failed to encrypt string: invalid passphrase length (expected length 32 characters)", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -631,7 +631,7 @@ func (s *RepoTestSuite) TestGetRepositoryByIDInvalidRepoID() {
|
|||
_, err := s.Store.GetRepositoryByID(s.adminCtx, "dummy-repo-id")
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching repo: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error fetching repo: error parsing id: invalid UUID length: 13", err.Error())
|
||||
}
|
||||
|
||||
func (s *RepoTestSuite) TestGetRepositoryByIDDBDecryptingErr() {
|
||||
|
|
@ -651,7 +651,7 @@ func (s *RepoTestSuite) TestGetRepositoryByIDDBDecryptingErr() {
|
|||
_, err := s.StoreSQLMocked.GetRepositoryByID(s.adminCtx, s.Fixtures.Repos[0].ID)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching repo: missing secret", err.Error())
|
||||
s.Require().Equal("error fetching repo: missing secret", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -690,7 +690,7 @@ func (s *RepoTestSuite) TestCreateRepositoryPoolInvalidRepoID() {
|
|||
_, err := s.Store.CreateEntityPool(s.adminCtx, entity, s.Fixtures.CreatePoolParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *RepoTestSuite) TestCreateRepositoryPoolDBFetchTagErr() {
|
||||
|
|
@ -709,7 +709,7 @@ func (s *RepoTestSuite) TestCreateRepositoryPoolDBFetchTagErr() {
|
|||
_, err = s.StoreSQLMocked.CreateEntityPool(s.adminCtx, entity, s.Fixtures.CreatePoolParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("creating tag: fetching tag from database: mocked fetching tag error", err.Error())
|
||||
s.Require().Equal("error creating tag: error fetching tag from database: mocked fetching tag error", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -738,7 +738,7 @@ func (s *RepoTestSuite) TestCreateRepositoryPoolDBAddingPoolErr() {
|
|||
_, err = s.StoreSQLMocked.CreateEntityPool(s.adminCtx, entity, s.Fixtures.CreatePoolParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("creating pool: mocked adding pool error", err.Error())
|
||||
s.Require().Equal("error creating pool: mocked adding pool error", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -769,7 +769,7 @@ func (s *RepoTestSuite) TestCreateRepositoryPoolDBSaveTagErr() {
|
|||
|
||||
_, err = s.StoreSQLMocked.CreateEntityPool(s.adminCtx, entity, s.Fixtures.CreatePoolParams)
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("associating tags: mocked saving tag error", err.Error())
|
||||
s.Require().Equal("error associating tags: mocked saving tag error", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -810,7 +810,7 @@ func (s *RepoTestSuite) TestCreateRepositoryPoolDBFetchPoolErr() {
|
|||
_, err = s.StoreSQLMocked.CreateEntityPool(s.adminCtx, entity, s.Fixtures.CreatePoolParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching pool: not found", err.Error())
|
||||
s.Require().Equal("error fetching pool: not found", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -841,7 +841,7 @@ func (s *RepoTestSuite) TestListRepoPoolsInvalidRepoID() {
|
|||
_, err := s.Store.ListEntityPools(s.adminCtx, entity)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching pools: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error fetching pools: error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *RepoTestSuite) TestGetRepositoryPool() {
|
||||
|
|
@ -866,7 +866,7 @@ func (s *RepoTestSuite) TestGetRepositoryPoolInvalidRepoID() {
|
|||
_, err := s.Store.GetEntityPool(s.adminCtx, entity, "dummy-pool-id")
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching pool: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("fetching pool: error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *RepoTestSuite) TestDeleteRepositoryPool() {
|
||||
|
|
@ -881,7 +881,7 @@ func (s *RepoTestSuite) TestDeleteRepositoryPool() {
|
|||
|
||||
s.Require().Nil(err)
|
||||
_, err = s.Store.GetEntityPool(s.adminCtx, entity, pool.ID)
|
||||
s.Require().Equal("fetching pool: finding pool: not found", err.Error())
|
||||
s.Require().Equal("fetching pool: error finding pool: not found", err.Error())
|
||||
}
|
||||
|
||||
func (s *RepoTestSuite) TestDeleteRepositoryPoolInvalidRepoID() {
|
||||
|
|
@ -892,7 +892,7 @@ func (s *RepoTestSuite) TestDeleteRepositoryPoolInvalidRepoID() {
|
|||
err := s.Store.DeleteEntityPool(s.adminCtx, entity, "dummy-pool-id")
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *RepoTestSuite) TestDeleteRepositoryPoolDBDeleteErr() {
|
||||
|
|
@ -913,7 +913,7 @@ func (s *RepoTestSuite) TestDeleteRepositoryPoolDBDeleteErr() {
|
|||
|
||||
err = s.StoreSQLMocked.DeleteEntityPool(s.adminCtx, entity, pool.ID)
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("removing pool: mocked deleting pool error", err.Error())
|
||||
s.Require().Equal("error removing pool: mocked deleting pool error", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -948,7 +948,7 @@ func (s *RepoTestSuite) TestListRepoInstancesInvalidRepoID() {
|
|||
_, err := s.Store.ListEntityInstances(s.adminCtx, entity)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching entity: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error fetching entity: error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *RepoTestSuite) TestUpdateRepositoryPool() {
|
||||
|
|
@ -976,7 +976,7 @@ func (s *RepoTestSuite) TestUpdateRepositoryPoolInvalidRepoID() {
|
|||
_, err := s.Store.UpdateEntityPool(s.adminCtx, entity, "dummy-repo-id", s.Fixtures.UpdatePoolParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching pool: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("error fetching pool: error parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *RepoTestSuite) TestAddRepoEntityEvent() {
|
||||
|
|
|
|||
|
|
@ -16,8 +16,7 @@ package sql
|
|||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/cloudbase/garm/database/common"
|
||||
"github.com/cloudbase/garm/params"
|
||||
|
|
@ -26,7 +25,7 @@ import (
|
|||
func (s *sqlDatabase) CreateScaleSetInstance(_ context.Context, scaleSetID uint, param params.CreateInstanceParams) (instance params.Instance, err error) {
|
||||
scaleSet, err := s.getScaleSetByID(s.conn, scaleSetID)
|
||||
if err != nil {
|
||||
return params.Instance{}, errors.Wrap(err, "fetching scale set")
|
||||
return params.Instance{}, fmt.Errorf("error fetching scale set: %w", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
|
|
@ -39,7 +38,7 @@ func (s *sqlDatabase) CreateScaleSetInstance(_ context.Context, scaleSetID uint,
|
|||
if len(param.JitConfiguration) > 0 {
|
||||
secret, err = s.marshalAndSeal(param.JitConfiguration)
|
||||
if err != nil {
|
||||
return params.Instance{}, errors.Wrap(err, "marshalling jit config")
|
||||
return params.Instance{}, fmt.Errorf("error marshalling jit config: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -58,7 +57,7 @@ func (s *sqlDatabase) CreateScaleSetInstance(_ context.Context, scaleSetID uint,
|
|||
}
|
||||
q := s.conn.Create(&newInstance)
|
||||
if q.Error != nil {
|
||||
return params.Instance{}, errors.Wrap(q.Error, "creating instance")
|
||||
return params.Instance{}, fmt.Errorf("error creating instance: %w", q.Error)
|
||||
}
|
||||
|
||||
return s.sqlToParamsInstance(newInstance)
|
||||
|
|
@ -72,7 +71,7 @@ func (s *sqlDatabase) ListScaleSetInstances(_ context.Context, scalesetID uint)
|
|||
Where("scale_set_fk_id = ?", scalesetID)
|
||||
|
||||
if err := query.Find(&instances); err.Error != nil {
|
||||
return nil, errors.Wrap(err.Error, "fetching instances")
|
||||
return nil, fmt.Errorf("error fetching instances: %w", err.Error)
|
||||
}
|
||||
|
||||
var err error
|
||||
|
|
@ -80,7 +79,7 @@ func (s *sqlDatabase) ListScaleSetInstances(_ context.Context, scalesetID uint)
|
|||
for idx, inst := range instances {
|
||||
ret[idx], err = s.sqlToParamsInstance(inst)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "converting instance")
|
||||
return nil, fmt.Errorf("error converting instance: %w", err)
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
|
|
|
|||
|
|
@ -16,10 +16,10 @@ package sql
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
|
||||
|
|
@ -42,7 +42,7 @@ func (s *sqlDatabase) ListAllScaleSets(_ context.Context) ([]params.ScaleSet, er
|
|||
Omit("status_messages").
|
||||
Find(&scaleSets)
|
||||
if q.Error != nil {
|
||||
return nil, errors.Wrap(q.Error, "fetching all scale sets")
|
||||
return nil, fmt.Errorf("error fetching all scale sets: %w", q.Error)
|
||||
}
|
||||
|
||||
ret := make([]params.ScaleSet, len(scaleSets))
|
||||
|
|
@ -50,7 +50,7 @@ func (s *sqlDatabase) ListAllScaleSets(_ context.Context) ([]params.ScaleSet, er
|
|||
for idx, val := range scaleSets {
|
||||
ret[idx], err = s.sqlToCommonScaleSet(val)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "converting scale sets")
|
||||
return nil, fmt.Errorf("error converting scale sets: %w", err)
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
|
|
@ -91,7 +91,7 @@ func (s *sqlDatabase) CreateEntityScaleSet(_ context.Context, entity params.Forg
|
|||
|
||||
entityID, err := uuid.Parse(entity.ID)
|
||||
if err != nil {
|
||||
return params.ScaleSet{}, errors.Wrap(runnerErrors.ErrBadRequest, "parsing id")
|
||||
return params.ScaleSet{}, fmt.Errorf("error parsing id: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
|
||||
switch entity.EntityType {
|
||||
|
|
@ -104,12 +104,12 @@ func (s *sqlDatabase) CreateEntityScaleSet(_ context.Context, entity params.Forg
|
|||
}
|
||||
err = s.conn.Transaction(func(tx *gorm.DB) error {
|
||||
if err := s.hasGithubEntity(tx, entity.EntityType, entity.ID); err != nil {
|
||||
return errors.Wrap(err, "checking entity existence")
|
||||
return fmt.Errorf("error checking entity existence: %w", err)
|
||||
}
|
||||
|
||||
q := tx.Create(&newScaleSet)
|
||||
if q.Error != nil {
|
||||
return errors.Wrap(q.Error, "creating scale set")
|
||||
return fmt.Errorf("error creating scale set: %w", q.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
@ -120,7 +120,7 @@ func (s *sqlDatabase) CreateEntityScaleSet(_ context.Context, entity params.Forg
|
|||
|
||||
dbScaleSet, err := s.getScaleSetByID(s.conn, newScaleSet.ID, "Instances", "Enterprise", "Organization", "Repository")
|
||||
if err != nil {
|
||||
return params.ScaleSet{}, errors.Wrap(err, "fetching scale set")
|
||||
return params.ScaleSet{}, fmt.Errorf("error fetching scale set: %w", err)
|
||||
}
|
||||
|
||||
return s.sqlToCommonScaleSet(dbScaleSet)
|
||||
|
|
@ -128,11 +128,11 @@ func (s *sqlDatabase) CreateEntityScaleSet(_ context.Context, entity params.Forg
|
|||
|
||||
func (s *sqlDatabase) listEntityScaleSets(tx *gorm.DB, entityType params.ForgeEntityType, entityID string, preload ...string) ([]ScaleSet, error) {
|
||||
if _, err := uuid.Parse(entityID); err != nil {
|
||||
return nil, errors.Wrap(runnerErrors.ErrBadRequest, "parsing id")
|
||||
return nil, fmt.Errorf("error parsing id: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
|
||||
if err := s.hasGithubEntity(tx, entityType, entityID); err != nil {
|
||||
return nil, errors.Wrap(err, "checking entity existence")
|
||||
return nil, fmt.Errorf("error checking entity existence: %w", err)
|
||||
}
|
||||
|
||||
var preloadEntity string
|
||||
|
|
@ -170,7 +170,7 @@ func (s *sqlDatabase) listEntityScaleSets(tx *gorm.DB, entityType params.ForgeEn
|
|||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return []ScaleSet{}, nil
|
||||
}
|
||||
return nil, errors.Wrap(err, "fetching scale sets")
|
||||
return nil, fmt.Errorf("error fetching scale sets: %w", err)
|
||||
}
|
||||
|
||||
return scaleSets, nil
|
||||
|
|
@ -179,14 +179,14 @@ func (s *sqlDatabase) listEntityScaleSets(tx *gorm.DB, entityType params.ForgeEn
|
|||
func (s *sqlDatabase) ListEntityScaleSets(_ context.Context, entity params.ForgeEntity) ([]params.ScaleSet, error) {
|
||||
scaleSets, err := s.listEntityScaleSets(s.conn, entity.EntityType, entity.ID)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "fetching scale sets")
|
||||
return nil, fmt.Errorf("error fetching scale sets: %w", err)
|
||||
}
|
||||
|
||||
ret := make([]params.ScaleSet, len(scaleSets))
|
||||
for idx, set := range scaleSets {
|
||||
ret[idx], err = s.sqlToCommonScaleSet(set)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "conbverting scale set")
|
||||
return nil, fmt.Errorf("error conbverting scale set: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -202,22 +202,22 @@ func (s *sqlDatabase) UpdateEntityScaleSet(ctx context.Context, entity params.Fo
|
|||
err = s.conn.Transaction(func(tx *gorm.DB) error {
|
||||
scaleSet, err := s.getEntityScaleSet(tx, entity.EntityType, entity.ID, scaleSetID, "Instances")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "fetching scale set")
|
||||
return fmt.Errorf("error fetching scale set: %w", err)
|
||||
}
|
||||
|
||||
old, err := s.sqlToCommonScaleSet(scaleSet)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "converting scale set")
|
||||
return fmt.Errorf("error converting scale set: %w", err)
|
||||
}
|
||||
|
||||
updatedScaleSet, err = s.updateScaleSet(tx, scaleSet, param)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "updating scale set")
|
||||
return fmt.Errorf("error updating scale set: %w", err)
|
||||
}
|
||||
|
||||
if callback != nil {
|
||||
if err := callback(old, updatedScaleSet); err != nil {
|
||||
return errors.Wrap(err, "executing update callback")
|
||||
return fmt.Errorf("error executing update callback: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
|
@ -235,11 +235,11 @@ func (s *sqlDatabase) UpdateEntityScaleSet(ctx context.Context, entity params.Fo
|
|||
|
||||
func (s *sqlDatabase) getEntityScaleSet(tx *gorm.DB, entityType params.ForgeEntityType, entityID string, scaleSetID uint, preload ...string) (ScaleSet, error) {
|
||||
if entityID == "" {
|
||||
return ScaleSet{}, errors.Wrap(runnerErrors.ErrBadRequest, "missing entity id")
|
||||
return ScaleSet{}, fmt.Errorf("error missing entity id: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
|
||||
if scaleSetID == 0 {
|
||||
return ScaleSet{}, errors.Wrap(runnerErrors.ErrBadRequest, "missing scaleset id")
|
||||
return ScaleSet{}, fmt.Errorf("error missing scaleset id: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
|
||||
var fieldName string
|
||||
|
|
@ -273,9 +273,9 @@ func (s *sqlDatabase) getEntityScaleSet(tx *gorm.DB, entityType params.ForgeEnti
|
|||
First(&scaleSet).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ScaleSet{}, errors.Wrap(runnerErrors.ErrNotFound, "finding scale set")
|
||||
return ScaleSet{}, fmt.Errorf("error finding scale set: %w", runnerErrors.ErrNotFound)
|
||||
}
|
||||
return ScaleSet{}, errors.Wrap(err, "fetching scale set")
|
||||
return ScaleSet{}, fmt.Errorf("error fetching scale set: %w", err)
|
||||
}
|
||||
|
||||
return scaleSet, nil
|
||||
|
|
@ -343,7 +343,7 @@ func (s *sqlDatabase) updateScaleSet(tx *gorm.DB, scaleSet ScaleSet, param param
|
|||
}
|
||||
|
||||
if q := tx.Save(&scaleSet); q.Error != nil {
|
||||
return params.ScaleSet{}, errors.Wrap(q.Error, "saving database entry")
|
||||
return params.ScaleSet{}, fmt.Errorf("error saving database entry: %w", q.Error)
|
||||
}
|
||||
|
||||
return s.sqlToCommonScaleSet(scaleSet)
|
||||
|
|
@ -362,7 +362,7 @@ func (s *sqlDatabase) GetScaleSetByID(_ context.Context, scaleSet uint) (params.
|
|||
"Repository.Endpoint",
|
||||
)
|
||||
if err != nil {
|
||||
return params.ScaleSet{}, errors.Wrap(err, "fetching scale set by ID")
|
||||
return params.ScaleSet{}, fmt.Errorf("error fetching scale set by ID: %w", err)
|
||||
}
|
||||
return s.sqlToCommonScaleSet(set)
|
||||
}
|
||||
|
|
@ -377,7 +377,7 @@ func (s *sqlDatabase) DeleteScaleSetByID(_ context.Context, scaleSetID uint) (er
|
|||
err = s.conn.Transaction(func(tx *gorm.DB) error {
|
||||
dbSet, err := s.getScaleSetByID(tx, scaleSetID, "Instances", "Enterprise", "Organization", "Repository")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "fetching scale set")
|
||||
return fmt.Errorf("error fetching scale set: %w", err)
|
||||
}
|
||||
|
||||
if len(dbSet.Instances) > 0 {
|
||||
|
|
@ -385,16 +385,16 @@ func (s *sqlDatabase) DeleteScaleSetByID(_ context.Context, scaleSetID uint) (er
|
|||
}
|
||||
scaleSet, err = s.sqlToCommonScaleSet(dbSet)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "converting scale set")
|
||||
return fmt.Errorf("error converting scale set: %w", err)
|
||||
}
|
||||
|
||||
if q := tx.Unscoped().Delete(&dbSet); q.Error != nil {
|
||||
return errors.Wrap(q.Error, "deleting scale set")
|
||||
return fmt.Errorf("error deleting scale set: %w", q.Error)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "removing scale set")
|
||||
return fmt.Errorf("error removing scale set: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -409,19 +409,19 @@ func (s *sqlDatabase) SetScaleSetLastMessageID(_ context.Context, scaleSetID uin
|
|||
if err := s.conn.Transaction(func(tx *gorm.DB) error {
|
||||
dbSet, err := s.getScaleSetByID(tx, scaleSetID, "Instances", "Enterprise", "Organization", "Repository")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "fetching scale set")
|
||||
return fmt.Errorf("error fetching scale set: %w", err)
|
||||
}
|
||||
dbSet.LastMessageID = lastMessageID
|
||||
if err := tx.Save(&dbSet).Error; err != nil {
|
||||
return errors.Wrap(err, "saving database entry")
|
||||
return fmt.Errorf("error saving database entry: %w", err)
|
||||
}
|
||||
scaleSet, err = s.sqlToCommonScaleSet(dbSet)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "converting scale set")
|
||||
return fmt.Errorf("error converting scale set: %w", err)
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return errors.Wrap(err, "setting last message ID")
|
||||
return fmt.Errorf("error setting last message ID: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -436,19 +436,19 @@ func (s *sqlDatabase) SetScaleSetDesiredRunnerCount(_ context.Context, scaleSetI
|
|||
if err := s.conn.Transaction(func(tx *gorm.DB) error {
|
||||
dbSet, err := s.getScaleSetByID(tx, scaleSetID, "Instances", "Enterprise", "Organization", "Repository")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "fetching scale set")
|
||||
return fmt.Errorf("error fetching scale set: %w", err)
|
||||
}
|
||||
dbSet.DesiredRunnerCount = desiredRunnerCount
|
||||
if err := tx.Save(&dbSet).Error; err != nil {
|
||||
return errors.Wrap(err, "saving database entry")
|
||||
return fmt.Errorf("error saving database entry: %w", err)
|
||||
}
|
||||
scaleSet, err = s.sqlToCommonScaleSet(dbSet)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "converting scale set")
|
||||
return fmt.Errorf("error converting scale set: %w", err)
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return errors.Wrap(err, "setting desired runner count")
|
||||
return fmt.Errorf("error setting desired runner count: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,12 +16,12 @@ package sql
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
|
|
@ -46,7 +46,7 @@ const (
|
|||
func newDBConn(dbCfg config.Database) (conn *gorm.DB, err error) {
|
||||
dbType, connURI, err := dbCfg.GormParams()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "getting DB URI string")
|
||||
return nil, fmt.Errorf("error getting DB URI string: %w", err)
|
||||
}
|
||||
|
||||
gormConfig := &gorm.Config{}
|
||||
|
|
@ -61,7 +61,7 @@ func newDBConn(dbCfg config.Database) (conn *gorm.DB, err error) {
|
|||
conn, err = gorm.Open(sqlite.Open(connURI), gormConfig)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "connecting to database")
|
||||
return nil, fmt.Errorf("error connecting to database: %w", err)
|
||||
}
|
||||
|
||||
if dbCfg.Debug {
|
||||
|
|
@ -73,11 +73,11 @@ func newDBConn(dbCfg config.Database) (conn *gorm.DB, err error) {
|
|||
func NewSQLDatabase(ctx context.Context, cfg config.Database) (common.Store, error) {
|
||||
conn, err := newDBConn(cfg)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "creating DB connection")
|
||||
return nil, fmt.Errorf("error creating DB connection: %w", err)
|
||||
}
|
||||
producer, err := watcher.RegisterProducer(ctx, "sql")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "registering producer")
|
||||
return nil, fmt.Errorf("error registering producer: %w", err)
|
||||
}
|
||||
db := &sqlDatabase{
|
||||
conn: conn,
|
||||
|
|
@ -87,7 +87,7 @@ func NewSQLDatabase(ctx context.Context, cfg config.Database) (common.Store, err
|
|||
}
|
||||
|
||||
if err := db.migrateDB(); err != nil {
|
||||
return nil, errors.Wrap(err, "migrating database")
|
||||
return nil, fmt.Errorf("error migrating database: %w", err)
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
|
@ -221,14 +221,14 @@ func (s *sqlDatabase) ensureGithubEndpoint() error {
|
|||
var epCount int64
|
||||
if err := s.conn.Model(&GithubEndpoint{}).Count(&epCount).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.Wrap(err, "counting github endpoints")
|
||||
return fmt.Errorf("error counting github endpoints: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if epCount == 0 {
|
||||
if _, err := s.CreateGithubEndpoint(context.Background(), createEndpointParams); err != nil {
|
||||
if !errors.Is(err, runnerErrors.ErrDuplicateEntity) {
|
||||
return errors.Wrap(err, "creating default github endpoint")
|
||||
return fmt.Errorf("error creating default github endpoint: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -246,7 +246,7 @@ func (s *sqlDatabase) migrateCredentialsToDB() (err error) {
|
|||
// Admin user doesn't exist. This is a new deploy. Nothing to migrate.
|
||||
return nil
|
||||
}
|
||||
return errors.Wrap(err, "getting admin user")
|
||||
return fmt.Errorf("error getting admin user: %w", err)
|
||||
}
|
||||
|
||||
// Impersonate the admin user. We're migrating from config credentials to
|
||||
|
|
@ -259,7 +259,7 @@ func (s *sqlDatabase) migrateCredentialsToDB() (err error) {
|
|||
slog.Info("migrating credentials to DB")
|
||||
slog.Info("creating github endpoints table")
|
||||
if err := s.conn.AutoMigrate(&GithubEndpoint{}); err != nil {
|
||||
return errors.Wrap(err, "migrating github endpoints")
|
||||
return fmt.Errorf("error migrating github endpoints: %w", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
|
|
@ -271,7 +271,7 @@ func (s *sqlDatabase) migrateCredentialsToDB() (err error) {
|
|||
|
||||
slog.Info("creating github credentials table")
|
||||
if err := s.conn.AutoMigrate(&GithubCredentials{}); err != nil {
|
||||
return errors.Wrap(err, "migrating github credentials")
|
||||
return fmt.Errorf("error migrating github credentials: %w", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
|
|
@ -291,12 +291,12 @@ func (s *sqlDatabase) migrateCredentialsToDB() (err error) {
|
|||
slog.Info("importing credential", "name", cred.Name)
|
||||
parsed, err := url.Parse(cred.BaseEndpoint())
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "parsing base URL")
|
||||
return fmt.Errorf("error parsing base URL: %w", err)
|
||||
}
|
||||
|
||||
certBundle, err := cred.CACertBundle()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "getting CA cert bundle")
|
||||
return fmt.Errorf("error getting CA cert bundle: %w", err)
|
||||
}
|
||||
hostname := parsed.Hostname()
|
||||
createParams := params.CreateGithubEndpointParams{
|
||||
|
|
@ -312,11 +312,11 @@ func (s *sqlDatabase) migrateCredentialsToDB() (err error) {
|
|||
endpoint, err = s.GetGithubEndpoint(adminCtx, hostname)
|
||||
if err != nil {
|
||||
if !errors.Is(err, runnerErrors.ErrNotFound) {
|
||||
return errors.Wrap(err, "getting github endpoint")
|
||||
return fmt.Errorf("error getting github endpoint: %w", err)
|
||||
}
|
||||
endpoint, err = s.CreateGithubEndpoint(adminCtx, createParams)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "creating default github endpoint")
|
||||
return fmt.Errorf("error creating default github endpoint: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -330,7 +330,7 @@ func (s *sqlDatabase) migrateCredentialsToDB() (err error) {
|
|||
case params.ForgeAuthTypeApp:
|
||||
keyBytes, err := cred.App.PrivateKeyBytes()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "getting private key bytes")
|
||||
return fmt.Errorf("error getting private key bytes: %w", err)
|
||||
}
|
||||
credParams.App = params.GithubApp{
|
||||
AppID: cred.App.AppID,
|
||||
|
|
@ -339,7 +339,7 @@ func (s *sqlDatabase) migrateCredentialsToDB() (err error) {
|
|||
}
|
||||
|
||||
if err := credParams.App.Validate(); err != nil {
|
||||
return errors.Wrap(err, "validating app credentials")
|
||||
return fmt.Errorf("error validating app credentials: %w", err)
|
||||
}
|
||||
case params.ForgeAuthTypePAT:
|
||||
token := cred.PAT.OAuth2Token
|
||||
|
|
@ -356,19 +356,19 @@ func (s *sqlDatabase) migrateCredentialsToDB() (err error) {
|
|||
|
||||
creds, err := s.CreateGithubCredentials(adminCtx, credParams)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "creating github credentials")
|
||||
return fmt.Errorf("error creating github credentials: %w", err)
|
||||
}
|
||||
|
||||
if err := s.conn.Exec("update repositories set credentials_id = ?,endpoint_name = ? where credentials_name = ?", creds.ID, creds.Endpoint.Name, creds.Name).Error; err != nil {
|
||||
return errors.Wrap(err, "updating repositories")
|
||||
return fmt.Errorf("error updating repositories: %w", err)
|
||||
}
|
||||
|
||||
if err := s.conn.Exec("update organizations set credentials_id = ?,endpoint_name = ? where credentials_name = ?", creds.ID, creds.Endpoint.Name, creds.Name).Error; err != nil {
|
||||
return errors.Wrap(err, "updating organizations")
|
||||
return fmt.Errorf("error updating organizations: %w", err)
|
||||
}
|
||||
|
||||
if err := s.conn.Exec("update enterprises set credentials_id = ?,endpoint_name = ? where credentials_name = ?", creds.ID, creds.Endpoint.Name, creds.Name).Error; err != nil {
|
||||
return errors.Wrap(err, "updating enterprises")
|
||||
return fmt.Errorf("error updating enterprises: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
|
@ -380,10 +380,10 @@ func (s *sqlDatabase) migrateWorkflow() error {
|
|||
// Remove jobs that are not in "queued" status. We really only care about queued jobs. Once they transition
|
||||
// to something else, we don't really consume them anyway.
|
||||
if err := s.conn.Exec("delete from workflow_jobs where status is not 'queued'").Error; err != nil {
|
||||
return errors.Wrap(err, "updating workflow_jobs")
|
||||
return fmt.Errorf("error updating workflow_jobs: %w", err)
|
||||
}
|
||||
if err := s.conn.Migrator().DropColumn(&WorkflowJob{}, "runner_name"); err != nil {
|
||||
return errors.Wrap(err, "updating workflow_jobs")
|
||||
return fmt.Errorf("error updating workflow_jobs: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -404,34 +404,34 @@ func (s *sqlDatabase) migrateDB() error {
|
|||
}
|
||||
|
||||
if err := s.cascadeMigration(); err != nil {
|
||||
return errors.Wrap(err, "running cascade migration")
|
||||
return fmt.Errorf("error running cascade migration: %w", err)
|
||||
}
|
||||
|
||||
if s.conn.Migrator().HasTable(&Pool{}) {
|
||||
if err := s.conn.Exec("update pools set repo_id=NULL where repo_id='00000000-0000-0000-0000-000000000000'").Error; err != nil {
|
||||
return errors.Wrap(err, "updating pools")
|
||||
return fmt.Errorf("error updating pools %w", err)
|
||||
}
|
||||
|
||||
if err := s.conn.Exec("update pools set org_id=NULL where org_id='00000000-0000-0000-0000-000000000000'").Error; err != nil {
|
||||
return errors.Wrap(err, "updating pools")
|
||||
return fmt.Errorf("error updating pools: %w", err)
|
||||
}
|
||||
|
||||
if err := s.conn.Exec("update pools set enterprise_id=NULL where enterprise_id='00000000-0000-0000-0000-000000000000'").Error; err != nil {
|
||||
return errors.Wrap(err, "updating pools")
|
||||
return fmt.Errorf("error updating pools: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := s.migrateWorkflow(); err != nil {
|
||||
return errors.Wrap(err, "migrating workflows")
|
||||
return fmt.Errorf("error migrating workflows: %w", err)
|
||||
}
|
||||
|
||||
if s.conn.Migrator().HasTable(&GithubEndpoint{}) {
|
||||
if !s.conn.Migrator().HasColumn(&GithubEndpoint{}, "endpoint_type") {
|
||||
if err := s.conn.Migrator().AutoMigrate(&GithubEndpoint{}); err != nil {
|
||||
return errors.Wrap(err, "migrating github endpoints")
|
||||
return fmt.Errorf("error migrating github endpoints: %w", err)
|
||||
}
|
||||
if err := s.conn.Exec("update github_endpoints set endpoint_type = 'github' where endpoint_type is null").Error; err != nil {
|
||||
return errors.Wrap(err, "updating github endpoints")
|
||||
return fmt.Errorf("error updating github endpoints: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -467,7 +467,7 @@ func (s *sqlDatabase) migrateDB() error {
|
|||
&WorkflowJob{},
|
||||
&ScaleSet{},
|
||||
); err != nil {
|
||||
return errors.Wrap(err, "running auto migrate")
|
||||
return fmt.Errorf("error running auto migrate: %w", err)
|
||||
}
|
||||
s.conn.Exec("PRAGMA foreign_keys = ON")
|
||||
|
||||
|
|
@ -475,23 +475,23 @@ func (s *sqlDatabase) migrateDB() error {
|
|||
var controller ControllerInfo
|
||||
if err := s.conn.First(&controller).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.Wrap(err, "updating controller info")
|
||||
return fmt.Errorf("error updating controller info: %w", err)
|
||||
}
|
||||
} else {
|
||||
controller.MinimumJobAgeBackoff = 30
|
||||
if err := s.conn.Save(&controller).Error; err != nil {
|
||||
return errors.Wrap(err, "updating controller info")
|
||||
return fmt.Errorf("error updating controller info: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := s.ensureGithubEndpoint(); err != nil {
|
||||
return errors.Wrap(err, "ensuring github endpoint")
|
||||
return fmt.Errorf("error ensuring github endpoint: %w", err)
|
||||
}
|
||||
|
||||
if needsCredentialMigration {
|
||||
if err := s.migrateCredentialsToDB(); err != nil {
|
||||
return errors.Wrap(err, "migrating credentials")
|
||||
return fmt.Errorf("error migrating credentials: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -16,9 +16,9 @@ package sql
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
|
||||
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
|
||||
|
|
@ -39,7 +39,7 @@ func (s *sqlDatabase) getUserByUsernameOrEmail(tx *gorm.DB, user string) (User,
|
|||
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return User{}, runnerErrors.ErrNotFound
|
||||
}
|
||||
return User{}, errors.Wrap(q.Error, "fetching user")
|
||||
return User{}, fmt.Errorf("error fetching user: %w", q.Error)
|
||||
}
|
||||
return dbUser, nil
|
||||
}
|
||||
|
|
@ -51,7 +51,7 @@ func (s *sqlDatabase) getUserByID(tx *gorm.DB, userID string) (User, error) {
|
|||
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return User{}, runnerErrors.ErrNotFound
|
||||
}
|
||||
return User{}, errors.Wrap(q.Error, "fetching user")
|
||||
return User{}, fmt.Errorf("error fetching user: %w", q.Error)
|
||||
}
|
||||
return dbUser, nil
|
||||
}
|
||||
|
|
@ -82,12 +82,12 @@ func (s *sqlDatabase) CreateUser(_ context.Context, user params.NewUserParams) (
|
|||
|
||||
q := tx.Save(&newUser)
|
||||
if q.Error != nil {
|
||||
return errors.Wrap(q.Error, "creating user")
|
||||
return fmt.Errorf("error creating user: %w", q.Error)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return params.User{}, errors.Wrap(err, "creating user")
|
||||
return params.User{}, fmt.Errorf("error creating user: %w", err)
|
||||
}
|
||||
return s.sqlToParamsUser(newUser), nil
|
||||
}
|
||||
|
|
@ -105,7 +105,7 @@ func (s *sqlDatabase) HasAdminUser(_ context.Context) bool {
|
|||
func (s *sqlDatabase) GetUser(_ context.Context, user string) (params.User, error) {
|
||||
dbUser, err := s.getUserByUsernameOrEmail(s.conn, user)
|
||||
if err != nil {
|
||||
return params.User{}, errors.Wrap(err, "fetching user")
|
||||
return params.User{}, fmt.Errorf("error fetching user: %w", err)
|
||||
}
|
||||
return s.sqlToParamsUser(dbUser), nil
|
||||
}
|
||||
|
|
@ -113,7 +113,7 @@ func (s *sqlDatabase) GetUser(_ context.Context, user string) (params.User, erro
|
|||
func (s *sqlDatabase) GetUserByID(_ context.Context, userID string) (params.User, error) {
|
||||
dbUser, err := s.getUserByID(s.conn, userID)
|
||||
if err != nil {
|
||||
return params.User{}, errors.Wrap(err, "fetching user")
|
||||
return params.User{}, fmt.Errorf("error fetching user: %w", err)
|
||||
}
|
||||
return s.sqlToParamsUser(dbUser), nil
|
||||
}
|
||||
|
|
@ -124,7 +124,7 @@ func (s *sqlDatabase) UpdateUser(_ context.Context, user string, param params.Up
|
|||
err = s.conn.Transaction(func(tx *gorm.DB) error {
|
||||
dbUser, err = s.getUserByUsernameOrEmail(tx, user)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "fetching user")
|
||||
return fmt.Errorf("error fetching user: %w", err)
|
||||
}
|
||||
|
||||
if param.FullName != "" {
|
||||
|
|
@ -141,12 +141,12 @@ func (s *sqlDatabase) UpdateUser(_ context.Context, user string, param params.Up
|
|||
}
|
||||
|
||||
if q := tx.Save(&dbUser); q.Error != nil {
|
||||
return errors.Wrap(q.Error, "saving user")
|
||||
return fmt.Errorf("error saving user: %w", q.Error)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return params.User{}, errors.Wrap(err, "updating user")
|
||||
return params.User{}, fmt.Errorf("error updating user: %w", err)
|
||||
}
|
||||
return s.sqlToParamsUser(dbUser), nil
|
||||
}
|
||||
|
|
@ -159,7 +159,7 @@ func (s *sqlDatabase) GetAdminUser(_ context.Context) (params.User, error) {
|
|||
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return params.User{}, runnerErrors.ErrNotFound
|
||||
}
|
||||
return params.User{}, errors.Wrap(q.Error, "fetching admin user")
|
||||
return params.User{}, fmt.Errorf("error fetching admin user: %w", q.Error)
|
||||
}
|
||||
return s.sqlToParamsUser(user), nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -161,7 +161,7 @@ func (s *UserTestSuite) TestCreateUserUsernameAlreadyExist() {
|
|||
_, err := s.Store.CreateUser(context.Background(), s.Fixtures.NewUserParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal(("creating user: username already exists"), err.Error())
|
||||
s.Require().Equal(("error creating user: username already exists"), err.Error())
|
||||
}
|
||||
|
||||
func (s *UserTestSuite) TestCreateUserEmailAlreadyExist() {
|
||||
|
|
@ -170,7 +170,7 @@ func (s *UserTestSuite) TestCreateUserEmailAlreadyExist() {
|
|||
_, err := s.Store.CreateUser(context.Background(), s.Fixtures.NewUserParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal(("creating user: email already exists"), err.Error())
|
||||
s.Require().Equal(("error creating user: email already exists"), err.Error())
|
||||
}
|
||||
|
||||
func (s *UserTestSuite) TestCreateUserDBCreateErr() {
|
||||
|
|
@ -191,7 +191,7 @@ func (s *UserTestSuite) TestCreateUserDBCreateErr() {
|
|||
_, err := s.StoreSQLMocked.CreateUser(context.Background(), s.Fixtures.NewUserParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("creating user: creating user: creating user mock error", err.Error())
|
||||
s.Require().Equal("error creating user: error creating user: creating user mock error", err.Error())
|
||||
s.assertSQLMockExpectations()
|
||||
}
|
||||
|
||||
|
|
@ -230,7 +230,7 @@ func (s *UserTestSuite) TestGetUserNotFound() {
|
|||
_, err := s.Store.GetUser(context.Background(), "dummy-user")
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching user: not found", err.Error())
|
||||
s.Require().Equal("error fetching user: not found", err.Error())
|
||||
}
|
||||
|
||||
func (s *UserTestSuite) TestGetUserByID() {
|
||||
|
|
@ -244,7 +244,7 @@ func (s *UserTestSuite) TestGetUserByIDNotFound() {
|
|||
_, err := s.Store.GetUserByID(context.Background(), "dummy-user-id")
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("fetching user: not found", err.Error())
|
||||
s.Require().Equal("error fetching user: not found", err.Error())
|
||||
}
|
||||
|
||||
func (s *UserTestSuite) TestUpdateUser() {
|
||||
|
|
@ -260,7 +260,7 @@ func (s *UserTestSuite) TestUpdateUserNotFound() {
|
|||
_, err := s.Store.UpdateUser(context.Background(), "dummy-user", s.Fixtures.UpdateUserParams)
|
||||
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("updating user: fetching user: not found", err.Error())
|
||||
s.Require().Equal("error updating user: error fetching user: not found", err.Error())
|
||||
}
|
||||
|
||||
func (s *UserTestSuite) TestUpdateUserDBSaveErr() {
|
||||
|
|
@ -278,7 +278,7 @@ func (s *UserTestSuite) TestUpdateUserDBSaveErr() {
|
|||
|
||||
s.assertSQLMockExpectations()
|
||||
s.Require().NotNil(err)
|
||||
s.Require().Equal("updating user: saving user: saving user mock error", err.Error())
|
||||
s.Require().Equal("error updating user: error saving user: saving user mock error", err.Error())
|
||||
}
|
||||
|
||||
func TestUserTestSuite(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -17,10 +17,10 @@ package sql
|
|||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
|
||||
|
|
@ -41,14 +41,14 @@ func (s *sqlDatabase) sqlToParamsInstance(instance Instance) (params.Instance, e
|
|||
var labels []string
|
||||
if len(instance.AditionalLabels) > 0 {
|
||||
if err := json.Unmarshal(instance.AditionalLabels, &labels); err != nil {
|
||||
return params.Instance{}, errors.Wrap(err, "unmarshalling labels")
|
||||
return params.Instance{}, fmt.Errorf("error unmarshalling labels: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
var jitConfig map[string]string
|
||||
if len(instance.JitConfiguration) > 0 {
|
||||
if err := s.unsealAndUnmarshal(instance.JitConfiguration, &jitConfig); err != nil {
|
||||
return params.Instance{}, errors.Wrap(err, "unmarshalling jit configuration")
|
||||
return params.Instance{}, fmt.Errorf("error unmarshalling jit configuration: %w", err)
|
||||
}
|
||||
}
|
||||
ret := params.Instance{
|
||||
|
|
@ -95,7 +95,7 @@ func (s *sqlDatabase) sqlToParamsInstance(instance Instance) (params.Instance, e
|
|||
if instance.Job != nil {
|
||||
paramJob, err := sqlWorkflowJobToParamsJob(*instance.Job)
|
||||
if err != nil {
|
||||
return params.Instance{}, errors.Wrap(err, "converting job")
|
||||
return params.Instance{}, fmt.Errorf("error converting job: %w", err)
|
||||
}
|
||||
ret.Job = ¶mJob
|
||||
}
|
||||
|
|
@ -132,12 +132,12 @@ func (s *sqlDatabase) sqlToCommonOrganization(org Organization, detailed bool) (
|
|||
}
|
||||
secret, err := util.Unseal(org.WebhookSecret, []byte(s.cfg.Passphrase))
|
||||
if err != nil {
|
||||
return params.Organization{}, errors.Wrap(err, "decrypting secret")
|
||||
return params.Organization{}, fmt.Errorf("error decrypting secret: %w", err)
|
||||
}
|
||||
|
||||
endpoint, err := s.sqlToCommonGithubEndpoint(org.Endpoint)
|
||||
if err != nil {
|
||||
return params.Organization{}, errors.Wrap(err, "converting endpoint")
|
||||
return params.Organization{}, fmt.Errorf("error converting endpoint: %w", err)
|
||||
}
|
||||
ret := params.Organization{
|
||||
ID: org.ID.String(),
|
||||
|
|
@ -163,7 +163,7 @@ func (s *sqlDatabase) sqlToCommonOrganization(org Organization, detailed bool) (
|
|||
}
|
||||
|
||||
if err != nil {
|
||||
return params.Organization{}, errors.Wrap(err, "converting credentials")
|
||||
return params.Organization{}, fmt.Errorf("error converting credentials: %w", err)
|
||||
}
|
||||
|
||||
if len(org.Events) > 0 {
|
||||
|
|
@ -191,7 +191,7 @@ func (s *sqlDatabase) sqlToCommonOrganization(org Organization, detailed bool) (
|
|||
for idx, pool := range org.Pools {
|
||||
ret.Pools[idx], err = s.sqlToCommonPool(pool)
|
||||
if err != nil {
|
||||
return params.Organization{}, errors.Wrap(err, "converting pool")
|
||||
return params.Organization{}, fmt.Errorf("error converting pool: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -204,12 +204,12 @@ func (s *sqlDatabase) sqlToCommonEnterprise(enterprise Enterprise, detailed bool
|
|||
}
|
||||
secret, err := util.Unseal(enterprise.WebhookSecret, []byte(s.cfg.Passphrase))
|
||||
if err != nil {
|
||||
return params.Enterprise{}, errors.Wrap(err, "decrypting secret")
|
||||
return params.Enterprise{}, fmt.Errorf("error decrypting secret: %w", err)
|
||||
}
|
||||
|
||||
endpoint, err := s.sqlToCommonGithubEndpoint(enterprise.Endpoint)
|
||||
if err != nil {
|
||||
return params.Enterprise{}, errors.Wrap(err, "converting endpoint")
|
||||
return params.Enterprise{}, fmt.Errorf("error converting endpoint: %w", err)
|
||||
}
|
||||
ret := params.Enterprise{
|
||||
ID: enterprise.ID.String(),
|
||||
|
|
@ -243,7 +243,7 @@ func (s *sqlDatabase) sqlToCommonEnterprise(enterprise Enterprise, detailed bool
|
|||
if detailed {
|
||||
creds, err := s.sqlToCommonForgeCredentials(enterprise.Credentials)
|
||||
if err != nil {
|
||||
return params.Enterprise{}, errors.Wrap(err, "converting credentials")
|
||||
return params.Enterprise{}, fmt.Errorf("error converting credentials: %w", err)
|
||||
}
|
||||
ret.Credentials = creds
|
||||
}
|
||||
|
|
@ -255,7 +255,7 @@ func (s *sqlDatabase) sqlToCommonEnterprise(enterprise Enterprise, detailed bool
|
|||
for idx, pool := range enterprise.Pools {
|
||||
ret.Pools[idx], err = s.sqlToCommonPool(pool)
|
||||
if err != nil {
|
||||
return params.Enterprise{}, errors.Wrap(err, "converting pool")
|
||||
return params.Enterprise{}, fmt.Errorf("error converting pool: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -309,7 +309,7 @@ func (s *sqlDatabase) sqlToCommonPool(pool Pool) (params.Pool, error) {
|
|||
|
||||
endpoint, err := s.sqlToCommonGithubEndpoint(ep)
|
||||
if err != nil {
|
||||
return params.Pool{}, errors.Wrap(err, "converting endpoint")
|
||||
return params.Pool{}, fmt.Errorf("error converting endpoint: %w", err)
|
||||
}
|
||||
ret.Endpoint = endpoint
|
||||
|
||||
|
|
@ -320,7 +320,7 @@ func (s *sqlDatabase) sqlToCommonPool(pool Pool) (params.Pool, error) {
|
|||
for idx, inst := range pool.Instances {
|
||||
ret.Instances[idx], err = s.sqlToParamsInstance(inst)
|
||||
if err != nil {
|
||||
return params.Pool{}, errors.Wrap(err, "converting instance")
|
||||
return params.Pool{}, fmt.Errorf("error converting instance: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -380,14 +380,14 @@ func (s *sqlDatabase) sqlToCommonScaleSet(scaleSet ScaleSet) (params.ScaleSet, e
|
|||
|
||||
endpoint, err := s.sqlToCommonGithubEndpoint(ep)
|
||||
if err != nil {
|
||||
return params.ScaleSet{}, errors.Wrap(err, "converting endpoint")
|
||||
return params.ScaleSet{}, fmt.Errorf("error converting endpoint: %w", err)
|
||||
}
|
||||
ret.Endpoint = endpoint
|
||||
|
||||
for idx, inst := range scaleSet.Instances {
|
||||
ret.Instances[idx], err = s.sqlToParamsInstance(inst)
|
||||
if err != nil {
|
||||
return params.ScaleSet{}, errors.Wrap(err, "converting instance")
|
||||
return params.ScaleSet{}, fmt.Errorf("error converting instance: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -407,11 +407,11 @@ func (s *sqlDatabase) sqlToCommonRepository(repo Repository, detailed bool) (par
|
|||
}
|
||||
secret, err := util.Unseal(repo.WebhookSecret, []byte(s.cfg.Passphrase))
|
||||
if err != nil {
|
||||
return params.Repository{}, errors.Wrap(err, "decrypting secret")
|
||||
return params.Repository{}, fmt.Errorf("error decrypting secret: %w", err)
|
||||
}
|
||||
endpoint, err := s.sqlToCommonGithubEndpoint(repo.Endpoint)
|
||||
if err != nil {
|
||||
return params.Repository{}, errors.Wrap(err, "converting endpoint")
|
||||
return params.Repository{}, fmt.Errorf("error converting endpoint: %w", err)
|
||||
}
|
||||
ret := params.Repository{
|
||||
ID: repo.ID.String(),
|
||||
|
|
@ -442,7 +442,7 @@ func (s *sqlDatabase) sqlToCommonRepository(repo Repository, detailed bool) (par
|
|||
}
|
||||
|
||||
if err != nil {
|
||||
return params.Repository{}, errors.Wrap(err, "converting credentials")
|
||||
return params.Repository{}, fmt.Errorf("error converting credentials: %w", err)
|
||||
}
|
||||
|
||||
if len(repo.Events) > 0 {
|
||||
|
|
@ -470,7 +470,7 @@ func (s *sqlDatabase) sqlToCommonRepository(repo Repository, detailed bool) (par
|
|||
for idx, pool := range repo.Pools {
|
||||
ret.Pools[idx], err = s.sqlToCommonPool(pool)
|
||||
if err != nil {
|
||||
return params.Repository{}, errors.Wrap(err, "converting pool")
|
||||
return params.Repository{}, fmt.Errorf("error converting pool: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -499,14 +499,14 @@ func (s *sqlDatabase) getOrCreateTag(tx *gorm.DB, tagName string) (Tag, error) {
|
|||
return tag, nil
|
||||
}
|
||||
if !errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return Tag{}, errors.Wrap(q.Error, "fetching tag from database")
|
||||
return Tag{}, fmt.Errorf("error fetching tag from database: %w", q.Error)
|
||||
}
|
||||
newTag := Tag{
|
||||
Name: tagName,
|
||||
}
|
||||
|
||||
if err := tx.Create(&newTag).Error; err != nil {
|
||||
return Tag{}, errors.Wrap(err, "creating tag")
|
||||
return Tag{}, fmt.Errorf("error creating tag: %w", err)
|
||||
}
|
||||
return newTag, nil
|
||||
}
|
||||
|
|
@ -561,7 +561,7 @@ func (s *sqlDatabase) updatePool(tx *gorm.DB, pool Pool, param params.UpdatePool
|
|||
}
|
||||
|
||||
if q := tx.Save(&pool); q.Error != nil {
|
||||
return params.Pool{}, errors.Wrap(q.Error, "saving database entry")
|
||||
return params.Pool{}, fmt.Errorf("error saving database entry: %w", q.Error)
|
||||
}
|
||||
|
||||
tags := []Tag{}
|
||||
|
|
@ -569,13 +569,13 @@ func (s *sqlDatabase) updatePool(tx *gorm.DB, pool Pool, param params.UpdatePool
|
|||
for _, val := range param.Tags {
|
||||
t, err := s.getOrCreateTag(tx, val)
|
||||
if err != nil {
|
||||
return params.Pool{}, errors.Wrap(err, "fetching tag")
|
||||
return params.Pool{}, fmt.Errorf("error fetching tag: %w", err)
|
||||
}
|
||||
tags = append(tags, t)
|
||||
}
|
||||
|
||||
if err := tx.Model(&pool).Association("Tags").Replace(&tags); err != nil {
|
||||
return params.Pool{}, errors.Wrap(err, "replacing tags")
|
||||
return params.Pool{}, fmt.Errorf("error replacing tags: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -585,7 +585,7 @@ func (s *sqlDatabase) updatePool(tx *gorm.DB, pool Pool, param params.UpdatePool
|
|||
func (s *sqlDatabase) getPoolByID(tx *gorm.DB, poolID string, preload ...string) (Pool, error) {
|
||||
u, err := uuid.Parse(poolID)
|
||||
if err != nil {
|
||||
return Pool{}, errors.Wrap(runnerErrors.ErrBadRequest, "parsing id")
|
||||
return Pool{}, fmt.Errorf("error parsing id: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
var pool Pool
|
||||
q := tx.Model(&Pool{})
|
||||
|
|
@ -601,7 +601,7 @@ func (s *sqlDatabase) getPoolByID(tx *gorm.DB, poolID string, preload ...string)
|
|||
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return Pool{}, runnerErrors.ErrNotFound
|
||||
}
|
||||
return Pool{}, errors.Wrap(q.Error, "fetching org from database")
|
||||
return Pool{}, fmt.Errorf("error fetching org from database: %w", q.Error)
|
||||
}
|
||||
return pool, nil
|
||||
}
|
||||
|
|
@ -621,7 +621,7 @@ func (s *sqlDatabase) getScaleSetByID(tx *gorm.DB, scaleSetID uint, preload ...s
|
|||
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return ScaleSet{}, runnerErrors.ErrNotFound
|
||||
}
|
||||
return ScaleSet{}, errors.Wrap(q.Error, "fetching scale set from database")
|
||||
return ScaleSet{}, fmt.Errorf("error fetching scale set from database: %w", q.Error)
|
||||
}
|
||||
return scaleSet, nil
|
||||
}
|
||||
|
|
@ -629,7 +629,7 @@ func (s *sqlDatabase) getScaleSetByID(tx *gorm.DB, scaleSetID uint, preload ...s
|
|||
func (s *sqlDatabase) hasGithubEntity(tx *gorm.DB, entityType params.ForgeEntityType, entityID string) error {
|
||||
u, err := uuid.Parse(entityID)
|
||||
if err != nil {
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "parsing id")
|
||||
return fmt.Errorf("error parsing id: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
var q *gorm.DB
|
||||
switch entityType {
|
||||
|
|
@ -640,15 +640,15 @@ func (s *sqlDatabase) hasGithubEntity(tx *gorm.DB, entityType params.ForgeEntity
|
|||
case params.ForgeEntityTypeEnterprise:
|
||||
q = tx.Model(&Enterprise{}).Where("id = ?", u)
|
||||
default:
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "invalid entity type")
|
||||
return fmt.Errorf("error invalid entity type: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
|
||||
var entity interface{}
|
||||
if err := q.First(entity).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.Wrap(runnerErrors.ErrNotFound, "entity not found")
|
||||
return fmt.Errorf("error entity not found: %w", runnerErrors.ErrNotFound)
|
||||
}
|
||||
return errors.Wrap(err, "fetching entity from database")
|
||||
return fmt.Errorf("error fetching entity from database: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -656,7 +656,7 @@ func (s *sqlDatabase) hasGithubEntity(tx *gorm.DB, entityType params.ForgeEntity
|
|||
func (s *sqlDatabase) marshalAndSeal(data interface{}) ([]byte, error) {
|
||||
enc, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "marshalling data")
|
||||
return nil, fmt.Errorf("error marshalling data: %w", err)
|
||||
}
|
||||
return util.Seal(enc, []byte(s.cfg.Passphrase))
|
||||
}
|
||||
|
|
@ -664,10 +664,10 @@ func (s *sqlDatabase) marshalAndSeal(data interface{}) ([]byte, error) {
|
|||
func (s *sqlDatabase) unsealAndUnmarshal(data []byte, target interface{}) error {
|
||||
decrypted, err := util.Unseal(data, []byte(s.cfg.Passphrase))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "decrypting data")
|
||||
return fmt.Errorf("error decrypting data: %w", err)
|
||||
}
|
||||
if err := json.Unmarshal(decrypted, target); err != nil {
|
||||
return errors.Wrap(err, "unmarshalling data")
|
||||
return fmt.Errorf("error unmarshalling data: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -699,15 +699,15 @@ func (s *sqlDatabase) GetForgeEntity(_ context.Context, entityType params.ForgeE
|
|||
case params.ForgeEntityTypeRepository:
|
||||
ghEntity, err = s.GetRepositoryByID(s.ctx, entityID)
|
||||
default:
|
||||
return params.ForgeEntity{}, errors.Wrap(runnerErrors.ErrBadRequest, "invalid entity type")
|
||||
return params.ForgeEntity{}, fmt.Errorf("error invalid entity type: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
if err != nil {
|
||||
return params.ForgeEntity{}, errors.Wrap(err, "failed to get ")
|
||||
return params.ForgeEntity{}, fmt.Errorf("error failed to get entity from db: %w", err)
|
||||
}
|
||||
|
||||
entity, err := ghEntity.GetEntity()
|
||||
if err != nil {
|
||||
return params.ForgeEntity{}, errors.Wrap(err, "failed to get entity")
|
||||
return params.ForgeEntity{}, fmt.Errorf("error failed to get entity: %w", err)
|
||||
}
|
||||
return entity, nil
|
||||
}
|
||||
|
|
@ -715,7 +715,7 @@ func (s *sqlDatabase) GetForgeEntity(_ context.Context, entityType params.ForgeE
|
|||
func (s *sqlDatabase) addRepositoryEvent(ctx context.Context, repoID string, event params.EventType, eventLevel params.EventLevel, statusMessage string, maxEvents int) error {
|
||||
repo, err := s.getRepoByID(ctx, s.conn, repoID)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "updating instance")
|
||||
return fmt.Errorf("error updating instance: %w", err)
|
||||
}
|
||||
|
||||
msg := RepositoryEvent{
|
||||
|
|
@ -725,7 +725,7 @@ func (s *sqlDatabase) addRepositoryEvent(ctx context.Context, repoID string, eve
|
|||
}
|
||||
|
||||
if err := s.conn.Model(&repo).Association("Events").Append(&msg); err != nil {
|
||||
return errors.Wrap(err, "adding status message")
|
||||
return fmt.Errorf("error adding status message: %w", err)
|
||||
}
|
||||
|
||||
if maxEvents > 0 {
|
||||
|
|
@ -734,12 +734,12 @@ func (s *sqlDatabase) addRepositoryEvent(ctx context.Context, repoID string, eve
|
|||
Limit(maxEvents).Order("id desc").
|
||||
Where("repo_id = ?", repo.ID).Find(&latestEvents)
|
||||
if q.Error != nil {
|
||||
return errors.Wrap(q.Error, "fetching latest events")
|
||||
return fmt.Errorf("error fetching latest events: %w", q.Error)
|
||||
}
|
||||
if len(latestEvents) == maxEvents {
|
||||
lastInList := latestEvents[len(latestEvents)-1]
|
||||
if err := s.conn.Where("repo_id = ? and id < ?", repo.ID, lastInList.ID).Unscoped().Delete(&RepositoryEvent{}).Error; err != nil {
|
||||
return errors.Wrap(err, "deleting old events")
|
||||
return fmt.Errorf("error deleting old events: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -749,7 +749,7 @@ func (s *sqlDatabase) addRepositoryEvent(ctx context.Context, repoID string, eve
|
|||
func (s *sqlDatabase) addOrgEvent(ctx context.Context, orgID string, event params.EventType, eventLevel params.EventLevel, statusMessage string, maxEvents int) error {
|
||||
org, err := s.getOrgByID(ctx, s.conn, orgID)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "updating instance")
|
||||
return fmt.Errorf("error updating instance: %w", err)
|
||||
}
|
||||
|
||||
msg := OrganizationEvent{
|
||||
|
|
@ -759,7 +759,7 @@ func (s *sqlDatabase) addOrgEvent(ctx context.Context, orgID string, event param
|
|||
}
|
||||
|
||||
if err := s.conn.Model(&org).Association("Events").Append(&msg); err != nil {
|
||||
return errors.Wrap(err, "adding status message")
|
||||
return fmt.Errorf("error adding status message: %w", err)
|
||||
}
|
||||
|
||||
if maxEvents > 0 {
|
||||
|
|
@ -768,12 +768,12 @@ func (s *sqlDatabase) addOrgEvent(ctx context.Context, orgID string, event param
|
|||
Limit(maxEvents).Order("id desc").
|
||||
Where("org_id = ?", org.ID).Find(&latestEvents)
|
||||
if q.Error != nil {
|
||||
return errors.Wrap(q.Error, "fetching latest events")
|
||||
return fmt.Errorf("error fetching latest events: %w", q.Error)
|
||||
}
|
||||
if len(latestEvents) == maxEvents {
|
||||
lastInList := latestEvents[len(latestEvents)-1]
|
||||
if err := s.conn.Where("org_id = ? and id < ?", org.ID, lastInList.ID).Unscoped().Delete(&OrganizationEvent{}).Error; err != nil {
|
||||
return errors.Wrap(err, "deleting old events")
|
||||
return fmt.Errorf("error deleting old events: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -783,7 +783,7 @@ func (s *sqlDatabase) addOrgEvent(ctx context.Context, orgID string, event param
|
|||
func (s *sqlDatabase) addEnterpriseEvent(ctx context.Context, entID string, event params.EventType, eventLevel params.EventLevel, statusMessage string, maxEvents int) error {
|
||||
ent, err := s.getEnterpriseByID(ctx, s.conn, entID)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "updating instance")
|
||||
return fmt.Errorf("error updating instance: %w", err)
|
||||
}
|
||||
|
||||
msg := EnterpriseEvent{
|
||||
|
|
@ -793,7 +793,7 @@ func (s *sqlDatabase) addEnterpriseEvent(ctx context.Context, entID string, even
|
|||
}
|
||||
|
||||
if err := s.conn.Model(&ent).Association("Events").Append(&msg); err != nil {
|
||||
return errors.Wrap(err, "adding status message")
|
||||
return fmt.Errorf("error adding status message: %w", err)
|
||||
}
|
||||
|
||||
if maxEvents > 0 {
|
||||
|
|
@ -802,12 +802,12 @@ func (s *sqlDatabase) addEnterpriseEvent(ctx context.Context, entID string, even
|
|||
Limit(maxEvents).Order("id desc").
|
||||
Where("enterprise_id = ?", ent.ID).Find(&latestEvents)
|
||||
if q.Error != nil {
|
||||
return errors.Wrap(q.Error, "fetching latest events")
|
||||
return fmt.Errorf("error fetching latest events: %w", q.Error)
|
||||
}
|
||||
if len(latestEvents) == maxEvents {
|
||||
lastInList := latestEvents[len(latestEvents)-1]
|
||||
if err := s.conn.Where("enterprise_id = ? and id < ?", ent.ID, lastInList.ID).Unscoped().Delete(&EnterpriseEvent{}).Error; err != nil {
|
||||
return errors.Wrap(err, "deleting old events")
|
||||
return fmt.Errorf("error deleting old events: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -817,7 +817,7 @@ func (s *sqlDatabase) addEnterpriseEvent(ctx context.Context, entID string, even
|
|||
|
||||
func (s *sqlDatabase) AddEntityEvent(ctx context.Context, entity params.ForgeEntity, event params.EventType, eventLevel params.EventLevel, statusMessage string, maxEvents int) error {
|
||||
if maxEvents == 0 {
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "max events cannot be 0")
|
||||
return fmt.Errorf("max events cannot be 0: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
|
||||
switch entity.EntityType {
|
||||
|
|
@ -828,7 +828,7 @@ func (s *sqlDatabase) AddEntityEvent(ctx context.Context, entity params.ForgeEnt
|
|||
case params.ForgeEntityTypeEnterprise:
|
||||
return s.addEnterpriseEvent(ctx, entity.ID, event, eventLevel, statusMessage, maxEvents)
|
||||
default:
|
||||
return errors.Wrap(runnerErrors.ErrBadRequest, "invalid entity type")
|
||||
return fmt.Errorf("invalid entity type: %w", runnerErrors.ErrBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -838,12 +838,12 @@ func (s *sqlDatabase) sqlToCommonForgeCredentials(creds GithubCredentials) (para
|
|||
}
|
||||
data, err := util.Unseal(creds.Payload, []byte(s.cfg.Passphrase))
|
||||
if err != nil {
|
||||
return params.ForgeCredentials{}, errors.Wrap(err, "unsealing credentials")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("error unsealing credentials: %w", err)
|
||||
}
|
||||
|
||||
ep, err := s.sqlToCommonGithubEndpoint(creds.Endpoint)
|
||||
if err != nil {
|
||||
return params.ForgeCredentials{}, errors.Wrap(err, "converting github endpoint")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("error converting github endpoint: %w", err)
|
||||
}
|
||||
|
||||
commonCreds := params.ForgeCredentials{
|
||||
|
|
@ -865,7 +865,7 @@ func (s *sqlDatabase) sqlToCommonForgeCredentials(creds GithubCredentials) (para
|
|||
for _, repo := range creds.Repositories {
|
||||
commonRepo, err := s.sqlToCommonRepository(repo, false)
|
||||
if err != nil {
|
||||
return params.ForgeCredentials{}, errors.Wrap(err, "converting github repository")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("error converting github repository: %w", err)
|
||||
}
|
||||
commonCreds.Repositories = append(commonCreds.Repositories, commonRepo)
|
||||
}
|
||||
|
|
@ -873,7 +873,7 @@ func (s *sqlDatabase) sqlToCommonForgeCredentials(creds GithubCredentials) (para
|
|||
for _, org := range creds.Organizations {
|
||||
commonOrg, err := s.sqlToCommonOrganization(org, false)
|
||||
if err != nil {
|
||||
return params.ForgeCredentials{}, errors.Wrap(err, "converting github organization")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("error converting github organization: %w", err)
|
||||
}
|
||||
commonCreds.Organizations = append(commonCreds.Organizations, commonOrg)
|
||||
}
|
||||
|
|
@ -881,7 +881,7 @@ func (s *sqlDatabase) sqlToCommonForgeCredentials(creds GithubCredentials) (para
|
|||
for _, ent := range creds.Enterprises {
|
||||
commonEnt, err := s.sqlToCommonEnterprise(ent, false)
|
||||
if err != nil {
|
||||
return params.ForgeCredentials{}, errors.Wrapf(err, "converting github enterprise: %s", ent.Name)
|
||||
return params.ForgeCredentials{}, fmt.Errorf("error converting github enterprise %s: %w", ent.Name, err)
|
||||
}
|
||||
commonCreds.Enterprises = append(commonCreds.Enterprises, commonEnt)
|
||||
}
|
||||
|
|
@ -895,12 +895,12 @@ func (s *sqlDatabase) sqlGiteaToCommonForgeCredentials(creds GiteaCredentials) (
|
|||
}
|
||||
data, err := util.Unseal(creds.Payload, []byte(s.cfg.Passphrase))
|
||||
if err != nil {
|
||||
return params.ForgeCredentials{}, errors.Wrap(err, "unsealing credentials")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("error unsealing credentials: %w", err)
|
||||
}
|
||||
|
||||
ep, err := s.sqlToCommonGithubEndpoint(creds.Endpoint)
|
||||
if err != nil {
|
||||
return params.ForgeCredentials{}, errors.Wrap(err, "converting github endpoint")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("error converting github endpoint: %w", err)
|
||||
}
|
||||
|
||||
commonCreds := params.ForgeCredentials{
|
||||
|
|
@ -921,7 +921,7 @@ func (s *sqlDatabase) sqlGiteaToCommonForgeCredentials(creds GiteaCredentials) (
|
|||
for _, repo := range creds.Repositories {
|
||||
commonRepo, err := s.sqlToCommonRepository(repo, false)
|
||||
if err != nil {
|
||||
return params.ForgeCredentials{}, errors.Wrap(err, "converting github repository")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("error converting github repository: %w", err)
|
||||
}
|
||||
commonCreds.Repositories = append(commonCreds.Repositories, commonRepo)
|
||||
}
|
||||
|
|
@ -929,7 +929,7 @@ func (s *sqlDatabase) sqlGiteaToCommonForgeCredentials(creds GiteaCredentials) (
|
|||
for _, org := range creds.Organizations {
|
||||
commonOrg, err := s.sqlToCommonOrganization(org, false)
|
||||
if err != nil {
|
||||
return params.ForgeCredentials{}, errors.Wrap(err, "converting github organization")
|
||||
return params.ForgeCredentials{}, fmt.Errorf("error converting github organization: %w", err)
|
||||
}
|
||||
commonCreds.Organizations = append(commonCreds.Organizations, commonOrg)
|
||||
}
|
||||
|
|
@ -954,12 +954,12 @@ func (s *sqlDatabase) sqlToCommonGithubEndpoint(ep GithubEndpoint) (params.Forge
|
|||
func getUIDFromContext(ctx context.Context) (uuid.UUID, error) {
|
||||
userID := auth.UserID(ctx)
|
||||
if userID == "" {
|
||||
return uuid.Nil, errors.Wrap(runnerErrors.ErrUnauthorized, "getting UID from context")
|
||||
return uuid.Nil, fmt.Errorf("error getting UID from context: %w", runnerErrors.ErrUnauthorized)
|
||||
}
|
||||
|
||||
asUUID, err := uuid.Parse(userID)
|
||||
if err != nil {
|
||||
return uuid.Nil, errors.Wrap(runnerErrors.ErrUnauthorized, "parsing UID from context")
|
||||
return uuid.Nil, fmt.Errorf("error parsing UID from context: %w", runnerErrors.ErrUnauthorized)
|
||||
}
|
||||
return asUUID, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue