-
Notifications
You must be signed in to change notification settings - Fork 881
refactor: generate application URL on backend side #9618
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
f80df13
to
41459dd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: this isn't technically a refactor, as we are changing the external-facing logic here.
Looks good to me, this particular endpoint does not appear to be called very often (I can only see it referenced by a few CLI commands), so the additional DB queries should not be an issue.
|
||
resource, err := api.Database.GetWorkspaceResourceByID(ctx, workspaceAgent.ResourceID) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Internal error fetching workspace resource.", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
build, err := api.Database.GetWorkspaceBuildByJobID(ctx, resource.JobID) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Internal error fetching workspace build.", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
workspace, err := api.Database.GetWorkspaceByID(ctx, build.WorkspaceID) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Internal error fetching workspace.", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
owner, err := api.Database.GetUserByID(ctx, workspace.OwnerID) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Internal error fetching workspace owner.", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential follow-up: would it make sense to modify GetWorkspaceAppsByAgentID
to return the username and workspace name as well? Although this endpoint does not appear to be used by the UI at all, only by the CLI and unit tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had that in mind as potential optimization, but as long it is not used, and could be deprecated, I will focus on deprecation instead 👍
func convertApps(dbApps []database.WorkspaceApp) []codersdk.WorkspaceApp { | ||
// convertProvisionedApps converts applications that are in the middle of provisioning process. | ||
// It means that they may not have an agent or workspace assigned (dry-run job). | ||
func convertProvisionedApps(dbApps []database.WorkspaceApp) []codersdk.WorkspaceApp { | ||
return convertApps(dbApps, database.WorkspaceAgent{}, database.User{}, database.Workspace{}) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: why have this extra function at all, you could simply just call convertApps()
with the empty structs instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true, I just personally find it "dirty" to call functions with multiple nil, nil, nil, {}, {}, {}, or "", "", "". 😅
Related: #8145
This PR delegates application URL generation from the site to the backend. Once it is merged, the generation will be performed in one place.
Note: The last step of the issue will adjust the URL generation routine to prevent the bug of generating a too long subdomain.