3 * Copyright (C) 2009-2021 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 import * as React from 'react';
21 import { FormattedMessage } from 'react-intl';
22 import { Link } from 'react-router';
23 import HelpTooltip from 'sonar-ui-common/components/controls/HelpTooltip';
24 import { Alert } from 'sonar-ui-common/components/ui/Alert';
25 import { translate } from 'sonar-ui-common/helpers/l10n';
26 import { ALM_DOCUMENTATION_PATHS } from '../../../../helpers/constants';
27 import { AlmKeys, ProjectAlmBindingResponse } from '../../../../types/alm-settings';
28 import InputForBoolean from '../inputs/InputForBoolean';
30 export interface AlmSpecificFormProps {
32 formData: T.Omit<ProjectAlmBindingResponse, 'alm'>;
33 onFieldChange: (id: keyof ProjectAlmBindingResponse, value: string | boolean) => void;
34 monorepoEnabled: boolean;
37 interface LabelProps {
39 helpParams?: T.Dict<string | JSX.Element>;
44 interface CommonFieldProps extends LabelProps {
45 onFieldChange: (id: keyof ProjectAlmBindingResponse, value: string | boolean) => void;
46 propKey: keyof ProjectAlmBindingResponse;
49 function renderLabel(props: LabelProps) {
50 const { help, helpParams, optional, id } = props;
52 <label className="display-flex-center" htmlFor={id}>
53 {translate('settings.pr_decoration.binding.form', id)}
54 {!optional && <em className="mandatory">*</em>}
57 className="spacer-left"
60 defaultMessage={translate('settings.pr_decoration.binding.form', id, 'help')}
61 id={`settings.pr_decoration.binding.form.${id}.help`}
72 function renderBooleanField(
73 props: Omit<CommonFieldProps, 'optional'> & {
75 inputExtra?: React.ReactNode;
78 const { id, value, onFieldChange, propKey, inputExtra } = props;
80 <div className="form-field">
81 {renderLabel({ ...props, optional: true })}
82 <div className="display-flex-center">
86 onChange={v => onFieldChange(propKey, v)}
96 props: CommonFieldProps & {
100 const { id, propKey, value, onFieldChange } = props;
102 <div className="form-field">
105 className="input-super-large"
109 onChange={e => onFieldChange(propKey, e.currentTarget.value)}
117 function renderMonoRepoField(props: {
118 monorepoEnabled: boolean;
121 onFieldChange: (id: keyof ProjectAlmBindingResponse, value: string | boolean) => void;
123 if (!props.monorepoEnabled) {
127 return renderBooleanField({
131 <Link to={props.docLink} target="_blank">
132 {translate('learn_more')}
137 onFieldChange: props.onFieldChange,
139 value: props.value ?? false,
140 inputExtra: props.value && (
141 <Alert className="no-margin-bottom spacer-left" variant="warning" display="inline">
142 {translate('settings.pr_decoration.binding.form.monorepo.warning')}
148 export default function AlmSpecificForm(props: AlmSpecificFormProps) {
151 formData: { repository, slug, summaryCommentEnabled, monorepo },
155 const renderMonoRepoFieldWithDocLink = (docLink: string) => {
156 return renderMonoRepoField({
160 onFieldChange: props.onFieldChange
171 onFieldChange: props.onFieldChange,
177 id: 'azure.repository',
178 onFieldChange: props.onFieldChange,
179 propKey: 'repository',
180 value: repository || ''
182 {renderMonoRepoFieldWithDocLink(ALM_DOCUMENTATION_PATHS[AlmKeys.Azure])}
185 case AlmKeys.Bitbucket:
194 <strong>{'{KEY}'}</strong>
195 {'/repos/{SLUG}/browse'}
199 id: 'bitbucket.repository',
200 onFieldChange: props.onFieldChange,
201 propKey: 'repository',
202 value: repository || ''
209 {'.../projects/{KEY}/repos/'}
210 <strong>{'{SLUG}'}</strong>
215 id: 'bitbucket.slug',
216 onFieldChange: props.onFieldChange,
220 {renderMonoRepoFieldWithDocLink(ALM_DOCUMENTATION_PATHS[AlmKeys.Bitbucket])}
228 helpParams: { example: 'SonarSource/sonarqube' },
229 id: 'github.repository',
230 onFieldChange: props.onFieldChange,
231 propKey: 'repository',
232 value: repository || ''
234 {renderBooleanField({
236 id: 'github.summary_comment_setting',
237 onFieldChange: props.onFieldChange,
238 propKey: 'summaryCommentEnabled',
239 value: summaryCommentEnabled === undefined ? true : summaryCommentEnabled
241 {renderMonoRepoFieldWithDocLink(ALM_DOCUMENTATION_PATHS[AlmKeys.GitHub])}
248 id: 'gitlab.repository',
249 onFieldChange: props.onFieldChange,
250 propKey: 'repository',
251 value: repository || ''
253 {renderMonoRepoFieldWithDocLink(ALM_DOCUMENTATION_PATHS[AlmKeys.GitLab])}