Skip to content

Closes #48 - added attribute path to account #52

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 19, 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
3 changes: 2 additions & 1 deletion .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
GIT_BRANCH=main
GIT_PATH=resources
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
```
36 changes: 36 additions & 0 deletions resources/postman/Switcher GitOps.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/controller/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 16 additions & 5 deletions src/core/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -158,3 +161,11 @@ func (g *GitService) getAuth() *http.BasicAuth {
Password: decryptedToken,
}
}

func sanitzePath(path string) string {
if path != "" {
path += "/"
}

return path
}
25 changes: 17 additions & 8 deletions src/core/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,34 @@ 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) {
// Given
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) {
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion src/core/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (c *CoreHandler) InitCoreHandlerGoroutine() (int, error) {
account.Repository,
account.Token,
account.Branch,
account.Path,
))
}

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/core/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
5 changes: 1 addition & 4 deletions src/model/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
1 change: 1 addition & 0 deletions src/repository/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading