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.

AnalysisErrorMessage.tsx 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 { Link } from 'design-system';
  21. import React from 'react';
  22. import { FormattedMessage } from 'react-intl';
  23. import { useLocation } from 'react-router-dom';
  24. import { isBranch, isMainBranch, isPullRequest } from '../../../helpers/branch-like';
  25. import { hasMessage, translate } from '../../../helpers/l10n';
  26. import { getComponentBackgroundTaskUrl } from '../../../helpers/urls';
  27. import { useBranchesQuery } from '../../../queries/branch';
  28. import { BranchLike } from '../../../types/branch-like';
  29. import { Task } from '../../../types/tasks';
  30. import { Component } from '../../../types/types';
  31. interface Props {
  32. component: Component;
  33. currentTask: Task;
  34. onLeave: () => void;
  35. }
  36. function isSameBranch(task: Task, branchLike?: BranchLike) {
  37. if (branchLike) {
  38. if (isMainBranch(branchLike)) {
  39. return (!task.pullRequest && !task.branch) || branchLike.name === task.branch;
  40. }
  41. if (isPullRequest(branchLike)) {
  42. return branchLike.key === task.pullRequest;
  43. }
  44. if (isBranch(branchLike)) {
  45. return branchLike.name === task.branch;
  46. }
  47. }
  48. return !task.branch && !task.pullRequest;
  49. }
  50. export function AnalysisErrorMessage(props: Props) {
  51. const { component, currentTask } = props;
  52. const { data: { branchLike } = {} } = useBranchesQuery(component);
  53. const currentTaskOnSameBranch = isSameBranch(currentTask, branchLike);
  54. const location = useLocation();
  55. const backgroundTaskUrl = getComponentBackgroundTaskUrl(component.key);
  56. const canSeeBackgroundTasks = component.configuration?.showBackgroundTasks;
  57. const isOnBackgroundTaskPage = location.pathname === backgroundTaskUrl.pathname;
  58. const branch =
  59. currentTask.branch ??
  60. `${currentTask.pullRequest ?? ''}${
  61. currentTask.pullRequestTitle ? ' - ' + currentTask.pullRequestTitle : ''
  62. }`;
  63. let messageKey;
  64. if (currentTaskOnSameBranch === false && branch) {
  65. messageKey = 'component_navigation.status.failed_branch';
  66. } else {
  67. messageKey = 'component_navigation.status.failed';
  68. }
  69. let type;
  70. if (hasMessage('background_task.type', currentTask.type)) {
  71. messageKey += '_X';
  72. type = translate('background_task.type', currentTask.type);
  73. }
  74. let url;
  75. let stacktrace;
  76. if (canSeeBackgroundTasks) {
  77. messageKey += '.admin';
  78. if (isOnBackgroundTaskPage) {
  79. messageKey += '.help';
  80. stacktrace = translate('background_tasks.show_stacktrace');
  81. } else {
  82. messageKey += '.link';
  83. url = (
  84. <Link onClick={props.onLeave} to={backgroundTaskUrl}>
  85. {translate('background_tasks.page')}
  86. </Link>
  87. );
  88. }
  89. }
  90. return (
  91. <FormattedMessage
  92. defaultMessage={translate(messageKey)}
  93. id={messageKey}
  94. values={{ branch, url, stacktrace, type }}
  95. />
  96. );
  97. }