Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions acceptance/bundle/auto-deploy/delete/databricks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
bundle:
name: auto-deploy

targets:
dev:
default: true
prod:
git:
branch: main
2 changes: 2 additions & 0 deletions acceptance/bundle/auto-deploy/delete/out.test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cloud = false
EnvMatrix.DMS = []
78 changes: 78 additions & 0 deletions acceptance/bundle/auto-deploy/delete/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

=== Without a terminal, delete needs --auto-approve

>>> musterr [CLI] bundle auto-deploy delete
Error: this deletes the auto-deploy trigger of target dev. To proceed, use --auto-approve

=== Deleting the target's trigger leaves the other target's

>>> [CLI] bundle auto-deploy delete --auto-approve
Deleted the auto-deploy trigger of target dev.
{
"method": "DELETE",
"path": "/api/2.0/git-webhooks/trigger",
"body": {
"repo_url": "https://github.com/acme/repo",
"git_provider": "GITHUB",
"trigger_config": {
"push_event": {
"branch": "main"
},
"dabs_deploy": {
"bundle_yml_repo_path": "databricks.yml",
"deployment_mode": "dev"
}
}
}
}

>>> [CLI] bundle auto-deploy list
Target Branch Deploy as Trigger ID
prod main its creator [UUID]

=== A retried delete sends its body again

>>> [CLI] bundle auto-deploy delete -t prod --auto-approve
Deleted the auto-deploy trigger of target prod.
{
"method": "DELETE",
"path": "/api/2.0/git-webhooks/trigger",
"body": {
"repo_url": "https://github.com/acme/repo",
"git_provider": "GITHUB",
"trigger_config": {
"push_event": {
"branch": "main"
},
"dabs_deploy": {
"bundle_yml_repo_path": "databricks.yml",
"deployment_mode": "prod"
}
}
}
}
{
"method": "DELETE",
"path": "/api/2.0/git-webhooks/trigger",
"body": {
"repo_url": "https://github.com/acme/repo",
"git_provider": "GITHUB",
"trigger_config": {
"push_event": {
"branch": "main"
},
"dabs_deploy": {
"bundle_yml_repo_path": "databricks.yml",
"deployment_mode": "prod"
}
}
}
}

>>> [CLI] bundle auto-deploy list
Target Branch Deploy as Trigger ID

=== A target without a trigger

>>> musterr [CLI] bundle auto-deploy delete --auto-approve
Error: target dev has no auto-deploy trigger that you can view. To create one, run: databricks bundle auto-deploy create -t dev
23 changes: 23 additions & 0 deletions acceptance/bundle/auto-deploy/delete/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
git-repo-init
git remote add origin https://github.com/acme/repo

$CLI bundle auto-deploy create >> LOG.setup 2>&1
$CLI bundle auto-deploy create -t prod >> LOG.setup 2>&1
rm "$OUT_REQUESTS"

title "Without a terminal, delete needs --auto-approve"
trace musterr $CLI bundle auto-deploy delete

title "Deleting the target's trigger leaves the other target's"
trace $CLI bundle auto-deploy delete --auto-approve
print_requests.py //git-webhooks
trace $CLI bundle auto-deploy list

title "A retried delete sends its body again"
fault.py --body-contains deployment_mode "DELETE /api/2.0/git-webhooks/trigger" 503 0 1 TEMPORARILY_UNAVAILABLE
trace $CLI bundle auto-deploy delete -t prod --auto-approve
print_requests.py //git-webhooks

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nice to have] After the retried delete -t prod, the test only prints the requests. It never shows that prod's trigger is gone. If the retry returned a 2xx without the fake deleting anything, this golden would still be produced. The last section runs against dev, which an earlier section already deleted. Can we add trace $CLI bundle auto-deploy list here, or point the "target without a trigger" section at -t prod, so the output shows the state after the retry?

trace $CLI bundle auto-deploy list

title "A target without a trigger"
trace musterr $CLI bundle auto-deploy delete --auto-approve
1 change: 1 addition & 0 deletions cmd/bundle/autodeploy/autodeploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ Example:
cmd.AddCommand(newCreateCommand())
cmd.AddCommand(newGetCommand())
cmd.AddCommand(newListCommand())
cmd.AddCommand(newDeleteCommand())
return cmd
}
67 changes: 67 additions & 0 deletions cmd/bundle/autodeploy/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package autodeploy

