Path: blob/main/components/dashboard/src/repositories/detail/prebuilds/EnablePrebuildsError.tsx
2506 views
/**1* Copyright (c) 2023 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import { FC, useCallback } from "react";7import type { RepositoryUnauthorizedError } from "@gitpod/public-api/lib/gitpod/v1/error_pb";8import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";9import { openAuthorizeWindow } from "../../../provider-utils";10import { useToast } from "../../../components/toasts/Toasts";11import { Button } from "@podkit/buttons/Button";12import { ConfigurationSettingsField } from "../ConfigurationSettingsField";13import { AlertTriangleIcon } from "lucide-react";1415type Props = {16error: Error;17onReconnect: () => void;18};19export const EnablePrebuildsError: FC<Props> = ({ error, onReconnect }) => {20// Handle RepositoryUnauthorizedError21// We need to authorize with the provider to acquire the correct scopes to install webhooks22if (error instanceof ApplicationError && error.code === ErrorCodes.NOT_AUTHENTICATED) {23return (24<RepositoryUnauthorizedErrorMessage25error={error.data as RepositoryUnauthorizedError}26onReconnect={onReconnect}27/>28);29}3031// Otherwise we just show a generic error message as we can't solve it via reconnecting w/ provider32return <GenericErrorMessage message={error.message} />;33};3435// Error cases36// 1. Un-specific error (we can't initiate a reconnect flow, it may not help either)3738// Failed to install webhooks while enabling prebuilds39// 2. User hasn't connected w/ the git provider40// 3. User has connected w/ the git provider but we don't have the correct scopes41// 4. User has connected w/ git provider, we have the correct scopes, but we failed to install the webhooks42// - This could be because the user doesn't have admin/write permissions on the repo or org43// - This could be because our token is invalid / was revoked44// - We can provide a link to the gitpod oauth app settings for them to have an admin approve it4546type GenericErrorMessageProps = {47message: string;48};49const GenericErrorMessage: FC<GenericErrorMessageProps> = ({ message }) => {50return (51<ConfigurationSettingsField className="text-pk-content-danger">52<div className="flex flex-row gap-2 mb-4">53<span className="w-6">54<AlertTriangleIcon />55</span>56<span>57Unable to enable prebuilds. Please try again later. If the problem persists, please contact support.58</span>59</div>60{message && <pre className="text-sm text-pk-content-secondary">{`> ${message}`}</pre>}61</ConfigurationSettingsField>62);63};6465type RepositoryUnauthorizedErrorMessageProps = {66error: RepositoryUnauthorizedError;67onReconnect: () => void;68};69const RepositoryUnauthorizedErrorMessage: FC<RepositoryUnauthorizedErrorMessageProps> = ({ error, onReconnect }) => {70const { toast } = useToast();7172const authorizeWithProvider = useCallback(async () => {73await openAuthorizeWindow({74host: error.host,75scopes: error.requiredScopes,76onSuccess: async () => {77onReconnect();78},79onError: (payload) => {80let errorMessage: string;81if (typeof payload === "string") {82errorMessage = payload;83} else {84errorMessage = payload.description ? payload.description : `Error: ${payload.error}`;85}8687// TODO: don't use toast, update error message inline88toast(errorMessage || `Oh no, there was a problem with ${error.host}.`);89},90});91}, [error.host, error.requiredScopes, onReconnect, toast]);9293const definitelyNeedsReconnect = !error.providerIsConnected || error.isMissingScopes;9495return (96<ConfigurationSettingsField className="text-pk-content-danger">97<div className="flex flex-row gap-2">98<span className="w-6">99<AlertTriangleIcon />100</span>101{definitelyNeedsReconnect ? (102<span>103It looks like your need to reconnect with your git provider (<strong>{error.host}</strong>).104Please reconnect and try again.105</span>106) : (107<span>108Unable to enable prebuilds. This could be because you don’t have admin/write permissions for109this repo or it could be an invalid token. Please try to reconnect. If the problem persists, you110can contact support.111</span>112)}113</div>114115<Button className="mt-4" onClick={authorizeWithProvider}>116{error.providerIsConnected ? "Reconnect" : "Connect"}117</Button>118</ConfigurationSettingsField>119);120};121122123