Use git_source configurations for GOPRIVATE

This commit is contained in:
Michael B. Gale
2025-09-04 15:52:53 +01:00
parent 895399ff05
commit a8fa1a76c4
2 changed files with 44 additions and 1 deletions

View File

@@ -14,6 +14,7 @@ const PROXY_PORT = "CODEQL_PROXY_PORT"
const PROXY_CA_CERTIFICATE = "CODEQL_PROXY_CA_CERTIFICATE"
const PROXY_URLS = "CODEQL_PROXY_URLS"
const GOPROXY_SERVER = "goproxy_server"
const GIT_SOURCE = "git_source"
type RegistryConfig struct {
Type string `json:"type"`
@@ -29,6 +30,9 @@ var proxy_cert_file string
// An array of goproxy server URLs.
var goproxy_servers []string
// An array of Git URLs.
var git_sources []string
// Stores the environment variables that we wish to pass on to `go` commands.
var proxy_vars []string = nil
@@ -98,9 +102,14 @@ func getEnvVars() []string {
if cfg.Type == GOPROXY_SERVER {
goproxy_servers = append(goproxy_servers, cfg.URL)
slog.Info("Found GOPROXY server", slog.String("url", cfg.URL))
} else if cfg.Type == GIT_SOURCE {
git_sources = append(git_sources, cfg.URL)
slog.Info("Found Git source", slog.String("url", cfg.URL))
}
}
goprivate := []string{}
if len(goproxy_servers) > 0 {
goproxy_val := "https://proxy.golang.org,direct"
@@ -108,8 +117,14 @@ func getEnvVars() []string {
goproxy_val = url + "," + goproxy_val
}
result = append(result, fmt.Sprintf("GOPROXY=%s", goproxy_val), "GOPRIVATE=", "GONOPROXY=")
result = append(result, fmt.Sprintf("GOPROXY=%s", goproxy_val), "GONOPROXY=")
}
if len(git_sources) > 0 {
goprivate = append(goprivate, git_sources...)
}
result = append(result, fmt.Sprintf("GOPRIVATE=%s", strings.Join(goprivate, ",")))
}
}

View File

@@ -47,3 +47,31 @@ func TestParseRegistryConfigs(t *testing.T) {
t.Fatalf("Expected `URL` to be `https://proxy.example.com/mod`, but got `%s`", first.URL)
}
}
func TestParseRegistryConfigsMultiple(t *testing.T) {
multiple := parseRegistryConfigsSuccess(t, "[{ \"type\": \"git_source\", \"url\": \"https://github.com/github\" }, { \"type\": \"goproxy_server\", \"url\": \"https://proxy.example.com/mod\" }]")
if len(multiple) != 2 {
t.Fatalf("Expected `parseRegistryConfigs` to return two configurations, but got %d.", len(multiple))
}
first := multiple[0]
if first.Type != "git_source" {
t.Fatalf("Expected `Type` to be `git_source`, but got `%s`", first.Type)
}
if first.URL != "https://github.com/github" {
t.Fatalf("Expected `URL` to be `https://github.com/github`, but got `%s`", first.URL)
}
second := multiple[1]
if second.Type != "goproxy_server" {
t.Fatalf("Expected `Type` to be `goproxy_server`, but got `%s`", second.Type)
}
if second.URL != "https://proxy.example.com/mod" {
t.Fatalf("Expected `URL` to be `https://proxy.example.com/mod`, but got `%s`", second.URL)
}
}