Skip to content

Added git initial logic to retrieve repository data #15

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 2 commits into from
Jul 7, 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
7 changes: 6 additions & 1 deletion .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ MONGO_URI=mongodb://localhost:27017
MONGO_DB=switcher-gitops-test

SWITCHER_API_URL=https://switcherapi.com/api/gitops-graphql
SWITCHER_API_JWT_SECRET=[YOUR_JWT_SECRET]
SWITCHER_API_JWT_SECRET=[YOUR_JWT_SECRET]

# Only for testing purposes. Values are loaded from accounts
GIT_TOKEN=[GIT_TOKEN]
GIT_REPO_URL=[GIT_REPO_URL]
GIT_BRANCH=[GIT_BRANCH]
3 changes: 3 additions & 0 deletions .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ jobs:
GO_ENV: test
MONGODB_URI: mongodb://127.0.0.1:27017
MONGO_DB: switcher-gitops-test
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
GIT_REPO_URL: ${{ secrets.GIT_REPO_URL }}
GIT_BRANCH: ${{ secrets.GIT_BRANCH }}

- name: SonarCloud Scan
uses: sonarsource/sonarcloud-github-action@v2.0.2
Expand Down
31 changes: 28 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,52 @@ go 1.22
toolchain go1.22.4

require (
github.com/go-git/go-billy/v5 v5.5.0
github.com/go-git/go-git/v5 v5.12.0
github.com/joho/godotenv v1.5.1
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/cloudflare/circl v1.3.9 // indirect
github.com/cyphar/filepath-securejoin v0.2.5 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/skeema/knownhosts v1.2.2 // indirect
github.com/src-d/gcfg v1.4.0 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20240424034433-3c2c7870ae76 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/tools v0.22.0 // indirect
gopkg.in/src-d/go-billy.v4 v4.3.2 // indirect
gopkg.in/src-d/go-git.v4 v4.13.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gorilla/mux v1.8.1
github.com/pmezard/go-difflib v1.0.0 // indirect
go.mongodb.org/mongo-driver v1.15.1
go.mongodb.org/mongo-driver v1.16.0
gopkg.in/yaml.v3 v3.0.1 // indirect
)
135 changes: 135 additions & 0 deletions go.sum

Large diffs are not rendered by default.

89 changes: 83 additions & 6 deletions src/core/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ package core
import (
"time"

"github.com/go-git/go-billy/v5/memfs"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/switcherapi/switcher-gitops/src/model"
)

type IGitService interface {
GetRepositoryData() (string, string, string)
GetRepositoryData(environment string) (string, string, string, error)
CheckForChanges(account model.Account, lastCommit string, date string, content string) (status string, message string)
}

Expand All @@ -25,15 +31,86 @@ func NewGitService(repoURL string, token string, branchName string) *GitService
}
}

func (g *GitService) GetRepositoryData() (string, string, string) {
lastCommit := "123"
date := time.Now().Format(time.ANSIC)
content := "Content"
func (g *GitService) GetRepositoryData(environment string) (string, string, string, error) {
println("GetRepositoryData")
commitHash, commitDate, content, err := g.getLastCommitData(model.FilePath + environment + ".json")

return lastCommit, date, content
if err != nil {
return "", "", "", err
}

return commitHash, commitDate.Format(time.ANSIC), content, nil
}

func (g *GitService) CheckForChanges(account model.Account, lastCommit string,
date string, content string) (status string, message string) {
return model.StatusSynced, "Synced successfully"
}

func (g *GitService) getLastCommitData(filePath string) (string, time.Time, string, error) {
c, err := g.getCommitObject()

if err != nil {
return "", time.Time{}, "", err
}

// Get the tree from the commit object
tree, _ := c.Tree()

// Get the file
f, err := tree.File(filePath)

if err != nil {
return "", time.Time{}, "", err
}

// Get the content
content, _ := f.Contents()

// Return the date of the commit
return c.Hash.String(), c.Author.When, content, nil
}

func (g *GitService) getCommitObject() (*object.Commit, error) {
r, err := g.getRepository()

if err != nil {
return nil, err
}

// Get the HEAD reference
ref, _ := r.Head()

// Get the commit object from the reference
return r.CommitObject(ref.Hash())
}

func (g *GitService) getRepository() (*git.Repository, error) {
// Clone repository using in-memory storage
r, _ := git.Clone(memory.NewStorage(), memfs.New(), &git.CloneOptions{
URL: g.RepoURL,
Auth: &http.BasicAuth{
Username: "git-user",
Password: g.Token,
},
})

// Checkout branch
return g.checkoutBranch(*r)
}

func (g *GitService) checkoutBranch(r git.Repository) (*git.Repository, error) {
// Fetch worktree
w, err := r.Worktree()

if err != nil {
return nil, err
}

// Checkout remote branch
err = w.Checkout(&git.CheckoutOptions{
Branch: plumbing.NewRemoteReferenceName("origin", g.BranchName),
})

return &r, err
}
51 changes: 47 additions & 4 deletions src/core/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/switcherapi/switcher-gitops/src/config"
"github.com/switcherapi/switcher-gitops/src/model"
)

