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/attempts/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/attempts/out.test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cloud = false
EnvMatrix.DMS = []
47 changes: 47 additions & 0 deletions acceptance/bundle/auto-deploy/attempts/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

=== A target without a trigger

>>> musterr [CLI] bundle auto-deploy attempts
Error: target dev has no auto-deploy trigger that you can view. To create one, run: databricks bundle auto-deploy create -t dev

=== Creating the trigger queues its first attempt

>>> [CLI] bundle auto-deploy attempts
Attempt ID Status Commit Created
[UUID][0] QUEUED [TIMESTAMP]

>>> [CLI] bundle auto-deploy attempts [UUID][0]
Attempt ID: [UUID][0]
Status: QUEUED
Created: [TIMESTAMP]
{
"method": "GET",
"path": "/api/2.0/git-webhooks/webhook-triggers/[UUID][1]/attempts"
}
{
"method": "GET",
"path": "/api/2.0/git-webhooks/webhook-triggers/[UUID][1]/attempts/[UUID][0]"
}

=== A failed attempt

>>> [CLI] bundle auto-deploy attempts [UUID][2]
Attempt ID: [UUID][2]
Status: FAILED
Commit: 0123456789abcdef0123456789abcdef01234567
Created: [TIMESTAMP]
Completed: [TIMESTAMP]
Stage: RUN_BUNDLE_DEPLOY
Run ID: 7012345678901234
Error: cannot create job: invalid cluster spec

=== An attempt that does not exist

>>> musterr [CLI] bundle auto-deploy attempts [UUID][3]
Error: Auto-deploy attempt 'webhook-triggers/[UUID][1]/attempts/[UUID][3]' does not exist

>>> musterr [CLI] bundle auto-deploy attempts webhook-triggers/x/attempts/y
Error: "webhook-triggers/x/attempts/y" is not an attempt ID. Pass the attempt_id of an attempt, not its name

>>> musterr [CLI] bundle auto-deploy attempts ..
Error: ".." is not an attempt ID. Pass the attempt_id of an attempt, not its name
22 changes: 22 additions & 0 deletions acceptance/bundle/auto-deploy/attempts/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
git-repo-init
git remote add origin https://github.com/acme/repo

title "A target without a trigger"
trace musterr $CLI bundle auto-deploy attempts

$CLI bundle auto-deploy create >> LOG.setup 2>&1

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[nit] testing.md asks for the acceptance/bin/ helpers instead of inline jq, and recommends gron.py | grep field for single values. Can attempt_id be read with gron.py here instead of jq -r '.[0].attempt_id'?

attempt_id=$($CLI bundle auto-deploy attempts -o json | jq -r '.[0].attempt_id')

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[nit] .agents/rules/testing.md asks for the acceptance/bin/ helpers instead of inline jq pipelines, and prefers gron.py | grep field for single values. This line pulls .[0].attempt_id with jq -r. Can it use the helpers instead?

rm "$OUT_REQUESTS"

title "Creating the trigger queues its first attempt"
trace $CLI bundle auto-deploy attempts
trace $CLI bundle auto-deploy attempts $attempt_id
print_requests.py --get //attempts

title "A failed attempt"
trace $CLI bundle auto-deploy attempts 11111111-1111-4111-8111-111111111111

