diff options
author | Grégoire Aubert <gregoire.aubert@sonarsource.com> | 2018-10-24 10:36:44 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2018-11-16 20:21:04 +0100 |
commit | 7c133fcc9d877837e18fef5c9d83cce463adbd7f (patch) | |
tree | f0e71b401bb7e9348e26d1576c7f9285b0ea6d3a /server/sonar-web/src/main/js/apps/create/organization/CreateOrganization.tsx | |
parent | f714aa7a66fd87a39898390edd0c15f0c77dd248 (diff) | |
download | sonarqube-7c133fcc9d877837e18fef5c9d83cce463adbd7f.tar.gz sonarqube-7c133fcc9d877837e18fef5c9d83cce463adbd7f.zip |
SONAR-11321 Suggest valid organization key
Diffstat (limited to 'server/sonar-web/src/main/js/apps/create/organization/CreateOrganization.tsx')
-rw-r--r-- | server/sonar-web/src/main/js/apps/create/organization/CreateOrganization.tsx | 31 |
1 files changed, 25 insertions, 6 deletions
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 = () => { |