Expand All @@ -24,22 +25,64 @@ func TestNewGitService(t *testing.T) {

func TestGetRepositoryData(t *testing.T) {
// Given
gitService := NewGitService("repoURL", "token", "main")
gitService := NewGitService(
config.GetEnv("GIT_REPO_URL"),
config.GetEnv("GIT_TOKEN"),
config.GetEnv("GIT_BRANCH"))

// Test
lastCommit, date, content := gitService.GetRepositoryData()
lastCommit, date, content, err := gitService.GetRepositoryData("default")

// Assert
assert.Nil(t, err)
assert.NotEmpty(t, lastCommit)
assert.NotEmpty(t, date)
assert.NotEmpty(t, content)
}

func TestGetRepositoryDataErrorInvalidEnvironment(t *testing.T) {
// Given
gitService := NewGitService(
config.GetEnv("GIT_REPO_URL"),
config.GetEnv("GIT_TOKEN"),
config.GetEnv("GIT_BRANCH"))

// Test
lastCommit, date, content, err := gitService.GetRepositoryData("invalid")

// Assert
assert.NotNil(t, err)
assert.Empty(t, lastCommit)
assert.Empty(t, date)
assert.Empty(t, content)
}

func TestGetRepositoryDataErrorInvalidToken(t *testing.T) {
// Given
gitService := NewGitService(
config.GetEnv("GIT_REPO_URL"),
"invalid",
config.GetEnv("GIT_BRANCH"))

// Test
lastCommit, date, content, err := gitService.GetRepositoryData("default")

// Assert
assert.NotNil(t, err)
assert.Empty(t, lastCommit)
assert.Empty(t, date)
assert.Empty(t, content)
}

func TestCheckForChanges(t *testing.T) {
// Given
gitService := NewGitService("repoURL", "token", "main")
gitService := NewGitService(
config.GetEnv("GIT_REPO_URL"),
config.GetEnv("GIT_TOKEN"),
config.GetEnv("GIT_BRANCH"))

account := givenAccount()
lastCommit, date, content := gitService.GetRepositoryData()
lastCommit, date, content, _ := gitService.GetRepositoryData(account.Environment)

// Test
status, message := gitService.CheckForChanges(account, lastCommit, date, content)
Expand Down
4 changes: 2 additions & 2 deletions src/core/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ func (c *CoreHandler) StartAccountHandler(account model.Account, quit chan bool,
case <-quit:
return
default:
lastCommit, date, content := c.GitService.GetRepositoryData()
lastCommit, date, content, err := c.GitService.GetRepositoryData(account.Environment)

if isRepositoryOutSync(account, lastCommit) {
if err == nil && isRepositoryOutSync(account, lastCommit) {
c.syncUp(account, lastCommit, date, content)
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/core/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (

func TestInitCoreHandlerCoroutine(t *testing.T) {
// Given
fakeGitService := NewFakeGitService()
coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeGitService)

account := givenAccount()
coreHandler.AccountRepository.Create(&account)

Expand Down Expand Up @@ -108,8 +111,8 @@ func NewFakeGitService() *FakeGitService {
}
}

func (f *FakeGitService) GetRepositoryData() (string, string, string) {
return f.lastCommit, f.date, f.content
func (f *FakeGitService) GetRepositoryData(environment string) (string, string, string, error) {
return f.lastCommit, f.date, f.content, nil
}

func (f *FakeGitService) CheckForChanges(account model.Account, lastCommit string,
Expand Down
4 changes: 4 additions & 0 deletions src/model/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const (
StatusError = "Error"
)

const (
FilePath = "resources/"
)

type Account struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
Repository string `json:"repository"`
Expand Down