Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/repositories/detail/prebuilds/EnablePrebuildsError.tsx
2506 views
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 { FC, useCallback } from "react";
8
import type { RepositoryUnauthorizedError } from "@gitpod/public-api/lib/gitpod/v1/error_pb";
9
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
10
import { openAuthorizeWindow } from "../../../provider-utils";
11
import { useToast } from "../../../components/toasts/Toasts";
12
import { Button } from "@podkit/buttons/Button";
13
import { ConfigurationSettingsField } from "../ConfigurationSettingsField";
14
import { AlertTriangleIcon } from "lucide-react";
15
16
type Props = {
17
error: Error;
18
onReconnect: () => void;
19
};
20
export const EnablePrebuildsError: FC<Props> = ({ error, onReconnect }) => {
21
// Handle RepositoryUnauthorizedError
22
// We need to authorize with the provider to acquire the correct scopes to install webhooks
23
if (error instanceof ApplicationError && error.code === ErrorCodes.NOT_AUTHENTICATED) {
24
return (
25
<RepositoryUnauthorizedErrorMessage
26
error={error.data as RepositoryUnauthorizedError}
27
onReconnect={onReconnect}
28
/>
29
);
30
}
31
32
// Otherwise we just show a generic error message as we can't solve it via reconnecting w/ provider
33
return <GenericErrorMessage message={error.message} />;
34
};
35
36
// Error cases
37
// 1. Un-specific error (we can't initiate a reconnect flow, it may not help either)
38
39
// Failed to install webhooks while enabling prebuilds
40
// 2. User hasn't connected w/ the git provider
41
// 3. User has connected w/ the git provider but we don't have the correct scopes
42
// 4. User has connected w/ git provider, we have the correct scopes, but we failed to install the webhooks
43
// - This could be because the user doesn't have admin/write permissions on the repo or org
44
// - This could be because our token is invalid / was revoked
45
// - We can provide a link to the gitpod oauth app settings for them to have an admin approve it
46
47
type GenericErrorMessageProps = {
48
message: string;
49
};
50
const GenericErrorMessage: FC<GenericErrorMessageProps> = ({ message }) => {
51
return (
52
<ConfigurationSettingsField className="text-pk-content-danger">
53
<div className="flex flex-row gap-2 mb-4">
54
<span className="w-6">
55
<AlertTriangleIcon />
56
</span>
57
<span>
58
Unable to enable prebuilds. Please try again later. If the problem persists, please contact support.
59
</span>
60
</div>
61
{message && <pre className="text-sm text-pk-content-secondary">{`> ${message}`}</pre>}
62
</ConfigurationSettingsField>
63
);
64
};
65
66
type RepositoryUnauthorizedErrorMessageProps = {
67
error: RepositoryUnauthorizedError;
68
onReconnect: () => void;
69
};
70
const RepositoryUnauthorizedErrorMessage: FC<RepositoryUnauthorizedErrorMessageProps> = ({ error, onReconnect }) => {
71
const { toast } = useToast();
72
73
const authorizeWithProvider = useCallback(async () => {
74
await openAuthorizeWindow({
75
host: error.host,
76
scopes: error.requiredScopes,
77
onSuccess: async () => {
78
onReconnect();
79
},
80
onError: (payload) => {
81
let errorMessage: string;
82
if (typeof payload === "string") {
83
errorMessage = payload;
84
} else {
85
errorMessage = payload.description ? payload.description : `Error: ${payload.error}`;
86
}
87
88
// TODO: don't use toast, update error message inline
89
toast(errorMessage || `Oh no, there was a problem with ${error.host}.`);
90
},
91
});
92
}, [error.host, error.requiredScopes, onReconnect, toast]);
93
94
const definitelyNeedsReconnect = !error.providerIsConnected || error.isMissingScopes;
95
96
return (
97
<ConfigurationSettingsField className="text-pk-content-danger">
98
<div className="flex flex-row gap-2">
99
<span className="w-6">
100
<AlertTriangleIcon />
101
</span>
102
{definitelyNeedsReconnect ? (
103
<span>
104
It looks like your need to reconnect with your git provider (<strong>{error.host}</strong>).
105
Please reconnect and try again.
106
</span>
107
) : (
108
<span>
109
Unable to enable prebuilds. This could be because you don’t have admin/write permissions for
110
this repo or it could be an invalid token. Please try to reconnect. If the problem persists, you
111
can contact support.
112
</span>
113
)}
114
</div>
115
116
<Button className="mt-4" onClick={authorizeWithProvider}>
117
{error.providerIsConnected ? "Reconnect" : "Connect"}
118
</Button>
119
</ConfigurationSettingsField>
120
);
121
};
122
123