Skip to content

Commit 3d40f96

Browse files
committed
first pass at organizing components for project creation
1 parent 7d27150 commit 3d40f96

11 files changed

+879
-597
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
import { useAuthProviders } from "../auth-providers/auth-provider-query";
10+
import { useCurrentUser } from "../../user-context";
11+
12+
export const useIsGithubAppEnabled = () => {
13+
return useQuery(["github-app-enabled"], async () => {
14+
return await getGitpodService().server.isGitHubAppEnabled();
15+
});
16+
};
17+
18+
export const useAreGithubWebhooksUnauthorized = (providerHost: string) => {
19+
const { data: authProviders } = useAuthProviders();
20+
const { data: isGitHubAppEnabled } = useIsGithubAppEnabled();
21+
const { data: token } = useGetGitToken(providerHost);
22+
23+
// If the app is enabled, authorized
24+
if (isGitHubAppEnabled) {
25+
return false;
26+
}
27+
28+
// If we don't have auth providers or the provider host, we can't check yet, treat as authorized
29+
if (!authProviders || !providerHost) {
30+
return false;
31+
}
32+
33+
// Find matching auth provider - if none, treat as authorized
34+
const ap = authProviders?.find((ap) => ap.host === providerHost);
35+
if (!ap || ap.authProviderType !== "GitHub") {
36+
return false;
37+
}
38+
39+
if (!token || !token.scopes.includes("repo")) {
40+
return true;
41+
}
42+
};
43+
44+
export const useGetGitToken = (providerHost: string) => {
45+
const user = useCurrentUser();
46+
47+
return useQuery(
48+
["git-token", { userId: user?.id }, { providerHost }],
49+
async () => {
50+
return await getGitpodService().server.getToken({ host: providerHost });
51+
},
52+
{
53+
enabled: !!user && !!providerHost,
54+
},
55+
);
56+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
import { useCurrentUser } from "../../user-context";
10+
11+
type UseProviderRepositoriesQueryArgs = {
12+
provider: string;
13+
installationId?: string;
14+
};
15+
export const useProviderRepositoriesForUser = ({ provider, installationId }: UseProviderRepositoriesQueryArgs) => {
16+
const user = useCurrentUser();
17+
18+
return useQuery(
19+
["provider-repositories", { userId: user?.id }, { provider, installationId }],
20+
async () => {
21+
return await getGitpodService().server.getProviderRepositoriesForUser({
22+
provider,
23+
hints: { installationId },
24+
});
25+
},
26+
{
27+
enabled: !!provider,
28+
},
29+
);
30+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 { useMutation } from "@tanstack/react-query";
8+
import { getGitpodService } from "../../service/service";
9+
import { useCurrentOrg } from "../organizations/orgs-query";
10+
import { useRefreshProjects } from "./list-projects-query";
11+
import { CreateProjectParams, Project } from "@gitpod/gitpod-protocol";
12+
13+
type CreateProjectArgs = Omit<CreateProjectParams, "teamId">;
14+
15+
export const useCreateProject = () => {
16+
const refreshProjects = useRefreshProjects();
17+
const { data: org } = useCurrentOrg();
18+
19+
return useMutation<Project, Error, CreateProjectArgs>(
20+
async ({ name, slug, cloneUrl, appInstallationId }) => {
21+
if (!org) {
22+
throw new Error("No org currently selected");
23+
}
24+
25+
return await getGitpodService().server.createProject({
26+
name,
27+
slug,
28+
cloneUrl,
29+
teamId: org.id,
30+
appInstallationId,
31+
});
32+
},
33+
{
34+
onSuccess: (project) => {
35+
if (org) {
36+
refreshProjects(org.id);
37+
}
38+
39+
// Kick off a prebuild for the new project
40+
getGitpodService().server.triggerPrebuild(project.id, null);
41+
},
42+
},
43+
);
44+
};

0 commit comments

Comments
 (0)