You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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 { FlagMessage, LargeCenteredLayout, PageContentFontWrapper, Spinner } from 'design-system';
  21. import * as React from 'react';
  22. import { Navigate } from 'react-router-dom';
  23. import { getScannableProjects } from '../../../api/components';
  24. import withCurrentUserContext from '../../../app/components/current-user/withCurrentUserContext';
  25. import { getBranchLikeDisplayName, isBranch, isMainBranch } from '../../../helpers/branch-like';
  26. import { translate, translateWithParameters } from '../../../helpers/l10n';
  27. import { getProjectTutorialLocation } from '../../../helpers/urls';
  28. import { hasGlobalPermission } from '../../../helpers/users';
  29. import { useTaskForComponentQuery } from '../../../queries/component';
  30. import { BranchLike } from '../../../types/branch-like';
  31. import { ComponentQualifier } from '../../../types/component';
  32. import { Permissions } from '../../../types/permissions';
  33. import { TaskTypes } from '../../../types/tasks';
  34. import { Component } from '../../../types/types';
  35. import { CurrentUser, isLoggedIn } from '../../../types/users';
  36. export interface EmptyOverviewProps {
  37. branchLike?: BranchLike;
  38. branchLikes: BranchLike[];
  39. component: Component;
  40. currentUser: CurrentUser;
  41. }
  42. export function EmptyOverview(props: Readonly<EmptyOverviewProps>) {
  43. const { branchLike, branchLikes, component, currentUser } = props;
  44. const [currentUserCanScanProject, setCurrentUserCanScanProject] = React.useState(
  45. hasGlobalPermission(currentUser, Permissions.Scan),
  46. );
  47. const { data, isLoading } = useTaskForComponentQuery(component);
  48. const hasQueuedAnalyses =
  49. data && data.queue.filter((task) => task.type === TaskTypes.Report).length > 0;
  50. const hasPermissionSyncInProgess =
  51. data &&
  52. data.queue.filter((task) => task.type === TaskTypes.GithubProjectPermissionsProvisioning)
  53. .length > 0;
  54. React.useEffect(() => {
  55. if (currentUserCanScanProject || !isLoggedIn(currentUser)) {
  56. return;
  57. }
  58. getScannableProjects()
  59. .then(({ projects }) => {
  60. setCurrentUserCanScanProject(projects.find((p) => p.key === component.key) !== undefined);
  61. })
  62. .catch(() => {});
  63. }, [component.key, currentUser, currentUserCanScanProject]);
  64. if (isLoading) {
  65. return <Spinner loading />;
  66. }
  67. if (component.qualifier === ComponentQualifier.Application) {
  68. return (
  69. <LargeCenteredLayout className="sw-pt-8">
  70. <FlagMessage className="sw-w-full" variant="warning">
  71. {translate('provisioning.no_analysis.application')}
  72. </FlagMessage>
  73. </LargeCenteredLayout>
  74. );
  75. } else if (!isBranch(branchLike)) {
  76. return null;
  77. }
  78. const hasBranches = branchLikes.length > 1;
  79. const hasBadBranchConfig =
  80. branchLikes.length > 2 ||
  81. (branchLikes.length === 2 && branchLikes.some((branch) => isBranch(branch)));
  82. if (hasPermissionSyncInProgess) {
  83. return (
  84. <LargeCenteredLayout className="sw-pt-8">
  85. <PageContentFontWrapper>
  86. <FlagMessage variant="warning">
  87. {translate('provisioning.permission_synch_in_progress')}
  88. <Spinner className="sw-ml-8 sw-hidden" aria-hidden loading />
  89. </FlagMessage>
  90. </PageContentFontWrapper>
  91. </LargeCenteredLayout>
  92. );
  93. }
  94. const showTutorial =
  95. currentUserCanScanProject && isMainBranch(branchLike) && !hasBranches && !hasQueuedAnalyses;
  96. if (showTutorial && isLoggedIn(currentUser)) {
  97. return <Navigate replace to={getProjectTutorialLocation(component.key)} />;
  98. }
  99. let warning;
  100. if (isLoggedIn(currentUser) && isMainBranch(branchLike) && hasBranches && hasBadBranchConfig) {
  101. warning = translateWithParameters(
  102. 'provisioning.no_analysis_on_main_branch.bad_configuration',
  103. getBranchLikeDisplayName(branchLike),
  104. translate('branches.main_branch'),
  105. );
  106. } else {
  107. warning = translateWithParameters(
  108. 'provisioning.no_analysis_on_main_branch',
  109. getBranchLikeDisplayName(branchLike),
  110. );
  111. }
  112. return (
  113. <LargeCenteredLayout className="sw-pt-8">
  114. <PageContentFontWrapper>
  115. <FlagMessage className="sw-w-full" variant="warning">
  116. {warning}
  117. </FlagMessage>
  118. </PageContentFontWrapper>
  119. </LargeCenteredLayout>
  120. );
  121. }
  122. export default withCurrentUserContext(EmptyOverview);