Repository navigation
feat(core): allow other git servers than GitHub #7511
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
Changes from all commits
02fae71
349f4b0
24e9e94
6f4bf83
e0f49c6
f6c1cd6
0a44c16
ab72abf
ec33e8b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| kind: Added | ||
| body: allow other CIs than GitHub | ||
| time: 2024-06-12T14:51:59.394243036Z | ||
| custom: | ||
| Author: grouville | ||
| PR: "7511" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package core | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "dagger.io/dagger" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestRefIntegration(t *testing.T) { | ||
| t.Parallel() | ||
| c, ctx := connect(t) | ||
|
|
||
| // This test handles a very edgy case: | ||
| // goGitBase inits a /work directory with a git context | ||
| // we then create a local dir with the same structure as a git remote: `/work/github.com/dagger/dagger` | ||
| // It should be resolved as a local ref, not a remote one | ||
| t.Run("local module with same format as remote: github.com/dagger/dagger", func(t *testing.T) { | ||
| t.Parallel() | ||
| out, err := goGitBase(t, c). | ||
| WithMountedFile(testCLIBinPath, daggerCliFile(t, c)). | ||
| WithWorkdir("/work/github.com/dagger/dagger"). | ||
| WithExec([]string{"pwd"}). | ||
| With(daggerExec("init", "--source=.", "--name=dep", "--sdk=go")). | ||
| WithNewFile("/work/github.com/dagger/dagger/main.go", dagger.ContainerWithNewFileOpts{ | ||
| Contents: `package main | ||
|
|
||
| import "context" | ||
|
|
||
| type Dep struct {} | ||
|
|
||
| func (m *Dep) GetSource(ctx context.Context) string { | ||
| return "hello" | ||
| } | ||
| `, | ||
| }). | ||
| WithWorkdir("/work"). | ||
| With(daggerCallAt("github.com/dagger/dagger", "get-source")). | ||
| Stdout(ctx) | ||
| require.NoError(t, err) | ||
| require.Equal(t, "hello", strings.TrimSpace(out)) | ||
| }) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,8 @@ import ( | |
| "github.com/dagger/dagger/core/modules" | ||
| "github.com/dagger/dagger/dagql" | ||
| "github.com/dagger/dagger/engine/buildkit" | ||
| "github.com/dagger/dagger/engine/vcs" | ||
| "github.com/tonistiigi/fsutil/types" | ||
| ) | ||
|
|
||
| type moduleSourceArgs struct { | ||
|
|
@@ -27,10 +29,8 @@ type moduleSourceArgs struct { | |
| } | ||
|
|
||
| func (s *moduleSchema) moduleSource(ctx context.Context, query *core.Query, args moduleSourceArgs) (*core.ModuleSource, error) { | ||
| parsed := parseRefString(args.RefString) | ||
| modPath, modVersion, hasVersion, isGitHub := parsed.modPath, parsed.modVersion, parsed.hasVersion, parsed.isGitHub | ||
|
|
||
| if !hasVersion && isGitHub && args.Stable { | ||
| parsed := parseRefString(ctx, query.Buildkit, args.RefString) | ||
| if args.Stable && !parsed.hasVersion && parsed.kind == core.ModuleSourceKindGit { | ||
| return nil, fmt.Errorf("no version provided for stable remote ref: %s", args.RefString) | ||
| } | ||
|
|
||
|
|
@@ -41,51 +41,43 @@ func (s *moduleSchema) moduleSource(ctx context.Context, query *core.Query, args | |
|
|
||
| switch src.Kind { | ||
| case core.ModuleSourceKindLocal: | ||
| if filepath.IsAbs(modPath) { | ||
| return nil, fmt.Errorf("local module source root path is absolute: %s", modPath) | ||
| if filepath.IsAbs(parsed.modPath) { | ||
| return nil, fmt.Errorf("local module source root path is absolute: %s", parsed.modPath) | ||
| } | ||
|
|
||
| src.AsLocalSource = dagql.NonNull(&core.LocalModuleSource{ | ||
| RootSubpath: modPath, | ||
| RootSubpath: parsed.modPath, | ||
| }) | ||
|
|
||
| case core.ModuleSourceKindGit: | ||
| if !isGitHub { | ||
| return nil, fmt.Errorf("for now, only github.com/ paths are supported: %q", args.RefString) | ||
| } | ||
|
|
||
| src.AsGitSource = dagql.NonNull(&core.GitModuleSource{}) | ||
|
|
||
| segments := strings.SplitN(modPath, "/", 4) | ||
| if len(segments) < 3 { | ||
| return nil, fmt.Errorf("invalid github.com path: %s", modPath) | ||
| } | ||
|
|
||
| src.AsGitSource.Value.URLParent = segments[0] + "/" + segments[1] + "/" + segments[2] | ||
| src.AsGitSource.Value.Root = parsed.repoRoot.Root | ||
| src.AsGitSource.Value.CloneURL = parsed.repoRoot.Repo | ||
|
|
||
| cloneURL := src.AsGitSource.Value.CloneURL() | ||
|
|
||
| if !hasVersion { | ||
| var modVersion string | ||
| if parsed.hasVersion { | ||
| modVersion = parsed.modVersion | ||
| } else { | ||
| if args.Stable { | ||
| return nil, fmt.Errorf("no version provided for stable remote ref: %s", args.RefString) | ||
| } | ||
| var err error | ||
| modVersion, err = defaultBranch(ctx, cloneURL) | ||
| modVersion, err = defaultBranch(ctx, parsed.repoRoot.Repo) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("determine default branch: %w", err) | ||
| } | ||
| } | ||
| src.AsGitSource.Value.Version = modVersion | ||
|
|
||
| var subPath string | ||
| if len(segments) == 4 { | ||
| subPath = segments[3] | ||
| } else { | ||
| subPath = "/" | ||
| subPath := "/" | ||
| if parsed.repoRootSubdir != "" { | ||
| subPath = parsed.repoRootSubdir | ||
| } | ||
|
|
||
| commitRef := modVersion | ||
| if hasVersion && isSemver(modVersion) { | ||
| allTags, err := gitTags(ctx, cloneURL) | ||
| if parsed.hasVersion && isSemver(modVersion) { | ||
| allTags, err := gitTags(ctx, parsed.repoRoot.Repo) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("get git tags: %w", err) | ||
| } | ||
|
|
@@ -102,7 +94,7 @@ func (s *moduleSchema) moduleSource(ctx context.Context, query *core.Query, args | |
| dagql.Selector{ | ||
| Field: "git", | ||
| Args: []dagql.NamedInput{ | ||
| {Name: "url", Value: dagql.String(cloneURL)}, | ||
| {Name: "url", Value: dagql.String(parsed.repoRoot.Repo)}, | ||
| }, | ||
| }, | ||
| dagql.Selector{ | ||
|
|
@@ -146,23 +138,49 @@ func (s *moduleSchema) moduleSource(ctx context.Context, query *core.Query, args | |
| } | ||
|
|
||
| type parsedRefString struct { | ||
| modPath string | ||
| modVersion string | ||
| hasVersion bool | ||
| isGitHub bool | ||
| kind core.ModuleSourceKind | ||
| modPath string | ||
| modVersion string | ||
| hasVersion bool | ||
| kind core.ModuleSourceKind | ||
| repoRoot *vcs.RepoRoot | ||
| repoRootSubdir string | ||
| } | ||
|
|
||
| // interface used for host interaction mocking | ||
| type buildkitClient interface { | ||
| StatCallerHostPath(ctx context.Context, path string, followLinks bool) (*types.Stat, error) | ||
| } | ||
|
|
||
| func parseRefString(refString string) parsedRefString { | ||
| // parseRefString parses a ref string into its components | ||
| // New heuristic: | ||
| // - stat folder to see if dir is present | ||
| // - if not, try to isolate root of git repo from the ref | ||
| // - if nothing worked, fallback as local ref, as before | ||
| func parseRefString(ctx context.Context, bk buildkitClient, refString string) parsedRefString { | ||
| var parsed parsedRefString | ||
| parsed.modPath, parsed.modVersion, parsed.hasVersion = strings.Cut(refString, "@") | ||
| parsed.isGitHub = strings.HasPrefix(parsed.modPath, "github.com/") | ||
|
|
||
| if !parsed.hasVersion && !parsed.isGitHub { | ||
| parsed.kind = core.ModuleSourceKindLocal | ||
| // We do a stat in case the mod path github.com/username is a local directory | ||
| stat, err := bk.StatCallerHostPath(ctx, parsed.modPath, false) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we're not checking for this
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are, it is being returned in https://github.com/dagger/dagger/blob/a257857d89659ee6f1fd86c6252021d4361f19ab/core/schema/modulesource.go#L52, in the edge case where we fallback to a local source kind. This is currently not being exploited to its fullest potential I agree, but it is being checked here: https://github.com/dagger/dagger/blob/a257857d89659ee6f1fd86c6252021d4361f19ab/core/schema/modulesource.go#L52-L57, currently only as a log to specify the user that a fallback just happened. It could be refined a bit more afterwards to not log every case ?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed the logic to log directly inside parseRefString, to be able to debug when wrongful fallback happen |
||
| if err == nil { | ||
| if !parsed.hasVersion && stat.IsDir() { | ||
| parsed.kind = core.ModuleSourceKindLocal | ||
| return parsed | ||
| } | ||
| } | ||
|
|
||
| // we try to isolate the root of the git repo | ||
| repoRoot, err := vcs.RepoRootForImportPath(parsed.modPath, false) | ||
| if err == nil && repoRoot != nil && repoRoot.VCS != nil && repoRoot.VCS.Name == "Git" { | ||
| parsed.kind = core.ModuleSourceKindGit | ||
| parsed.repoRoot = repoRoot | ||
| parsed.repoRootSubdir = strings.TrimPrefix(parsed.modPath, repoRoot.Root) | ||
| // the extra "/" is important as subpath traversal such as /../ are being cleaned by filePath.Clean | ||
| parsed.repoRootSubdir = strings.TrimPrefix(parsed.repoRootSubdir, "/") | ||
| return parsed | ||
| } | ||
| parsed.kind = core.ModuleSourceKindGit | ||
|
|
||
| parsed.kind = core.ModuleSourceKindLocal | ||
| return parsed | ||
| } | ||
|
|
||
|
|
@@ -192,14 +210,6 @@ func (s *moduleSchema) moduleSourceAsString(ctx context.Context, src *core.Modul | |
| return src.RefString() | ||
| } | ||
|
|
||
| func (s *moduleSchema) gitModuleSourceCloneURL( | ||
| ctx context.Context, | ||
| ref *core.GitModuleSource, | ||
| args struct{}, | ||
| ) (string, error) { | ||
| return ref.CloneURL(), nil | ||
| } | ||
|
|
||
| func (s *moduleSchema) gitModuleSourceHTMLURL( | ||
| ctx context.Context, | ||
| ref *core.GitModuleSource, | ||
|
|
@@ -642,6 +652,10 @@ func (s *moduleSchema) moduleSourceResolveFromCaller( | |
| if err != nil { | ||
| return inst, fmt.Errorf("failed to get source root relative path: %w", err) | ||
| } | ||
| // ensure sourceRootRelPath has a local path structure | ||
| // even when subdir relative to git source has ref structure | ||
| // (cf. test TestRefFormat) | ||
| sourceRootRelPath = "./" + sourceRootRelPath | ||
|
|
||
| collectedDeps := dagql.NewCacheMap[string, *callerLocalDep]() | ||
| if err := s.collectCallerLocalDeps(ctx, src.Query, contextAbsPath, sourceRootAbsPath, true, src, collectedDeps); err != nil { | ||
|
|
@@ -970,7 +984,7 @@ func (s *moduleSchema) collectCallerLocalDeps( | |
| } | ||
|
|
||
| for _, depCfg := range modCfg.Dependencies { | ||
| parsed := parseRefString(depCfg.Source) | ||
| parsed := parseRefString(ctx, query.Buildkit, depCfg.Source) | ||
| if parsed.kind != core.ModuleSourceKindLocal { | ||
| continue | ||
| } | ||
|
|
@@ -991,7 +1005,7 @@ func (s *moduleSchema) collectCallerLocalDeps( | |
| switch { | ||
| case err == nil: | ||
| case errors.Is(err, errUnknownBuiltinSDK): | ||
| parsed := parseRefString(modCfg.SDK) | ||
| parsed := parseRefString(ctx, query.Buildkit, modCfg.SDK) | ||
| switch parsed.kind { | ||
| case core.ModuleSourceKindLocal: | ||
| // SDK is a local custom one, it needs to be included | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also have some more complete integration tests - see
dagger/core/integration/module_config_test.go
Lines 1169 to 1185 in 9d8f5cb
We can mirror https://github.com/dagger/dagger-test-modules to some places in gitlab, and parameterize the tests to run against that repo hosted in different places (like a subgroup in gitlab). That way, we're testing against these other VCSs in the same way that we're testing against github, so they're both first-class citizens.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I discussed it with @marcosnils and I am not sure that integration tests are necessary here. Most of the logic here is to basically split the ref into root of repo and subdir. With the tests on vcs.repoRootForImportPath and the new
vcs.ExtractRootAndSubdirFromRef: https://github.com/dagger/dagger/pull/7511/files#diff-8432d1812b1f1b774598e92639e78ab8a25fcb6804617f7587d692149d155e21, we cover a lotThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will re-review in a bit - but. I don't necessarily think we need to test this for an initial merge, but we absolutely need some sanity integration tests at some point soon.
Previously, changes to some logic in git resolution/etc changes this in incredibly subtle and unexpected ways. If we aren't testing this, this becomes much more likely. This has happened before, multiple times - the module source logic gets touched a lot, and from experience it seems to be very fragile.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, this makes sense 👌. I was planning on adding integration tests for the private git support, it seems a great complementary add at this time, wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM 🚀