From 020f8274f068a26187df8966c61118f0003057b7 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Fri, 18 Oct 2024 18:30:58 -0700 Subject: [PATCH] Closes #48 - added attribute path to account --- .env.test | 3 +- README.md | 1 + .../Switcher GitOps.postman_collection.json | 36 +++++++++++++++++++ src/controller/account.go | 2 +- src/core/git.go | 21 ++++++++--- src/core/git_test.go | 25 ++++++++----- src/core/handler.go | 3 +- src/core/handler_test.go | 2 +- src/model/account.go | 5 +-- src/repository/account.go | 1 + src/repository/repository_test.go | 1 + 11 files changed, 79 insertions(+), 21 deletions(-) diff --git a/.env.test b/.env.test index 8153da4..173c6c2 100644 --- a/.env.test +++ b/.env.test @@ -20,4 +20,5 @@ GIT_USER= GIT_TOKEN= GIT_TOKEN_READ_ONLY= GIT_REPO_URL=https://github.com/switcherapi/switcher-gitops-fixture -GIT_BRANCH=main \ No newline at end of file +GIT_BRANCH=main +GIT_PATH=resources \ No newline at end of file diff --git a/README.md b/README.md index 4ea01fc..52ba494 100644 --- a/README.md +++ b/README.md @@ -84,4 +84,5 @@ GIT_TOKEN=[YOUR_GIT_TOKEN] GIT_TOKEN_READ_ONLY=[YOUR_GIT_TOKEN_READ_ONLY] GIT_REPO_URL=[YOUR_GIT_REPO_URL] GIT_BRANCH=[YOUR_GIT_BRANCH] +GIT_PATH=[YOUR_GIT_PATH] ``` \ No newline at end of file diff --git a/resources/postman/Switcher GitOps.postman_collection.json b/resources/postman/Switcher GitOps.postman_collection.json index c236d70..d5a1e2d 100644 --- a/resources/postman/Switcher GitOps.postman_collection.json +++ b/resources/postman/Switcher GitOps.postman_collection.json @@ -81,6 +81,42 @@ }, "response": [] }, + { + "name": "Update (path)", + "request": { + "auth": { + "type": "bearer", + "bearer": [ + { + "key": "token", + "value": "{{gitopsToken}}", + "type": "string" + } + ] + }, + "method": "PUT", + "header": [], + "body": { + "mode": "raw", + "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}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{url}}/account", + "host": [ + "{{url}}" + ], + "path": [ + "account" + ] + } + }, + "response": [] + }, { "name": "Update (token)", "request": { diff --git a/src/controller/account.go b/src/controller/account.go index d150a6e..5b1fc70 100644 --- a/src/controller/account.go +++ b/src/controller/account.go @@ -83,7 +83,7 @@ func (controller *AccountController) CreateAccountHandler(w http.ResponseWriter, } // Initialize account handler - gitService := core.NewGitService(accountCreated.Repository, accountCreated.Token, accountCreated.Branch) + gitService := core.NewGitService(accountCreated.Repository, accountCreated.Token, accountCreated.Branch, accountCreated.Path) go controller.coreHandler.StartAccountHandler(accountCreated.ID.Hex(), gitService) opaqueTokenFromResponse(accountCreated) diff --git a/src/core/git.go b/src/core/git.go index 4be1163..dc6250c 100644 --- a/src/core/git.go +++ b/src/core/git.go @@ -18,31 +18,34 @@ import ( type IGitService interface { GetRepositoryData(environment string) (*model.RepositoryData, error) PushChanges(environment string, content string) (string, error) - UpdateRepositorySettings(repository string, encryptedToken string, branch string) + UpdateRepositorySettings(repository string, encryptedToken string, branch string, path string) } type GitService struct { repoURL string encryptedToken string branchName string + path string } -func NewGitService(repoURL string, encryptedToken string, branchName string) *GitService { +func NewGitService(repoURL string, encryptedToken string, branchName string, path string) *GitService { return &GitService{ repoURL: repoURL, encryptedToken: encryptedToken, branchName: branchName, + path: sanitzePath(path), } } -func (g *GitService) UpdateRepositorySettings(repository string, encryptedToken string, branch string) { +func (g *GitService) UpdateRepositorySettings(repository string, encryptedToken string, branch string, path string) { g.repoURL = repository g.encryptedToken = encryptedToken g.branchName = branch + g.path = sanitzePath(path) } func (g *GitService) GetRepositoryData(environment string) (*model.RepositoryData, error) { - commitHash, commitDate, content, err := g.getLastCommitData(model.FilePath + environment + ".json") + commitHash, commitDate, content, err := g.getLastCommitData(g.path + environment + ".json") if err != nil { return nil, err @@ -63,7 +66,7 @@ func (g *GitService) PushChanges(environment string, content string) (string, er r, _ := g.getRepository(fs) // Write the content to the in-memory file - filePath := model.FilePath + environment + ".json" + filePath := g.path + environment + ".json" file, _ := fs.Create(filePath) file.Write([]byte(content)) file.Close() @@ -158,3 +161,11 @@ func (g *GitService) getAuth() *http.BasicAuth { Password: decryptedToken, } } + +func sanitzePath(path string) string { + if path != "" { + path += "/" + } + + return path +} diff --git a/src/core/git_test.go b/src/core/git_test.go index a6f49cb..266f447 100644 --- a/src/core/git_test.go +++ b/src/core/git_test.go @@ -24,14 +24,16 @@ func TestNewGitService(t *testing.T) { repoURL := "repoURL" encryptedToken := "encryptedToken" branchName := "main" + path := "snapshots" // Test - gitService := NewGitService(repoURL, encryptedToken, branchName) + gitService := NewGitService(repoURL, encryptedToken, branchName, path) // Assert assert.Equal(t, repoURL, gitService.repoURL) assert.Equal(t, encryptedToken, gitService.encryptedToken) assert.Equal(t, branchName, gitService.branchName) + assert.Equal(t, path+"/", gitService.path) } func TestUpdateRepositorySettings(t *testing.T) { @@ -39,15 +41,17 @@ func TestUpdateRepositorySettings(t *testing.T) { repoURL := "repoURL" encryptedToken := "encryptedToken" branchName := "main" - gitService := NewGitService(repoURL, encryptedToken, branchName) + path := "snapshots" + gitService := NewGitService(repoURL, encryptedToken, branchName, path) // Test - gitService.UpdateRepositorySettings("newRepoURL", "newEncryptedToken", "newBranch") + gitService.UpdateRepositorySettings("newRepoURL", "newEncryptedToken", "newBranch", "newPath") // Assert assert.Equal(t, "newRepoURL", gitService.repoURL) assert.Equal(t, "newEncryptedToken", gitService.encryptedToken) assert.Equal(t, "newBranch", gitService.branchName) + assert.Equal(t, "newPath/", gitService.path) } func TestGetRepositoryData(t *testing.T) { @@ -60,7 +64,8 @@ func TestGetRepositoryData(t *testing.T) { gitService := NewGitService( appConfig.GetEnv("GIT_REPO_URL"), utils.Encrypt(appConfig.GetEnv("GIT_TOKEN"), appConfig.GetEnv("GIT_TOKEN_PRIVATE_KEY")), - appConfig.GetEnv("GIT_BRANCH")) + appConfig.GetEnv("GIT_BRANCH"), + appConfig.GetEnv("GIT_PATH")) // Test repositoryData, err := gitService.GetRepositoryData("default") @@ -77,7 +82,8 @@ func TestGetRepositoryData(t *testing.T) { gitService := NewGitService( appConfig.GetEnv("GIT_REPO_URL"), utils.Encrypt(appConfig.GetEnv("GIT_TOKEN"), appConfig.GetEnv("GIT_TOKEN_PRIVATE_KEY")), - appConfig.GetEnv("GIT_BRANCH")) + appConfig.GetEnv("GIT_BRANCH"), + appConfig.GetEnv("GIT_PATH")) // Test repositoryData, err := gitService.GetRepositoryData("invalid") @@ -92,7 +98,8 @@ func TestGetRepositoryData(t *testing.T) { gitService := NewGitService( appConfig.GetEnv("GIT_REPO_URL"), "invalid", - appConfig.GetEnv("GIT_BRANCH")) + appConfig.GetEnv("GIT_BRANCH"), + appConfig.GetEnv("GIT_PATH")) // Test repositoryData, err := gitService.GetRepositoryData("default") @@ -115,7 +122,8 @@ func TestPushChanges(t *testing.T) { gitService := NewGitService( appConfig.GetEnv("GIT_REPO_URL"), utils.Encrypt(appConfig.GetEnv("GIT_TOKEN"), appConfig.GetEnv("GIT_TOKEN_PRIVATE_KEY")), - branchName) + branchName, + appConfig.GetEnv("GIT_PATH")) // Test commitHash, err := gitService.PushChanges("default", "content") @@ -137,7 +145,8 @@ func TestPushChanges(t *testing.T) { gitService := NewGitService( appConfig.GetEnv("GIT_REPO_URL"), utils.Encrypt(appConfig.GetEnv("GIT_TOKEN_READ_ONLY"), appConfig.GetEnv("GIT_TOKEN_PRIVATE_KEY")), - appConfig.GetEnv("GIT_BRANCH")) + appConfig.GetEnv("GIT_BRANCH"), + appConfig.GetEnv("GIT_PATH")) // Test commitHash, err := gitService.PushChanges("default", "content") diff --git a/src/core/handler.go b/src/core/handler.go index 2006dde..98677eb 100644 --- a/src/core/handler.go +++ b/src/core/handler.go @@ -55,6 +55,7 @@ func (c *CoreHandler) InitCoreHandlerGoroutine() (int, error) { account.Repository, account.Token, account.Branch, + account.Path, )) } @@ -85,7 +86,7 @@ func (c *CoreHandler) StartAccountHandler(accountId string, gitService IGitServi } // Refresh account repository settings - gitService.UpdateRepositorySettings(account.Repository, account.Token, account.Branch) + gitService.UpdateRepositorySettings(account.Repository, account.Token, account.Branch, account.Path) // Fetch repository data repositoryData, err := gitService.GetRepositoryData(account.Environment) diff --git a/src/core/handler_test.go b/src/core/handler_test.go index cfa67f3..c5b36e7 100644 --- a/src/core/handler_test.go +++ b/src/core/handler_test.go @@ -575,7 +575,7 @@ func (f *FakeGitService) PushChanges(environment string, content string) (string return f.lastCommit, nil } -func (f *FakeGitService) UpdateRepositorySettings(repository string, token string, branch string) { +func (f *FakeGitService) UpdateRepositorySettings(repository string, token string, branch string, path string) { // Do nothing } diff --git a/src/model/account.go b/src/model/account.go index d5933c5..a232c0c 100644 --- a/src/model/account.go +++ b/src/model/account.go @@ -18,14 +18,11 @@ const ( MessageSynced = "Synced successfully" ) -const ( - FilePath = "resources/" -) - type Account struct { ID primitive.ObjectID `bson:"_id,omitempty"` Repository string `json:"repository"` Branch string `json:"branch"` + Path string `json:"path"` Token string `json:"token"` Environment string `json:"environment"` Domain DomainDetails `json:"domain"` diff --git a/src/repository/account.go b/src/repository/account.go index 922269f..fc126fc 100644 --- a/src/repository/account.go +++ b/src/repository/account.go @@ -171,6 +171,7 @@ func getUpdateFields(account *model.Account) bson.M { setMap["repository"] = account.Repository setMap["branch"] = account.Branch + setMap["path"] = account.Path setMap["environment"] = account.Environment setMap["domain.name"] = account.Domain.Name setMap["settings.active"] = account.Settings.Active diff --git a/src/repository/repository_test.go b/src/repository/repository_test.go index 6b39301..3027ed2 100644 --- a/src/repository/repository_test.go +++ b/src/repository/repository_test.go @@ -41,6 +41,7 @@ func givenAccount(active bool) model.Account { return model.Account{ Repository: "switcherapi/switcher-gitops", Branch: "master", + Path: "snapshots", Environment: "default", Domain: model.DomainDetails{ ID: "123-repository-test",