Skip to content

Commit

Permalink
Added GitService API to interface repo sync events (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
petruki authored Jun 23, 2024
1 parent 8014cda commit 53dc21c
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 18 deletions.
8 changes: 5 additions & 3 deletions src/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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",
Expand Down
39 changes: 39 additions & 0 deletions src/core/git.go
Original file line number Diff line number Diff line change
@@ -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
}
49 changes: 49 additions & 0 deletions src/core/git_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
21 changes: 14 additions & 7 deletions src/core/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -49,21 +51,26 @@ 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)
}
}

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

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
}
35 changes: 34 additions & 1 deletion src/core/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

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

func TestInitCoreHandlerCoroutine(t *testing.T) {
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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
}
18 changes: 13 additions & 5 deletions src/model/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions src/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 53dc21c

Please sign in to comment.