Skip to content

Commit 9662690

Browse files
authored
Creates respository data when snapshot not found (#57)
1 parent fe4f4f7 commit 9662690

File tree

6 files changed

+185
-51
lines changed

6 files changed

+185
-51
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ test:
2222

2323
cover:
2424
go test -p 1 -coverprofile="coverage.out" ./...
25+
26+
cover-html:
2527
go tool cover -html="coverage.out"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ You might need to remove mongodb setting from docker-compose.yml if launching th
7171

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

7676
# Integrated tests
7777

src/core/git.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
type IGitService interface {
1919
GetRepositoryData(environment string) (*model.RepositoryData, error)
20-
PushChanges(environment string, content string) (string, error)
20+
PushChanges(environment string, content string, message string) (*model.RepositoryData, error)
2121
UpdateRepositorySettings(repository string, encryptedToken string, branch string, path string)
2222
}
2323

@@ -58,7 +58,7 @@ func (g *GitService) GetRepositoryData(environment string) (*model.RepositoryDat
5858
}, nil
5959
}
6060

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

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

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

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

8989
if err != nil {
90-
return "", err
90+
return nil, err
9191
}
9292

93-
return commit.String(), nil
93+
return &model.RepositoryData{
94+
CommitHash: commit.String(),
95+
CommitDate: time.Now().Format(time.ANSIC),
96+
Content: content,
97+
}, nil
9498
}
9599

96100
func (g *GitService) createCommitOptions() *git.CommitOptions {

src/core/git_test.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,42 @@ func TestPushChanges(t *testing.T) {
126126
appConfig.GetEnv("GIT_PATH"))
127127

128128
// Test
129-
commitHash, err := gitService.PushChanges("default", "content")
129+
repositoryData, err := gitService.PushChanges("default", "content", "updated default.json")
130+
data, _ := gitService.GetRepositoryData("default")
131+
deleteBranch(branchName)
130132

131133
// Assert
132134
assert.Nil(t, err)
133-
assert.NotEmpty(t, commitHash)
135+
assert.NotNil(t, repositoryData)
134136

135-
data, _ := gitService.GetRepositoryData("default")
136-
assert.Equal(t, commitHash, data.CommitHash)
137-
assert.Equal(t, "content", data.Content)
137+
assert.Equal(t, repositoryData.CommitHash, data.CommitHash)
138+
assert.Equal(t, repositoryData.CommitDate[0:10], data.CommitDate[0:10])
139+
assert.Equal(t, repositoryData.Content, data.Content)
140+
})
141+
142+
t.Run("Should push changes to repository for a newly created account", func(t *testing.T) {
143+
// Given
144+
branchName := "test-branch-" + time.Now().Format("20060102150405")
145+
createBranch(branchName)
138146

139-
// Clean up
147+
gitService := NewGitService(
148+
appConfig.GetEnv("GIT_REPO_URL"),
149+
utils.Encrypt(appConfig.GetEnv("GIT_TOKEN"), appConfig.GetEnv("GIT_TOKEN_PRIVATE_KEY")),
150+
branchName,
151+
"path/to/snapshots")
152+
153+
// Test
154+
repositoryData, err := gitService.PushChanges("default", "content", "created default.json")
155+
data, _ := gitService.GetRepositoryData("default")
140156
deleteBranch(branchName)
157+
158+
// Assert
159+
assert.Nil(t, err)
160+
assert.NotNil(t, repositoryData)
161+
162+
assert.Equal(t, repositoryData.CommitHash, data.CommitHash)
163+
assert.Equal(t, repositoryData.CommitDate[0:10], data.CommitDate[0:10])
164+
assert.Equal(t, repositoryData.Content, data.Content)
141165
})
142166

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

151175
// Test
152-
commitHash, err := gitService.PushChanges("default", "content")
176+
commitHash, err := gitService.PushChanges("default", "content", "")
153177

154178
// Assert
155179
assert.NotNil(t, err)

src/core/handler.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package core
22

33
import (
4+
"fmt"
45
"time"
56

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

9192
// Fetch repository data
92-
repositoryData, err := gitService.GetRepositoryData(account.Environment)
93+
repositoryData, err := c.getRepositoryData(gitService, account)
9394

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

129+
func (c *CoreHandler) getRepositoryData(gitService IGitService, account *model.Account) (*model.RepositoryData, error) {
130+
repositoryData, err := gitService.GetRepositoryData(account.Environment)
131+
132+
if err != nil && err.Error() == "file not found" {
133+
repositoryData, err = c.createRepositoryData(*account, gitService)
134+
}
135+
136+
return repositoryData, err
137+
}
138+
128139
func (c *CoreHandler) syncUp(account model.Account, repositoryData *model.RepositoryData, gitService IGitService) {
129140
// Update account status: Out of sync
130141
account.Domain.LastCommit = repositoryData.CommitHash
@@ -234,19 +245,32 @@ func (c *CoreHandler) pushChangesToRepository(account model.Account, snapshot mo
234245
snapshotContent.Domain.Version = 0
235246

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

239251
if err != nil {
240252
return account, err
241253
}
242254

243255
// Update domain
244256
account.Domain.Version = snapshot.Domain.Version
245-
account.Domain.LastCommit = lastCommit
257+
account.Domain.LastCommit = repositoryData.CommitHash
246258

247259
return account, nil
248260
}
249261

262+
func (c *CoreHandler) createRepositoryData(account model.Account, gitService IGitService) (*model.RepositoryData, error) {
263+
utils.LogInfo("[%s - %s (%s)] Creating repository data", account.ID.Hex(), account.Domain.Name, account.Environment)
264+
265+
snapshotJsonFromApi, err := c.apiService.FetchSnapshot(account.Domain.ID, account.Environment)
266+
267+
if err != nil {
268+
return nil, err
269+
}
270+
271+
return gitService.PushChanges(account.Environment, snapshotJsonFromApi, fmt.Sprintf("created %s.json", account.Environment))
272+
}
273+
250274
func (c *CoreHandler) isOutSync(account model.Account, lastCommit string, snapshotVersionPayload string) bool {
251275
snapshotVersion := c.apiService.NewDataFromJson([]byte(snapshotVersionPayload)).Snapshot.Domain.Version
252276

0 commit comments

Comments
 (0)