feat(git): disable link generation (#1748)

Add an option to disable link creation. If there are any weird edge cases, it shouldn't block scanning
This commit is contained in:
Richard Gomez authored and GitHub committed 2025-02-10 13:15:44 -06:00
1 parent c6424a6fc3
commit ae26eff464
6 files changed
+27 -26

No files matched your search

+1 -4
View File
@@ -99,7 +99,7 @@ func runDetect(cmd *cobra.Command, args []string) {
logOpts = mustGetStringFlag(cmd, "log-opts") logOpts = mustGetStringFlag(cmd, "log-opts")
gitCmd *sources.GitCmd gitCmd *sources.GitCmd
scmPlatform scm.Platform scmPlatform scm.Platform
remote *detect.RemoteInfo remote = detect.NewRemoteInfo(scmPlatform, source)
) )
if gitCmd, err = sources.NewGitLogCmd(source, logOpts); err != nil { if gitCmd, err = sources.NewGitLogCmd(source, logOpts); err != nil {
logging.Fatal().Err(err).Msg("could not create Git cmd") logging.Fatal().Err(err).Msg("could not create Git cmd")
@@ -107,9 +107,6 @@ func runDetect(cmd *cobra.Command, args []string) {
if scmPlatform, err = scm.PlatformFromString(mustGetStringFlag(cmd, "platform")); err != nil { if scmPlatform, err = scm.PlatformFromString(mustGetStringFlag(cmd, "platform")); err != nil {
logging.Fatal().Err(err).Send() logging.Fatal().Err(err).Send()
} }
if remote, err = detect.NewRemoteInfo(scmPlatform, source); err != nil {
logging.Fatal().Err(err).Msg("failed to scan Git repository")
}
if findings, err = detector.DetectGit(gitCmd, remote); err != nil { if findings, err = detector.DetectGit(gitCmd, remote); err != nil {
// don't exit on error, just log it // don't exit on error, just log it
+1 -3
View File
@@ -74,9 +74,7 @@ func runGit(cmd *cobra.Command, args []string) {
if scmPlatform, err = scm.PlatformFromString(mustGetStringFlag(cmd, "platform")); err != nil { if scmPlatform, err = scm.PlatformFromString(mustGetStringFlag(cmd, "platform")); err != nil {
logging.Fatal().Err(err).Send() logging.Fatal().Err(err).Send()
} }
if remote, err = detect.NewRemoteInfo(scmPlatform, source); err != nil { remote = detect.NewRemoteInfo(scmPlatform, source)
logging.Fatal().Err(err).Msg("failed to scan Git repository")
}
} }
findings, err = detector.DetectGit(gitCmd, remote) findings, err = detector.DetectGit(gitCmd, remote)
+7 -3
View File
@@ -8,7 +8,8 @@ import (
type Platform int type Platform int
const ( const (
NoPlatform Platform = iota UnknownPlatform Platform = iota
NoPlatform // Explicitly disable the feature
GitHubPlatform GitHubPlatform
GitLabPlatform GitLabPlatform
// TODO: Add others. // TODO: Add others.
@@ -16,6 +17,7 @@ const (
func (p Platform) String() string { func (p Platform) String() string {
return [...]string{ return [...]string{
"unknown",
"none", "none",
"github", "github",
"gitlab", "gitlab",
@@ -24,13 +26,15 @@ func (p Platform) String() string {
func PlatformFromString(s string) (Platform, error) { func PlatformFromString(s string) (Platform, error) {
switch strings.ToLower(s) { switch strings.ToLower(s) {
case "", "none": case "", "unknown":
return UnknownPlatform, nil
case "none":
return NoPlatform, nil return NoPlatform, nil
case "github": case "github":
return GitHubPlatform, nil return GitHubPlatform, nil
case "gitlab": case "gitlab":
return GitLabPlatform, nil return GitLabPlatform, nil
default: default:
return NoPlatform, fmt.Errorf("invalid scm platform value: %s", s) return UnknownPlatform, fmt.Errorf("invalid scm platform value: %s", s)
} }
} }
+2 -5
View File
@@ -614,9 +614,7 @@ func TestFromGit(t *testing.T) {
gitCmd, err := sources.NewGitLogCmd(tt.source, tt.logOpts) gitCmd, err := sources.NewGitLogCmd(tt.source, tt.logOpts)
require.NoError(t, err) require.NoError(t, err)
remote, err := NewRemoteInfo(scm.NoPlatform, tt.source) remote := NewRemoteInfo(scm.UnknownPlatform, tt.source)
require.NoError(t, err)
findings, err := detector.DetectGit(gitCmd, remote) findings, err := detector.DetectGit(gitCmd, remote)
require.NoError(t, err) require.NoError(t, err)
@@ -688,8 +686,7 @@ func TestFromGitStaged(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
gitCmd, err := sources.NewGitDiffCmd(tt.source, true) gitCmd, err := sources.NewGitDiffCmd(tt.source, true)
require.NoError(t, err) require.NoError(t, err)
remote, err := NewRemoteInfo(scm.NoPlatform, tt.source) remote := NewRemoteInfo(scm.UnknownPlatform, tt.source)
require.NoError(t, err)
findings, err := detector.DetectGit(gitCmd, remote) findings, err := detector.DetectGit(gitCmd, remote)
require.NoError(t, err) require.NoError(t, err)
+13 -8
View File
@@ -62,7 +62,7 @@ func (d *Detector) DetectGit(cmd *sources.GitCmd, remote *RemoteInfo) ([]report.
} }
for _, finding := range d.Detect(fragment) { for _, finding := range d.Detect(fragment) {
d.addFinding(augmentGitFinding(remote.Platform, remote.Url, finding, textFragment, gitdiffFile)) d.addFinding(augmentGitFinding(remote, finding, textFragment, gitdiffFile))
} }
} }
return nil return nil
@@ -90,20 +90,25 @@ type RemoteInfo struct {
Url string Url string
} }
func NewRemoteInfo(platform scm.Platform, source string) (*RemoteInfo, error) { func NewRemoteInfo(platform scm.Platform, source string) *RemoteInfo {
if platform == scm.NoPlatform {
return &RemoteInfo{Platform: platform}
}
remoteUrl, err := getRemoteUrl(source) remoteUrl, err := getRemoteUrl(source)
if err != nil { if err != nil {
if strings.Contains(err.Error(), "No remote configured") { if strings.Contains(err.Error(), "No remote configured") {
logging.Debug().Msg("skipping finding links: repository has no configured remote.") logging.Debug().Msg("skipping finding links: repository has no configured remote.")
platform = scm.NoPlatform platform = scm.NoPlatform
goto End } else {
logging.Error().Err(err).Msg("skipping finding links: unable to parse remote URL")
} }
return nil, fmt.Errorf("unable to get remote URL: %w", err) goto End
} }
if platform == scm.NoPlatform { if platform == scm.UnknownPlatform {
platform = platformFromHost(remoteUrl) platform = platformFromHost(remoteUrl)
if platform == scm.NoPlatform { if platform == scm.UnknownPlatform {
logging.Info(). logging.Info().
Str("host", remoteUrl.Hostname()). Str("host", remoteUrl.Hostname()).
Msg("Unknown SCM platform. Use --platform to include links in findings.") Msg("Unknown SCM platform. Use --platform to include links in findings.")
@@ -123,7 +128,7 @@ End:
return &RemoteInfo{ return &RemoteInfo{
Platform: platform, Platform: platform,
Url: rUrl, Url: rUrl,
}, nil }
} }
var sshUrlpat = regexp.MustCompile(`^git@([a-zA-Z0-9.-]+):([\w/.-]+?)(?:\.git)?$`) var sshUrlpat = regexp.MustCompile(`^git@([a-zA-Z0-9.-]+):([\w/.-]+?)(?:\.git)?$`)
@@ -167,6 +172,6 @@ func platformFromHost(u *url.URL) scm.Platform {
case "gitlab.com": case "gitlab.com":
return scm.GitLabPlatform return scm.GitLabPlatform
default: default:
return scm.NoPlatform return scm.UnknownPlatform
} }
} }
+3 -3
View File
@@ -18,7 +18,7 @@ import (
// augmentGitFinding updates the start and end line numbers of a finding to include the // augmentGitFinding updates the start and end line numbers of a finding to include the
// delta from the git diff // delta from the git diff
func augmentGitFinding(scmPlatform scm.Platform, remoteUrl string, finding report.Finding, textFragment *gitdiff.TextFragment, f *gitdiff.File) report.Finding { func augmentGitFinding(remote *RemoteInfo, finding report.Finding, textFragment *gitdiff.TextFragment, f *gitdiff.File) report.Finding {
if !strings.HasPrefix(finding.Match, "file detected") { if !strings.HasPrefix(finding.Match, "file detected") {
finding.StartLine += int(textFragment.NewPosition) finding.StartLine += int(textFragment.NewPosition)
finding.EndLine += int(textFragment.NewPosition) finding.EndLine += int(textFragment.NewPosition)
@@ -34,7 +34,7 @@ func augmentGitFinding(scmPlatform scm.Platform, remoteUrl string, finding repor
finding.Message = f.PatchHeader.Message() finding.Message = f.PatchHeader.Message()
// Results from `git diff` shouldn't have a link. // Results from `git diff` shouldn't have a link.
if finding.Commit != "" { if finding.Commit != "" {
finding.Link = createScmLink(scmPlatform, remoteUrl, finding) finding.Link = createScmLink(remote.Platform, remote.Url, finding)
} }
} }
return finding return finding
@@ -46,7 +46,7 @@ var linkCleaner = strings.NewReplacer(
) )
func createScmLink(scmPlatform scm.Platform, remoteUrl string, finding report.Finding) string { func createScmLink(scmPlatform scm.Platform, remoteUrl string, finding report.Finding) string {
if scmPlatform == scm.NoPlatform { if scmPlatform == scm.UnknownPlatform || scmPlatform == scm.NoPlatform {
return "" return ""
} }