3 * Copyright (C) 2009-2020 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 { Button, SubmitButton } from 'sonar-ui-common/components/controls/buttons';
24 import HelpTooltip from 'sonar-ui-common/components/controls/HelpTooltip';
25 import Select from 'sonar-ui-common/components/controls/Select';
26 import AlertSuccessIcon from 'sonar-ui-common/components/icons/AlertSuccessIcon';
27 import { Alert } from 'sonar-ui-common/components/ui/Alert';
28 import DeferredSpinner from 'sonar-ui-common/components/ui/DeferredSpinner';
29 import { translate } from 'sonar-ui-common/helpers/l10n';
30 import { AlmKeys, AlmSettingsInstance, ProjectAlmBinding } from '../../../../types/alm-settings';
32 export interface PRDecorationBindingRendererProps {
33 formData: ProjectAlmBinding;
34 instances: AlmSettingsInstance[];
37 onFieldChange: (id: keyof ProjectAlmBinding, value: string) => void;
40 originalData?: ProjectAlmBinding;
45 function optionRenderer(instance: AlmSettingsInstance) {
46 return instance.url ? (
48 <span>{instance.key} — </span>
49 <span className="text-muted">{instance.url}</span>
52 <span>{instance.key}</span>
56 function renderField(props: {
58 helpParams?: { [key: string]: string | JSX.Element };
60 onFieldChange: (id: keyof ProjectAlmBinding, value: string) => void;
62 propKey: keyof ProjectAlmBinding;
65 const { help, helpParams, id, propKey, optional, value, onFieldChange } = props;
67 <div className="form-field">
68 <label className="display-flex-center" htmlFor={id}>
69 {translate('settings.pr_decoration.binding.form', id)}
70 {!optional && <em className="mandatory">*</em>}
73 className="spacer-left"
76 defaultMessage={translate('settings.pr_decoration.binding.form', id, 'help')}
77 id={`settings.pr_decoration.binding.form.${id}.help`}
86 className="input-super-large"
90 onChange={e => onFieldChange(propKey, e.currentTarget.value)}
99 { key, repository = '', slug = '' }: ProjectAlmBinding,
100 { key: oKey = '', repository: oRepository = '', slug: oSlug = '' }: ProjectAlmBinding
102 return key === oKey && repository === oRepository && slug === oSlug;
105 export default function PRDecorationBindingRenderer(props: PRDecorationBindingRendererProps) {
107 formData: { key, repository, slug },
117 return <DeferredSpinner />;
120 if (instances.length < 1) {
123 <Alert className="spacer-top huge-spacer-bottom" variant="info">
125 defaultMessage={translate('settings.pr_decoration.binding.no_bindings')}
126 id="settings.pr_decoration.binding.no_bindings"
129 <Link to="/documentation/analysis/pull-request/#pr-decoration">
130 {translate('learn_more')}
140 const selected = key && instances.find(i => i.key === key);
141 const alm = selected && selected.alm;
143 const isChanged = !isDataSame({ key, repository, slug }, originalData || { key: '' });
147 <header className="page-header">
148 <h1 className="page-title">{translate('settings.pr_decoration.binding.title')}</h1>
151 <div className="markdown small spacer-top big-spacer-bottom">
152 {translate('settings.pr_decoration.binding.description')}
156 onSubmit={(event: React.SyntheticEvent<HTMLFormElement>) => {
157 event.preventDefault();
160 <div className="form-field">
161 <label htmlFor="name">
162 {translate('settings.pr_decoration.binding.form.name')}
163 <em className="mandatory spacer-right">*</em>
167 className="abs-width-400"
170 menuContainerStyle={{
171 maxWidth: '210%' /* Allow double the width of the select */,
174 onChange={(instance: AlmSettingsInstance) => props.onFieldChange('key', instance.key)}
175 optionRenderer={optionRenderer}
180 valueRenderer={optionRenderer}
184 {alm === AlmKeys.Bitbucket && (
192 <strong>{'{KEY}'}</strong>
193 {'/repos/{SLUG}/browse'}
197 id: 'bitbucket.repository',
198 onFieldChange: props.onFieldChange,
199 propKey: 'repository',
200 value: repository || ''
207 {'.../projects/{KEY}/repos/'}
208 <strong>{'{SLUG}'}</strong>
213 id: 'bitbucket.slug',
214 onFieldChange: props.onFieldChange,
221 {alm === AlmKeys.GitHub &&
224 helpParams: { example: 'SonarSource/sonarqube' },
225 id: 'github.repository',
226 onFieldChange: props.onFieldChange,
227 propKey: 'repository',
228 value: repository || ''
231 {alm === AlmKeys.GitLab &&
234 id: 'gitlab.repository',
235 onFieldChange: props.onFieldChange,
237 propKey: 'repository',
238 value: repository || ''
241 <div className="display-flex-center">
242 <DeferredSpinner className="spacer-right" loading={saving} />
244 <SubmitButton className="spacer-right button-success" disabled={saving || !isValid}>
245 <span data-test="project-settings__alm-save">{translate('save')}</span>
249 <Button className="spacer-right" onClick={props.onReset}>
250 <span data-test="project-settings__alm-reset">{translate('reset_verb')}</span>
253 {!saving && success && (
254 <span className="text-success">
255 <AlertSuccessIcon className="spacer-right" />
256 {translate('settings.state.saved')}