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.

AlmSpecificForm.tsx 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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 { FormattedMessage } from 'react-intl';
  22. import { Link } from 'react-router';
  23. import withAppStateContext from '../../../../app/components/app-state/withAppStateContext';
  24. import Toggle from '../../../../components/controls/Toggle';
  25. import { Alert } from '../../../../components/ui/Alert';
  26. import MandatoryFieldMarker from '../../../../components/ui/MandatoryFieldMarker';
  27. import { ALM_DOCUMENTATION_PATHS } from '../../../../helpers/constants';
  28. import { translate } from '../../../../helpers/l10n';
  29. import { convertGithubApiUrlToLink, stripTrailingSlash } from '../../../../helpers/urls';
  30. import {
  31. AlmKeys,
  32. AlmSettingsInstance,
  33. ProjectAlmBindingResponse
  34. } from '../../../../types/alm-settings';
  35. import { AppState } from '../../../../types/appstate';
  36. import { EditionKey } from '../../../../types/editions';
  37. import { Dict } from '../../../../types/types';
  38. export interface AlmSpecificFormProps {
  39. alm: AlmKeys;
  40. instances: AlmSettingsInstance[];
  41. formData: Omit<ProjectAlmBindingResponse, 'alm'>;
  42. onFieldChange: (id: keyof ProjectAlmBindingResponse, value: string | boolean) => void;
  43. appState: AppState;
  44. }
  45. interface LabelProps {
  46. id: string;
  47. optional?: boolean;
  48. }
  49. interface CommonFieldProps extends LabelProps {
  50. help?: boolean;
  51. helpParams?: Dict<string | JSX.Element>;
  52. helpExample?: JSX.Element;
  53. onFieldChange: (id: keyof ProjectAlmBindingResponse, value: string | boolean) => void;
  54. propKey: keyof ProjectAlmBindingResponse;
  55. }
  56. function renderFieldWrapper(
  57. label: React.ReactNode,
  58. input: React.ReactNode,
  59. help?: React.ReactNode
  60. ) {
  61. return (
  62. <div className="settings-definition">
  63. <div className="settings-definition-left">
  64. {label}
  65. {help && <div className="markdown small spacer-top">{help}</div>}
  66. </div>
  67. <div className="settings-definition-right padded-top">{input}</div>
  68. </div>
  69. );
  70. }
  71. function renderHelp({ help, helpExample, helpParams, id }: CommonFieldProps) {
  72. return (
  73. help && (
  74. <>
  75. <FormattedMessage
  76. defaultMessage={translate('settings.pr_decoration.binding.form', id, 'help')}
  77. id={`settings.pr_decoration.binding.form.${id}.help`}
  78. values={helpParams}
  79. />
  80. {helpExample && (
  81. <div className="spacer-top nowrap">
  82. {translate('example')}: <em>{helpExample}</em>
  83. </div>
  84. )}
  85. </>
  86. )
  87. );
  88. }
  89. function renderLabel(props: LabelProps) {
  90. const { optional, id } = props;
  91. return (
  92. <label className="h3" htmlFor={id}>
  93. {translate('settings.pr_decoration.binding.form', id)}
  94. {!optional && <MandatoryFieldMarker />}
  95. </label>
  96. );
  97. }
  98. function renderBooleanField(
  99. props: Omit<CommonFieldProps, 'optional'> & {
  100. value: boolean;
  101. inputExtra?: React.ReactNode;
  102. }
  103. ) {
  104. const { id, value, onFieldChange, propKey, inputExtra } = props;
  105. return renderFieldWrapper(
  106. renderLabel({ ...props, optional: true }),
  107. <div className="display-flex-center big-spacer-top">
  108. <div className="display-inline-block text-top">
  109. <Toggle name={id} onChange={v => onFieldChange(propKey, v)} value={value} />
  110. {value == null && <span className="spacer-left note">{translate('settings.not_set')}</span>}
  111. </div>
  112. {inputExtra}
  113. </div>,
  114. renderHelp(props)
  115. );
  116. }
  117. function renderField(
  118. props: CommonFieldProps & {
  119. value: string;
  120. }
  121. ) {
  122. const { id, propKey, value, onFieldChange } = props;
  123. return renderFieldWrapper(
  124. renderLabel(props),
  125. <input
  126. className="input-super-large big-spacer-top"
  127. id={id}
  128. maxLength={256}
  129. name={id}
  130. onChange={e => onFieldChange(propKey, e.currentTarget.value)}
  131. type="text"
  132. value={value}
  133. />,
  134. renderHelp(props)
  135. );
  136. }
  137. export function AlmSpecificForm(props: AlmSpecificFormProps) {
  138. const {
  139. alm,
  140. instances,
  141. formData: { repository, slug, summaryCommentEnabled, monorepo },
  142. appState
  143. } = props;
  144. let formFields: JSX.Element;
  145. const instance = instances.find(i => i.alm === alm);
  146. switch (alm) {
  147. case AlmKeys.Azure:
  148. formFields = (
  149. <>
  150. {renderField({
  151. help: true,
  152. helpExample: <strong>My Project</strong>,
  153. id: 'azure.project',
  154. onFieldChange: props.onFieldChange,
  155. propKey: 'slug',
  156. value: slug || ''
  157. })}
  158. {renderField({
  159. help: true,
  160. helpExample: <strong>My Repository</strong>,
  161. id: 'azure.repository',
  162. onFieldChange: props.onFieldChange,
  163. propKey: 'repository',
  164. value: repository || ''
  165. })}
  166. </>
  167. );
  168. break;
  169. case AlmKeys.BitbucketServer:
  170. formFields = (
  171. <>
  172. {renderField({
  173. help: true,
  174. helpExample: (
  175. <>
  176. {instance?.url
  177. ? `${stripTrailingSlash(instance.url)}/projects/`
  178. : 'https://bb.company.com/projects/'}
  179. <strong>{'MY_PROJECT_KEY'}</strong>
  180. {'/repos/my-repository-slug/browse'}
  181. </>
  182. ),
  183. id: 'bitbucket.repository',
  184. onFieldChange: props.onFieldChange,
  185. propKey: 'repository',
  186. value: repository || ''
  187. })}
  188. {renderField({
  189. help: true,
  190. helpExample: (
  191. <>
  192. {instance?.url
  193. ? `${stripTrailingSlash(instance.url)}/projects/MY_PROJECT_KEY/repos/`
  194. : 'https://bb.company.com/projects/MY_PROJECT_KEY/repos/'}
  195. <strong>{'my-repository-slug'}</strong>
  196. {'/browse'}
  197. </>
  198. ),
  199. id: 'bitbucket.slug',
  200. onFieldChange: props.onFieldChange,
  201. propKey: 'slug',
  202. value: slug || ''
  203. })}
  204. </>
  205. );
  206. break;
  207. case AlmKeys.BitbucketCloud:
  208. formFields = (
  209. <>
  210. {renderField({
  211. help: true,
  212. helpExample: (
  213. <>
  214. {'https://bitbucket.org/my-workspace/'}
  215. <strong>{'my-repository-slug'}</strong>
  216. </>
  217. ),
  218. id: 'bitbucketcloud.repository',
  219. onFieldChange: props.onFieldChange,
  220. propKey: 'repository',
  221. value: repository || ''
  222. })}
  223. </>
  224. );
  225. break;
  226. case AlmKeys.GitHub:
  227. formFields = (
  228. <>
  229. {renderField({
  230. help: true,
  231. helpExample: (
  232. <>
  233. {instance?.url
  234. ? `${stripTrailingSlash(convertGithubApiUrlToLink(instance.url))}/`
  235. : 'https://github.com/'}
  236. <strong>{'sonarsource/sonarqube'}</strong>
  237. </>
  238. ),
  239. id: 'github.repository',
  240. onFieldChange: props.onFieldChange,
  241. propKey: 'repository',
  242. value: repository || ''
  243. })}
  244. {renderBooleanField({
  245. help: true,
  246. id: 'github.summary_comment_setting',
  247. onFieldChange: props.onFieldChange,
  248. propKey: 'summaryCommentEnabled',
  249. value: summaryCommentEnabled === undefined ? true : summaryCommentEnabled
  250. })}
  251. </>
  252. );
  253. break;
  254. case AlmKeys.GitLab:
  255. formFields = (
  256. <>
  257. {renderField({
  258. help: true,
  259. helpExample: <strong>123456</strong>,
  260. id: 'gitlab.repository',
  261. onFieldChange: props.onFieldChange,
  262. propKey: 'repository',
  263. value: repository || ''
  264. })}
  265. </>
  266. );
  267. break;
  268. }
  269. // This feature trigger will be replaced when SONAR-14349 is implemented
  270. const monorepoEnabled = [EditionKey.enterprise, EditionKey.datacenter].includes(
  271. appState.edition as EditionKey
  272. );
  273. return (
  274. <>
  275. {formFields}
  276. {monorepoEnabled &&
  277. renderBooleanField({
  278. help: true,
  279. helpParams: {
  280. doc_link: (
  281. <Link to={ALM_DOCUMENTATION_PATHS[alm]} target="_blank">
  282. {translate('learn_more')}
  283. </Link>
  284. )
  285. },
  286. id: 'monorepo',
  287. onFieldChange: props.onFieldChange,
  288. propKey: 'monorepo',
  289. value: monorepo,
  290. inputExtra: monorepo && (
  291. <Alert className="no-margin-bottom spacer-left" variant="warning" display="inline">
  292. {translate('settings.pr_decoration.binding.form.monorepo.warning')}
  293. </Alert>
  294. )
  295. })}
  296. </>
  297. );
  298. }
  299. export default withAppStateContext(AlmSpecificForm);