3 * Copyright (C) 2009-2023 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 '../../../../components/common/Link';
23 import { Button, SubmitButton } from '../../../../components/controls/buttons';
24 import AlmSettingsInstanceSelector from '../../../../components/devops-platform/AlmSettingsInstanceSelector';
25 import AlertSuccessIcon from '../../../../components/icons/AlertSuccessIcon';
26 import { Alert } from '../../../../components/ui/Alert';
27 import MandatoryFieldMarker from '../../../../components/ui/MandatoryFieldMarker';
28 import MandatoryFieldsExplanation from '../../../../components/ui/MandatoryFieldsExplanation';
29 import Spinner from '../../../../components/ui/Spinner';
30 import { translate } from '../../../../helpers/l10n';
31 import { getGlobalSettingsUrl } from '../../../../helpers/urls';
34 ProjectAlmBindingConfigurationErrorScope,
35 ProjectAlmBindingConfigurationErrors,
36 ProjectAlmBindingResponse,
37 } from '../../../../types/alm-settings';
38 import { ALM_INTEGRATION_CATEGORY } from '../../constants';
39 import AlmSpecificForm from './AlmSpecificForm';
41 export interface PRDecorationBindingRendererProps {
42 formData: Omit<ProjectAlmBindingResponse, 'alm'>;
43 instances: AlmSettingsInstance[];
45 isConfigured: boolean;
48 onFieldChange: (id: keyof ProjectAlmBindingResponse, value: string | boolean) => void;
52 successfullyUpdated: boolean;
53 onCheckConfiguration: () => void;
54 checkingConfiguration: boolean;
55 configurationErrors?: ProjectAlmBindingConfigurationErrors;
59 export default function PRDecorationBindingRenderer(props: PRDecorationBindingRendererProps) {
69 checkingConfiguration,
78 if (instances.length < 1) {
81 <Alert className="spacer-top huge-spacer-bottom" variant="info">
84 defaultMessage={translate('settings.pr_decoration.binding.no_bindings.admin')}
85 id="settings.pr_decoration.binding.no_bindings.admin"
88 <Link to={getGlobalSettingsUrl(ALM_INTEGRATION_CATEGORY)}>
89 {translate('settings.pr_decoration.binding.no_bindings.link')}
95 translate('settings.pr_decoration.binding.no_bindings')
102 const selected = formData.key && instances.find((i) => i.key === formData.key);
103 const alm = selected && selected.alm;
107 <header className="page-header">
108 <h1 className="page-title">{translate('settings.pr_decoration.binding.title')}</h1>
111 <div className="markdown small spacer-top big-spacer-bottom">
112 {translate('settings.pr_decoration.binding.description')}
116 onSubmit={(event: React.SyntheticEvent<HTMLFormElement>) => {
117 event.preventDefault();
121 <MandatoryFieldsExplanation className="form-field" />
123 <div className="settings-definition big-spacer-bottom">
124 <div className="settings-definition-left">
125 <label className="h3" htmlFor="name">
126 {translate('settings.pr_decoration.binding.form.name')}
127 <MandatoryFieldMarker className="spacer-right" />
129 <div className="markdown small spacer-top">
130 {translate('settings.pr_decoration.binding.form.name.help')}
133 <div className="settings-definition-right">
134 <AlmSettingsInstanceSelector
135 instances={instances}
136 onChange={(instance: AlmSettingsInstance) => props.onFieldChange('key', instance.key)}
137 initialValue={formData.key}
138 className="sw-w-abs-400 sw-mt-4 it__configuration-name-select"
147 instances={instances}
149 onFieldChange={props.onFieldChange}
153 <div className="display-flex-center big-spacer-top action-section">
155 <SubmitButton className="spacer-right button-success" disabled={updating || !isValid}>
156 <span data-test="project-settings__alm-save">{translate('save')}</span>
157 <Spinner className="spacer-left" loading={updating} />
160 {!updating && successfullyUpdated && (
161 <span className="text-success spacer-right">
162 <AlertSuccessIcon className="spacer-right" />
163 {translate('settings.state.saved')}
168 <Button className="spacer-right" onClick={props.onReset}>
169 <span data-test="project-settings__alm-reset">{translate('reset_verb')}</span>
172 <Button onClick={props.onCheckConfiguration} disabled={checkingConfiguration}>
173 {translate('settings.pr_decoration.binding.check_configuration')}
174 <Spinner className="spacer-left" loading={checkingConfiguration} />
180 {!checkingConfiguration && configurationErrors?.errors && (
181 <Alert variant="error" display="inline" className="big-spacer-top">
182 <p className="spacer-bottom">
183 {translate('settings.pr_decoration.binding.check_configuration.failure')}
185 <ul className="list-styled">
186 {configurationErrors.errors.map((error, i) => (
187 // eslint-disable-next-line react/no-array-index-key
188 <li key={i}>{error.msg}</li>
191 {configurationErrors.scope === ProjectAlmBindingConfigurationErrorScope.Global && (
195 id="settings.pr_decoration.binding.check_configuration.failure.check_global_settings"
196 defaultMessage={translate(
197 'settings.pr_decoration.binding.check_configuration.failure.check_global_settings',
201 <Link to={getGlobalSettingsUrl(ALM_INTEGRATION_CATEGORY, { alm })}>
203 'settings.pr_decoration.binding.check_configuration.failure.check_global_settings.link',
210 translate('settings.pr_decoration.binding.check_configuration.contact_admin')
216 {isConfigured && !isChanged && !checkingConfiguration && !configurationErrors && (
217 <Alert variant="success" display="inline" className="big-spacer-top">
218 {translate('settings.pr_decoration.binding.check_configuration.success')}