From a03ece107e90339cf3f76474f7ac8e505c748a5d Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Sun, 23 Jun 2024 15:06:39 -0700 Subject: [PATCH] Added GitService API to interface repo sync events --- src/core/core_test.go | 8 +++-- src/core/git.go | 39 ++++++++++++++++++++++++ src/core/git_test.go | 49 +++++++++++++++++++++++++++++++ src/core/handler.go | 21 ++++++++----- src/core/handler_test.go | 35 +++++++++++++++++++++- src/model/account.go | 18 ++++++++---- src/repository/repository_test.go | 5 ++-- 7 files changed, 157 insertions(+), 18 deletions(-) create mode 100644 src/core/git.go create mode 100644 src/core/git_test.go diff --git a/src/core/core_test.go b/src/core/core_test.go index 1b19b13..84218f6 100644 --- a/src/core/core_test.go +++ b/src/core/core_test.go @@ -28,7 +28,8 @@ func setup() { mongoDb = db.InitDb() accountRepository := repository.NewAccountRepositoryMongo(mongoDb) - coreHandler = NewCoreHandler(accountRepository) + gitService := NewGitService("repoURL", "token", "main") + coreHandler = NewCoreHandler(accountRepository, gitService) } func shutdown() { @@ -40,8 +41,9 @@ func shutdown() { func givenAccount() model.Account { return model.Account{ - Repository: "switcherapi/switcher-gitops", - Branch: "master", + Repository: "switcherapi/switcher-gitops", + Branch: "master", + Environment: "default", Domain: model.DomainDetails{ ID: "123", Name: "Switcher GitOps", diff --git a/src/core/git.go b/src/core/git.go new file mode 100644 index 0000000..170ed19 --- /dev/null +++ b/src/core/git.go @@ -0,0 +1,39 @@ +package core + +import ( + "time" + + "github.com/switcherapi/switcher-gitops/src/model" +) + +type IGitService interface { + GetRepositoryData() (string, string, string) + CheckForChanges(account model.Account, lastCommit string, date string, content string) (status string) +} + +type GitService struct { + RepoURL string + Token string + BranchName string +} + +func NewGitService(repoURL string, token string, branchName string) *GitService { + return &GitService{ + RepoURL: repoURL, + Token: token, + BranchName: branchName, + } +} + +func (g *GitService) GetRepositoryData() (string, string, string) { + lastCommit := "123" + date := time.Now().Format(time.ANSIC) + content := "Content" + + return lastCommit, date, content +} + +func (g *GitService) CheckForChanges(account model.Account, lastCommit string, + date string, content string) (status string) { + return model.StatusSynced +} diff --git a/src/core/git_test.go b/src/core/git_test.go new file mode 100644 index 0000000..7aab8c1 --- /dev/null +++ b/src/core/git_test.go @@ -0,0 +1,49 @@ +package core + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/switcherapi/switcher-gitops/src/model" +) + +func TestNewGitService(t *testing.T) { + // Given + repoURL := "repoURL" + token := "token" + branchName := "main" + + // Test + gitService := NewGitService(repoURL, token, branchName) + + // Assert + assert.Equal(t, repoURL, gitService.RepoURL) + assert.Equal(t, token, gitService.Token) + assert.Equal(t, branchName, gitService.BranchName) +} + +func TestGetRepositoryData(t *testing.T) { + // Given + gitService := NewGitService("repoURL", "token", "main") + + // Test + lastCommit, date, content := gitService.GetRepositoryData() + + // Assert + assert.NotEmpty(t, lastCommit) + assert.NotEmpty(t, date) + assert.NotEmpty(t, content) +} + +func TestCheckForChanges(t *testing.T) { + // Given + gitService := NewGitService("repoURL", "token", "main") + account := givenAccount() + lastCommit, date, content := gitService.GetRepositoryData() + + // Test + status := gitService.CheckForChanges(account, lastCommit, date, content) + + // Assert + assert.Equal(t, model.StatusSynced, status) +} diff --git a/src/core/handler.go b/src/core/handler.go index 6294759..52d135b 100644 --- a/src/core/handler.go +++ b/src/core/handler.go @@ -10,12 +10,14 @@ import ( type CoreHandler struct { AccountRepository repository.AccountRepository + GitService IGitService status int } -func NewCoreHandler(repo repository.AccountRepository) *CoreHandler { +func NewCoreHandler(repo repository.AccountRepository, gitService IGitService) *CoreHandler { return &CoreHandler{ AccountRepository: repo, + GitService: gitService, } } @@ -49,11 +51,10 @@ func (c *CoreHandler) StartAccountHandler(account model.Account, quit chan bool, case <-quit: return default: - lastCommit := "123" - date := time.Now().Format("2006-01-02 15:04:05") + lastCommit, date, content := c.GitService.GetRepositoryData() - if account.Domain.LastCommit == "" || account.Domain.LastCommit != lastCommit { - c.CheckForChanges(account, lastCommit, date) + if isRepositoryOutSync(account, lastCommit) { + c.syncUp(account, lastCommit, date, content) } } @@ -61,9 +62,15 @@ func (c *CoreHandler) StartAccountHandler(account model.Account, quit chan bool, } } -func (c *CoreHandler) CheckForChanges(account model.Account, lastCommit string, date string) { +func (c *CoreHandler) syncUp(account model.Account, lastCommit string, date string, content string) { + status := c.GitService.CheckForChanges(account, lastCommit, date, content) + account.Domain.LastCommit = lastCommit - account.Domain.Status = "Synced" + account.Domain.Status = status account.Domain.Message = "Synced successfully" c.AccountRepository.Update(&account) } + +func isRepositoryOutSync(account model.Account, lastCommit string) bool { + return account.Domain.LastCommit == "" || account.Domain.LastCommit != lastCommit +} diff --git a/src/core/handler_test.go b/src/core/handler_test.go index efc8336..5bafc21 100644 --- a/src/core/handler_test.go +++ b/src/core/handler_test.go @@ -7,6 +7,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/switcherapi/switcher-gitops/src/model" ) func TestInitCoreHandlerCoroutine(t *testing.T) { @@ -51,6 +52,10 @@ func TestStartAccountHandlerInactiveAccount(t *testing.T) { func TestStartAccountHandler(t *testing.T) { // Given + fakeGitService := NewFakeGitService() + fakeGitService.status = model.StatusSynced + coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeGitService) + account := givenAccount() coreHandler.AccountRepository.Create(&account) @@ -69,6 +74,7 @@ func TestStartAccountHandler(t *testing.T) { // Assert accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(account.Domain.ID) + assert.Equal(t, model.StatusSynced, accountFromDb.Domain.Status) assert.Equal(t, "Synced successfully", accountFromDb.Domain.Message) assert.Equal(t, "123", accountFromDb.Domain.LastCommit) @@ -78,6 +84,33 @@ func TestStartAccountHandler(t *testing.T) { // Helpers func tearDown() { - collection := mongoDb.Collection("accounts") + collection := mongoDb.Collection(model.CollectionName) collection.Drop(context.Background()) } + +// Fakes + +type FakeGitService struct { + lastCommit string + date string + content string + status string +} + +func NewFakeGitService() *FakeGitService { + return &FakeGitService{ + lastCommit: "123", + date: time.Now().Format(time.ANSIC), + content: "Content", + status: model.StatusOutSync, + } +} + +func (f *FakeGitService) GetRepositoryData() (string, string, string) { + return f.lastCommit, f.date, f.content +} + +func (f *FakeGitService) CheckForChanges(account model.Account, lastCommit string, + date string, content string) (status string) { + return f.status +} diff --git a/src/model/account.go b/src/model/account.go index 56e8dda..2359b10 100644 --- a/src/model/account.go +++ b/src/model/account.go @@ -6,12 +6,20 @@ const ( CollectionName = "accounts" ) +const ( + StatusCreated = "Created" + StatusSynced = "Synced" + StatusOutSync = "OutSync" + StatusError = "Error" +) + type Account struct { - ID primitive.ObjectID `bson:"_id,omitempty"` - Repository string `json:"repository"` - Branch string `json:"branch"` - Domain DomainDetails `json:"domain"` - Settings Settings `json:"settings"` + ID primitive.ObjectID `bson:"_id,omitempty"` + Repository string `json:"repository"` + Branch string `json:"branch"` + Environment string `json:"environment"` + Domain DomainDetails `json:"domain"` + Settings Settings `json:"settings"` } type DomainDetails struct { diff --git a/src/repository/repository_test.go b/src/repository/repository_test.go index f5fee84..090ffa5 100644 --- a/src/repository/repository_test.go +++ b/src/repository/repository_test.go @@ -38,8 +38,9 @@ func shutdown() { func givenAccount(active bool) model.Account { return model.Account{ - Repository: "switcherapi/switcher-gitops", - Branch: "master", + Repository: "switcherapi/switcher-gitops", + Branch: "master", + Environment: "default", Domain: model.DomainDetails{ ID: "123", Name: "Switcher GitOps",