From 5443039b8c47d62cdeeaf54633fec42e554dc6f0 Mon Sep 17 00:00:00 2001 From: Amir Malka Date: Tue, 24 May 2022 10:09:04 +0300 Subject: [PATCH] updated commit date to time.Time and added length checking for remote URLs --- .../pkg/resourcehandler/localgitrepository.go | 26 +++++++++++++------ .../localgitrepository_test.go | 15 +++++++---- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/core/pkg/resourcehandler/localgitrepository.go b/core/pkg/resourcehandler/localgitrepository.go index 3a2d33bd..b301933a 100644 --- a/core/pkg/resourcehandler/localgitrepository.go +++ b/core/pkg/resourcehandler/localgitrepository.go @@ -4,6 +4,7 @@ import ( "fmt" "path" "strings" + "time" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/config" @@ -21,7 +22,7 @@ type GitCommit struct { authorName string authorEmail string message string - date string + date time.Time } func NewLocalGitRepository(path string) (*LocalGitRepository, error) { @@ -55,22 +56,31 @@ func (g *LocalGitRepository) GetBranchName() string { return g.head.Name().Short() } -func (g *LocalGitRepository) GetOriginUrl() string { +func (g *LocalGitRepository) GetOriginUrl() (string, error) { branchName := g.GetBranchName() if branchRef, branchFound := g.config.Branches[branchName]; branchFound { remoteName := branchRef.Remote - return g.config.Remotes[remoteName].URLs[0] + if len(g.config.Remotes[remoteName].URLs) == 0 { + return "", fmt.Errorf("expected to find URLs for remote '%s', branch '%s'", remoteName, branchName) + } + return g.config.Remotes[remoteName].URLs[0], nil } const defaultRemoteName string = "origin" - return g.config.Remotes[defaultRemoteName].URLs[0] + if len(g.config.Remotes[defaultRemoteName].URLs) == 0 { + return "", fmt.Errorf("expected to find URLs for remote '%s'", defaultRemoteName) + } + return g.config.Remotes[defaultRemoteName].URLs[0], nil } -func (g *LocalGitRepository) GetName() string { - originUrl := g.GetOriginUrl() +func (g *LocalGitRepository) GetName() (string, error) { + originUrl, err := g.GetOriginUrl() + if err != nil { + return "", err + } baseName := path.Base(originUrl) // remove .git - return strings.TrimSuffix(baseName, ".git") + return strings.TrimSuffix(baseName, ".git"), nil } func (g *LocalGitRepository) GetLastCommit() (*GitCommit, error) { @@ -102,6 +112,6 @@ func (g *LocalGitRepository) GetFileLastCommit(filePath string) (*GitCommit, err hash: commit.Hash.String(), authorName: commit.Author.Name, authorEmail: commit.Author.Email, - date: commit.Author.When.String(), + date: commit.Author.When, }, nil } diff --git a/core/pkg/resourcehandler/localgitrepository_test.go b/core/pkg/resourcehandler/localgitrepository_test.go index a41ef38b..d6a49551 100644 --- a/core/pkg/resourcehandler/localgitrepository_test.go +++ b/core/pkg/resourcehandler/localgitrepository_test.go @@ -101,13 +101,18 @@ func (s *LocalGitRepositoryTestSuite) TestGetBranchName() { func (s *LocalGitRepositoryTestSuite) TestGetName() { if localRepo, err := NewLocalGitRepository(s.gitRepositoryPath); s.NoError(err) { - s.Equal("localrepo", localRepo.GetName()) + if name, err := localRepo.GetName(); s.NoError(err) { + s.Equal("localrepo", name) + } + } } func (s *LocalGitRepositoryTestSuite) TestGetOriginUrl() { if localRepo, err := NewLocalGitRepository(s.gitRepositoryPath); s.NoError(err) { - s.Equal("git@github.com:testuser/localrepo", localRepo.GetOriginUrl()) + if url, err := localRepo.GetOriginUrl(); s.NoError(err) { + s.Equal("git@github.com:testuser/localrepo", url) + } } } @@ -117,7 +122,7 @@ func (s *LocalGitRepositoryTestSuite) TestGetLastCommit() { s.Equal("7e09312b8017695fadcd606882e3779f10a5c832", commit.hash) s.Equal("Amir Malka", commit.authorName) s.Equal("amirm@armosec.io", commit.authorEmail) - s.Equal("2022-05-22 19:11:57 +0300 +0300", commit.date) + s.Equal("2022-05-22 19:11:57 +0300 +0300", commit.date.String()) s.Equal("added file B\n", commit.message) } } @@ -130,7 +135,7 @@ func (s *LocalGitRepositoryTestSuite) TestGetFileLastCommit() { s.Equal("9fae4be19624297947d2b605cefbff516628612d", commit.hash) s.Equal("Amir Malka", commit.authorName) s.Equal("amirm@armosec.io", commit.authorEmail) - s.Equal("2022-05-22 18:55:48 +0300 +0300", commit.date) + s.Equal("2022-05-22 18:55:48 +0300 +0300", commit.date.String()) s.Equal("added file A\n", commit.message) } } @@ -142,7 +147,7 @@ func (s *LocalGitRepositoryTestSuite) TestGetFileLastCommit() { s.Equal("7e09312b8017695fadcd606882e3779f10a5c832", commit.hash) s.Equal("Amir Malka", commit.authorName) s.Equal("amirm@armosec.io", commit.authorEmail) - s.Equal("2022-05-22 19:11:57 +0300 +0300", commit.date) + s.Equal("2022-05-22 19:11:57 +0300 +0300", commit.date.String()) s.Equal("added file B\n", commit.message) } }