useAutofixSetup.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import type {AutofixRepoDefinition} from 'sentry/components/events/autofix/types';
  2. import {
  3. type ApiQueryKey,
  4. useApiQuery,
  5. type UseApiQueryOptions,
  6. } from 'sentry/utils/queryClient';
  7. import type RequestError from 'sentry/utils/requestError/requestError';
  8. export interface AutofixSetupRepoDefinition extends AutofixRepoDefinition {
  9. ok: boolean;
  10. }
  11. export type AutofixSetupResponse = {
  12. genAIConsent: {
  13. ok: boolean;
  14. };
  15. integration: {
  16. ok: boolean;
  17. reason: string | null;
  18. };
  19. githubWriteIntegration?: {
  20. ok: boolean;
  21. repos: AutofixSetupRepoDefinition[];
  22. } | null;
  23. };
  24. export function makeAutofixSetupQueryKey(
  25. groupId: string,
  26. checkWriteAccess?: boolean
  27. ): ApiQueryKey {
  28. return [
  29. `/issues/${groupId}/autofix/setup/${checkWriteAccess ? '?check_write_access=true' : ''}`,
  30. ];
  31. }
  32. export function useAutofixSetup(
  33. {groupId, checkWriteAccess}: {groupId: string; checkWriteAccess?: boolean},
  34. options: Omit<UseApiQueryOptions<AutofixSetupResponse, RequestError>, 'staleTime'> = {}
  35. ) {
  36. const queryData = useApiQuery<AutofixSetupResponse>(
  37. makeAutofixSetupQueryKey(groupId, checkWriteAccess),
  38. {
  39. enabled: Boolean(groupId),
  40. staleTime: 0,
  41. retry: false,
  42. ...options,
  43. }
  44. );
  45. return {
  46. ...queryData,
  47. canStartAutofix: Boolean(
  48. queryData.data?.integration.ok && queryData.data?.genAIConsent.ok
  49. ),
  50. canCreatePullRequests: Boolean(queryData.data?.githubWriteIntegration?.ok),
  51. };
  52. }