Skip to content

Commit a2b5b00

Browse files
authored
[server,dashboard] improve org default image input placeholder (#18746)
* [server,dashboard] improve org default image input placeholder * fix build failed * Use null instead of `NULL_TO_UNDEFINED` * fixup * fixup toast not shown
1 parent c7c1221 commit a2b5b00

File tree

9 files changed

+43
-20
lines changed

9 files changed

+43
-20
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
3+
* Licensed under the GNU Affero General Public License (AGPL).
4+
* See License.AGPL.txt in the project root for license information.
5+
*/
6+
7+
import { useQuery } from "@tanstack/react-query";
8+
import { getGitpodService } from "../../service/service";
9+
10+
export const useDefaultWorkspaceImageQuery = () => {
11+
return useQuery<string>({
12+
queryKey: ["default-workspace-image"],
13+
staleTime: 1000 * 60 * 10, // 10 minute
14+
queryFn: async () => {
15+
const image = await getGitpodService().server.getDefaultWorkspaceImage();
16+
return image;
17+
},
18+
});
19+
};

components/dashboard/src/teams/TeamSettings.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { gitpodHostUrl } from "../service/service";
2222
import { useCurrentUser } from "../user-context";
2323
import { OrgSettingsPage } from "./OrgSettingsPage";
2424
import { useToast } from "../components/toasts/Toasts";
25+
import { useDefaultWorkspaceImageQuery } from "../data/workspaces/default-workspace-image-query";
2526

2627
export default function TeamSettingsPage() {
2728
const user = useCurrentUser();
@@ -169,6 +170,7 @@ export default function TeamSettingsPage() {
169170
function OrgSettingsForm(props: { org?: OrganizationInfo }) {
170171
const { org } = props;
171172
const { data: settings, isLoading } = useOrgSettingsQuery();
173+
const { data: globalDefaultImage } = useDefaultWorkspaceImageQuery();
172174
const updateTeamSettings = useUpdateOrgSettingsMutation();
173175
const [defaultWorkspaceImage, setDefaultWorkspaceImage] = useState(settings?.defaultWorkspaceImage ?? "");
174176
const { toast } = useToast();
@@ -190,11 +192,10 @@ function OrgSettingsForm(props: { org?: OrganizationInfo }) {
190192
}
191193
try {
192194
await updateTeamSettings.mutateAsync({
193-
// We don't want to have original setting passed, since defaultWorkspaceImage could be undefined
194-
// to bring compatibility when we're going to change Gitpod install value's defaultImage setting
195+
...settings,
195196
...newSettings,
196197
});
197-
if (newSettings.defaultWorkspaceImage) {
198+
if (newSettings.defaultWorkspaceImage !== undefined) {
198199
toast("Default workspace image has been updated.");
199200
}
200201
} catch (error) {
@@ -206,7 +207,7 @@ function OrgSettingsForm(props: { org?: OrganizationInfo }) {
206207
);
207208
}
208209
},
209-
[updateTeamSettings, org?.id, org?.isOwner, toast],
210+
[updateTeamSettings, org?.id, org?.isOwner, settings, toast],
210211
);
211212

212213
return (
@@ -241,6 +242,7 @@ function OrgSettingsForm(props: { org?: OrganizationInfo }) {
241242
label="Default Image"
242243
// TODO: Provide document links
243244
hint="Use any official Gitpod Docker image, or Docker image reference"
245+
placeholder={globalDefaultImage}
244246
value={defaultWorkspaceImage}
245247
onChange={setDefaultWorkspaceImage}
246248
disabled={isLoading || !org?.isOwner}

components/gitpod-db/src/typeorm/entity/db-team-settings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class DBOrgSettings implements OrganizationSettings {
1919
workspaceSharingDisabled?: boolean;
2020

2121
@Column("varchar", { nullable: true })
22-
defaultWorkspaceImage?: string | null; // TODO: migration
22+
defaultWorkspaceImage?: string | null;
2323

2424
@Column()
2525
deleted: boolean;

components/gitpod-db/src/typeorm/team-db-impl.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -377,15 +377,13 @@ export class TeamDBImpl extends TransactionalDBImpl<TeamDB> implements TeamDB {
377377
public async setOrgSettings(orgId: string, settings: Partial<OrganizationSettings>): Promise<void> {
378378
const repo = await this.getOrgSettingsRepo();
379379
const team = await repo.findOne({ where: { orgId, deleted: false } });
380-
const update: Partial<OrganizationSettings> = {};
381-
if (settings.workspaceSharingDisabled != undefined) {
382-
update.workspaceSharingDisabled = settings.workspaceSharingDisabled;
383-
}
384-
// Set to null if defaultWorkspaceImage is empty string, so that we can fallback to default when getting org settings
385-
if (settings.defaultWorkspaceImage?.trim() === "") {
380+
const update: Partial<OrganizationSettings> = {
381+
defaultWorkspaceImage: settings.defaultWorkspaceImage,
382+
workspaceSharingDisabled: settings.workspaceSharingDisabled,
383+
};
384+
// Set to null if defaultWorkspaceImage is empty string, so that it can fallback to default image
385+
if (update.defaultWorkspaceImage?.trim() === "") {
386386
update.defaultWorkspaceImage = null;
387-
} else if (settings.defaultWorkspaceImage != undefined) {
388-
update.defaultWorkspaceImage = settings.defaultWorkspaceImage;
389387
}
390388
if (!team) {
391389
await repo.insert({

components/gitpod-protocol/go/gitpod-service.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,8 +2256,8 @@ type TeamMembershipInvite struct {
22562256
}
22572257

22582258
type OrganizationSettings struct {
2259-
WorkspaceSharingDisabled bool `json:"workspaceSharingDisabled,omitempty"`
2260-
DefaultWorkspaceImage *string `json:"defaultWorkspaceImage,omitempty"`
2259+
WorkspaceSharingDisabled bool `json:"workspaceSharingDisabled,omitempty"`
2260+
DefaultWorkspaceImage string `json:"defaultWorkspaceImage,omitempty"`
22612261
}
22622262

22632263
type Project struct {

components/gitpod-protocol/src/gitpod-service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ export interface GitpodServer extends JsonRpcServer<GitpodClient>, AdminServer,
173173
getOrgAuthProviders(params: GitpodServer.GetOrgAuthProviderParams): Promise<AuthProviderEntry[]>;
174174
deleteOrgAuthProvider(params: GitpodServer.DeleteOrgAuthProviderParams): Promise<void>;
175175

176+
getDefaultWorkspaceImage(): Promise<string>;
177+
176178
// Dedicated, Dedicated, Dedicated
177179
getOnboardingState(): Promise<GitpodServer.OnboardingState>;
178180

components/server/src/auth/rate-limiter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ const defaultFunctions: FunctionsConfig = {
111111
deleteTeam: { group: "default", points: 1 },
112112
getOrgSettings: { group: "default", points: 1 },
113113
updateOrgSettings: { group: "default", points: 1 },
114+
getDefaultWorkspaceImage: { group: "default", points: 1 },
114115
getProviderRepositoriesForUser: { group: "default", points: 1 },
115116
createProject: { group: "default", points: 1 },
116117
getTeamProjects: { group: "default", points: 1 },

components/server/src/orgs/organization-service.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { inject, injectable } from "inversify";
1919
import { Authorizer } from "../authorization/authorizer";
2020
import { ProjectsService } from "../projects/projects-service";
2121
import { TransactionalContext } from "@gitpod/gitpod-db/lib/typeorm/transactional-db-impl";
22-
import { Config } from "../config";
2322

2423
@injectable()
2524
export class OrganizationService {
@@ -29,7 +28,6 @@ export class OrganizationService {
2928
@inject(ProjectsService) private readonly projectsService: ProjectsService,
3029
@inject(Authorizer) private readonly auth: Authorizer,
3130
@inject(IAnalyticsWriter) private readonly analytics: IAnalyticsWriter,
32-
@inject(Config) protected readonly config: Config,
3331
) {}
3432

3533
async listOrganizations(
@@ -330,9 +328,6 @@ export class OrganizationService {
330328
async getSettings(userId: string, orgId: string): Promise<OrganizationSettings> {
331329
await this.auth.checkPermissionOnOrganization(userId, "read_settings", orgId);
332330
const settings = (await this.teamDB.findOrgSettings(orgId)) || {};
333-
if (!settings.defaultWorkspaceImage) {
334-
settings.defaultWorkspaceImage = this.config.workspaceDefaults.workspaceImage;
335-
}
336331
return settings;
337332
}
338333

components/server/src/workspace/gitpod-server-impl.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2497,6 +2497,12 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
24972497
return this.organizationService.updateSettings(user.id, orgId, settings);
24982498
}
24992499

2500+
async getDefaultWorkspaceImage(ctx: TraceContextWithSpan): Promise<string> {
2501+
const userId = this.userID;
2502+
traceAPIParams(ctx, { userId });
2503+
return this.config.workspaceDefaults.workspaceImage;
2504+
}
2505+
25002506
public async getTeamProjects(ctx: TraceContext, teamId: string): Promise<Project[]> {
25012507
traceAPIParams(ctx, { teamId });
25022508

0 commit comments

Comments
 (0)