updated commit date to time.Time and added length checking for remote URLs

This commit is contained in:
Amir Malka
2022-05-24 10:09:04 +03:00
parent 7e90956b50
commit 5443039b8c
2 changed files with 28 additions and 13 deletions
+18 -8
View File
@@ -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
}
@@ -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)
}
}