Skip to content

[dashboard] Use public-api to CreateTeams #14232

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

Merged
merged 1 commit into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions components/dashboard/BUILD.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ packages:
- scripts/run-integration-tests.sh
deps:
- components/gitpod-protocol:lib
- components/public-api/typescript:lib
config:
commands:
build: ["yarn", "build"]
Expand Down
2 changes: 2 additions & 0 deletions components/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"version": "0.0.0",
"private": true,
"dependencies": {
"@bufbuild/connect-web": "^0.2.1",
"@gitpod/gitpod-protocol": "0.1.5",
"@gitpod/public-api": "0.1.5",
"@stripe/react-stripe-js": "^1.7.2",
"@stripe/stripe-js": "^1.29.0",
"configcat-js": "^6.0.0",
Expand Down
11 changes: 10 additions & 1 deletion components/dashboard/src/contexts/FeatureFlagContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ const FeatureFlagContext = createContext<{
showPersistentVolumeClaimUI: boolean;
showUsageView: boolean;
showUseLastSuccessfulPrebuild: boolean;
usePublicApiTeamsService: boolean;
}>({
showPersistentVolumeClaimUI: false,
showUsageView: false,
showUseLastSuccessfulPrebuild: false,
usePublicApiTeamsService: false,
});

const FeatureFlagContextProvider: React.FC = ({ children }) => {
Expand All @@ -34,6 +36,7 @@ const FeatureFlagContextProvider: React.FC = ({ children }) => {
const [showPersistentVolumeClaimUI, setShowPersistentVolumeClaimUI] = useState<boolean>(false);
const [showUsageView, setShowUsageView] = useState<boolean>(false);
const [showUseLastSuccessfulPrebuild, setShowUseLastSuccessfulPrebuild] = useState<boolean>(false);
const [usePublicApiTeamsService, setUsePublicApiTeamsService] = useState<boolean>(false);

useEffect(() => {
if (!user) return;
Expand All @@ -42,6 +45,7 @@ const FeatureFlagContextProvider: React.FC = ({ children }) => {
persistent_volume_claim: { defaultValue: true, setter: setShowPersistentVolumeClaimUI },
usage_view: { defaultValue: false, setter: setShowUsageView },
showUseLastSuccessfulPrebuild: { defaultValue: false, setter: setShowUseLastSuccessfulPrebuild },
usePublicApiTeamsService: { defaultValue: false, setter: setUsePublicApiTeamsService },
};
for (const [flagName, config] of Object.entries(featureFlags)) {
if (teams) {
Expand Down Expand Up @@ -74,7 +78,12 @@ const FeatureFlagContextProvider: React.FC = ({ children }) => {

return (
<FeatureFlagContext.Provider
value={{ showPersistentVolumeClaimUI, showUsageView, showUseLastSuccessfulPrebuild }}
value={{
showPersistentVolumeClaimUI,
showUsageView,
showUseLastSuccessfulPrebuild,
usePublicApiTeamsService,
}}
>
{children}
</FeatureFlagContext.Provider>
Expand Down
27 changes: 27 additions & 0 deletions components/dashboard/src/projects/NewProject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { trackEvent } from "../Analytics";
import exclamation from "../images/exclamation.svg";
import ErrorMessage from "../components/ErrorMessage";
import Spinner from "../icons/Spinner.svg";
import { teamsService } from "../service/public-api";
import { FeatureFlagContext } from "../contexts/FeatureFlagContext";

export default function NewProject() {
const location = useLocation();
Expand Down Expand Up @@ -721,6 +723,7 @@ function GitProviders(props: {

function NewTeam(props: { onSuccess: (team: Team) => void }) {
const { setTeams } = useContext(TeamsContext);
const { usePublicApiTeamsService } = useContext(FeatureFlagContext);

const [teamName, setTeamName] = useState<string | undefined>();
const [error, setError] = useState<string | undefined>();
Expand All @@ -729,6 +732,30 @@ function NewTeam(props: { onSuccess: (team: Team) => void }) {
if (!teamName) {
return;
}

if (usePublicApiTeamsService) {
try {
const response = await teamsService.createTeam({
name: teamName,
});
const team = response.team;
setTeams(await getGitpodService().server.getTeams());

const mappedTeam: Team = {
id: team?.id || "",
name: team?.name || "",
slug: team?.slug || "",
creationTime: "",
};
props.onSuccess(mappedTeam);
return;
} catch (error) {
console.error(error);
setError(error?.message || "Failed to create new team!");
return;
}
}

try {
const team = await getGitpodService().server.createTeam(teamName);
setTeams(await getGitpodService().server.getTeams());
Expand Down
43 changes: 43 additions & 0 deletions components/dashboard/src/service/public-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License-AGPL.txt in the project root for license information.
*/

import { createConnectTransport, createPromiseClient, Interceptor } from "@bufbuild/connect-web";

// Import service definition that you want to connect to.
import { TeamsService } from "@gitpod/public-api/lib/gitpod/experimental/v1/teams_connectweb";
import { getGitpodService } from "./service";

let token: string | undefined;

const authInterceptor: Interceptor = (next) => async (req) => {
if (!token) {
const newToken = await getGitpodService().server.generateNewGitpodToken({
type: 1,
scopes: [
"function:getGitpodTokenScopes",
"function:getWorkspace",
"function:getWorkspaces",
"function:createTeam",
"function:joinTeam",
"function:getTeamMembers",
"function:getGenericInvite",
"function:listenForWorkspaceInstanceUpdates",
"resource:default",
Comment on lines +17 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it necessary to generate a new token here? Won't the user always have a suitable token when performing this action?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They have a cookie based session which is not compatible with the AccessToken based auth on Public API. I agree this is not great, or pretty, but it's a temporary solution until we have first class Access Tokens.

],
});
token = newToken;
}

req.header.set("Authorization", `Bearer ${token}`);
return await next(req);
};

const transport = createConnectTransport({
baseUrl: `${window.location.protocol}//api.${window.location.host}`,
interceptors: [authInterceptor],
});

export const teamsService = createPromiseClient(TeamsService, transport);
20 changes: 20 additions & 0 deletions components/dashboard/src/teams/NewTeam.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,35 @@ import { FormEvent, useContext, useEffect, useState } from "react";
import { useHistory } from "react-router-dom";
import { getGitpodService } from "../service/service";
import { TeamsContext } from "./teams-context";
import { teamsService } from "../service/public-api";
import { FeatureFlagContext } from "../contexts/FeatureFlagContext";

export default function () {
const { setTeams } = useContext(TeamsContext);
const { usePublicApiTeamsService } = useContext(FeatureFlagContext);

const history = useHistory();

const [creationError, setCreationError] = useState<Error>();
let name = "";
const createTeam = async (event: FormEvent) => {
event.preventDefault();

if (usePublicApiTeamsService) {
try {
const response = await teamsService.createTeam({ name });
const team = response.team;
setTeams(await getGitpodService().server.getTeams());

history.push(`/t/${team!.slug}`);
return;
} catch (error) {
console.error(error);
setCreationError(error);
return;
}
}

try {
const team = await getGitpodService().server.createTeam(name);
const teams = await getGitpodService().server.getTeams();
Expand Down