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.

RuleDetailsIssues.tsx 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 { ContentCell, Link, Spinner, SubTitle, Table, TableRow } from 'design-system';
  21. import { keyBy } from 'lodash';
  22. import * as React from 'react';
  23. import { FormattedMessage } from 'react-intl';
  24. import { getComponentData } from '../../../api/components';
  25. import { getFacet } from '../../../api/issues';
  26. import withAvailableFeatures, {
  27. WithAvailableFeaturesProps,
  28. } from '../../../app/components/available-features/withAvailableFeatures';
  29. import Tooltip from '../../../components/controls/Tooltip';
  30. import { DEFAULT_ISSUES_QUERY } from '../../../components/shared/utils';
  31. import { translate } from '../../../helpers/l10n';
  32. import { formatMeasure } from '../../../helpers/measures';
  33. import { getIssuesUrl } from '../../../helpers/urls';
  34. import { Feature } from '../../../types/features';
  35. import { FacetName } from '../../../types/issues';
  36. import { MetricType } from '../../../types/metrics';
  37. import { Dict, RuleDetails } from '../../../types/types';
  38. interface Props extends WithAvailableFeaturesProps {
  39. ruleDetails: Pick<RuleDetails, 'key' | 'type'>;
  40. }
  41. interface Project {
  42. count: number;
  43. key: string;
  44. name: string;
  45. }
  46. interface State {
  47. loading: boolean;
  48. projects?: Project[];
  49. totalIssues?: number;
  50. totalProjects?: number;
  51. }
  52. const MAX_VIOLATING_PROJECTS = 10;
  53. export class RuleDetailsIssues extends React.PureComponent<Props, State> {
  54. mounted = false;
  55. state: State = { loading: false };
  56. componentDidMount() {
  57. this.mounted = true;
  58. this.fetchIssues();
  59. }
  60. componentDidUpdate(prevProps: Props) {
  61. if (prevProps.ruleDetails !== this.props.ruleDetails) {
  62. this.fetchIssues();
  63. }
  64. }
  65. componentWillUnmount() {
  66. this.mounted = false;
  67. }
  68. fetchIssues = () => {
  69. const {
  70. ruleDetails: { key },
  71. } = this.props;
  72. this.setState({ loading: true });
  73. getFacet(
  74. {
  75. ...DEFAULT_ISSUES_QUERY,
  76. rules: key,
  77. },
  78. FacetName.Projects,
  79. ).then(
  80. async ({ facet, response }) => {
  81. if (this.mounted) {
  82. const { paging } = response;
  83. this.setState({
  84. projects: await this.getProjects(facet.slice(0, MAX_VIOLATING_PROJECTS)),
  85. loading: false,
  86. totalIssues: paging.total,
  87. totalProjects: facet.length,
  88. });
  89. }
  90. },
  91. () => {
  92. if (this.mounted) {
  93. this.setState({ loading: false });
  94. }
  95. },
  96. );
  97. };
  98. /**
  99. * Retrieve the names of the projects, to display nicely
  100. * (The facet only contains key & count)
  101. */
  102. getProjects = async (facet: { count: number; val: string }[]) => {
  103. const projects: Dict<{ key: string; name: string }> = keyBy(
  104. await Promise.all(
  105. facet.map((item) =>
  106. getComponentData({ component: item.val })
  107. .then((response) => ({
  108. key: item.val,
  109. name: response.component.name,
  110. }))
  111. .catch(() => ({ key: item.val, name: item.val })),
  112. ),
  113. ),
  114. 'key',
  115. );
  116. return facet.map((item) => {
  117. return { count: item.count, key: item.val, name: projects[item.val].name };
  118. });
  119. };
  120. renderTotal = () => {
  121. const {
  122. ruleDetails: { key },
  123. } = this.props;
  124. const { totalIssues: total } = this.state;
  125. if (total === undefined) {
  126. return null;
  127. }
  128. const path = getIssuesUrl({ ...DEFAULT_ISSUES_QUERY, rules: key });
  129. const totalItem = (
  130. <span className="sw-ml-1">
  131. {'('}
  132. <Link to={path}>{total}</Link>
  133. {')'}
  134. </span>
  135. );
  136. if (!this.props.hasFeature(Feature.BranchSupport)) {
  137. return totalItem;
  138. }
  139. return (
  140. <Tooltip overlay={translate('coding_rules.issues.only_main_branches')}>{totalItem}</Tooltip>
  141. );
  142. };
  143. renderProject = (project: Project) => {
  144. const {
  145. ruleDetails: { key },
  146. } = this.props;
  147. const path = getIssuesUrl({ ...DEFAULT_ISSUES_QUERY, rules: key, projects: project.key });
  148. return (
  149. <TableRow key={project.key}>
  150. <ContentCell>{project.name}</ContentCell>
  151. <ContentCell>
  152. <Link to={path}>{formatMeasure(project.count, MetricType.Integer)}</Link>
  153. </ContentCell>
  154. </TableRow>
  155. );
  156. };
  157. render() {
  158. const { ruleDetails } = this.props;
  159. const { loading, projects = [], totalProjects } = this.state;
  160. return (
  161. <div className="sw-mb-8">
  162. <Spinner loading={loading}>
  163. <SubTitle>
  164. {translate('coding_rules.issues')}
  165. {this.renderTotal()}
  166. </SubTitle>
  167. {projects.length > 0 ? (
  168. <>
  169. <Table
  170. className="sw-mt-6"
  171. columnCount={2}
  172. header={
  173. <TableRow>
  174. <ContentCell colSpan={2}>
  175. {translate('coding_rules.most_violating_projects')}
  176. </ContentCell>
  177. </TableRow>
  178. }
  179. >
  180. {projects.map(this.renderProject)}
  181. </Table>
  182. {totalProjects !== undefined && totalProjects > projects.length && (
  183. <div className="sw-text-center sw-mt-4">
  184. <FormattedMessage
  185. id="coding_rules.most_violating_projects.more_x"
  186. values={{
  187. count: totalProjects - projects.length,
  188. link: (
  189. <Link to={getIssuesUrl({ resolved: 'false', rules: ruleDetails.key })}>
  190. <FormattedMessage id="coding_rules.most_violating_projects.link" />
  191. </Link>
  192. ),
  193. }}
  194. />
  195. </div>
  196. )}
  197. </>
  198. ) : (
  199. <div className="sw-mb-6">
  200. {translate('coding_rules.no_issue_detected_for_projects')}
  201. </div>
  202. )}
  203. </Spinner>
  204. </div>
  205. );
  206. }
  207. }
  208. export default withAvailableFeatures(RuleDetailsIssues);