From 68991ae80d905ada7e9e55844ecedef4ddcead47 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Sun, 25 Aug 2024 14:30:40 -0700 Subject: [PATCH] Added error handler for applyChangesToRepository --- src/core/handler.go | 19 +++++++++---- src/core/handler_test.go | 58 +++++++++++++++++++++++++++++++++------- 2 files changed, 62 insertions(+), 15 deletions(-) diff --git a/src/core/handler.go b/src/core/handler.go index d89b376..65cb4c1 100644 --- a/src/core/handler.go +++ b/src/core/handler.go @@ -68,16 +68,25 @@ func (c *CoreHandler) syncUp(account model.Account, repositoryData *model.Reposi diff, snapshotApi, err := c.checkForChanges(account.Domain.ID, account.Environment, repositoryData.Content) if err != nil { - c.updateDomainStatus(account, model.StatusError, "Failed to check for changes") + c.updateDomainStatus(account, model.StatusError, "Failed to check for changes - "+err.Error()) return } + // Apply changes + changeSource := "" if snapshotApi.Domain.Version > account.Domain.Version { - account = c.applyChangesToRepository(account, snapshotApi) + changeSource = "Repository" + account, err = c.applyChangesToRepository(account, snapshotApi) } else if len(diff.Changes) > 0 { + changeSource = "API" account = c.applyChangesToAPI(account, repositoryData) } + if err != nil { + c.updateDomainStatus(account, model.StatusError, "Failed to apply changes ["+changeSource+"] - "+err.Error()) + return + } + // Update account status: Synced c.updateDomainStatus(account, model.StatusSynced, "Synced successfully") } @@ -117,18 +126,18 @@ func (c *CoreHandler) applyChangesToAPI(account model.Account, repositoryData *m return account } -func (c *CoreHandler) applyChangesToRepository(account model.Account, snapshot model.Snapshot) model.Account { +func (c *CoreHandler) applyChangesToRepository(account model.Account, snapshot model.Snapshot) (model.Account, error) { // Remove version from domain snapshotContent := snapshot snapshotContent.Domain.Version = "" - lastCommit, _ := c.GitService.PushChanges(account.Environment, utils.ToJsonFromObject(snapshotContent)) + lastCommit, err := c.GitService.PushChanges(account.Environment, utils.ToJsonFromObject(snapshotContent)) // Update domain account.Domain.Version = snapshot.Domain.Version account.Domain.LastCommit = lastCommit - return account + return account, err } func isRepositoryOutSync(account model.Account, lastCommit string) bool { diff --git a/src/core/handler_test.go b/src/core/handler_test.go index cb19b18..bb1ac8e 100644 --- a/src/core/handler_test.go +++ b/src/core/handler_test.go @@ -3,6 +3,7 @@ package core import ( "context" "encoding/json" + "errors" "testing" "time" @@ -13,7 +14,7 @@ import ( func TestInitCoreHandlerCoroutine(t *testing.T) { // Given fakeGitService := NewFakeGitService() - fakeApiService := NewFakeApiService(false) + fakeApiService := NewFakeApiService() coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeGitService, fakeApiService, NewComparatorService()) account := givenAccount() @@ -58,7 +59,7 @@ func TestStartAccountHandler(t *testing.T) { t.Run("Should sync successfully when repository is out of sync", func(t *testing.T) { // Given fakeGitService := NewFakeGitService() - fakeApiService := NewFakeApiService(false) + fakeApiService := NewFakeApiService() coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeGitService, fakeApiService, NewComparatorService()) account := givenAccount() @@ -89,7 +90,7 @@ func TestStartAccountHandler(t *testing.T) { fakeGitService := NewFakeGitService() fakeGitService.lastCommit = "111" - fakeApiService := NewFakeApiService(false) + fakeApiService := NewFakeApiService() coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeGitService, fakeApiService, NewComparatorService()) account := givenAccount() @@ -118,7 +119,9 @@ func TestStartAccountHandler(t *testing.T) { t.Run("Should not sync when API returns an error", func(t *testing.T) { // Given fakeGitService := NewFakeGitService() - fakeApiService := NewFakeApiService(true) + fakeApiService := NewFakeApiService() + fakeApiService.throwError = true + coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeGitService, fakeApiService, NewComparatorService()) account := givenAccount() @@ -141,6 +144,36 @@ func TestStartAccountHandler(t *testing.T) { tearDown() }) + + t.Run("Should not sync when git token has no permission to push changes", func(t *testing.T) { + // Given + fakeGitService := NewFakeGitService() + fakeGitService.errorPushChanges = "authorization failed" + fakeApiService := NewFakeApiService() + + coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeGitService, fakeApiService, NewComparatorService()) + + account := givenAccount() + coreHandler.AccountRepository.Create(&account) + + // Test + go coreHandler.StartAccountHandler(account) + + // Terminate the goroutine + account.Settings.Active = false + coreHandler.AccountRepository.Update(&account) + time.Sleep(1 * time.Second) + + // Assert + accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(account.Domain.ID) + assert.Equal(t, model.StatusError, accountFromDb.Domain.Status) + assert.Contains(t, accountFromDb.Domain.Message, "authorization failed") + assert.Contains(t, accountFromDb.Domain.Message, "Failed to apply changes [Repository]") + assert.Equal(t, "", accountFromDb.Domain.LastCommit) + assert.NotEqual(t, "", accountFromDb.Domain.LastDate) + + tearDown() + }) } // Helpers @@ -153,10 +186,11 @@ func tearDown() { // Fakes type FakeGitService struct { - lastCommit string - date string - content string - status string + lastCommit string + date string + content string + status string + errorPushChanges string } func NewFakeGitService() *FakeGitService { @@ -194,6 +228,10 @@ func (f *FakeGitService) GetRepositoryData(environment string) (*model.Repositor } func (f *FakeGitService) PushChanges(environment string, content string) (string, error) { + if f.errorPushChanges != "" { + return "", errors.New(f.errorPushChanges) + } + return f.lastCommit, nil } @@ -202,9 +240,9 @@ type FakeApiService struct { response string } -func NewFakeApiService(throwError bool) *FakeApiService { +func NewFakeApiService() *FakeApiService { return &FakeApiService{ - throwError: throwError, + throwError: false, response: `{ "data": { "domain": {