Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
feat(resource-service): Improve git implementation and testing (#6346) (
Browse files Browse the repository at this point in the history
#6529)

* added keptn resource service error

Signed-off-by: RealAnna <[email protected]>

* added component test

Signed-off-by: RealAnna <[email protected]>

* added  configure git user test

Signed-off-by: RealAnna <[email protected]>

* fix:  init function

Signed-off-by: RealAnna <[email protected]>

* fix:  init function

Signed-off-by: RealAnna <[email protected]>

* fix: put back tmp repo

Signed-off-by: RealAnna <[email protected]>

* fix: moved ensure directory exists to utils.go

Signed-off-by: RealAnna <[email protected]>

* fix: one test

Signed-off-by: RealAnna <[email protected]>

* fix: testing ubuntu

Signed-off-by: RealAnna <[email protected]>

* fix: testing ubuntu

Signed-off-by: RealAnna <[email protected]>

* fix: testing ubuntu

Signed-off-by: RealAnna <[email protected]>
  • Loading branch information
RealAnna authored Jan 11, 2022
1 parent f21ae9c commit 91c5417
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 97 deletions.
2 changes: 2 additions & 0 deletions resource-service/common/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const testTgzContent = "H4sIAOu80mEAA+2Vz0rDQBCHc84zeNgnSGf2b/ZQsFjFkxTRg+BltasG

func TestFileSystem_WriteAndReadFile(t *testing.T) {
// create a tmp directory in test/tmp
err := ensureDirectoryExists("../test/tmp")
require.Nil(t, err)
dir, err := ioutil.TempDir("../test/tmp/", "project-")
require.Nil(t, err)
defer func(name string) {
Expand Down
45 changes: 32 additions & 13 deletions resource-service/common/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (g Git) CloneRepo(gitContext common_models.GitContext) (bool, error) {
)

if err != nil {
if strings.Contains(err.Error(), "empty") {
if kerrors.ErrEmptyRemoteRepository.Is(err) {
clone, err = g.init(gitContext, projectPath)
if err != nil {
return false, fmt.Errorf(kerrors.ErrMsgCouldNotGitAction, "init", gitContext.Project, err)
Expand Down Expand Up @@ -132,14 +132,34 @@ func (g Git) init(gitContext common_models.GitContext, projectPath string) (*git
if err != nil {
return nil, err
}
f, err := os.Create(projectPath + "/metadata.yaml")
if err != nil {
return nil, err
}
_, err = f.Write([]byte{})
if err != nil {
return nil, err
}
err = f.Close()
if err != nil {
return nil, err
}

os.MkdirAll(projectPath+"/.git", 0700)
w, err := init.Worktree()
if err != nil {
return nil, err
}
w.Add("/.git")
_, err = g.commitAll(gitContext, "init repo")

w.Add(projectPath + "/metadata.yaml")
_, err = w.Commit("init git empty repo",
&git.CommitOptions{
All: true,
Author: &object.Signature{
Name: gitKeptnUserDefault,
Email: gitKeptnEmailDefault,
When: time.Now(),
},
})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -172,7 +192,7 @@ func (g Git) commitAll(gitContext common_models.GitContext, message string) (str

func (g Git) StageAndCommitAll(gitContext common_models.GitContext, message string) (string, error) {

_, err := g.commitAll(gitContext, message)
id, err := g.commitAll(gitContext, message)
if err != nil {
return "", fmt.Errorf(kerrors.ErrMsgCouldNotCommit, gitContext.Project, err)
}
Expand Down Expand Up @@ -204,6 +224,7 @@ func (g Git) StageAndCommitAll(gitContext common_models.GitContext, message stri
if !updated {
return "", fmt.Errorf(kerrors.ErrMsgCouldNotCommit, gitContext.Project, kerrors.ErrForceNeeded)
}

return id, nil
}

Expand Down Expand Up @@ -427,6 +448,12 @@ func (g *Git) GetFileRevision(gitContext common_models.GitContext, revision stri

var re (io.Reader)
re, err = blob.Reader()

if err != nil {
return []byte{},
fmt.Errorf(kerrors.ErrMsgCouldNotGitAction, "retrieve revision in ", gitContext.Project, err)
}

return ioutil.ReadAll(re)
}

Expand Down Expand Up @@ -535,11 +562,3 @@ func resolve(obj object.Object, path string) (*object.Blob, error) {
return nil, object.ErrUnsupportedObject
}
}

func ensureDirectoryExists(path string) error {
if _, err := os.Stat(path); err != nil {
err := os.MkdirAll(path, 0700)
return err
}
return nil
}
Loading

0 comments on commit 91c5417

Please sign in to comment.