From 7c133fcc9d877837e18fef5c9d83cce463adbd7f Mon Sep 17 00:00:00 2001
From: Grégoire Aubert <gregoire.aubert@sonarsource.com>
Date: Wed, 24 Oct 2018 10:36:44 +0200
Subject: SONAR-11321 Suggest valid organization key

---
 .../create/organization/CreateOrganization.tsx     | 31 +++++++++++++++++-----
 1 file changed, 25 insertions(+), 6 deletions(-)

(limited to 'server/sonar-web/src/main/js/apps/create/organization/CreateOrganization.tsx')

diff --git a/server/sonar-web/src/main/js/apps/create/organization/CreateOrganization.tsx b/server/sonar-web/src/main/js/apps/create/organization/CreateOrganization.tsx
index 81b219784cd..1023408535c 100644
--- a/server/sonar-web/src/main/js/apps/create/organization/CreateOrganization.tsx
+++ b/server/sonar-web/src/main/js/apps/create/organization/CreateOrganization.tsx
@@ -18,6 +18,7 @@
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
 import * as React from 'react';
+import { times } from 'lodash';
 import { connect } from 'react-redux';
 import { Dispatch } from 'redux';
 import { Helmet } from 'react-helmet';
@@ -48,6 +49,7 @@ import {
 } from '../../../app/types';
 import { hasAdvancedALMIntegration, isPersonal } from '../../../helpers/almIntegrations';
 import { translate } from '../../../helpers/l10n';
+import { slugify } from '../../../helpers/strings';
 import { getOrganizationUrl } from '../../../helpers/urls';
 import * as api from '../../../api/organizations';
 import * as actions from '../../../store/organizations';
@@ -117,20 +119,37 @@ export class CreateOrganization extends React.PureComponent<Props & WithRouterPr
     });
   };
 
+  fetchValidOrgKey = (almOrganization: AlmOrganization) => {
+    const key = slugify(almOrganization.key);
+    const keys = [key, ...times(9, i => `${key}-${i + 1}`)];
+    return api
+      .getOrganizations({ organizations: keys.join(',') })
+      .then(
+        ({ organizations }) => {
+          const availableKey = keys.find(key => !organizations.find(o => o.key === key));
+          return availableKey || `${key}-${Math.ceil(Math.random() * 1000) + 10}`;
+        },
+        () => key
+      )
+      .then(key => {
+        return { ...almOrganization, key };
+      });
+  };
+
   fetchAlmOrganization = (installationId: string) => {
     this.setState({ almOrgLoading: true });
-    return getAlmOrganization({ installationId }).then(
-      almOrganization => {
+    return getAlmOrganization({ installationId })
+      .then(this.fetchValidOrgKey)
+      .then(almOrganization => {
         if (this.mounted) {
           this.setState({ almOrganization, almOrgLoading: false });
         }
-      },
-      () => {
+      })
+      .catch(() => {
         if (this.mounted) {
           this.setState({ almOrgLoading: false });
         }
-      }
-    );
+      });
   };
 
   fetchSubscriptionPlans = () => {
-- 
cgit v1.2.3