title "An attempt that does not exist"
trace musterr $CLI bundle auto-deploy attempts 22222222-2222-4222-8222-222222222222
trace musterr $CLI bundle auto-deploy attempts webhook-triggers/x/attempts/y
trace musterr $CLI bundle auto-deploy attempts ..
21 changes: 21 additions & 0 deletions acceptance/bundle/auto-deploy/attempts/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# The fake has no runner, so no attempt of it ever fails. This stub covers how a failed attempt
# renders. It answers for any trigger, as it only checks the output.
[[Server]]
Pattern = "GET /api/2.0/git-webhooks/webhook-triggers/{trigger_id}/attempts/11111111-1111-4111-8111-111111111111"

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] .agents/rules/testing.md says to reserve [[Server]] for routes the testserver doesn't model and for responses a stateful handler can't express. This PR adds a handler for this exact route, and a failed attempt is a state GitWebhookAttempts could hold. Because the pattern matches any {trigger_id} and the body names trigger aaaaaaaa-…, the "A failed attempt" case passes no matter which trigger the CLI looked up or put in the path. Could the test put a failed attempt into the fake for the real trigger instead?

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] The stub matches any {trigger_id} and returns trigger_id/name for aaaaaaaa-…, not the trigger this test created ([UUID][1]). The fake would never return that body for this request. testing.md asks to model API behavior in libs/testserver/ instead of [[Server]] stubs, and this route is now modelled there. Could the test seed a failed attempt into the fake (for example a helper that sets an attempt's status), so this case goes through the real handler?

Response.Body = '''
{
"name": "webhook-triggers/aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa/attempts/11111111-1111-4111-8111-111111111111",
"trigger_id": "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa",
"attempt_id": "11111111-1111-4111-8111-111111111111",
"attempt_status": "ATTEMPT_STATUS_FAILED",
"error_message": "cannot create job: invalid cluster spec",
"resolved_commit_sha": "0123456789abcdef0123456789abcdef01234567",
"create_time": "2026-11-24T10:00:00Z",
"complete_time": "2026-11-24T10:02:00Z",
"dabs_result": {
"stage": "STAGE_RUN_BUNDLE_DEPLOY",
"run_id": 7012345678901234,
"job_id": 7012345678900000
}
}
'''
71 changes: 71 additions & 0 deletions cmd/bundle/autodeploy/attempts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package autodeploy

import (
"fmt"
"strings"

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

// Every status gets a color, so all of them carry the same escape bytes, which the list's column
// alignment counts as width.
const attemptStatusTemplate = `{{$s := replace .AttemptStatus "ATTEMPT_STATUS_" ""}}{{if eq $s "SUCCEEDED"}}{{green "%s" $s}}{{else if eq $s "FAILED"}}{{red "%s" $s}}{{else}}{{blue "%s" $s}}{{end}}`

const attemptTemplate = `Attempt ID: {{.AttemptID}}
Status: ` + attemptStatusTemplate + `
{{with .ResolvedCommitSha}}Commit: {{.}}
{{end}}Created: {{.CreateTime}}
{{with .CompleteTime}}Completed: {{.}}
{{end}}{{with .DabsResult}}{{with .Stage}}Stage: {{replace . "STAGE_" ""}}
{{end}}{{with .DeploymentVersion}}Version: {{.}}
{{end}}{{with .RunID}}Run ID: {{.}}
{{end}}{{end}}{{with .ErrorMessage}}Error: {{.}}
{{end}}`

func newAttemptsCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "attempts [ATTEMPT_ID]",
Short: "List the auto-deploy attempts of this bundle target, or show one",
Long: `List the auto-deploy attempts of this bundle target, newest first, or show one.

An attempt is one deploy of the target for one commit: the first deploy when
the trigger is created, or the deploy for a push to its branch.`,
Args: root.MaximumNArgs(1),
Annotations: map[string]string{
"headerTemplate": `{{header "Attempt ID"}} {{header "Status"}} {{header "Commit"}} {{header "Created"}}`,
"template": cmdio.Heredoc(`
{{range .}}{{.AttemptID}} ` + attemptStatusTemplate + ` {{printf "%.12s" .ResolvedCommitSha}} {{.CreateTime}}
{{end}}`),
},
}

cmd.RunE = func(cmd *cobra.Command, args []string) error {
t, api, err := loadTrigger(cmd)
if err != nil {
return err
}
ctx := cmd.Context()
// The ID becomes a path segment of the attempt's name, where . and .. would change the path.
if len(args) == 1 && (args[0] == "" || args[0] == "." || args[0] == ".." || strings.Contains(args[0], "/")) {
return fmt.Errorf("%q is not an attempt ID. Pass the attempt_id of an attempt, not its name", args[0])
}
found, err := findTrigger(ctx, api, t)

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] attempts finds the trigger through findTrigger before it lists or gets anything. After bundle auto-deploy delete, that lookup fails with "target dev has no auto-deploy trigger that you can view. To create one, run: … create". But delete's help (delete.go:19) says "The target's past attempts stay", and DeleteWebhookTrigger's doc says the same. Running create again makes a new trigger ID, so the old attempts stay out of reach. After a failed deploy and a delete, the user can't see why the deploy failed. Can attempts be looked up without a live trigger? If not, the delete help should say they can no longer be viewed from the CLI.

if err != nil {
return err
}

if len(args) == 0 {
return cmdio.RenderIterator(ctx, api.ListAutoDeployAttempts(ctx, gitwebhooks.ListAutoDeployAttemptsRequest{Parent: found.Name}))
}
attempt, err := api.GetAutoDeployAttempt(ctx, gitwebhooks.GetAutoDeployAttemptRequest{Name: found.Name + "/attempts/" + args[0]})

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] args[0] is appended to the name as is, and EncodeMultiSegmentPathParameter in GetAutoDeployAttempt keeps / as a path separator. So attempts "" sends GET .../webhook-triggers/T/attempts/, which is the list route with a trailing slash. And pasting the name that -o json prints (webhook-triggers/T/attempts/A) requests .../attempts/webhook-triggers/T/attempts/A, so the user gets a 404 for a path they never typed. Could we reject an empty ID or one containing / before building the name? Or accept the full name as well?

if err != nil {
return err
}
return cmdio.RenderWithTemplate(ctx, attempt, "", attemptTemplate)
}

