Skip to content

Commit 282e93d

Browse files
committed
Minor improvement
1 parent 2c6c73b commit 282e93d

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

artifactory/commands/buildinfo/addgit.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414

1515
"github.com/forPelevin/gomoji"
1616
"github.com/jfrog/jfrog-client-go/utils/errorutils"
17-
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
1817
"github.com/jfrog/jfrog-client-go/utils/log"
1918
"github.com/spf13/viper"
2019
)
@@ -80,15 +79,9 @@ func (config *BuildAddGitCommand) Run() error {
8079
}
8180

8281
// Find .git if it wasn't provided in the command.
83-
if config.dotGitPath == "" {
84-
var exists bool
85-
config.dotGitPath, exists, err = fileutils.FindUpstream(".git", fileutils.Any)
86-
if err != nil {
87-
return err
88-
}
89-
if !exists {
90-
return errorutils.CheckErrorf("Could not find .git")
91-
}
82+
config.dotGitPath, err = utils.GetDotGit(config.dotGitPath)
83+
if err != nil {
84+
return err
9285
}
9386

9487
// Collect URL, branch and revision into GitManager.
@@ -101,7 +94,7 @@ func (config *BuildAddGitCommand) Run() error {
10194
// Collect issues if required.
10295
var issues []buildinfo.AffectedIssue
10396
if config.configFilePath != "" {
104-
issues, err = config.collectBuildIssues(gitManager.GetUrl())
97+
issues, err = config.collectBuildIssues()
10598
if err != nil {
10699
return err
107100
}
@@ -159,7 +152,7 @@ func (config *BuildAddGitCommand) CommandName() string {
159152
return "rt_build_add_git"
160153
}
161154

162-
func (config *BuildAddGitCommand) collectBuildIssues(vcsUrl string) ([]buildinfo.AffectedIssue, error) {
155+
func (config *BuildAddGitCommand) collectBuildIssues() ([]buildinfo.AffectedIssue, error) {
163156
log.Info("Collecting build issues from VCS...")
164157

165158
// Initialize issues-configuration.
@@ -178,7 +171,7 @@ func (config *BuildAddGitCommand) collectBuildIssues(vcsUrl string) ([]buildinfo
178171
}
179172

180173
// Run issues collection.
181-
gitDetails := utils.GitParsingDetails{DotGitPath: config.dotGitPath, VcsUrl: vcsUrl, LogLimit: config.issuesConfig.LogLimit, PrettyFormat: gitParsingPrettyFormat}
174+
gitDetails := utils.GitParsingDetails{DotGitPath: config.dotGitPath, LogLimit: config.issuesConfig.LogLimit, PrettyFormat: gitParsingPrettyFormat}
182175
err = utils.ParseGitLogsFromLastBuild(config.issuesConfig.ServerDetails, config.buildConfiguration, gitDetails, logRegExp)
183176
if err != nil {
184177
return nil, err

artifactory/utils/vcs.go

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
artclientutils "github.com/jfrog/jfrog-client-go/artifactory/services/utils"
1111
clientutils "github.com/jfrog/jfrog-client-go/utils"
1212
"github.com/jfrog/jfrog-client-go/utils/errorutils"
13+
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
1314
"github.com/jfrog/jfrog-client-go/utils/log"
1415
"io"
1516
"os"
@@ -62,21 +63,33 @@ func getLatestBuildInfo(serverDetails *utilsconfig.ServerDetails, buildConfigura
6263
}
6364

6465
type GitParsingDetails struct {
65-
DotGitPath string
66-
VcsUrl string
6766
LogLimit int
6867
PrettyFormat string
68+
// Optional
69+
DotGitPath string
6970
}
7071

72+
// Parses git logs from the last build's VCS revision.
73+
// Calls git log with a custom format, and parses each line of the output with regexp. logRegExp is used to parse the log lines.
7174
func ParseGitLogsFromLastBuild(serverDetails *utilsconfig.ServerDetails, buildConfiguration *build.BuildConfiguration, gitDetails GitParsingDetails, logRegExp *gofrogcmd.CmdOutputPattern) error {
7275
// Check that git exists in path.
7376
_, err := exec.LookPath("git")
7477
if err != nil {
7578
return errorutils.CheckError(err)
7679
}
7780

81+
gitDetails.DotGitPath, err = GetDotGit(gitDetails.DotGitPath)
82+
if err != nil {
83+
return err
84+
}
85+
86+
vcsUrl, err := getVcsUrl(gitDetails.DotGitPath)
87+
if err != nil {
88+
return err
89+
}
90+
7891
// Get latest build's VCS revision from Artifactory.
79-
lastVcsRevision, err := getLatestVcsRevision(serverDetails, buildConfiguration, gitDetails.VcsUrl)
92+
lastVcsRevision, err := getLatestVcsRevision(serverDetails, buildConfiguration, vcsUrl)
8093
if err != nil {
8194
return err
8295
}
@@ -143,6 +156,30 @@ func createErrRegExpHandler(lastVcsRevision string) (*gofrogcmd.CmdOutputPattern
143156
return &errRegExp, nil
144157
}
145158

159+
// Looks for the .git directory in the current directory and its parents.
160+
func GetDotGit(providedDotGitPath string) (string, error) {
161+
if providedDotGitPath != "" {
162+
return providedDotGitPath, nil
163+
}
164+
dotGitPath, exists, err := fileutils.FindUpstream(".git", fileutils.Any)
165+
if err != nil {
166+
return "", err
167+
}
168+
if !exists {
169+
return "", errorutils.CheckErrorf("Could not find .git")
170+
}
171+
return dotGitPath, nil
172+
173+
}
174+
175+
func getVcsUrl(dotGitPath string) (string, error) {
176+
gitManager := clientutils.NewGitManager(dotGitPath)
177+
if err := gitManager.ReadConfig(); err != nil {
178+
return "", err
179+
}
180+
return gitManager.GetUrl(), nil
181+
}
182+
146183
type LogCmd struct {
147184
logLimit int
148185
lastVcsRevision string

0 commit comments

Comments
 (0)