Răsfoiți Sursa

Add Platform Gitea (#1884)

Tilo Brueckner 8 luni în urmă
părinte
comite
722ce8213a
4 a modificat fișierele cu 63 adăugiri și 0 ștergeri
  1. 4 0
      cmd/scm/scm.go
  2. 16 0
      detect/utils.go
  3. 41 0
      detect/utils_test.go
  4. 2 0
      sources/git.go

+ 4 - 0
cmd/scm/scm.go

@@ -13,6 +13,7 @@ const (
 	GitHubPlatform
 	GitLabPlatform
 	AzureDevOpsPlatform
+	GiteaPlatform
 	// TODO: Add others.
 )
 
@@ -23,6 +24,7 @@ func (p Platform) String() string {
 		"github",
 		"gitlab",
 		"azuredevops",
+		"gitea",
 	}[p]
 }
 
@@ -38,6 +40,8 @@ func PlatformFromString(s string) (Platform, error) {
 		return GitLabPlatform, nil
 	case "azuredevops":
 		return AzureDevOpsPlatform, nil
+	case "gitea":
+		return GiteaPlatform, nil
 	default:
 		return UnknownPlatform, fmt.Errorf("invalid scm platform value: %s", s)
 	}

+ 16 - 0
detect/utils.go

@@ -75,6 +75,22 @@ func createScmLink(remote *sources.RemoteInfo, finding report.Finding) string {
 		// This is a bit dirty, but Azure DevOps does not highlight the line when the lineStartColumn and lineEndColumn are not provided
 		link += "&lineStartColumn=1&lineEndColumn=10000000&type=2&lineStyle=plain&_a=files"
 		return link
+	case scm.GiteaPlatform:
+		link := fmt.Sprintf("%s/src/commit/%s/%s", remote.Url, finding.Commit, filePath)
+		if hasInnerPath {
+			return link
+		}
+		ext := strings.ToLower(filepath.Ext(filePath))
+		if ext == ".ipynb" || ext == ".md" {
+			link += "?display=source"
+		}
+		if finding.StartLine != 0 {
+			link += fmt.Sprintf("#L%d", finding.StartLine)
+		}
+		if finding.EndLine != finding.StartLine {
+			link += fmt.Sprintf("-L%d", finding.EndLine)
+		}
+		return link
 	default:
 		// This should never happen.
 		return ""

+ 41 - 0
detect/utils_test.go

@@ -136,6 +136,47 @@ func Test_createScmLink(t *testing.T) {
 			},
 			want: "https://dev.azure.com/exampleorganisation/exampleproject/_git/exampleRepository/commit/20553ad96a4a080c94a54d677db97eed8ce2560d?path=/examplefile.json&line=25&lineEnd=30&lineStartColumn=1&lineEndColumn=10000000&type=2&lineStyle=plain&_a=files",
 		},
+
+		// Gitea
+		"gitea - single line": {
+			remote: &sources.RemoteInfo{
+				Platform: scm.GiteaPlatform,
+				Url:      "https://gitea.com/exampleorganisation/exampleproject",
+			},
+			finding: report.Finding{
+				Commit:    "20553ad96a4a080c94a54d677db97eed8ce2560d",
+				File:      "examplefile.json",
+				StartLine: 25,
+				EndLine:   25,
+			},
+			want: "https://gitea.com/exampleorganisation/exampleproject/src/commit/20553ad96a4a080c94a54d677db97eed8ce2560d/examplefile.json#L25",
+		},
+		"gitea- multi line": {
+			remote: &sources.RemoteInfo{
+				Platform: scm.GiteaPlatform,
+				Url:      "https://gitea.com/exampleorganisation/exampleproject",
+			},
+			finding: report.Finding{
+				Commit:    "20553ad96a4a080c94a54d677db97eed8ce2560d",
+				File:      "examplefile.json",
+				StartLine: 25,
+				EndLine:   30,
+			},
+			want: "https://gitea.com/exampleorganisation/exampleproject/src/commit/20553ad96a4a080c94a54d677db97eed8ce2560d/examplefile.json#L25-L30",
+		},
+		"gitea - markdown": {
+			remote: &sources.RemoteInfo{
+				Platform: scm.GiteaPlatform,
+				Url:      "https://gitea.com/exampleorganisation/exampleproject",
+			},
+			finding: report.Finding{
+				Commit:    "20553ad96a4a080c94a54d677db97eed8ce2560d",
+				File:      "Readme.md",
+				StartLine: 34,
+				EndLine:   34,
+			},
+			want: "https://gitea.com/exampleorganisation/exampleproject/src/commit/20553ad96a4a080c94a54d677db97eed8ce2560d/Readme.md?display=source#L34",
+		},
 	}
 	for name, tt := range tests {
 		t.Run(name, func(t *testing.T) {

+ 2 - 0
sources/git.go

@@ -482,6 +482,8 @@ func platformFromHost(u *url.URL) scm.Platform {
 		return scm.GitLabPlatform
 	case "dev.azure.com", "visualstudio.com":
 		return scm.AzureDevOpsPlatform
+	case "gitea.com", "code.forgejo.org", "codeberg.org":
+		return scm.GiteaPlatform
 	default:
 		return scm.UnknownPlatform
 	}