@@ -22,7 +22,6 @@ import (
22
22
"cdr.dev/slog"
23
23
"github.com/coder/coder/v2/coderd/audit"
24
24
"github.com/coder/coder/v2/coderd/database"
25
- "github.com/coder/coder/v2/coderd/database/dbauthz"
26
25
"github.com/coder/coder/v2/coderd/httpapi"
27
26
"github.com/coder/coder/v2/coderd/httpapi/httperror"
28
27
"github.com/coder/coder/v2/coderd/httpmw"
@@ -725,25 +724,21 @@ func (api *API) authAndDoWithTaskSidebarAppClient(
725
724
Message :"Task is not configured with a sidebar app." ,
726
725
})
727
726
}
728
- sidebarAppID := * build .AITaskSidebarAppID
729
727
730
728
// Find the sidebar app details to get the URL and validate app health.
731
- agentIDs := make ([]uuid.UUID ,0 ,len (build .Resources ))
732
- for _ ,res := range build .Resources {
733
- for _ ,agent := range res .Agents {
734
- agentIDs = append (agentIDs ,agent .ID )
729
+ sidebarAppID := * build .AITaskSidebarAppID
730
+ agentID ,sidebarApp ,ok := func () (uuid.UUID , codersdk.WorkspaceApp ,bool ) {
731
+ for _ ,res := range build .Resources {
732
+ for _ ,agent := range res .Agents {
733
+ for _ ,app := range agent .Apps {
734
+ if app .ID == sidebarAppID {
735
+ return agent .ID ,app ,true
736
+ }
737
+ }
738
+ }
735
739
}
736
- }
737
- // TODO(mafredri): Can we avoid dbauthz.AsSystemRestricted(ctx)?
738
- //nolint:gocritic // GetWorkspaceAppsByAgentIDs requires system restricted context.
739
- apps ,err := api .Database .GetWorkspaceAppsByAgentIDs (dbauthz .AsSystemRestricted (ctx ),agentIDs )
740
- if err != nil && ! errors .Is (err ,sql .ErrNoRows ) {
741
- return httperror .NewResponseError (http .StatusInternalServerError , codersdk.Response {
742
- Message :"Internal error fetching workspace apps." ,
743
- Detail :err .Error (),
744
- })
745
- }
746
- sidebarApp ,ok := slice .Find (apps ,func (app database.WorkspaceApp )bool {return app .ID == sidebarAppID })
740
+ return uuid .Nil , codersdk.WorkspaceApp {},false
741
+ }()
747
742
if ! ok {
748
743
return httperror .NewResponseError (http .StatusBadRequest , codersdk.Response {
749
744
Message :"Task sidebar app not found in latest build." ,
@@ -753,25 +748,25 @@ func (api *API) authAndDoWithTaskSidebarAppClient(
753
748
// Return an informative error if the app isn't healthy rather than trying
754
749
// and failing.
755
750
switch sidebarApp .Health {
756
- case database .WorkspaceAppHealthDisabled :
751
+ case codersdk .WorkspaceAppHealthDisabled :
757
752
// No health check, pass through.
758
- case database .WorkspaceAppHealthInitializing :
753
+ case codersdk .WorkspaceAppHealthInitializing :
759
754
return httperror .NewResponseError (http .StatusServiceUnavailable , codersdk.Response {
760
755
Message :"Task sidebar app is initializing. Try again shortly." ,
761
756
})
762
- case database .WorkspaceAppHealthUnhealthy :
757
+ case codersdk .WorkspaceAppHealthUnhealthy :
763
758
return httperror .NewResponseError (http .StatusServiceUnavailable , codersdk.Response {
764
759
Message :"Task sidebar app is unhealthy." ,
765
760
})
766
761
}
767
762
768
763
// Build the direct app URL and dial the agent.
769
- if ! sidebarApp .Url . Valid || sidebarApp . Url . String == "" {
764
+ if sidebarApp .URL == "" {
770
765
return httperror .NewResponseError (http .StatusInternalServerError , codersdk.Response {
771
766
Message :"Task sidebar app URL is not configured." ,
772
767
})
773
768
}
774
- parsedURL ,err := url .Parse (sidebarApp .Url . String )
769
+ parsedURL ,err := url .Parse (sidebarApp .URL )
775
770
if err != nil {
776
771
return httperror .NewResponseError (http .StatusInternalServerError , codersdk.Response {
777
772
Message :"Internal error parsing task app URL." ,
@@ -786,7 +781,7 @@ func (api *API) authAndDoWithTaskSidebarAppClient(
786
781
787
782
dialCtx ,dialCancel := context .WithTimeout (ctx ,time .Second * 30 )
788
783
defer dialCancel ()
789
- agentConn ,release ,err := api .agentProvider .AgentConn (dialCtx ,sidebarApp . AgentID )
784
+ agentConn ,release ,err := api .agentProvider .AgentConn (dialCtx ,agentID )
790
785
if err != nil {
791
786
return httperror .NewResponseError (http .StatusBadGateway , codersdk.Response {
792
787
Message :"Failed to reach task app endpoint." ,