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): reduce git fetch operations (#9410)
Browse files Browse the repository at this point in the history
* feat(resource-service): reduce git fetch operations

Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>

* added comment

Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>

* trigger build

Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>

* added more unit tests

Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>

Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
  • Loading branch information
bacherfl committed Jan 9, 2023
1 parent 5b96ef7 commit 4a2b091
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
6 changes: 1 addition & 5 deletions resource-service/common/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,15 +556,11 @@ func (g *Git) CheckoutBranch(gitContext common_models.GitContext, branch string)

func (g *Git) checkoutBranch(gitContext common_models.GitContext, options *git.CheckoutOptions) error {
if g.ProjectExists(gitContext) {
r, w, err := g.getWorkTree(gitContext)
_, w, err := g.getWorkTree(gitContext)
if err != nil {
logger.Debugf("checkoutBranch(): Could not get worktree for project '%s': %s", gitContext.Project, err.Error())
return err
}
if err = g.fetch(gitContext, r); err != nil {
logger.Debugf("checkoutBranch(): Ccould not fetch for project '%s': %s", gitContext.Project, err.Error())
return err
}

return w.Checkout(options)
}
Expand Down
8 changes: 7 additions & 1 deletion resource-service/handler/resource_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (
"github.com/keptn/keptn/resource-service/models"
)

//IResourceManager provides an interface for resource CRUD operations
// IResourceManager provides an interface for resource CRUD operations
//
//go:generate moq -pkg handler_mock -skip-ensure -out ./fake/resource_manager_mock.go . IResourceManager
type IResourceManager interface {
CreateResources(params models.CreateResourcesParams) (*models.WriteResourceResponse, error)
Expand Down Expand Up @@ -63,6 +64,11 @@ func (p ResourceManager) GetResources(params models.GetResourcesParams) (*models
if err != nil {
return nil, err
}
// since we do not automatically fetch each time when we check out a branch, we need to pull
// here to get the latest state from the upstream
if err := p.git.Pull(*gitContext); err != nil {
return nil, err
}
revision, err := p.git.GetCurrentRevision(*gitContext)
if err != nil {
return nil, err
Expand Down
62 changes: 62 additions & 0 deletions resource-service/handler/resource_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,68 @@ func TestResourceManager_GetResources(t *testing.T) {
require.Len(t, fields.fileSystem.WalkPathCalls(), 1)
}

func TestResourceManager_GetResourcesPullFailed(t *testing.T) {
fields := getTestResourceManagerFields()

fields.git.PullFunc = func(gitContext common_models.GitContext) error {
return errors.New("oops")
}

rm := NewResourceManager(fields.git, fields.credentialReader, fields.fileSystem, fields.stageContext)

result, err := rm.GetResources(models.GetResourcesParams{
ResourceContext: models.ResourceContext{
Project: models.Project{ProjectName: "my-project"},
},
GetResourcesQuery: models.GetResourcesQuery{
PageSize: 10,
},
})

require.NotNil(t, err)

require.Nil(t, result)

require.Len(t, fields.stageContext.EstablishCalls(), 1)

require.Len(t, fields.git.PullCalls(), 1)

require.Empty(t, fields.git.GetCurrentRevisionCalls())

require.Empty(t, fields.fileSystem.WalkPathCalls())
}

func TestResourceManager_GetCurrentRevisionFailed(t *testing.T) {
fields := getTestResourceManagerFields()

fields.git.GetCurrentRevisionFunc = func(gitContext common_models.GitContext) (string, error) {
return "", errors.New("oops")
}

rm := NewResourceManager(fields.git, fields.credentialReader, fields.fileSystem, fields.stageContext)

result, err := rm.GetResources(models.GetResourcesParams{
ResourceContext: models.ResourceContext{
Project: models.Project{ProjectName: "my-project"},
},
GetResourcesQuery: models.GetResourcesQuery{
PageSize: 10,
},
})

require.NotNil(t, err)

require.Nil(t, result)

require.Len(t, fields.stageContext.EstablishCalls(), 1)

require.Len(t, fields.git.PullCalls(), 1)

require.Len(t, fields.git.GetCurrentRevisionCalls(), 1)

require.Empty(t, fields.fileSystem.WalkPathCalls())
}

type fakeFileInfo struct {
name string
isDir bool
Expand Down

0 comments on commit 4a2b091

Please sign in to comment.