Skip to content

Creates respository data when snapshot not found #57

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
Oct 24, 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
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ test:

cover:
go test -p 1 -coverprofile="coverage.out" ./...

cover-html:
go tool cover -html="coverage.out"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ You might need to remove mongodb setting from docker-compose.yml if launching th

1. Clone the repository
2. Configure the environment variables in the `.env.test` file
3. `make run:test` to start the application
3. `make run-test` to start the application

# Integrated tests

Expand Down
14 changes: 9 additions & 5 deletions src/core/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

type IGitService interface {
GetRepositoryData(environment string) (*model.RepositoryData, error)
PushChanges(environment string, content string) (string, error)
PushChanges(environment string, content string, message string) (*model.RepositoryData, error)
UpdateRepositorySettings(repository string, encryptedToken string, branch string, path string)
}

Expand Down Expand Up @@ -58,7 +58,7 @@ func (g *GitService) GetRepositoryData(environment string) (*model.RepositoryDat
}, nil
}

func (g *GitService) PushChanges(environment string, content string) (string, error) {
func (g *GitService) PushChanges(environment string, content string, message string) (*model.RepositoryData, error) {
// Create an in-memory file system
fs := memfs.New()

Expand All @@ -78,7 +78,7 @@ func (g *GitService) PushChanges(environment string, content string) (string, er
w.Add(filePath)

// Commit the changes
commit, _ := w.Commit("[switcher-gitops] updated "+environment+".json", g.createCommitOptions())
commit, _ := w.Commit("[switcher-gitops] "+message, g.createCommitOptions())

// Push the changes
err := r.Push(&git.PushOptions{
Expand All @@ -87,10 +87,14 @@ func (g *GitService) PushChanges(environment string, content string) (string, er
})

if err != nil {
return "", err
return nil, err
}

return commit.String(), nil
return &model.RepositoryData{
CommitHash: commit.String(),
CommitDate: time.Now().Format(time.ANSIC),
Content: content,
}, nil
}

func (g *GitService) createCommitOptions() *git.CommitOptions {
Expand Down
38 changes: 31 additions & 7 deletions src/core/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,42 @@ func TestPushChanges(t *testing.T) {
appConfig.GetEnv("GIT_PATH"))

// Test
commitHash, err := gitService.PushChanges("default", "content")
repositoryData, err := gitService.PushChanges("default", "content", "updated default.json")
data, _ := gitService.GetRepositoryData("default")
deleteBranch(branchName)

// Assert
assert.Nil(t, err)
assert.NotEmpty(t, commitHash)
assert.NotNil(t, repositoryData)

data, _ := gitService.GetRepositoryData("default")
assert.Equal(t, commitHash, data.CommitHash)
assert.Equal(t, "content", data.Content)
assert.Equal(t, repositoryData.CommitHash, data.CommitHash)
assert.Equal(t, repositoryData.CommitDate[0:10], data.CommitDate[0:10])
assert.Equal(t, repositoryData.Content, data.Content)
})

t.Run("Should push changes to repository for a newly created account", func(t *testing.T) {
// Given
branchName := "test-branch-" + time.Now().Format("20060102150405")
createBranch(branchName)

// Clean up
gitService := NewGitService(
appConfig.GetEnv("GIT_REPO_URL"),
utils.Encrypt(appConfig.GetEnv("GIT_TOKEN"), appConfig.GetEnv("GIT_TOKEN_PRIVATE_KEY")),
branchName,
"path/to/snapshots")

// Test
repositoryData, err := gitService.PushChanges("default", "content", "created default.json")
data, _ := gitService.GetRepositoryData("default")
deleteBranch(branchName)

// Assert
assert.Nil(t, err)
assert.NotNil(t, repositoryData)

assert.Equal(t, repositoryData.CommitHash, data.CommitHash)
assert.Equal(t, repositoryData.CommitDate[0:10], data.CommitDate[0:10])
assert.Equal(t, repositoryData.Content, data.Content)
})

t.Run("Should fail to push changes to repository - no write access", func(t *testing.T) {
Expand All @@ -149,7 +173,7 @@ func TestPushChanges(t *testing.T) {
appConfig.GetEnv("GIT_PATH"))

// Test
commitHash, err := gitService.PushChanges("default", "content")
commitHash, err := gitService.PushChanges("default", "content", "")

// Assert
assert.NotNil(t, err)
Expand Down
30 changes: 27 additions & 3 deletions src/core/handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core

import (
"fmt"
"time"

"github.com/switcherapi/switcher-gitops/src/config"
Expand Down Expand Up @@ -89,7 +90,7 @@ func (c *CoreHandler) StartAccountHandler(accountId string, gitService IGitServi
gitService.UpdateRepositorySettings(account.Repository, account.Token, account.Branch, account.Path)

// Fetch repository data
repositoryData, err := gitService.GetRepositoryData(account.Environment)
repositoryData, err := c.getRepositoryData(gitService, account)

if err != nil {
c.updateDomainStatus(*account, model.StatusError, "Failed to fetch repository data - "+err.Error(),
Expand Down Expand Up @@ -125,6 +126,16 @@ func (c *CoreHandler) StartAccountHandler(accountId string, gitService IGitServi
}
}

func (c *CoreHandler) getRepositoryData(gitService IGitService, account *model.Account) (*model.RepositoryData, error) {
repositoryData, err := gitService.GetRepositoryData(account.Environment)

if err != nil && err.Error() == "file not found" {
repositoryData, err = c.createRepositoryData(*account, gitService)
}

return repositoryData, err
}

func (c *CoreHandler) syncUp(account model.Account, repositoryData *model.RepositoryData, gitService IGitService) {
// Update account status: Out of sync
account.Domain.LastCommit = repositoryData.CommitHash
Expand Down Expand Up @@ -234,19 +245,32 @@ func (c *CoreHandler) pushChangesToRepository(account model.Account, snapshot mo
snapshotContent.Domain.Version = 0

// Push changes to repository
lastCommit, err := gitService.PushChanges(account.Environment, utils.ToJsonFromObject(snapshotContent))
repositoryData, err := gitService.PushChanges(account.Environment, utils.ToJsonFromObject(snapshotContent),
fmt.Sprintf("updated %s.json", account.Environment))

if err != nil {
return account, err
}

// Update domain
account.Domain.Version = snapshot.Domain.Version
account.Domain.LastCommit = lastCommit
account.Domain.LastCommit = repositoryData.CommitHash

return account, nil
}

func (c *CoreHandler) createRepositoryData(account model.Account, gitService IGitService) (*model.RepositoryData, error) {
utils.LogInfo("[%s - %s (%s)] Creating repository data", account.ID.Hex(), account.Domain.Name, account.Environment)

snapshotJsonFromApi, err := c.apiService.FetchSnapshot(account.Domain.ID, account.Environment)

if err != nil {
return nil, err
}

return gitService.PushChanges(account.Environment, snapshotJsonFromApi, fmt.Sprintf("created %s.json", account.Environment))
}

func (c *CoreHandler) isOutSync(account model.Account, lastCommit string, snapshotVersionPayload string) bool {
snapshotVersion := c.apiService.NewDataFromJson([]byte(snapshotVersionPayload)).Snapshot.Domain.Version

Expand Down
Loading
Loading