Skip to content
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
18 changes: 15 additions & 3 deletions src/core/comparator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import (
type DiffType string
type DiffResult string

type IComparatorService interface {
CheckSnapshotDiff(left model.Snapshot, right model.Snapshot, diffType DiffType) model.DiffResult
MergeResults(diffResults []model.DiffResult) model.DiffResult
NewSnapshotFromJson(jsonData []byte) model.Snapshot
}

type ComparatorService struct{}

const (
NEW DiffType = "NEW"
CHANGED DiffType = "CHANGED"
Expand All @@ -22,18 +30,22 @@ const (
COMPONENT DiffResult = "COMPONENT"
)

func NewSnapshotFromJson(jsonData []byte) model.Snapshot {
func NewComparatorService() *ComparatorService {
return &ComparatorService{}
}

func (c *ComparatorService) NewSnapshotFromJson(jsonData []byte) model.Snapshot {
var snapshot model.Snapshot
json.Unmarshal(jsonData, &snapshot)
return snapshot
}

func CheckSnapshotDiff(left model.Snapshot, right model.Snapshot, diffType DiffType) model.DiffResult {
func (c *ComparatorService) CheckSnapshotDiff(left model.Snapshot, right model.Snapshot, diffType DiffType) model.DiffResult {
diffResult := model.DiffResult{}
return checkGroupDiff(left, right, diffType, diffResult)
}

func MergeResults(diffResults []model.DiffResult) model.DiffResult {
func (c *ComparatorService) MergeResults(diffResults []model.DiffResult) model.DiffResult {
var result model.DiffResult

for _, diffResult := range diffResults {
Expand Down
195 changes: 105 additions & 90 deletions src/core/comparator_test.go

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func setup() {

accountRepository := repository.NewAccountRepositoryMongo(mongoDb)
gitService := NewGitService("repoURL", "token", "main")
coreHandler = NewCoreHandler(accountRepository, gitService)
comparatorService := NewComparatorService()
coreHandler = NewCoreHandler(accountRepository, gitService, comparatorService)
}

func shutdown() {
Expand All @@ -49,12 +50,13 @@ func givenAccount() model.Account {
Name: "Switcher GitOps",
Version: "",
LastCommit: "",
LastDate: "",
Status: "",
Message: "",
},
Settings: model.Settings{
Active: true,
Window: "10m",
Window: "5s",
ForcePrune: false,
},
}
Expand Down
6 changes: 0 additions & 6 deletions src/core/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

type IGitService interface {
GetRepositoryData(environment string) (string, string, string, error)
CheckForChanges(account model.Account, lastCommit string, date string, content string) (status string, message string)
}

type GitService struct {
Expand All @@ -41,11 +40,6 @@ func (g *GitService) GetRepositoryData(environment string) (string, string, stri
return commitHash, commitDate.Format(time.ANSIC), content, nil
}

func (g *GitService) CheckForChanges(account model.Account, lastCommit string,
date string, content string) (status string, message string) {
return model.StatusSynced, "Synced successfully"
}

func (g *GitService) getLastCommitData(filePath string) (string, time.Time, string, error) {
c, err := g.getCommitObject()

Expand Down
23 changes: 0 additions & 23 deletions src/core/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/stretchr/testify/assert"
"github.com/switcherapi/switcher-gitops/src/config"
"github.com/switcherapi/switcher-gitops/src/model"
)

const (
Expand Down Expand Up @@ -90,28 +89,6 @@ func TestGetRepositoryDataErrorInvalidToken(t *testing.T) {
assert.Empty(t, content)
}

func TestCheckForChanges(t *testing.T) {
if !canRunTests() {
t.Skip(SkipMessage)
}

// Given
gitService := NewGitService(
config.GetEnv("GIT_REPO_URL"),
config.GetEnv("GIT_TOKEN"),
config.GetEnv("GIT_BRANCH"))

account := givenAccount()
lastCommit, date, content, _ := gitService.GetRepositoryData(account.Environment)

// Test
status, message := gitService.CheckForChanges(account, lastCommit, date, content)

// Assert
assert.Equal(t, model.StatusSynced, status)
assert.Equal(t, "Synced successfully", message)
}

// Helpers

func canRunTests() bool {
Expand Down
56 changes: 49 additions & 7 deletions src/core/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import (
type CoreHandler struct {
AccountRepository repository.AccountRepository
GitService IGitService
ComparatorService IComparatorService
status int
}

func NewCoreHandler(repo repository.AccountRepository, gitService IGitService) *CoreHandler {
func NewCoreHandler(repo repository.AccountRepository, gitService IGitService, comparatorService IComparatorService) *CoreHandler {
return &CoreHandler{
AccountRepository: repo,
GitService: gitService,
ComparatorService: comparatorService,
}
}

Expand All @@ -44,7 +46,7 @@ func (c *CoreHandler) StartAccountHandler(account model.Account, quit chan bool,
}

// Reads Window setting
sleep := 1
timeWindow, unitWindow := getTimeWindow(account.Settings.Window)

for {
select {
Expand All @@ -58,19 +60,59 @@ func (c *CoreHandler) StartAccountHandler(account model.Account, quit chan bool,
}
}

time.Sleep(time.Duration(sleep) * time.Second)
time.Sleep(time.Duration(timeWindow) * unitWindow)
}
}

func (c *CoreHandler) syncUp(account model.Account, lastCommit string, date string, content string) {
status, message := c.GitService.CheckForChanges(account, lastCommit, date, content)

// Update account status: Out of sync
account.Domain.LastCommit = lastCommit
account.Domain.Status = status
account.Domain.Message = message
account.Domain.LastDate = date
account.Domain.Status = model.StatusOutSync
account.Domain.Message = "Syncing up..."
c.AccountRepository.Update(&account)

// Check for changes
diff := c.checkForChanges(content)

// Apply changes
c.applyChanges(account, diff)

// Update account status: Synced
account.Domain.Status = model.StatusSynced
account.Domain.Message = "Synced successfully"
c.AccountRepository.Update(&account)
}

func (c *CoreHandler) checkForChanges(content string) model.DiffResult {
// Get Snapshot from API

// Convert API JSON to model.Snapshot
jsonLeft := []byte(content)
left := c.ComparatorService.NewSnapshotFromJson(jsonLeft)

// Convert content to model.Snapshot
jsonRight := []byte(content)
right := c.ComparatorService.NewSnapshotFromJson(jsonRight)

// Compare Snapshots and get diff
diffNew := c.ComparatorService.CheckSnapshotDiff(left, right, NEW)
diffChanged := c.ComparatorService.CheckSnapshotDiff(left, right, CHANGED)
diffDeleted := c.ComparatorService.CheckSnapshotDiff(left, right, DELETED)

return c.ComparatorService.MergeResults([]model.DiffResult{diffNew, diffChanged, diffDeleted})
}

func (c *CoreHandler) applyChanges(account model.Account, diff model.DiffResult) {
// Apply changes
}

func isRepositoryOutSync(account model.Account, lastCommit string) bool {
return account.Domain.LastCommit == "" || account.Domain.LastCommit != lastCommit
}

func getTimeWindow(window string) (int, time.Duration) {
// Convert window string to time.Duration
duration, _ := time.ParseDuration(window)
return 1, duration
}
5 changes: 3 additions & 2 deletions src/core/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func TestInitCoreHandlerCoroutine(t *testing.T) {
// Given
fakeGitService := NewFakeGitService()
coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeGitService)
coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeGitService, NewComparatorService())

account := givenAccount()
coreHandler.AccountRepository.Create(&account)
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestStartAccountHandler(t *testing.T) {
fakeGitService := NewFakeGitService()
fakeGitService.status = model.StatusSynced
fakeGitService.message = "Synced successfully"
coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeGitService)
coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeGitService, NewComparatorService())

account := givenAccount()
coreHandler.AccountRepository.Create(&account)
Expand All @@ -81,6 +81,7 @@ func TestStartAccountHandler(t *testing.T) {
assert.Equal(t, model.StatusSynced, accountFromDb.Domain.Status)
assert.Equal(t, "Synced successfully", accountFromDb.Domain.Message)
assert.Equal(t, "123", accountFromDb.Domain.LastCommit)
assert.NotEqual(t, "", accountFromDb.Domain.LastDate)

tearDown()
}
Expand Down
1 change: 1 addition & 0 deletions src/model/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type DomainDetails struct {
Name string `json:"name"`
Version string `json:"version"`
LastCommit string `json:"lastcommit"`
LastDate string `json:"lastdate"`
Status string `json:"status"`
Message string `json:"message"`
}
Expand Down