|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/databricks/cli/libs/dbr" |
| 8 | + "github.com/databricks/cli/libs/testserver" |
| 9 | + "github.com/databricks/databricks-sdk-go" |
| 10 | + "github.com/databricks/databricks-sdk-go/service/workspace" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + // Bundle root passed to FetchRepositoryInfo: a subdirectory of the git folder. |
| 17 | + testBundleRoot = "/Workspace/Users/test/bundle-examples/dabs_in_ws_bundle" |
| 18 | + // Git folder path as get-status returns it (without the /Workspace prefix). |
| 19 | + testGitFolderRaw = "/Users/test/bundle-examples" |
| 20 | + // Expected worktree root after ensureWorkspacePrefix is applied. |
| 21 | + testWorktreeRoot = "/Workspace/Users/test/bundle-examples" |
| 22 | + testRepoID = int64(2884540697170475) |
| 23 | + testOriginURL = "https://github.com/databricks/bundle-examples.git" |
| 24 | +) |
| 25 | + |
| 26 | +func newTestWorkspaceClient(t *testing.T, server *testserver.Server) *databricks.WorkspaceClient { |
| 27 | + t.Helper() |
| 28 | + w, err := databricks.NewWorkspaceClient(&databricks.Config{ |
| 29 | + Host: server.URL, |
| 30 | + Token: "testtoken", |
| 31 | + }) |
| 32 | + require.NoError(t, err) |
| 33 | + return w |
| 34 | +} |
| 35 | + |
| 36 | +// runtimeContext forces the in-workspace API branch of FetchRepositoryInfo |
| 37 | +// without needing a real /databricks directory on the test host. |
| 38 | +func runtimeContext(t *testing.T) context.Context { |
| 39 | + return dbr.MockRuntime(t.Context(), dbr.Environment{IsDbr: true, Version: "15.4"}) |
| 40 | +} |
| 41 | + |
| 42 | +// New workspace git folders return only id+path from get-status; the missing |
| 43 | +// branch/commit/url are recovered from the Repos API by id. |
| 44 | +func TestFetchRepositoryInfoNewGitFolderFallsBackToReposAPI(t *testing.T) { |
| 45 | + server := testserver.New(t) |
| 46 | + server.Handle("GET", "/api/2.0/workspace/get-status", func(_ testserver.Request) any { |
| 47 | + return testserver.Response{Body: map[string]any{ |
| 48 | + "git_info": map[string]any{ |
| 49 | + "id": testRepoID, |
| 50 | + "path": testGitFolderRaw, |
| 51 | + }, |
| 52 | + }} |
| 53 | + }) |
| 54 | + server.Handle("GET", "/api/2.0/repos/{repo_id}", func(_ testserver.Request) any { |
| 55 | + return testserver.Response{Body: workspace.GetRepoResponse{ |
| 56 | + Id: testRepoID, |
| 57 | + Branch: "main", |
| 58 | + HeadCommitId: "d53214abc", |
| 59 | + Url: testOriginURL, |
| 60 | + Provider: "gitHub", |
| 61 | + Path: testGitFolderRaw, |
| 62 | + }} |
| 63 | + }) |
| 64 | + |
| 65 | + info, err := FetchRepositoryInfo(runtimeContext(t), testBundleRoot, newTestWorkspaceClient(t, server)) |
| 66 | + require.NoError(t, err) |
| 67 | + assert.Equal(t, "main", info.CurrentBranch) |
| 68 | + assert.Equal(t, "d53214abc", info.LatestCommit) |
| 69 | + assert.Equal(t, testOriginURL, info.OriginURL) |
| 70 | + assert.Equal(t, testWorktreeRoot, info.WorktreeRoot) |
| 71 | +} |
| 72 | + |
| 73 | +// Classic Repos return full git info inline from get-status, so the Repos API is |
| 74 | +// not called. |
| 75 | +func TestFetchRepositoryInfoClassicRepoSkipsReposAPI(t *testing.T) { |
| 76 | + server := testserver.New(t) |
| 77 | + server.Handle("GET", "/api/2.0/workspace/get-status", func(_ testserver.Request) any { |
| 78 | + return testserver.Response{Body: map[string]any{ |
| 79 | + "git_info": map[string]any{ |
| 80 | + "id": testRepoID, |
| 81 | + "path": testGitFolderRaw, |
| 82 | + "branch": "main", |
| 83 | + "head_commit_id": "abc123", |
| 84 | + "url": testOriginURL, |
| 85 | + }, |
| 86 | + }} |
| 87 | + }) |
| 88 | + server.Handle("GET", "/api/2.0/repos/{repo_id}", func(_ testserver.Request) any { |
| 89 | + t.Error("Repos API must not be called when get-status returns the URL inline") |
| 90 | + return testserver.Response{StatusCode: 500} |
| 91 | + }) |
| 92 | + |
| 93 | + info, err := FetchRepositoryInfo(runtimeContext(t), testBundleRoot, newTestWorkspaceClient(t, server)) |
| 94 | + require.NoError(t, err) |
| 95 | + assert.Equal(t, "main", info.CurrentBranch) |
| 96 | + assert.Equal(t, "abc123", info.LatestCommit) |
| 97 | + assert.Equal(t, testOriginURL, info.OriginURL) |
| 98 | + assert.Equal(t, testWorktreeRoot, info.WorktreeRoot) |
| 99 | +} |
| 100 | + |
| 101 | +// A failed Repos lookup must not fail the deploy: the worktree root stays set and |
| 102 | +// the provenance fields stay empty, with no error. |
| 103 | +func TestFetchRepositoryInfoReposLookupFailureDegradesGracefully(t *testing.T) { |
| 104 | + server := testserver.New(t) |
| 105 | + server.Handle("GET", "/api/2.0/workspace/get-status", func(_ testserver.Request) any { |
| 106 | + return testserver.Response{Body: map[string]any{ |
| 107 | + "git_info": map[string]any{ |
| 108 | + "id": testRepoID, |
| 109 | + "path": testGitFolderRaw, |
| 110 | + }, |
| 111 | + }} |
| 112 | + }) |
| 113 | + server.Handle("GET", "/api/2.0/repos/{repo_id}", func(_ testserver.Request) any { |
| 114 | + return testserver.Response{StatusCode: 404, Body: map[string]string{"message": "not found"}} |
| 115 | + }) |
| 116 | + |
| 117 | + info, err := FetchRepositoryInfo(runtimeContext(t), testBundleRoot, newTestWorkspaceClient(t, server)) |
| 118 | + require.NoError(t, err) |
| 119 | + assert.Empty(t, info.CurrentBranch) |
| 120 | + assert.Empty(t, info.LatestCommit) |
| 121 | + assert.Empty(t, info.OriginURL) |
| 122 | + assert.Equal(t, testWorktreeRoot, info.WorktreeRoot) |
| 123 | +} |
0 commit comments