Skip to content

Commit 700ce0b

Browse files
feat: expose per-ref diff statuses
1 parent 12de2af commit 700ce0b

13 files changed

Lines changed: 948 additions & 112 deletions

File tree

coderd/apidoc/docs.go

Lines changed: 66 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 66 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/db2sdk/db2sdk.go

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,6 +1839,17 @@ func decodeChatLastError(raw pqtype.NullRawMessage) *codersdk.ChatError {
18391839
// When files is non-empty the response includes file metadata;
18401840
// pass nil to omit the files field (e.g. list endpoints).
18411841
func Chat(c database.Chat, diffStatus *database.ChatDiffStatus, files []database.GetChatFileMetadataByChatIDRow) codersdk.Chat {
1842+
return ChatWithDiffStatuses(c, diffStatus, nil, files)
1843+
}
1844+
1845+
// ChatWithDiffStatuses converts a chat with its per-ref diff status
1846+
// list. diffStatus is the primary shown in the legacy diff_status field.
1847+
func ChatWithDiffStatuses(
1848+
c database.Chat,
1849+
diffStatus *database.ChatDiffStatus,
1850+
diffStatuses []database.ChatDiffStatus,
1851+
files []database.GetChatFileMetadataByChatIDRow,
1852+
) codersdk.Chat {
18421853
mcpServerIDs := c.MCPServerIDs
18431854
if mcpServerIDs == nil {
18441855
mcpServerIDs = []uuid.UUID{}
@@ -1913,6 +1924,12 @@ func Chat(c database.Chat, diffStatus *database.ChatDiffStatus, files []database
19131924
convertedDiffStatus := ChatDiffStatus(c.ID, diffStatus)
19141925
chat.DiffStatus = &convertedDiffStatus
19151926
}
1927+
if len(diffStatuses) > 0 {
1928+
chat.DiffStatuses = make([]codersdk.ChatDiffStatus, 0, len(diffStatuses))
1929+
for i := range diffStatuses {
1930+
chat.DiffStatuses = append(chat.DiffStatuses, ChatDiffStatus(c.ID, &diffStatuses[i]))
1931+
}
1932+
}
19161933
if len(files) > 0 {
19171934
chat.Files = make([]codersdk.ChatFileMetadata, 0, len(files))
19181935
for _, row := range files {
@@ -2071,13 +2088,13 @@ func ChatDebugRunDetail(r database.ChatDebugRun, steps []database.ChatDebugStep)
20712088
// is non-nil, children without an entry receive an empty DiffStatus.
20722089
func ChildChatRows(
20732090
children []database.GetChildChatsByParentIDsRow,
2074-
diffStatuses map[uuid.UUID]database.ChatDiffStatus,
2091+
diffStatuses map[uuid.UUID][]database.ChatDiffStatus,
20752092
) []codersdk.Chat {
20762093
result := make([]codersdk.Chat, len(children))
20772094
for i, row := range children {
2078-
diffStatus, ok := diffStatuses[row.Chat.ID]
2095+
statuses, ok := diffStatuses[row.Chat.ID]
20792096
if ok {
2080-
result[i] = Chat(row.Chat, &diffStatus, nil)
2097+
result[i] = ChatWithDiffStatuses(row.Chat, firstChatDiffStatus(statuses), statuses, nil)
20812098
} else {
20822099
result[i] = Chat(row.Chat, nil, nil)
20832100
if diffStatuses != nil {
@@ -2096,7 +2113,7 @@ func ChildChatRows(
20962113
func ChatRowsWithChildren(
20972114
roots []database.GetChatsRow,
20982115
children []database.GetChildChatsByParentIDsRow,
2099-
diffStatuses map[uuid.UUID]database.ChatDiffStatus,
2116+
diffStatuses map[uuid.UUID][]database.ChatDiffStatus,
21002117
) []codersdk.Chat {
21012118
// Group children by parent ID.
21022119
childrenByParent := make(map[uuid.UUID][]database.GetChildChatsByParentIDsRow, len(children))
@@ -2107,9 +2124,9 @@ func ChatRowsWithChildren(
21072124

21082125
result := make([]codersdk.Chat, len(roots))
21092126
for i, row := range roots {
2110-
diffStatus, ok := diffStatuses[row.Chat.ID]
2127+
statuses, ok := diffStatuses[row.Chat.ID]
21112128
if ok {
2112-
result[i] = Chat(row.Chat, &diffStatus, nil)
2129+
result[i] = ChatWithDiffStatuses(row.Chat, firstChatDiffStatus(statuses), statuses, nil)
21132130
} else {
21142131
result[i] = Chat(row.Chat, nil, nil)
21152132
if diffStatuses != nil {
@@ -2139,6 +2156,14 @@ func ChatDiffStatus(chatID uuid.UUID, status *database.ChatDiffStatus) codersdk.
21392156
}
21402157

21412158
result.ChatID = status.ChatID
2159+
if status.GitRemoteOrigin != "" {
2160+
remoteOrigin := status.GitRemoteOrigin
2161+
result.RemoteOrigin = &remoteOrigin
2162+
}
2163+
if status.GitBranch != "" {
2164+
gitBranch := status.GitBranch
2165+
result.GitBranch = &gitBranch
2166+
}
21422167
if status.Url.Valid {
21432168
u := strings.TrimSpace(status.Url.String)
21442169
if u != "" {
@@ -2210,6 +2235,15 @@ func ChatDiffStatus(chatID uuid.UUID, status *database.ChatDiffStatus) codersdk.
22102235
return result
22112236
}
22122237

2238+
// firstChatDiffStatus returns the primary status row of a chat. The
2239+
// status list is ordered newest first, so the first row is the primary.
2240+
func firstChatDiffStatus(statuses []database.ChatDiffStatus) *database.ChatDiffStatus {
2241+
if len(statuses) == 0 {
2242+
return nil
2243+
}
2244+
return &statuses[0]
2245+
}
2246+
22132247
// UserSecret converts a database ListUserSecretsRow (metadata only,
22142248
// no value) to an SDK UserSecret.
22152249
func UserSecret(secret database.ListUserSecretsRow) codersdk.UserSecret {

coderd/database/db2sdk/db2sdk_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,7 @@ func TestChat_AllFieldsPopulated(t *testing.T) {
889889
diffStatus := &database.ChatDiffStatus{
890890
ChatID: input.ID,
891891
}
892+
diffStatuses := []database.ChatDiffStatus{*diffStatus}
892893

893894
fileRows := []database.GetChatFileMetadataByChatIDRow{
894895
{
@@ -901,7 +902,7 @@ func TestChat_AllFieldsPopulated(t *testing.T) {
901902
},
902903
}
903904

904-
got := db2sdk.Chat(input, diffStatus, fileRows)
905+
got := db2sdk.ChatWithDiffStatuses(input, diffStatus, diffStatuses, fileRows)
905906

906907
require.Equal(t, &lastErrorPayload, got.LastError)
907908

0 commit comments

Comments
 (0)