return cmd
}
1 change: 1 addition & 0 deletions cmd/bundle/autodeploy/autodeploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ Example:
cmd.AddCommand(newGetCommand())
cmd.AddCommand(newListCommand())
cmd.AddCommand(newDeleteCommand())
cmd.AddCommand(newAttemptsCommand())
return cmd
}
33 changes: 33 additions & 0 deletions libs/gitwebhooks/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/databricks/cli/libs/auth"
"github.com/databricks/databricks-sdk-go"
databricksclient "github.com/databricks/databricks-sdk-go/client"
"github.com/databricks/databricks-sdk-go/httpclient"
"github.com/databricks/databricks-sdk-go/listing"
)

Expand Down Expand Up @@ -90,3 +91,35 @@ func withJSONBody(body []byte) func(*http.Request) error {
return nil
}
}

// ListAutoDeployAttempts lists the attempts of a trigger, newest first.
func (a *API) ListAutoDeployAttempts(ctx context.Context, req ListAutoDeployAttemptsRequest) listing.Iterator[AutoDeployAttempt] {
return listing.NewIterator(
&req,
a.listAutoDeployAttemptsPage,
func(resp *ListAutoDeployAttemptsResponse) []AutoDeployAttempt {
return resp.AutoDeployAttempts
},
func(resp *ListAutoDeployAttemptsResponse) *ListAutoDeployAttemptsRequest {
if resp.NextPageToken == "" {
return nil
}
req.PageToken = resp.NextPageToken
return &req
},
)
}

func (a *API) listAutoDeployAttemptsPage(ctx context.Context, req ListAutoDeployAttemptsRequest) (*ListAutoDeployAttemptsResponse, error) {
var resp ListAutoDeployAttemptsResponse
path := basePath + "/" + httpclient.EncodeMultiSegmentPathParameter(req.Parent) + "/attempts"
err := a.client.Do(ctx, http.MethodGet, path, auth.WorkspaceIDHeaders(a.client.Config), nil, req, &resp)
return &resp, err
}

func (a *API) GetAutoDeployAttempt(ctx context.Context, req GetAutoDeployAttemptRequest) (*AutoDeployAttempt, error) {
var resp AutoDeployAttempt
path := basePath + "/" + httpclient.EncodeMultiSegmentPathParameter(req.Name)
err := a.client.Do(ctx, http.MethodGet, path, auth.WorkspaceIDHeaders(a.client.Config), nil, nil, &resp)
return &resp, err
}
54 changes: 54 additions & 0 deletions libs/gitwebhooks/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,57 @@ type DeleteWebhookTrigger struct {
GitProvider GitProvider `json:"git_provider"`
TriggerConfig *DeploymentTriggerConfig `json:"trigger_config"`
}

// AutoDeployAttempt is one auto-deploy run of a trigger for one commit.
type AutoDeployAttempt struct {
// Name has the format webhook-triggers/{trigger_id}/attempts/{attempt_id}.
Name string `json:"name,omitempty"`
TriggerID string `json:"trigger_id,omitempty"`
AttemptID string `json:"attempt_id,omitempty"`

// AttemptStatus is one of ATTEMPT_STATUS_QUEUED, _RUNNING, _SUCCEEDED, _FAILED and _UNKNOWN.
AttemptStatus string `json:"attempt_status,omitempty"`

// ErrorMessage is set only for a failed attempt.
ErrorMessage string `json:"error_message,omitempty"`
ResolvedCommitSha string `json:"resolved_commit_sha,omitempty"`

// CreateTime and CompleteTime are RFC 3339 timestamps.
CreateTime string `json:"create_time,omitempty"`
CompleteTime string `json:"complete_time,omitempty"`

DeployAs *DeployAsIdentity `json:"deploy_as,omitempty"`
RepoURL string `json:"repo_url,omitempty"`
GitProvider GitProvider `json:"git_provider,omitempty"`
TriggerConfig *DeploymentTriggerConfig `json:"trigger_config,omitempty"`
DabsResult *DabsAttemptResult `json:"dabs_result,omitempty"`
}

type DabsAttemptResult struct {
// Stage is the last stage the attempt reached, such as STAGE_RUN_BUNDLE_DEPLOY. For a failed
// attempt, it is the stage that failed.
Stage string `json:"stage,omitempty"`

// DeploymentVersion has the format deployments/{deployment_id}/versions/{version_id}.
DeploymentVersion string `json:"deployment_version,omitempty"`

// RunID and JobID are the runner job run and the runner job that the attempt ran in.
RunID int64 `json:"run_id,omitempty"`
JobID int64 `json:"job_id,omitempty"`
}

type ListAutoDeployAttemptsRequest struct {
// Parent has the format webhook-triggers/{trigger_id}.
Parent string `json:"-" url:"-"`
PageToken string `json:"-" url:"page_token,omitempty"`
}

type ListAutoDeployAttemptsResponse struct {
AutoDeployAttempts []AutoDeployAttempt `json:"auto_deploy_attempts,omitempty"`
NextPageToken string `json:"next_page_token,omitempty"`
}

type GetAutoDeployAttemptRequest struct {
// Name has the format webhook-triggers/{trigger_id}/attempts/{attempt_id}.
Name string `json:"-" url:"-"`
}
Loading
Loading