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 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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 InputValidationField from '../../../components/controls/InputValidationField';
  23. import ValidationModal from '../../../components/controls/ValidationModal';
  24. import MandatoryFieldMarker from '../../../components/ui/MandatoryFieldMarker';
  25. import MandatoryFieldsExplanation from '../../../components/ui/MandatoryFieldsExplanation';
  26. import { translate } from '../../../helpers/l10n';
  27. import { Webhook } from '../../../types/webhook';
  28. interface Props {
  29. onClose: () => void;
  30. onDone: (data: Values) => Promise<void>;
  31. webhook?: Webhook;
  32. }
  33. interface Values {
  34. name: string;
  35. secret: string;
  36. url: string;
  37. }
  38. export default class CreateWebhookForm extends React.PureComponent<Props> {
  39. handleCancelClick = (event: React.SyntheticEvent<HTMLButtonElement>) => {
  40. event.preventDefault();
  41. event.currentTarget.blur();
  42. this.props.onClose();
  43. };
  44. handleValidate = (data: Values) => {
  45. const { name, secret, url } = data;
  46. const errors: { name?: string; secret?: string; url?: string } = {};
  47. if (!name.trim()) {
  48. errors.name = translate('webhooks.name.required');
  49. }
  50. if (!url.trim()) {
  51. errors.url = translate('webhooks.url.required');
  52. } else if (!url.startsWith('http://') && !url.startsWith('https://')) {
  53. errors.url = translate('webhooks.url.bad_protocol');
  54. } else if (!isWebUri(url)) {
  55. errors.url = translate('webhooks.url.bad_format');
  56. }
  57. if (secret && secret.length > 200) {
  58. errors.secret = translate('webhooks.secret.bad_format');
  59. }
  60. return errors;
  61. };
  62. render() {
  63. const { webhook } = this.props;
  64. const isUpdate = !!webhook;
  65. const modalHeader = isUpdate ? translate('webhooks.update') : translate('webhooks.create');
  66. const confirmButtonText = isUpdate ? translate('update_verb') : translate('create');
  67. return (
  68. <ValidationModal
  69. confirmButtonText={confirmButtonText}
  70. header={modalHeader}
  71. initialValues={{
  72. name: (webhook && webhook.name) || '',
  73. secret: (webhook && webhook.secret) || '',
  74. url: (webhook && webhook.url) || '',
  75. }}
  76. onClose={this.props.onClose}
  77. onSubmit={this.props.onDone}
  78. size="small"
  79. validate={this.handleValidate}
  80. >
  81. {({ dirty, errors, handleBlur, handleChange, isSubmitting, touched, values }) => (
  82. <>
  83. <MandatoryFieldsExplanation className="big-spacer-bottom" />
  84. <InputValidationField
  85. autoFocus={true}
  86. dirty={dirty}
  87. disabled={isSubmitting}
  88. error={errors.name}
  89. id="webhook-name"
  90. label={
  91. <label htmlFor="webhook-name">
  92. {translate('webhooks.name')}
  93. <MandatoryFieldMarker />
  94. </label>
  95. }
  96. name="name"
  97. onBlur={handleBlur}
  98. onChange={handleChange}
  99. touched={touched.name}
  100. type="text"
  101. value={values.name}
  102. />
  103. <InputValidationField
  104. description={translate('webhooks.url.description')}
  105. dirty={dirty}
  106. disabled={isSubmitting}
  107. error={errors.url}
  108. id="webhook-url"
  109. label={
  110. <label htmlFor="webhook-url">
  111. {translate('webhooks.url')}
  112. <MandatoryFieldMarker />
  113. </label>
  114. }
  115. name="url"
  116. onBlur={handleBlur}
  117. onChange={handleChange}
  118. touched={touched.url}
  119. type="text"
  120. value={values.url}
  121. />
  122. <InputValidationField
  123. description={translate('webhooks.secret.description')}
  124. dirty={dirty}
  125. disabled={isSubmitting}
  126. error={errors.secret}
  127. id="webhook-secret"
  128. label={<label htmlFor="webhook-secret">{translate('webhooks.secret')}</label>}
  129. name="secret"
  130. onBlur={handleBlur}
  131. onChange={handleChange}
  132. touched={touched.secret}
  133. type="password"
  134. value={values.secret}
  135. />
  136. </>
  137. )}
  138. </ValidationModal>
  139. );
  140. }
  141. }