apiNewToken.tsx 4.5 KB

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