]> source.dussan.org Git - sonarqube.git/blob
f6df013c699781c20040e915a5daacfd9f4dc5a3
[sonarqube.git] /
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 Modal from '../../../../components/controls/Modal';
22 import { ResetButtonLink, SubmitButton } from '../../../../components/controls/buttons';
23 import { Alert } from '../../../../components/ui/Alert';
24 import Spinner from '../../../../components/ui/Spinner';
25 import { translate } from '../../../../helpers/l10n';
26 import {
27   AlmBindingDefinition,
28   AlmKeys,
29   AzureBindingDefinition,
30   BitbucketCloudBindingDefinition,
31   BitbucketServerBindingDefinition,
32   GithubBindingDefinition,
33   GitlabBindingDefinition,
34 } from '../../../../types/alm-settings';
35 import AzureForm from './AzureForm';
36 import BitbucketForm from './BitbucketForm';
37 import GithubForm from './GithubForm';
38 import GitlabForm from './GitlabForm';
39
40 export interface Props {
41   alm: AlmKeys;
42   isUpdate: boolean;
43   canSubmit: boolean;
44   onCancel: () => void;
45   onSubmit: () => void;
46   onFieldChange: (fieldId: keyof AlmBindingDefinition, value: string) => void;
47   formData: AlmBindingDefinition;
48   submitting: boolean;
49   bitbucketVariant?: AlmKeys.BitbucketServer | AlmKeys.BitbucketCloud;
50   onBitbucketVariantChange: (
51     bitbucketVariant: AlmKeys.BitbucketServer | AlmKeys.BitbucketCloud,
52   ) => void;
53   validationError?: string;
54 }
55
56 export default class AlmBindingDefinitionFormRenderer extends React.PureComponent<Readonly<Props>> {
57   renderForm = () => {
58     const { alm, formData, isUpdate, bitbucketVariant } = this.props;
59
60     switch (alm) {
61       case AlmKeys.GitLab:
62         return (
63           <GitlabForm
64             onFieldChange={this.props.onFieldChange}
65             formData={formData as GitlabBindingDefinition}
66           />
67         );
68       case AlmKeys.Azure:
69         return (
70           <AzureForm
71             onFieldChange={this.props.onFieldChange}
72             formData={formData as AzureBindingDefinition}
73           />
74         );
75       case AlmKeys.GitHub:
76         return (
77           <GithubForm
78             onFieldChange={this.props.onFieldChange}
79             formData={formData as GithubBindingDefinition}
80           />
81         );
82       case AlmKeys.BitbucketServer:
83         return (
84           <BitbucketForm
85             onFieldChange={this.props.onFieldChange}
86             formData={
87               formData as BitbucketServerBindingDefinition | BitbucketCloudBindingDefinition
88             }
89             isUpdate={isUpdate}
90             variant={bitbucketVariant}
91             onVariantChange={this.props.onBitbucketVariantChange}
92           />
93         );
94       default:
95         return null;
96     }
97   };
98
99   render() {
100     const { isUpdate, canSubmit, submitting, validationError } = this.props;
101     const header = translate('settings.almintegration.form.header', isUpdate ? 'edit' : 'create');
102
103     const handleSubmit = (event: React.SyntheticEvent<HTMLFormElement>) => {
104       event.preventDefault();
105       this.props.onSubmit();
106     };
107
108     return (
109       <Modal
110         contentLabel={header}
111         onRequestClose={this.props.onCancel}
112         shouldCloseOnOverlayClick={false}
113         size="medium"
114       >
115         <form onSubmit={handleSubmit}>
116           <div className="modal-head">
117             <h2>{header}</h2>
118           </div>
119
120           <div className="modal-body modal-container">
121             {this.renderForm()}
122             {validationError && !canSubmit && (
123               <Alert variant="error">
124                 <p className="spacer-bottom">
125                   {translate('settings.almintegration.configuration_invalid')}
126                 </p>
127                 <ul className="list-styled">
128                   <li>{validationError}</li>
129                 </ul>
130               </Alert>
131             )}
132           </div>
133
134           <div className="modal-foot">
135             <SubmitButton disabled={!canSubmit || submitting}>
136               {translate('settings.almintegration.form.save')}
137               <Spinner className="spacer-left" loading={submitting} />
138             </SubmitButton>
139             <ResetButtonLink onClick={this.props.onCancel}>{translate('cancel')}</ResetButtonLink>
140           </div>
141         </form>
142       </Modal>
143     );
144   }
145 }