diff --git a/docs/resources/docker.md b/docs/resources/docker.md index e79d886..cd502d1 100644 --- a/docs/resources/docker.md +++ b/docs/resources/docker.md @@ -60,7 +60,7 @@ Can be either app_xxx or postgres_yyy ID format Optional: -- `commit` (String) Deploy application on the given commit/tag +- `commit` (String) Support multiple syntax like `refs/heads/[BRANCH]` or `[COMMIT]`, in most of the case, you can use `refs/heads/master` - `repository` (String) diff --git a/docs/resources/java_war.md b/docs/resources/java_war.md index 64a9b78..a1b578b 100644 --- a/docs/resources/java_war.md +++ b/docs/resources/java_war.md @@ -115,7 +115,7 @@ Can be either app_xxx or postgres_yyy ID format Optional: -- `commit` (String) Deploy application on the given commit/tag +- `commit` (String) Support multiple syntax like `refs/heads/[BRANCH]` or `[COMMIT]`, in most of the case, you can use `refs/heads/master` - `repository` (String) diff --git a/docs/resources/nodejs.md b/docs/resources/nodejs.md index 365a277..35f0c2f 100644 --- a/docs/resources/nodejs.md +++ b/docs/resources/nodejs.md @@ -119,7 +119,7 @@ Can be either app_xxx or postgres_yyy ID format Optional: -- `commit` (String) Deploy application on the given commit/tag +- `commit` (String) Support multiple syntax like `refs/heads/[BRANCH]` or `[COMMIT]`, in most of the case, you can use `refs/heads/master` - `repository` (String) diff --git a/docs/resources/php.md b/docs/resources/php.md index e443fac..4ee24b6 100644 --- a/docs/resources/php.md +++ b/docs/resources/php.md @@ -56,7 +56,7 @@ Can be either app_xxx or postgres_yyy ID format Optional: -- `commit` (String) Deploy application on the given commit/tag +- `commit` (String) Support multiple syntax like `refs/heads/[BRANCH]` or `[COMMIT]`, in most of the case, you can use `refs/heads/master` - `repository` (String) diff --git a/docs/resources/python.md b/docs/resources/python.md index a538b0f..65cf293 100644 --- a/docs/resources/python.md +++ b/docs/resources/python.md @@ -116,7 +116,7 @@ Can be either app_xxx or postgres_yyy ID format Optional: -- `commit` (String) Deploy application on the given commit/tag +- `commit` (String) Support multiple syntax like `refs/heads/[BRANCH]` or `[COMMIT]`, in most of the case, you can use `refs/heads/master` - `repository` (String) diff --git a/docs/resources/scala.md b/docs/resources/scala.md index 79843bf..b2134f3 100644 --- a/docs/resources/scala.md +++ b/docs/resources/scala.md @@ -114,7 +114,7 @@ Can be either app_xxx or postgres_yyy ID format Optional: -- `commit` (String) Deploy application on the given commit/tag +- `commit` (String) Support multiple syntax like `refs/heads/[BRANCH]` or `[COMMIT]`, in most of the case, you can use `refs/heads/master` - `repository` (String) diff --git a/docs/resources/static.md b/docs/resources/static.md index 3b6b7b0..9426b70 100644 --- a/docs/resources/static.md +++ b/docs/resources/static.md @@ -114,7 +114,7 @@ Can be either app_xxx or postgres_yyy ID format Optional: -- `commit` (String) Deploy application on the given commit/tag +- `commit` (String) Support multiple syntax like `refs/heads/[BRANCH]` or `[COMMIT]`, in most of the case, you can use `refs/heads/master` - `repository` (String) diff --git a/pkg/application/git.go b/pkg/application/git.go index 5c2b0e8..daa7bf8 100644 --- a/pkg/application/git.go +++ b/pkg/application/git.go @@ -2,6 +2,7 @@ package application import ( "context" + "fmt" "os" "strings" @@ -10,6 +11,7 @@ import ( "github.com/go-git/go-git/v5/plumbing/transport/http" "github.com/go-git/go-git/v5/storage/memory" "github.com/hashicorp/terraform-plugin-framework/diag" + "github.com/hashicorp/terraform-plugin-log/tflog" "go.clever-cloud.dev/client" ) @@ -21,8 +23,7 @@ func gitDeploy(ctx context.Context, d Deployment, cc *client.Client, cleverRemot cloneOpts := &git.CloneOptions{ URL: d.Repository, RemoteName: "origin", - //Depth: 1, - Progress: os.Stdout, + Progress: os.Stdout, } r, err := git.CloneContext(ctx, memory.NewStorage(), nil, cloneOpts) @@ -50,15 +51,26 @@ func gitDeploy(ctx context.Context, d Deployment, cc *client.Client, cleverRemot RemoteURL: cleverRemote, Force: true, Progress: os.Stdout, - // TODO: deploy right branch/tag/commit - /*RefSpecs: []config.RefSpec{ - "master:master", - },*/ - Auth: auth, - Atomic: true, + Auth: auth, } - err = remote.Push(pushOptions) - if err != nil { + if d.Commit != nil { + // refs/heads/[BRANCH] + // [COMMIT] + ref := config.RefSpec(fmt.Sprintf("%s:refs/heads/master", *d.Commit)) + if err := ref.Validate(); err != nil { + diags.AddError("failed to build ref spec to push", err.Error()) + return diags + } + + pushOptions.RefSpecs = []config.RefSpec{ref} + } + + tflog.Debug(ctx, "pushing...", map[string]interface{}{ + "options": fmt.Sprintf("%+v", pushOptions), + }) + + err = remote.PushContext(ctx, pushOptions) + if err != nil && err != git.NoErrAlreadyUpToDate { diags.AddError("failed to push to clever remote", err.Error()) return diags } diff --git a/pkg/attributes/blocks.go b/pkg/attributes/blocks.go index cdffae8..be8edaa 100644 --- a/pkg/attributes/blocks.go +++ b/pkg/attributes/blocks.go @@ -1,7 +1,12 @@ package attributes import ( + "context" + "strings" + + "github.com/go-git/go-git/v5/plumbing" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/types" "go.clever-cloud.com/terraform-provider/pkg" ) @@ -31,8 +36,26 @@ var blocks = map[string]schema.Block{ }, "commit": schema.StringAttribute{ Optional: true, - Description: "Support either ':' or ''", - MarkdownDescription: "Deploy application on the given commit/tag", + Description: "The git reference you want to deploy", + MarkdownDescription: "Support multiple syntax like `refs/heads/[BRANCH]` or `[COMMIT]`, in most of the case, you can use `refs/heads/master`", + Validators: []validator.String{ + pkg.NewValidator( + "if reference (not commit hash) is provided test it's syntax", + func(ctx context.Context, req validator.StringRequest, res *validator.StringResponse) { + if req.ConfigValue.IsNull() || !strings.Contains(req.ConfigValue.ValueString(), "/") { + return + } + + ref := plumbing.ReferenceName(req.ConfigValue.ValueString()) + if err := ref.Validate(); err != nil { + res.Diagnostics.AddAttributeError( + req.Path, + "invalid Git reference", + err.Error(), + ) + } + }), + }, }, }, }, diff --git a/pkg/resources/nodejs/crud.go b/pkg/resources/nodejs/crud.go index 06dd72a..6cb5e07 100644 --- a/pkg/resources/nodejs/crud.go +++ b/pkg/resources/nodejs/crud.go @@ -89,7 +89,6 @@ func (r *ResourceNodeJS) Create(ctx context.Context, req resource.CreateRequest, createRes, diags := application.CreateApp(ctx, createReq) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { - tflog.Error(ctx, "ERROR IS WELL HERE", map[string]interface{}{}) return }