Browse Source

fix(api): don't rely on list tokens exposing value (#64329)

We are relying on the list endpoint to be exposing the API tokens, but
this is not the direction we want to be going in. If the feature
requires the token value for easy copy/paste of the snippet, use the
generation feature to create a new token instead of forcing the list to
constantly show the value.



https://github.com/getsentry/sentry/assets/5581484/d1e1dbb0-0a23-45d1-ad02-c714333edb35



Resolves: https://github.com/getsentry/team-enterprise/issues/21
Yash Kamothi 1 year ago
parent
commit
da1d5627e2
1 changed files with 13 additions and 12 deletions
  1. 13 12
      static/app/views/releases/list/releasesPromo.tsx

+ 13 - 12
static/app/views/releases/list/releasesPromo.tsx

@@ -21,7 +21,12 @@ import {Tooltip} from 'sentry/components/tooltip';
 import {IconAdd} from 'sentry/icons';
 import {t} from 'sentry/locale';
 import {space} from 'sentry/styles/space';
-import type {Organization, Project, SentryApp} from 'sentry/types';
+import type {
+  NewInternalAppApiToken,
+  Organization,
+  Project,
+  SentryApp,
+} from 'sentry/types';
 import {trackAnalytics} from 'sentry/utils/analytics';
 import useApi from 'sentry/utils/useApi';
 import useApiRequests from 'sentry/utils/useApiRequests';
@@ -102,7 +107,7 @@ const ReleasesPromo = ({organization, project}: Props) => {
     ],
   });
   const api = useApi();
-  const [token, setToken] = useState(null);
+  const [token, setToken] = useState<string | null>(null);
   const [integrations, setIntegrations] = useState<SentryApp[]>([]);
   const [selectedItem, selectItem] = useState<Pick<Item, 'label' | 'value'> | null>(null);
 
@@ -144,17 +149,13 @@ const ReleasesPromo = ({organization, project}: Props) => {
     });
   }, [organization, project.id]);
 
-  const fetchToken = async sentryAppSlug => {
-    const tokens = await api.requestPromise(`/sentry-apps/${sentryAppSlug}/api-tokens/`);
-    if (!tokens.length) {
-      const newToken = await generateToken(sentryAppSlug);
-      return setToken(newToken);
-    }
-    return setToken(tokens[0].token);
+  const generateAndSetNewToken = async (sentryAppSlug: string) => {
+    const newToken = await generateToken(sentryAppSlug);
+    return setToken(newToken);
   };
 
   const generateToken = async (sentryAppSlug: string) => {
-    const newToken = await api.requestPromise(
+    const newToken: NewInternalAppApiToken = await api.requestPromise(
       `/sentry-apps/${sentryAppSlug}/api-tokens/`,
       {
         method: 'POST',
@@ -250,7 +251,7 @@ sentry-cli releases finalize "$VERSION"`,
                 alignMenu="left"
                 onSelect={({label, value}) => {
                   selectItem({label, value});
-                  fetchToken(value.slug);
+                  generateAndSetNewToken(value.slug);
                 }}
                 itemSize="small"
                 searchPlaceholder={t('Select Internal Integration')}
@@ -278,7 +279,7 @@ sentry-cli releases finalize "$VERSION"`,
                                   label,
                                   value,
                                 });
-                                fetchToken(value.slug);
+                                generateAndSetNewToken(value.slug);
                                 trackQuickstartCreatedIntegration(integration);
                               },
                               onCancel: () => {