Skip to content

Commit 4325424

Browse files
authored
Closes #48 - added attribute path to account (#52)
1 parent 65893fb commit 4325424

File tree

11 files changed

+79
-21
lines changed

11 files changed

+79
-21
lines changed

.env.test

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ GIT_USER=
2020
GIT_TOKEN=
2121
GIT_TOKEN_READ_ONLY=
2222
GIT_REPO_URL=https://github.com/switcherapi/switcher-gitops-fixture
23-
GIT_BRANCH=main
23+
GIT_BRANCH=main
24+
GIT_PATH=resources

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,5 @@ GIT_TOKEN=[YOUR_GIT_TOKEN]
8484
GIT_TOKEN_READ_ONLY=[YOUR_GIT_TOKEN_READ_ONLY]
8585
GIT_REPO_URL=[YOUR_GIT_REPO_URL]
8686
GIT_BRANCH=[YOUR_GIT_BRANCH]
87+
GIT_PATH=[YOUR_GIT_PATH]
8788
```

resources/postman/Switcher GitOps.postman_collection.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,42 @@
8181
},
8282
"response": []
8383
},
84+
{
85+
"name": "Update (path)",
86+
"request": {
87+
"auth": {
88+
"type": "bearer",
89+
"bearer": [
90+
{
91+
"key": "token",
92+
"value": "{{gitopsToken}}",
93+
"type": "string"
94+
}
95+
]
96+
},
97+
"method": "PUT",
98+
"header": [],
99+
"body": {
100+
"mode": "raw",
101+
"raw": "{\r\n\t\"repository\": \"{{github_url}}\",\r\n\t\"branch\": \"{{github_branch}}\",\r\n \"path\": \"snapshots/production\",\r\n \"environment\": \"{{environment}}\",\r\n \"domain\": {\r\n\t\t\"id\": \"{{domain_id}}\",\r\n \"name\": \"GitOps\"\r\n },\r\n \"settings\": {\r\n \"active\": true,\r\n \"window\": \"30s\",\r\n \"forceprune\": true\r\n }\r\n}",
102+
"options": {
103+
"raw": {
104+
"language": "json"
105+
}
106+
}
107+
},
108+
"url": {
109+
"raw": "{{url}}/account",
110+
"host": [
111+
"{{url}}"
112+
],
113+
"path": [
114+
"account"
115+
]
116+
}
117+
},
118+
"response": []
119+
},
84120
{
85121
"name": "Update (token)",
86122
"request": {

src/controller/account.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (controller *AccountController) CreateAccountHandler(w http.ResponseWriter,
8383
}
8484

8585
// Initialize account handler
86-
gitService := core.NewGitService(accountCreated.Repository, accountCreated.Token, accountCreated.Branch)
86+
gitService := core.NewGitService(accountCreated.Repository, accountCreated.Token, accountCreated.Branch, accountCreated.Path)
8787
go controller.coreHandler.StartAccountHandler(accountCreated.ID.Hex(), gitService)
8888

8989
opaqueTokenFromResponse(accountCreated)

src/core/git.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,34 @@ import (
1818
type IGitService interface {
1919
GetRepositoryData(environment string) (*model.RepositoryData, error)
2020
PushChanges(environment string, content string) (string, error)
21-
UpdateRepositorySettings(repository string, encryptedToken string, branch string)
21+
UpdateRepositorySettings(repository string, encryptedToken string, branch string, path string)
2222
}
2323

2424
type GitService struct {
2525
repoURL string
2626
encryptedToken string
2727
branchName string
28+
path string
2829
}
2930

30-
func NewGitService(repoURL string, encryptedToken string, branchName string) *GitService {
31+
func NewGitService(repoURL string, encryptedToken string, branchName string, path string) *GitService {
3132
return &GitService{
3233
repoURL: repoURL,
3334
encryptedToken: encryptedToken,
3435
branchName: branchName,
36+
path: sanitzePath(path),
3537
}
3638
}
3739

38-
func (g *GitService) UpdateRepositorySettings(repository string, encryptedToken string, branch string) {
40+
func (g *GitService) UpdateRepositorySettings(repository string, encryptedToken string, branch string, path string) {
3941
g.repoURL = repository
4042
g.encryptedToken = encryptedToken
4143
g.branchName = branch
44+
g.path = sanitzePath(path)
4245
}
4346

4447
func (g *GitService) GetRepositoryData(environment string) (*model.RepositoryData, error) {
45-
commitHash, commitDate, content, err := g.getLastCommitData(model.FilePath + environment + ".json")
48+
commitHash, commitDate, content, err := g.getLastCommitData(g.path + environment + ".json")
4649

4750
if err != nil {
4851
return nil, err
@@ -63,7 +66,7 @@ func (g *GitService) PushChanges(environment string, content string) (string, er
6366
r, _ := g.getRepository(fs)
6467

6568
// Write the content to the in-memory file
66-
filePath := model.FilePath + environment + ".json"
69+
filePath := g.path + environment + ".json"
6770
file, _ := fs.Create(filePath)
6871
file.Write([]byte(content))
6972
file.Close()
@@ -158,3 +161,11 @@ func (g *GitService) getAuth() *http.BasicAuth {
158161
Password: decryptedToken,
159162
}
160163
}
164+
165+
func sanitzePath(path string) string {
166+
if path != "" {
167+
path += "/"
168+
}
169+
170+
return path
171+
}

src/core/git_test.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,34 @@ func TestNewGitService(t *testing.T) {
2424
repoURL := "repoURL"
2525
encryptedToken := "encryptedToken"
2626
branchName := "main"
27+
path := "snapshots"
2728

2829
// Test
29-
gitService := NewGitService(repoURL, encryptedToken, branchName)
30+
gitService := NewGitService(repoURL, encryptedToken, branchName, path)
3031

3132
// Assert
3233
assert.Equal(t, repoURL, gitService.repoURL)
3334
assert.Equal(t, encryptedToken, gitService.encryptedToken)
3435
assert.Equal(t, branchName, gitService.branchName)
36+
assert.Equal(t, path+"/", gitService.path)
3537
}
3638

3739
func TestUpdateRepositorySettings(t *testing.T) {
3840
// Given
3941
repoURL := "repoURL"
4042
encryptedToken := "encryptedToken"
4143
branchName := "main"
42-
gitService := NewGitService(repoURL, encryptedToken, branchName)
44+
path := "snapshots"
45+
gitService := NewGitService(repoURL, encryptedToken, branchName, path)
4346

4447
// Test
45-
gitService.UpdateRepositorySettings("newRepoURL", "newEncryptedToken", "newBranch")
48+
gitService.UpdateRepositorySettings("newRepoURL", "newEncryptedToken", "newBranch", "newPath")
4649

4750
// Assert
4851
assert.Equal(t, "newRepoURL", gitService.repoURL)
4952
assert.Equal(t, "newEncryptedToken", gitService.encryptedToken)
5053
assert.Equal(t, "newBranch", gitService.branchName)
54+
assert.Equal(t, "newPath/", gitService.path)
5155
}
5256

5357
func TestGetRepositoryData(t *testing.T) {
@@ -60,7 +64,8 @@ func TestGetRepositoryData(t *testing.T) {
6064
gitService := NewGitService(
6165
appConfig.GetEnv("GIT_REPO_URL"),
6266
utils.Encrypt(appConfig.GetEnv("GIT_TOKEN"), appConfig.GetEnv("GIT_TOKEN_PRIVATE_KEY")),
63-
appConfig.GetEnv("GIT_BRANCH"))
67+
appConfig.GetEnv("GIT_BRANCH"),
68+
appConfig.GetEnv("GIT_PATH"))
6469

6570
// Test
6671
repositoryData, err := gitService.GetRepositoryData("default")
@@ -77,7 +82,8 @@ func TestGetRepositoryData(t *testing.T) {
7782
gitService := NewGitService(
7883
appConfig.GetEnv("GIT_REPO_URL"),
7984
utils.Encrypt(appConfig.GetEnv("GIT_TOKEN"), appConfig.GetEnv("GIT_TOKEN_PRIVATE_KEY")),
80-
appConfig.GetEnv("GIT_BRANCH"))
85+
appConfig.GetEnv("GIT_BRANCH"),
86+
appConfig.GetEnv("GIT_PATH"))
8187

8288
// Test
8389
repositoryData, err := gitService.GetRepositoryData("invalid")
@@ -92,7 +98,8 @@ func TestGetRepositoryData(t *testing.T) {
9298
gitService := NewGitService(
9399
appConfig.GetEnv("GIT_REPO_URL"),
94100
"invalid",
95-
appConfig.GetEnv("GIT_BRANCH"))
101+
appConfig.GetEnv("GIT_BRANCH"),
102+
appConfig.GetEnv("GIT_PATH"))
96103

97104
// Test
98105
repositoryData, err := gitService.GetRepositoryData("default")
@@ -115,7 +122,8 @@ func TestPushChanges(t *testing.T) {
115122
gitService := NewGitService(
116123
appConfig.GetEnv("GIT_REPO_URL"),
117124
utils.Encrypt(appConfig.GetEnv("GIT_TOKEN"), appConfig.GetEnv("GIT_TOKEN_PRIVATE_KEY")),
118-
branchName)
125+
branchName,
126+
appConfig.GetEnv("GIT_PATH"))
119127

120128
// Test
121129
commitHash, err := gitService.PushChanges("default", "content")
@@ -137,7 +145,8 @@ func TestPushChanges(t *testing.T) {
137145
gitService := NewGitService(
138146
appConfig.GetEnv("GIT_REPO_URL"),
139147
utils.Encrypt(appConfig.GetEnv("GIT_TOKEN_READ_ONLY"), appConfig.GetEnv("GIT_TOKEN_PRIVATE_KEY")),
140-
appConfig.GetEnv("GIT_BRANCH"))
148+
appConfig.GetEnv("GIT_BRANCH"),
149+
appConfig.GetEnv("GIT_PATH"))
141150

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

src/core/handler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func (c *CoreHandler) InitCoreHandlerGoroutine() (int, error) {
5555
account.Repository,
5656
account.Token,
5757
account.Branch,
58+
account.Path,
5859
))
5960
}
6061

@@ -85,7 +86,7 @@ func (c *CoreHandler) StartAccountHandler(accountId string, gitService IGitServi
8586
}
8687

8788
// Refresh account repository settings
88-
gitService.UpdateRepositorySettings(account.Repository, account.Token, account.Branch)
89+
gitService.UpdateRepositorySettings(account.Repository, account.Token, account.Branch, account.Path)
8990

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

src/core/handler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ func (f *FakeGitService) PushChanges(environment string, content string) (string
575575
return f.lastCommit, nil
576576
}
577577

578-
func (f *FakeGitService) UpdateRepositorySettings(repository string, token string, branch string) {
578+
func (f *FakeGitService) UpdateRepositorySettings(repository string, token string, branch string, path string) {
579579
// Do nothing
580580
}
581581

src/model/account.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@ const (
1818
MessageSynced = "Synced successfully"
1919
)
2020

21-
const (
22-
FilePath = "resources/"
23-
)
24-
2521
type Account struct {
2622
ID primitive.ObjectID `bson:"_id,omitempty"`
2723
Repository string `json:"repository"`
2824
Branch string `json:"branch"`
25+
Path string `json:"path"`
2926
Token string `json:"token"`
3027
Environment string `json:"environment"`
3128
Domain DomainDetails `json:"domain"`

src/repository/account.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ func getUpdateFields(account *model.Account) bson.M {
171171

172172
setMap["repository"] = account.Repository
173173
setMap["branch"] = account.Branch
174+
setMap["path"] = account.Path
174175
setMap["environment"] = account.Environment
175176
setMap["domain.name"] = account.Domain.Name
176177
setMap["settings.active"] = account.Settings.Active

0 commit comments

Comments
 (0)