]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9392 Add option to choose parent when creating new profile
authorWouter Admiraal <wouter.admiraal@sonarsource.com>
Tue, 8 Jan 2019 14:22:02 +0000 (15:22 +0100)
committersonartech <sonartech@sonarsource.com>
Wed, 16 Jan 2019 08:43:12 +0000 (09:43 +0100)
server/sonar-web/src/main/js/apps/quality-profiles/home/CreateProfileForm.tsx
server/sonar-web/src/main/js/apps/quality-profiles/home/PageHeader.tsx

index cb315fb940041827ca0a80f064cf2dbc3748376c..56d1032bd404bc53504327289f9e9979404c54eb 100644 (file)
  */
 import * as React from 'react';
 import { sortBy } from 'lodash';
-import { getImporters, createQualityProfile } from '../../../api/quality-profiles';
+import {
+  changeProfileParent,
+  createQualityProfile,
+  getImporters
+} from '../../../api/quality-profiles';
 import Modal from '../../../components/controls/Modal';
 import Select from '../../../components/controls/Select';
 import { SubmitButton, ResetButtonLink } from '../../../components/ui/buttons';
 import { translate } from '../../../helpers/l10n';
+import { Profile } from '../types';
 
 interface Props {
   languages: Array<{ key: string; name: string }>;
   onClose: () => void;
   onCreate: Function;
   organization: string | null;
+  profiles: Profile[];
 }
 
 interface State {
@@ -37,6 +43,7 @@ interface State {
   language?: string;
   loading: boolean;
   name: string;
+  parent?: string;
   preloading: boolean;
 }
 
@@ -76,6 +83,10 @@ export default class CreateProfileForm extends React.PureComponent<Props, State>
     this.setState({ language: option.value });
   };
 
+  handleParentChange = (option: { value: string } | null) => {
+    this.setState({ parent: option ? option.value : undefined });
+  };
+
   handleFormSubmit = (event: React.SyntheticEvent<HTMLFormElement>) => {
     event.preventDefault();
 
@@ -87,7 +98,19 @@ export default class CreateProfileForm extends React.PureComponent<Props, State>
     }
 
     createQualityProfile(data).then(
-      (response: any) => this.props.onCreate(response.profile),
+      ({ profile }: { profile: Profile }) => {
+        if (this.state.parent) {
+          // eslint-disable-next-line promise/no-nesting
+          changeProfileParent(profile.key, this.state.parent).then(
+            () => {
+              this.props.onCreate(profile);
+            },
+            () => {}
+          );
+        } else {
+          this.props.onCreate(profile);
+        }
+      },
       () => {
         if (this.mounted) {
           this.setState({ loading: false });
@@ -98,13 +121,27 @@ export default class CreateProfileForm extends React.PureComponent<Props, State>
 
   render() {
     const header = translate('quality_profiles.new_profile');
-
     const languages = sortBy(this.props.languages, 'name');
+    let profiles: Array<{ label: string; value: string }> = [];
+
     const selectedLanguage = this.state.language || languages[0].key;
     const importers = this.state.importers.filter(importer =>
       importer.languages.includes(selectedLanguage)
     );
 
+    if (selectedLanguage) {
+      const languageProfiles = this.props.profiles.filter(p => p.language === selectedLanguage);
+      profiles = [
+        { label: translate('none'), value: '' },
+        ...sortBy(languageProfiles, 'name').map(profile => ({
+          label: profile.isBuiltIn
+            ? `${profile.name} (${translate('quality_profiles.built_in')})`
+            : profile.name,
+          value: profile.key
+        }))
+      ];
+    }
+
     return (
       <Modal contentLabel={header} onRequestClose={this.props.onClose}>
         <form id="create-profile-form" onSubmit={this.handleFormSubmit}>
@@ -152,6 +189,22 @@ export default class CreateProfileForm extends React.PureComponent<Props, State>
                   value={selectedLanguage}
                 />
               </div>
+              {selectedLanguage &&
+                profiles.length && (
+                  <div className="modal-field">
+                    <label htmlFor="create-profile-parent">
+                      {translate('quality_profiles.parent')}
+                    </label>
+                    <Select
+                      clearable={true}
+                      id="create-profile-parent"
+                      name="parentKey"
+                      onChange={this.handleParentChange}
+                      options={profiles}
+                      value={this.state.parent || ''}
+                    />
+                  </div>
+                )}
               {importers.map(importer => (
                 <div
                   className="modal-field spacer-bottom js-importer"
index 5e95dd60e44572f6cdd4f387edcd9be689071c6f..8258c8fdab5fcefd4929d24b7489c4efc4dcf272 100644 (file)
@@ -32,6 +32,7 @@ interface Props {
   actions: Actions;
   languages: Array<{ key: string; name: string }>;
   organization: string | null;
+  profiles: Profile[];
   router: Pick<Router, 'push'>;
   updateProfiles: () => Promise<void>;
 }
@@ -121,6 +122,7 @@ class PageHeader extends React.PureComponent<Props, State> {
             onClose={this.closeCreateForm}
             onCreate={this.handleCreate}
             organization={this.props.organization}
+            profiles={this.props.profiles}
           />
         )}
       </header>