Skip to content

Added error handler for applyChangesToRepository #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/core/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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 {
Expand Down
58 changes: 48 additions & 10 deletions src/core/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"context"
"encoding/json"
"errors"
"testing"
"time"

Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

Expand All @@ -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": {
Expand Down