apiNewToken.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import {Component} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import ApiForm from 'sentry/components/forms/apiForm';
  4. import ExternalLink from 'sentry/components/links/externalLink';
  5. import Panel from 'sentry/components/panels/panel';
  6. import PanelBody from 'sentry/components/panels/panelBody';
  7. import PanelHeader from 'sentry/components/panels/panelHeader';
  8. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  9. import {t, tct} from 'sentry/locale';
  10. import type {NewInternalAppApiToken, Permissions} from 'sentry/types';
  11. import getDynamicText from 'sentry/utils/getDynamicText';
  12. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  13. import NewTokenHandler from 'sentry/views/settings/components/newTokenHandler';
  14. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  15. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  16. import PermissionSelection from 'sentry/views/settings/organizationDeveloperSettings/permissionSelection';
  17. const API_INDEX_ROUTE = '/settings/account/api/auth-tokens/';
  18. type State = {
  19. newToken: NewInternalAppApiToken | null;
  20. permissions: Permissions;
  21. };
  22. export default class ApiNewToken extends Component<{}, State> {
  23. constructor(props: {}) {
  24. super(props);
  25. this.state = {
  26. permissions: {
  27. Event: 'no-access',
  28. Team: 'no-access',
  29. Member: 'no-access',
  30. Project: 'no-access',
  31. Release: 'no-access',
  32. Organization: 'no-access',
  33. },
  34. newToken: null,
  35. };
  36. }
  37. onCancel = () => {
  38. browserHistory.push(normalizeUrl(API_INDEX_ROUTE));
  39. };
  40. handleGoBack = () => {
  41. browserHistory.push(normalizeUrl(API_INDEX_ROUTE));
  42. };
  43. render() {
  44. const {permissions, newToken} = this.state;
  45. return (
  46. <SentryDocumentTitle title={t('Create User Auth Token')}>
  47. <div>
  48. <SettingsPageHeader title={t('Create New User Auth Token')} />
  49. <TextBlock>
  50. {t(
  51. "Authentication tokens allow you to perform actions against the Sentry API on behalf of your account. They're the easiest way to get started using the API."
  52. )}
  53. </TextBlock>
  54. <TextBlock>
  55. {tct(
  56. 'For more information on how to use the web API, see our [link:documentation].',
  57. {
  58. link: <ExternalLink href="https://docs.sentry.io/api/" />,
  59. }
  60. )}
  61. </TextBlock>
  62. {newToken !== null ? (
  63. <NewTokenHandler
  64. token={
  65. getDynamicText({value: newToken.token, fixed: 'CI_AUTH_TOKEN'}) ||
  66. 'CI_AUTH_TOKEN'
  67. }
  68. handleGoBack={this.handleGoBack}
  69. />
  70. ) : (
  71. <Panel>
  72. <PanelHeader>{t('Permissions')}</PanelHeader>
  73. <ApiForm
  74. apiMethod="POST"
  75. apiEndpoint="/api-tokens/"
  76. initialData={{scopes: []}}
  77. onSubmitSuccess={response => {
  78. this.setState({newToken: response});
  79. }}
  80. onCancel={this.onCancel}
  81. footerStyle={{
  82. marginTop: 0,
  83. paddingRight: 20,
  84. }}
  85. submitDisabled={Object.values(permissions).every(
  86. value => value === 'no-access'
  87. )}
  88. submitLabel={t('Create Token')}
  89. >
  90. <PanelBody>
  91. <PermissionSelection
  92. appPublished={false}
  93. permissions={permissions}
  94. onChange={value => {
  95. this.setState({permissions: value});
  96. }}
  97. />
  98. </PanelBody>
  99. </ApiForm>
  100. </Panel>
  101. )}
  102. </div>
  103. </SentryDocumentTitle>
  104. );
  105. }
  106. }