Skip to content

Commit

Permalink
add gerrit provider
Browse files Browse the repository at this point in the history
  • Loading branch information
attiasas committed Jan 30, 2025
1 parent b910bb1 commit 0cba6fc
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 24 deletions.
2 changes: 1 addition & 1 deletion commands/git/audit/gitaudit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (

sourceAudit "github.com/jfrog/jfrog-cli-security/commands/audit"
"github.com/jfrog/jfrog-cli-security/utils"
"github.com/jfrog/jfrog-cli-security/utils/scm"
"github.com/jfrog/jfrog-cli-security/utils/results"
"github.com/jfrog/jfrog-cli-security/utils/results/output"
"github.com/jfrog/jfrog-cli-security/utils/scm"
"github.com/jfrog/jfrog-cli-security/utils/xsc"
)

Expand Down
Binary file added tests/testdata/git/projects/gerrit/gerrit.zip
Binary file not shown.
4 changes: 2 additions & 2 deletions utils/scm/gitmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewGitManager(localRepositoryWorkingDir string) (gm *GitManager, err error)
return
}

func GitCliExists() (bool) {
func GitCliExists() bool {
return true
}

Expand Down Expand Up @@ -154,10 +154,10 @@ func getRemoteUrl(remote *goGit.Remote) (remoteUrl string, err error) {

// Normalize the URL by removing protocol prefix and any trailing ".git"
func normalizeGitUrl(url string) string {
// jfrog-ignore - false positive, not used for communication
url = strings.TrimPrefix(url, "http://")
url = strings.TrimPrefix(url, "https://")
url = strings.TrimPrefix(url, "ssh://")
url = strings.TrimPrefix(url, "svn://")
return strings.TrimSuffix(url, ".git")
}

Expand Down
35 changes: 35 additions & 0 deletions utils/scm/gitmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,31 @@ func TestGetGitContext(t *testing.T) {
LastCommitAuthor: "attiasas",
},
},
{
name: "Gerrit Project (no owner)",
testProjectZipDirPath: filepath.Join(basePath, "gerrit"),
gitInfo: &services.XscGitInfoContext{
GitRepoHttpsCloneUrl: "https://gerrit.googlesource.com/git-repo",
GitRepoName: "git-repo",
GitProject: "git-repo",
GitProvider: "gerrit",
BranchName: "main",
LastCommitHash: "1711bc23c0ab6ff4a51bf948c703c81073dd3475",
LastCommitMessage: `git_config: prefer XDG config location
Currently, repo ignores the XDG path for the git config file, and
creates a new one in the user's home directory. This commit changes the
behavior to prefer the XDG path if it exists, which matches git behavior
and avoids littering the home directory.
Bug: 40012443
Change-Id: Icd3ec6db6b0832f47417bbe98ff9461306b51297
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/448385
Tested-by: lmaor xenix <[email protected]>
Reviewed-by: Mike Frysinger <[email protected]>`,
LastCommitAuthor: "flexagoon",
},
},
{
name: "Forked Project (multiple remotes)",
testProjectZipDirPath: filepath.Join(basePath, "forked"),
Expand Down Expand Up @@ -161,6 +186,11 @@ func TestGetGitProvider(t *testing.T) {
url: "https://dev.azure.com/attiasas/test-security-git/_git/test-security-git",
provider: Azure,
},
{
name: "Gerrit",
url: "https://gerrit.googlesource.com/git-repo",
provider: Gerrit,
},
{
name: "Unknown",
url: "ssh://[email protected]/assafa/test-security-git.git",
Expand Down Expand Up @@ -202,6 +232,11 @@ func TestGetGitProject(t *testing.T) {
url: "ssh://[email protected]/~assafa/test-security-git.git",
project: "~assafa",
},
{
name: "Gerrit - No project name",
url: "https://gerrit.googlesource.com/git-repo",
project: "git-repo",
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
Expand Down
35 changes: 14 additions & 21 deletions utils/scm/scmutils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scm

import (
"errors"
"fmt"
"path"

Expand All @@ -9,13 +10,11 @@ import (
)

const (
Github ScmProvider = "github"
Gitlab ScmProvider = "gitlab"
// git clone https://git.id.info/scm/repo-name/repo-name.git
Github ScmProvider = "github"
Gitlab ScmProvider = "gitlab"
Bitbucket ScmProvider = "bitbucket"
Azure ScmProvider = "azure"
// git clone https://gerrit.googlesource.com/git-repo
Gerrit ScmProvider = "gerrit"
Gerrit ScmProvider = "gerrit"

// TODO: Add support for other git providers

Expand All @@ -30,6 +29,7 @@ const (
Unknown ScmProvider = ""
)

// ScmProvider is the type of source control provider
type ScmProvider string

func (sp ScmProvider) String() string {
Expand All @@ -38,7 +38,6 @@ func (sp ScmProvider) String() string {

const (
Git ScmType = "git"
// Svn ScmType = "svn" // subversion
)

type ScmType string
Expand All @@ -49,34 +48,28 @@ func (st ScmType) String() string {

type ScmTypeData struct {
indicator string
ScmApplicable func() bool
}

var scmTypeData = map[ScmType]ScmTypeData{
Git: {".git", func() bool { return true }},
}
var scmTypeData = map[ScmType]ScmTypeData{Git: {".git"}}

type ScmManager interface {
GetGitContext() (gitInfo *services.XscGitInfoContext, err error)
}

func DetectScmInProject(projectPath string) (manager ScmManager, err error) {
for scmType, scmData := range scmTypeData {
if exists, err := isScmProject(projectPath, scmData); err != nil {
return nil, err
} else if exists {
if scmType == Git {
return NewGitManager(projectPath)
}
if exists, e := isScmProject(projectPath, scmData); !exists || err != nil {
err = errors.Join(e, err)
continue
}
if scmType == Git {
return NewGitManager(projectPath)
}
}
err = fmt.Errorf("failed to detect source control manager in project path: %s", projectPath)
err = errors.Join(err, fmt.Errorf("failed to detect source control manager in project path: %s", projectPath))
return
}

func isScmProject(projectPath string, scmData ScmTypeData) (bool, error) {
if cliExists := scmData.ScmApplicable(); !cliExists {
return false, nil
}
return fileutils.IsDirExists(path.Join(projectPath, scmData.indicator), false)
}
}

0 comments on commit 0cba6fc

Please sign in to comment.