import (
"fmt"

"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/gitwebhooks"
"github.com/spf13/cobra"
)

func newDeleteCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Short: "Stop auto-deploying this bundle target",
Long: `Stop auto-deploying this bundle target.

Deletes the target's auto-deploy trigger and the job that runs its deploys.
The service keeps the trigger's past attempts, but the attempts command can no
longer show them.`,
Args: root.NoArgs,
}

var autoApprove bool
cmd.Flags().BoolVar(&autoApprove, "auto-approve", false, "Skip the confirmation prompt")

cmd.RunE = func(cmd *cobra.Command, args []string) error {
t, api, err := loadTrigger(cmd)
if err != nil {
return err
}
ctx := cmd.Context()
found, err := findTrigger(ctx, api, t)
if err != nil {
return err
}

if !autoApprove {
if !cmdio.IsPromptSupported(ctx) {
return fmt.Errorf("this deletes the auto-deploy trigger of target %s. To proceed, use --auto-approve", t.target)
}
ok, err := cmdio.AskYesOrNo(ctx, fmt.Sprintf("Delete the auto-deploy trigger of target %s?", t.target))
if err != nil || !ok {
return err
}
}

err = api.DeleteWebhookTrigger(ctx, gitwebhooks.DeleteWebhookTrigger{
RepoURL: found.RepoURL,
GitProvider: found.GitProvider,
TriggerConfig: &gitwebhooks.DeploymentTriggerConfig{
PushEvent: found.TriggerConfig.PushEvent,
DabsDeploy: &gitwebhooks.DabsDeployTarget{
BundleYmlRepoPath: t.bundlePath,
DeploymentMode: t.target,
},
},
})
if err != nil {
return err
}
cmdio.LogString(ctx, fmt.Sprintf("Deleted the auto-deploy trigger of target %s.", t.target))
return nil
}

return cmd
}
30 changes: 30 additions & 0 deletions libs/gitwebhooks/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
package gitwebhooks

import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"

"github.com/databricks/cli/libs/auth"
Expand Down Expand Up @@ -60,3 +63,30 @@ func (a *API) listWebhookTriggersPage(ctx context.Context, req ListWebhookTrigge
err := a.client.Do(ctx, http.MethodGet, basePath+"/webhook-triggers", auth.WorkspaceIDHeaders(a.client.Config), nil, req, &resp)
return &resp, err
}

// DeleteWebhookTrigger deletes the trigger and its runner job. Its attempts stay, and only
// workspace admins can read them.
func (a *API) DeleteWebhookTrigger(ctx context.Context, req DeleteWebhookTrigger) error {
body, err := json.Marshal(req)
if err != nil {
return err
}
// The SDK sends the fields of a DELETE in the query string, but this endpoint reads them
// from the body. So the request carries no data, and the visitor sets the body.
return a.client.Do(ctx, http.MethodDelete, basePath+"/trigger", auth.WorkspaceIDHeaders(a.client.Config), nil, nil, nil, withJSONBody(body))
}

// withJSONBody returns a request visitor that sets body as the request's JSON body. The SDK runs
// visitors on every attempt, so each retry gets a fresh reader. GetBody lets the transport
// replay the request on a stale pooled connection, as it does for a body the SDK sets.
func withJSONBody(body []byte) func(*http.Request) error {
return func(r *http.Request) error {
r.GetBody = func() (io.ReadCloser, error) {
return io.NopCloser(bytes.NewReader(body)), nil
}
r.Body, _ = r.GetBody()
r.ContentLength = int64(len(body))
r.Header.Set("Content-Type", "application/json")
return nil
}
}
8 changes: 8 additions & 0 deletions libs/gitwebhooks/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,11 @@ type ListWebhookTriggersResponse struct {
WebhookTriggers []WebhookTrigger `json:"webhook_triggers,omitempty"`
NextPageToken string `json:"next_page_token,omitempty"`
}

