fix(collector): link a failed repository to the configured host

When the first scan of a repository failed, the repository link pointed at github.com. It now follows github.api_url: Enterprise Server hosts drop the /api/v3 suffix and Enterprise Cloud hosts drop the api. prefix.
This commit is contained in:
2026-10-02 10:15:48 +01:00
parent e004751d88
commit b74396f005
4 changed files with 58 additions and 1 deletions
+22
View File
@@ -4,6 +4,7 @@ package config
import (
"errors"
"fmt"
"net/url"
"os"
"strings"
"time"
@@ -161,3 +162,24 @@ func normalizeRepos(in []string) ([]string, error) {
}
return out, nil
}
const (
defaultWebURL = "https://github.com"
apiV3Suffix = "/api/v3"
apiHostPrefix = "api."
)
// WebBase returns the address of the web interface that belongs to a GitHub API address:
// github.com for the public API, the host without /api/v3 for Enterprise Server, and the host
// without its "api." prefix for Enterprise Cloud with data residency.
func WebBase(apiURL string) string {
u, err := url.Parse(strings.TrimRight(apiURL, "/"))
if err != nil || u.Host == "" || strings.EqualFold(u.Host, "api.github.com") {
return defaultWebURL
}
u.Path = strings.TrimSuffix(u.Path, apiV3Suffix)
if u.Path == "" {
u.Host = strings.TrimPrefix(u.Host, apiHostPrefix)
}
return strings.TrimRight(u.String(), "/")
}