Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional switch for disabling custom git fetch #42704

Merged
merged 7 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ All notable changes to Sourcegraph are documented in this file.
- All Perforce rules are now stored together in one column and evaluated on a "last rule takes precedence" basis. [#41785](https://github.com/sourcegraph/sourcegraph/pull/41785)
- Security events are now a part of the audit log. [#42653](https://github.com/sourcegraph/sourcegraph/pull/42653)
- "GC AUTO" is now the default garbage collection job. We disable sg maintenance, which had previously replace "GC AUTO", after repeated reports about repo corruption. [#42856](https://github.com/sourcegraph/sourcegraph/pull/42856)
- To use the optional `customGitFetch` feature, the `ENABLE_CUSTOM_GIT_FETCH` env var must be set on `gitserver`. [#42704](https://github.com/sourcegraph/sourcegraph/pull/42704)

### Fixed

Expand Down
13 changes: 12 additions & 1 deletion cmd/gitserver/server/customfetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"path"
"strings"

"github.com/sourcegraph/log"
"github.com/sourcegraph/sourcegraph/internal/conf"
"github.com/sourcegraph/sourcegraph/internal/env"
"github.com/sourcegraph/sourcegraph/internal/vcs"
"github.com/sourcegraph/sourcegraph/schema"
)
Expand All @@ -16,8 +18,17 @@ var customGitFetch = conf.Cached(func() map[string][]string {
return buildCustomFetchMappings(exp.CustomGitFetch)
})

var enableCustomGitFetch = env.Get("ENABLE_CUSTOM_GIT_FETCH", "false", "Enable custom git fetch")

func buildCustomFetchMappings(c []*schema.CustomGitFetchMapping) map[string][]string {
if c == nil {
// this is an edge case where a CustomGitFetchMapping has been made but enableCustomGitFetch is false
if c != nil && enableCustomGitFetch == "false" {
logger := log.Scoped("customfetch", "")
logger.Warn("a CustomGitFetchMapping is configured but ENABLE_CUSTOM_GIT_FETCH is not set")

return map[string][]string{}
}
if c == nil || enableCustomGitFetch == "false" {
return map[string][]string{}
evict marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
25 changes: 25 additions & 0 deletions cmd/gitserver/server/customfetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ func TestEmptyCustomGitFetch(t *testing.T) {
}
}

func TestDisabledCustomGitFetch(t *testing.T) {
mapping := []*schema.CustomGitFetchMapping{
{
DomainPath: "github.com/foo/normal/one",
Fetch: "echo normal one",
},
}
remoteUrl := "https://8cd1419f4d5c1e0527f2893c9422f1a2a435116d@github.com/foo/normal/one"

customGitFetch = func() map[string][]string {
return buildCustomFetchMappings(mapping)
}

remoteURL, _ := vcs.ParseURL(remoteUrl)
evict marked this conversation as resolved.
Show resolved Hide resolved
customCmd := customFetchCmd(context.Background(), remoteURL)
if customCmd != nil {
t.Errorf("expected nil custom cmd for empty configuration, got %+v", customCmd)
}
}

func TestCustomGitFetch(t *testing.T) {
mappings := []*schema.CustomGitFetchMapping{
{
Expand Down Expand Up @@ -66,6 +86,11 @@ func TestCustomGitFetch(t *testing.T) {
},
}

// env var ENABLE_CUSTOM_GIT_FETCH is set to true
enableCustomGitFetch = "true"
t.Cleanup(func() {
enableCustomGitFetch = "false"
})
customGitFetch = func() map[string][]string {
return buildCustomFetchMappings(mappings)
}
Expand Down
2 changes: 1 addition & 1 deletion schema/schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion schema/site.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@
}
},
"customGitFetch": {
"description": "JSON array of configuration that maps from Git clone URL domain/path to custom git fetch command.",
"description": "JSON array of configuration that maps from Git clone URL domain/path to custom git fetch command. To enable this feature set environment variable `ENABLE_CUSTOM_GIT_FETCH` as `true` on gitserver.",
"type": "array",
"items": {
"title": "CustomGitFetchMapping",
Expand Down