// DeleteWebhookTrigger deletes the trigger with the same repo_url, bundle_yml_repo_path and
// deployment_mode. The service ignores push_event, but rejects a request without it.
type DeleteWebhookTrigger struct {
RepoURL string `json:"repo_url"`
GitProvider GitProvider `json:"git_provider"`
TriggerConfig *DeploymentTriggerConfig `json:"trigger_config"`
}
48 changes: 41 additions & 7 deletions libs/testserver/gitwebhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,8 @@ func (s *FakeWorkspace) GitWebhooksUpsert(req Request) Response {
return gitWebhooksBadRequest(fmt.Sprintf("request parsing error: %s", err))
}
config := request.TriggerConfig
if request.RepoURL == "" || request.GitProvider == "" || config == nil || config.PushEvent == nil || config.DabsDeploy == nil {
return gitWebhooksBadRequest("repo_url, git_provider, trigger_config.push_event and trigger_config.dabs_deploy are required")
}
if config.DabsDeploy.BundleYmlRepoPath == "" || config.DabsDeploy.DeploymentMode == "" {
return gitWebhooksBadRequest("bundle_yml_repo_path and deployment_mode are required")
if message := validateGitWebhookTrigger(request.RepoURL, request.GitProvider, config); message != "" {
return gitWebhooksBadRequest(message)
}

stored := *config
Expand All @@ -70,8 +67,7 @@ func (s *FakeWorkspace) GitWebhooksUpsert(req Request) Response {
continue
}
webhookID = t.webhookID
existing := t.trigger.TriggerConfig.DabsDeploy
if existing.BundleYmlRepoPath == config.DabsDeploy.BundleYmlRepoPath && existing.DeploymentMode == config.DabsDeploy.DeploymentMode {
if sameDabsTarget(t.trigger, config.DabsDeploy) {
triggerID = id
}
}
Expand All @@ -95,6 +91,44 @@ func (s *FakeWorkspace) GitWebhooksUpsert(req Request) Response {
return Response{Body: gitwebhooks.UpsertWebhookTriggerResponse{WebhookID: webhookID, TriggerID: triggerID}}
}

// GitWebhooksDelete deletes the trigger with the same repository, bundle path and target. Like the
// service, it succeeds when there is none.
func (s *FakeWorkspace) GitWebhooksDelete(req Request) Response {
var request gitwebhooks.DeleteWebhookTrigger
if err := json.Unmarshal(req.Body, &request); err != nil {
return gitWebhooksBadRequest(fmt.Sprintf("request parsing error: %s", err))
}
if message := validateGitWebhookTrigger(request.RepoURL, request.GitProvider, request.TriggerConfig); message != "" {
return gitWebhooksBadRequest(message)
}

defer s.LockUnlock()()

for id, t := range s.GitWebhookTriggers {
if t.trigger.RepoURL == request.RepoURL && sameDabsTarget(t.trigger, request.TriggerConfig.DabsDeploy) {
delete(s.GitWebhookTriggers, id)
}
}
return Response{Body: map[string]any{}}
}

// validateGitWebhookTrigger returns what an upsert or delete request is missing, or "" if nothing.
// Both require the repo event, which delete does not match on.
func validateGitWebhookTrigger(repoURL string, provider gitwebhooks.GitProvider, config *gitwebhooks.DeploymentTriggerConfig) string {
if repoURL == "" || provider == "" || config == nil || config.PushEvent == nil || config.DabsDeploy == nil {
return "repo_url, git_provider, trigger_config.push_event and trigger_config.dabs_deploy are required"
}
if config.DabsDeploy.BundleYmlRepoPath == "" || config.DabsDeploy.DeploymentMode == "" {
return "bundle_yml_repo_path and deployment_mode are required"
}
return ""
}

func sameDabsTarget(t gitwebhooks.WebhookTrigger, target *gitwebhooks.DabsDeployTarget) bool {
stored := t.TriggerConfig.DabsDeploy
return stored.BundleYmlRepoPath == target.BundleYmlRepoPath && stored.DeploymentMode == target.DeploymentMode
}

// GitWebhooksList lists the triggers that match the filter, ordered by name.
func (s *FakeWorkspace) GitWebhooksList(req Request) Response {
query := req.URL.Query()
Expand Down
3 changes: 3 additions & 0 deletions libs/testserver/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,9 @@ func AddDefaultHandlers(server *Server) {
server.Handle("GET", "/api/2.0/git-webhooks/webhook-triggers", func(req Request) any {
return req.Workspace.GitWebhooksList(req)
})
server.Handle("DELETE", "/api/2.0/git-webhooks/trigger", func(req Request) any {
return req.Workspace.GitWebhooksDelete(req)
})

server.Handle("POST", "/api/2.2/jobs/delete", func(req Request) any {
var request jobs.DeleteJob
Expand Down
Loading