apiNewToken.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import {Component} from 'react';
  2. import ApiForm from 'sentry/components/forms/apiForm';
  3. import TextField from 'sentry/components/forms/fields/textField';
  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 {Permissions} from 'sentry/types/integrations';
  11. import type {NewInternalAppApiToken} from 'sentry/types/user';
  12. import {browserHistory} from 'sentry/utils/browserHistory';
  13. import getDynamicText from 'sentry/utils/getDynamicText';
  14. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  15. import NewTokenHandler from 'sentry/views/settings/components/newTokenHandler';
  16. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  17. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  18. import PermissionSelection from 'sentry/views/settings/organizationDeveloperSettings/permissionSelection';
  19. const API_INDEX_ROUTE = '/settings/account/api/auth-tokens/';
  20. type State = {
  21. name: string | null;
  22. newToken: NewInternalAppApiToken | null;
  23. permissions: Permissions;
  24. };
  25. export default class ApiNewToken extends Component<{}, State> {
  26. constructor(props: {}) {
  27. super(props);
  28. this.state = {
  29. name: null,
  30. permissions: {
  31. Event: 'no-access',
  32. Team: 'no-access',
  33. Member: 'no-access',
  34. Project: 'no-access',
  35. Release: 'no-access',
  36. Organization: 'no-access',
  37. },
  38. newToken: null,
  39. };
  40. }
  41. onCancel = () => {
  42. browserHistory.push(normalizeUrl(API_INDEX_ROUTE));
  43. };
  44. handleGoBack = () => {
  45. browserHistory.push(normalizeUrl(API_INDEX_ROUTE));
  46. };
  47. render() {
  48. const {permissions, newToken} = this.state;
  49. return (
  50. <SentryDocumentTitle title={t('Create User Auth Token')}>
  51. <div>
  52. <SettingsPageHeader title={t('Create New User Auth Token')} />
  53. <TextBlock>
  54. {t(
  55. "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."
  56. )}
  57. </TextBlock>
  58. <TextBlock>
  59. {tct(
  60. 'For more information on how to use the web API, see our [link:documentation].',
  61. {
  62. link: <ExternalLink href="https://docs.sentry.io/api/" />,
  63. }
  64. )}
  65. </TextBlock>
  66. {newToken !== null ? (
  67. <NewTokenHandler
  68. token={
  69. getDynamicText({value: newToken.token, fixed: 'CI_AUTH_TOKEN'}) ||
  70. 'CI_AUTH_TOKEN'
  71. }
  72. handleGoBack={this.handleGoBack}
  73. />
  74. ) : (
  75. <div>
  76. <ApiForm
  77. apiMethod="POST"
  78. apiEndpoint="/api-tokens/"
  79. initialData={{scopes: [], name: ''}}
  80. onSubmitSuccess={response => {
  81. this.setState({newToken: response});
  82. }}
  83. onCancel={this.onCancel}
  84. footerStyle={{
  85. marginTop: 0,
  86. paddingRight: 20,
  87. }}
  88. submitDisabled={Object.values(permissions).every(
  89. value => value === 'no-access'
  90. )}
  91. submitLabel={t('Create Token')}
  92. >
  93. <Panel>
  94. <PanelHeader>{t('General')}</PanelHeader>
  95. <PanelBody>
  96. <TextField
  97. name="name"
  98. label={t('Name')}
  99. help={t('A name to help you identify this token.')}
  100. onChange={value => {
  101. this.setState({name: value});
  102. }}
  103. />
  104. </PanelBody>
  105. </Panel>
  106. <Panel>
  107. <PanelHeader>{t('Permissions')}</PanelHeader>
  108. <PanelBody>
  109. <PermissionSelection
  110. appPublished={false}
  111. permissions={permissions}
  112. onChange={value => {
  113. this.setState({permissions: value});
  114. }}
  115. />
  116. </PanelBody>
  117. </Panel>
  118. </ApiForm>
  119. </div>
  120. )}
  121. </div>
  122. </SentryDocumentTitle>
  123. );
  124. }
  125. }