You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CreateWebhookForm.tsx 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import * as React from 'react';
  21. import { isWebUri } from 'valid-url';
  22. import { translate } from 'sonar-ui-common/helpers/l10n';
  23. import InputValidationField from 'sonar-ui-common/components/controls/InputValidationField';
  24. import ValidationModal from 'sonar-ui-common/components/controls/ValidationModal';
  25. interface Props {
  26. onClose: () => void;
  27. onDone: (data: Values) => Promise<void>;
  28. webhook?: T.Webhook;
  29. }
  30. interface Values {
  31. name: string;
  32. secret: string;
  33. url: string;
  34. }
  35. export default class CreateWebhookForm extends React.PureComponent<Props> {
  36. handleCancelClick = (event: React.SyntheticEvent<HTMLButtonElement>) => {
  37. event.preventDefault();
  38. event.currentTarget.blur();
  39. this.props.onClose();
  40. };
  41. handleValidate = (data: Values) => {
  42. const { name, secret, url } = data;
  43. const errors: { name?: string; secret?: string; url?: string } = {};
  44. if (!name.trim()) {
  45. errors.name = translate('webhooks.name.required');
  46. }
  47. if (!url.trim()) {
  48. errors.url = translate('webhooks.url.required');
  49. } else if (!url.startsWith('http://') && !url.startsWith('https://')) {
  50. errors.url = translate('webhooks.url.bad_protocol');
  51. } else if (!isWebUri(url)) {
  52. errors.url = translate('webhooks.url.bad_format');
  53. }
  54. if (secret && secret.length > 200) {
  55. errors.secret = translate('webhooks.secret.bad_format');
  56. }
  57. return errors;
  58. };
  59. render() {
  60. const { webhook } = this.props;
  61. const isUpdate = !!webhook;
  62. const modalHeader = isUpdate ? translate('webhooks.update') : translate('webhooks.create');
  63. const confirmButtonText = isUpdate ? translate('update_verb') : translate('create');
  64. return (
  65. <ValidationModal
  66. confirmButtonText={confirmButtonText}
  67. header={modalHeader}
  68. initialValues={{
  69. name: (webhook && webhook.name) || '',
  70. secret: (webhook && webhook.secret) || '',
  71. url: (webhook && webhook.url) || ''
  72. }}
  73. isInitialValid={isUpdate}
  74. onClose={this.props.onClose}
  75. onSubmit={this.props.onDone}
  76. size="small"
  77. validate={this.handleValidate}>
  78. {({ dirty, errors, handleBlur, handleChange, isSubmitting, touched, values }) => (
  79. <>
  80. <InputValidationField
  81. autoFocus={true}
  82. dirty={dirty}
  83. disabled={isSubmitting}
  84. error={errors.name}
  85. id="webhook-name"
  86. label={
  87. <label htmlFor="webhook-name">
  88. {translate('webhooks.name')}
  89. <em className="mandatory">*</em>
  90. </label>
  91. }
  92. name="name"
  93. onBlur={handleBlur}
  94. onChange={handleChange}
  95. touched={touched.name}
  96. type="text"
  97. value={values.name}
  98. />
  99. <InputValidationField
  100. description={translate('webhooks.url.description')}
  101. dirty={dirty}
  102. disabled={isSubmitting}
  103. error={errors.url}
  104. id="webhook-url"
  105. label={
  106. <label htmlFor="webhook-url">
  107. {translate('webhooks.url')}
  108. <em className="mandatory">*</em>
  109. </label>
  110. }
  111. name="url"
  112. onBlur={handleBlur}
  113. onChange={handleChange}
  114. touched={touched.url}
  115. type="text"
  116. value={values.url}
  117. />
  118. <InputValidationField
  119. description={translate('webhooks.secret.description')}
  120. dirty={dirty}
  121. disabled={isSubmitting}
  122. error={errors.secret}
  123. id="webhook-secret"
  124. label={<label htmlFor="webhook-secret">{translate('webhooks.secret')}</label>}
  125. name="secret"
  126. onBlur={handleBlur}
  127. onChange={handleChange}
  128. touched={touched.secret}
  129. type="password"
  130. value={values.secret}
  131. />
  132. </>
  133. )}
  134. </ValidationModal>
  135. );
  136. }
  137. }