]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-19454 New code definition is made part of GitHub project onboarding
authorAndrey Luiz <andrey.luiz@sonarsource.com>
Mon, 12 Jun 2023 09:29:04 +0000 (11:29 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 14 Jun 2023 09:51:06 +0000 (09:51 +0000)
server/sonar-web/src/main/js/api/alm-integrations.ts
server/sonar-web/src/main/js/api/mocks/AlmIntegrationsServiceMock.ts
server/sonar-web/src/main/js/apps/create/project/CreateProjectPage.tsx
server/sonar-web/src/main/js/apps/create/project/Github/GitHubProjectCreate.tsx
server/sonar-web/src/main/js/apps/create/project/Github/GitHubProjectCreateRenderer.tsx
server/sonar-web/src/main/js/apps/create/project/__tests__/GitHub-it.tsx

index bd225e1e2dcda182b6e54a10672ce5b47860c370..246da52cda2dc1b9f71e02c5125614ae64d89aac 100644 (file)
@@ -190,16 +190,23 @@ export function importBitbucketCloudRepository(data: {
   return postJSON('/api/alm_integrations/import_bitbucketcloud_repo', data).catch(throwGlobalError);
 }
 
-export function importGithubRepository(
-  almSetting: string,
-  organization: string,
-  repositoryKey: string
-): Promise<{ project: ProjectBase }> {
-  return postJSON('/api/alm_integrations/import_github_project', {
-    almSetting,
-    organization,
-    repositoryKey,
-  }).catch(throwGlobalError);
+export function setupGithubProjectCreation(data: {
+  almSetting: string;
+  organization: string;
+  repositoryKey: string;
+}) {
+  return (newCodeDefinitionType?: string, newCodeDefinitionValue?: string) =>
+    importGithubRepository({ ...data, newCodeDefinitionType, newCodeDefinitionValue });
+}
+
+export function importGithubRepository(data: {
+  almSetting: string;
+  organization: string;
+  repositoryKey: string;
+  newCodeDefinitionType?: string;
+  newCodeDefinitionValue?: string;
+}): Promise<{ project: ProjectBase }> {
+  return postJSON('/api/alm_integrations/import_github_project', data).catch(throwGlobalError);
 }
 
 export function getGithubOrganizations(
index e34143b5c02e1c6f950f4418886ada946ad80070..6adbdcd7ec8daee434ece1799d141e6228171601 100644 (file)
@@ -61,6 +61,7 @@ import {
   setupBitbucketCloudProjectCreation,
   setupBitbucketServerProjectCreation,
   setupGitlabProjectCreation,
+  setupGithubProjectCreation,
 } from '../alm-integrations';
 
 export default class AlmIntegrationsServiceMock {
@@ -200,6 +201,7 @@ export default class AlmIntegrationsServiceMock {
     jest.mocked(searchAzureRepositories).mockImplementation(this.searchAzureRepositories);
     jest.mocked(setupAzureProjectCreation).mockReturnValue(() => this.importAzureRepository());
     jest.mocked(importAzureRepository).mockImplementation(this.importAzureRepository);
+    jest.mocked(setupGithubProjectCreation).mockReturnValue(() => this.importGithubRepository());
     jest.mocked(importGithubRepository).mockImplementation(this.importGithubRepository);
     jest
       .mocked(searchForBitbucketCloudRepositories)
index 0a8d1add2b6bd89776af70b7f256a8ba04e30b02..afa24320ec467b601918eb4518cb6dbd0c8bbcda 100644 (file)
@@ -243,7 +243,7 @@ export class CreateProjectPage extends React.PureComponent<CreateProjectPageProp
             canAdmin={!!canAdmin}
             loadingBindings={loading}
             location={location}
-            onProjectCreate={this.handleProjectCreate}
+            onProjectSetupDone={this.handleProjectSetupDone}
             router={router}
             almInstances={githubSettings}
           />
index a07808a46ba821b27b8b2bb2d8e2841078a17159..4e625e2547f75e14ce6efd61fedb3f1be825ea13 100644 (file)
@@ -24,7 +24,7 @@ import {
   getGithubClientId,
   getGithubOrganizations,
   getGithubRepositories,
-  importGithubRepository,
+  setupGithubProjectCreation,
 } from '../../../../api/alm-integrations';
 import { Location, Router } from '../../../../components/hoc/withRouter';
 import { getHostUrl } from '../../../../helpers/urls';
@@ -32,11 +32,12 @@ import { GithubOrganization, GithubRepository } from '../../../../types/alm-inte
 import { AlmKeys, AlmSettingsInstance } from '../../../../types/alm-settings';
 import { Paging } from '../../../../types/types';
 import GitHubProjectCreateRenderer from './GitHubProjectCreateRenderer';
+import { CreateProjectApiCallback } from '../types';
 
 interface Props {
   canAdmin: boolean;
   loadingBindings: boolean;
-  onProjectCreate: (projectKey: string) => void;
+  onProjectSetupDone: (createProject: CreateProjectApiCallback) => void;
   almInstances: AlmSettingsInstance[];
   location: Location;
   router: Router;
@@ -44,7 +45,6 @@ interface Props {
 
 interface State {
   error: boolean;
-  importing: boolean;
   loadingOrganizations: boolean;
   loadingRepositories: boolean;
   organizations: GithubOrganization[];
@@ -66,7 +66,6 @@ export default class GitHubProjectCreate extends React.Component<Props, State> {
 
     this.state = {
       error: false,
-      importing: false,
       loadingOrganizations: true,
       loadingRepositories: false,
       organizations: [],
@@ -269,25 +268,17 @@ export default class GitHubProjectCreate extends React.Component<Props, State> {
     }
   };
 
-  handleImportRepository = async () => {
+  handleImportRepository = () => {
     const { selectedOrganization, selectedRepository, selectedAlmInstance } = this.state;
 
     if (selectedAlmInstance && selectedOrganization && selectedRepository) {
-      this.setState({ importing: true });
-
-      try {
-        const { project } = await importGithubRepository(
-          selectedAlmInstance.key,
-          selectedOrganization.key,
-          selectedRepository.key
-        );
-
-        this.props.onProjectCreate(project.key);
-      } finally {
-        if (this.mounted) {
-          this.setState({ importing: false });
-        }
-      }
+      this.props.onProjectSetupDone(
+        setupGithubProjectCreation({
+          almSetting: selectedAlmInstance.key,
+          organization: selectedOrganization.key,
+          repositoryKey: selectedRepository.key,
+        })
+      );
     }
   };
 
@@ -306,7 +297,6 @@ export default class GitHubProjectCreate extends React.Component<Props, State> {
     const { canAdmin, loadingBindings, almInstances } = this.props;
     const {
       error,
-      importing,
       loadingOrganizations,
       loadingRepositories,
       organizations,
@@ -322,7 +312,6 @@ export default class GitHubProjectCreate extends React.Component<Props, State> {
       <GitHubProjectCreateRenderer
         canAdmin={canAdmin}
         error={error}
-        importing={importing}
         loadingBindings={loadingBindings}
         loadingOrganizations={loadingOrganizations}
         loadingRepositories={loadingRepositories}
index ed2d238aad2f1e26c369c721bcf85db8f52a9d06..ec5442bb3e987c7d8ddd3ecb0b07ef7c3e6a4a54 100644 (file)
@@ -41,12 +41,10 @@ import { ComponentQualifier } from '../../../../types/component';
 import { Paging } from '../../../../types/types';
 import AlmSettingsInstanceDropdown from '../components/AlmSettingsInstanceDropdown';
 import CreateProjectPageHeader from '../components/CreateProjectPageHeader';
-import InstanceNewCodeDefinitionComplianceWarning from '../components/InstanceNewCodeDefinitionComplianceWarning';
 
 export interface GitHubProjectCreateRendererProps {
   canAdmin: boolean;
   error: boolean;
-  importing: boolean;
   loadingBindings: boolean;
   loadingOrganizations: boolean;
   loadingRepositories: boolean;
@@ -72,7 +70,6 @@ function orgToOption({ key, name }: GithubOrganization) {
 
 function renderRepositoryList(props: GitHubProjectCreateRendererProps) {
   const {
-    importing,
     loadingRepositories,
     repositories,
     repositoryPaging,
@@ -86,7 +83,7 @@ function renderRepositoryList(props: GitHubProjectCreateRendererProps) {
     (!!selectedRepository && selectedRepository.key === repository.key);
 
   const isDisabled = (repository: GithubRepository) =>
-    !!repository.sqProjectKey || loadingRepositories || importing;
+    !!repository.sqProjectKey || loadingRepositories;
 
   return (
     selectedOrganization &&
@@ -175,7 +172,6 @@ export default function GitHubProjectCreateRenderer(props: GitHubProjectCreateRe
   const {
     canAdmin,
     error,
-    importing,
     loadingBindings,
     loadingOrganizations,
     organizations,
@@ -195,10 +191,9 @@ export default function GitHubProjectCreateRenderer(props: GitHubProjectCreateRe
         additionalActions={
           selectedOrganization && (
             <div className="display-flex-center pull-right">
-              <DeferredSpinner className="spacer-right" loading={importing} />
               <Button
                 className="button-large button-primary"
-                disabled={!selectedRepository || importing}
+                disabled={!selectedRepository}
                 onClick={props.onImportRepository}
               >
                 {translate('onboarding.create_project.import_selected_repo')}
@@ -256,49 +251,44 @@ export default function GitHubProjectCreateRenderer(props: GitHubProjectCreateRe
       )}
 
       {!error && (
-        <>
-          <InstanceNewCodeDefinitionComplianceWarning />
-          <DeferredSpinner loading={loadingOrganizations}>
-            <div className="form-field">
-              <label htmlFor="github-choose-organization">
-                {translate('onboarding.create_project.github.choose_organization')}
-              </label>
-              {organizations.length > 0 ? (
-                <Select
-                  inputId="github-choose-organization"
-                  className="input-super-large"
-                  options={organizations.map(orgToOption)}
-                  onChange={({ value }: LabelValueSelectOption) =>
-                    props.onSelectOrganization(value)
-                  }
-                  value={selectedOrganization ? orgToOption(selectedOrganization) : null}
-                />
-              ) : (
-                !loadingOrganizations && (
-                  <Alert className="spacer-top" variant="error">
-                    {canAdmin ? (
-                      <FormattedMessage
-                        id="onboarding.create_project.github.no_orgs_admin"
-                        defaultMessage={translate('onboarding.create_project.github.no_orgs_admin')}
-                        values={{
-                          link: (
-                            <Link to="/admin/settings?category=almintegration">
-                              {translate(
-                                'onboarding.create_project.github.warning.message_admin.link'
-                              )}
-                            </Link>
-                          ),
-                        }}
-                      />
-                    ) : (
-                      translate('onboarding.create_project.github.no_orgs')
-                    )}
-                  </Alert>
-                )
-              )}
-            </div>
-          </DeferredSpinner>
-        </>
+        <DeferredSpinner loading={loadingOrganizations}>
+          <div className="form-field">
+            <label htmlFor="github-choose-organization">
+              {translate('onboarding.create_project.github.choose_organization')}
+            </label>
+            {organizations.length > 0 ? (
+              <Select
+                inputId="github-choose-organization"
+                className="input-super-large"
+                options={organizations.map(orgToOption)}
+                onChange={({ value }: LabelValueSelectOption) => props.onSelectOrganization(value)}
+                value={selectedOrganization ? orgToOption(selectedOrganization) : null}
+              />
+            ) : (
+              !loadingOrganizations && (
+                <Alert className="spacer-top" variant="error">
+                  {canAdmin ? (
+                    <FormattedMessage
+                      id="onboarding.create_project.github.no_orgs_admin"
+                      defaultMessage={translate('onboarding.create_project.github.no_orgs_admin')}
+                      values={{
+                        link: (
+                          <Link to="/admin/settings?category=almintegration">
+                            {translate(
+                              'onboarding.create_project.github.warning.message_admin.link'
+                            )}
+                          </Link>
+                        ),
+                      }}
+                    />
+                  ) : (
+                    translate('onboarding.create_project.github.no_orgs')
+                  )}
+                </Alert>
+              )
+            )}
+          </div>
+        </DeferredSpinner>
       )}
 
       {renderRepositoryList(props)}
index 481a7db84cadd6f7cf8ab51bece6d010ed3b23fd..0f2de817cb7a128dd155ebffd5bee9de9713fd43 100644 (file)
@@ -140,6 +140,17 @@ it('should show import project feature when the authentication is successfull',
   expect(importButton).toBeEnabled();
   await user.click(importButton);
 
+  expect(
+    screen.getByRole('heading', { name: 'onboarding.create_project.new_code_definition.title' })
+  ).toBeInTheDocument();
+
+  await user.click(screen.getByRole('radio', { name: 'new_code_definition.global_setting' }));
+  await user.click(
+    screen.getByRole('button', {
+      name: 'onboarding.create_project.new_code_definition.create_project',
+    })
+  );
+
   expect(await screen.findByText('/dashboard?id=key')).toBeInTheDocument();
 });