From 722ce8213aae1fcc9163c751872eb5dbec9f9543 Mon Sep 17 00:00:00 2001 From: Tilo Brueckner Date: Wed, 4 Jun 2025 14:15:06 +0000 Subject: [PATCH] Add Platform Gitea (#1884) --- cmd/scm/scm.go | 4 ++++ detect/utils.go | 16 ++++++++++++++++ detect/utils_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ sources/git.go | 2 ++ 4 files changed, 63 insertions(+) diff --git a/cmd/scm/scm.go b/cmd/scm/scm.go index a89136af..8e021e64 100644 --- a/cmd/scm/scm.go +++ b/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) } diff --git a/detect/utils.go b/detect/utils.go index 14be6419..cecbcc92 100644 --- a/detect/utils.go +++ b/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 "" diff --git a/detect/utils_test.go b/detect/utils_test.go index cfebbfdb..0b655b55 100644 --- a/detect/utils_test.go +++ b/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) { diff --git a/sources/git.go b/sources/git.go index 16a51879..7490ef38 100644 --- a/sources/git.go +++ b/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 }