diff --git a/plan/task/gitPull.go b/plan/task/gitPull.go new file mode 100644 index 00000000..657fae86 --- /dev/null +++ b/plan/task/gitPull.go @@ -0,0 +1,80 @@ +package task + +import ( + "context" + "net/url" + + "github.com/moby/buildkit/client/llb" + "go.dagger.io/dagger/compiler" + "go.dagger.io/dagger/plancontext" + "go.dagger.io/dagger/solver" +) + +func init() { + Register("GitPull", func() Task { return &gitPullTask{} }) +} + +type gitPullTask struct { +} + +func (c gitPullTask) Run(ctx context.Context, pctx *plancontext.Context, s solver.Solver, v *compiler.Value) (*compiler.Value, error) { + remote, err := v.Lookup("remote").String() + if err != nil { + return nil, err + } + ref, err := v.Lookup("ref").String() + if err != nil { + return nil, err + } + + remoteRedacted := remote + if u, err := url.Parse(remote); err == nil { + remoteRedacted = u.Redacted() + } + + gitOpts := []llb.GitOption{} + var opts struct { + KeepGitDir bool + } + + if err := v.Decode(&opts); err != nil { + return nil, err + } + + if opts.KeepGitDir { + gitOpts = append(gitOpts, llb.KeepGitDir()) + } + // Secret + if authToken := v.Lookup("auth.token"); authToken.Exists() { + authTokenSecret, err := pctx.Secrets.FromValue(authToken) + if err != nil { + return nil, err + } + gitOpts = append(gitOpts, llb.AuthTokenSecret(authTokenSecret.ID())) + } + if authHeader := v.Lookup("auth.header"); authHeader.Exists() { + authHeaderSecret, err := pctx.Secrets.FromValue(authHeader) + if err != nil { + return nil, err + } + gitOpts = append(gitOpts, llb.AuthHeaderSecret(authHeaderSecret.ID())) + } + + gitOpts = append(gitOpts, withCustomName(v, "FetchGit %s@%s", remoteRedacted, ref)) + + st := llb.Git( + remote, + ref, + gitOpts..., + ) + + result, err := s.Solve(ctx, st, pctx.Platform.Get()) + if err != nil { + return nil, err + } + + fs := pctx.FS.New(result) + return compiler.NewValue().FillFields(map[string]interface{}{ + "contents": fs.MarshalCUE(), + }) +} diff --git a/stdlib/europa/dagger/engine/git.cue b/stdlib/europa/dagger/engine/git.cue index f27db9ca..957c4091 100644 --- a/stdlib/europa/dagger/engine/git.cue +++ b/stdlib/europa/dagger/engine/git.cue @@ -12,10 +12,12 @@ package engine // Pull a directory from a git remote #GitPull: { - @dagger(notimplemented) $dagger: task: _name: "GitPull" - remote: string ref: string + auth: { + token?: #Secret + header?: #Secret + } output: #FS } diff --git a/tests/tasks.bats b/tests/tasks.bats index dda02fb9..9d893739 100644 --- a/tests/tasks.bats +++ b/tests/tasks.bats @@ -93,3 +93,7 @@ setup() { "$DAGGER" --europa up ./scratch.cue -l debug } +@test "task: #GitPull" { + cd "$TESTDIR"/tasks/gitPull/ + "$DAGGER" --europa up ./exists.cue +} diff --git a/tests/tasks/gitPull/exists.cue b/tests/tasks/gitPull/exists.cue new file mode 100644 index 00000000..ef6a7ded --- /dev/null +++ b/tests/tasks/gitPull/exists.cue @@ -0,0 +1,10 @@ +package main + +import "alpha.dagger.io/europa/dagger/engine" + +engine.#Plan & { + actions: gitPull: engine.#GitPull & { + remote: "https://github.com/blocklayerhq/acme-clothing.git" + ref: "master" + } +}