aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/projects/store/actions.js
blob: 5a6f5affdec93fcb960b82974fa5d4ebfe6d581e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * SonarQube
 * Copyright (C) 2009-2016 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import groupBy from 'lodash/groupBy';
import { searchProjects } from '../../../api/components';
import { addGlobalErrorMessage } from '../../../store/globalMessages/duck';
import { parseError } from '../../code/utils';
import { receiveComponents } from '../../../store/components/actions';
import { receiveProjects, receiveMoreProjects } from './projectsDuck';
import { updateState } from './stateDuck';
import { getProjectsAppState } from '../../../store/rootReducer';
import { getMeasuresForProjects } from '../../../api/measures';
import { receiveComponentsMeasures } from '../../../store/measures/actions';
import { convertToFilter } from './utils';
import { receiveFavorites } from '../../../store/favorites/duck';

const PAGE_SIZE = 50;

const METRICS = [
  'alert_status',
  'reliability_rating',
  'security_rating',
  'sqale_rating',
  'duplicated_lines_density',
  'coverage',
  'ncloc',
  'ncloc_language_distribution'
];

const FACETS = [
  'reliability_rating',
  'security_rating',
  'sqale_rating',
  'coverage',
  'duplicated_lines_density',
  'ncloc',
  'alert_status'
];

const onFail = dispatch => error => {
  parseError(error).then(message => dispatch(addGlobalErrorMessage(message)));
  dispatch(updateState({ loading: false }));
};

const onReceiveMeasures = (dispatch, expectedProjectKeys) => response => {
  const byComponentKey = groupBy(response.measures, 'component');

  const toStore = {};

  // fill store with empty objects for expected projects
  // this is required to not have "null"s for provisioned projects
  expectedProjectKeys.forEach(projectKey => toStore[projectKey] = {});

  Object.keys(byComponentKey).forEach(componentKey => {
    const measures = {};
    byComponentKey[componentKey].forEach(measure => {
      measures[measure.metric] = measure.value;
    });
    toStore[componentKey] = measures;
  });

  dispatch(receiveComponentsMeasures(toStore));
};

const fetchProjectMeasures = projects => dispatch => {
  if (!projects.length) {
    return Promise.resolve();
  }

  const projectKeys = projects.map(project => project.key);
  return getMeasuresForProjects(projectKeys, METRICS).then(onReceiveMeasures(dispatch, projectKeys), onFail(dispatch));
};

const handleFavorites = (dispatch, projects) => {
  const favorites = projects.filter(project => project.isFavorite);
  if (favorites.length) {
    dispatch(receiveFavorites(favorites));
  }
};

const onReceiveProjects = dispatch => response => {
  dispatch(receiveComponents(response.components));
  dispatch(receiveProjects(response.components, response.facets));
  handleFavorites(dispatch, response.components);
  dispatch(fetchProjectMeasures(response.components)).then(() => {
    dispatch(updateState({ loading: false }));
  });
  dispatch(updateState({
    total: response.paging.total,
    pageIndex: response.paging.pageIndex
  }));
};

const onReceiveMoreProjects = dispatch => response => {
  dispatch(receiveComponents(response.components));
  dispatch(receiveMoreProjects(response.components));
  handleFavorites(dispatch, response.components);
  dispatch(fetchProjectMeasures(response.components)).then(() => {
    dispatch(updateState({ loading: false }));
  });
  dispatch(updateState({ pageIndex: response.paging.pageIndex }));
};

export const fetchProjects = (query, isFavorite) => dispatch => {
  dispatch(updateState({ loading: true }));
  const data = { ps: PAGE_SIZE, facets: FACETS.join() };
  const filter = convertToFilter(query, isFavorite);
  if (filter) {
    data.filter = filter;
  }
  return searchProjects(data).then(onReceiveProjects(dispatch), onFail(dispatch));
};

export const fetchMoreProjects = (query, isFavorite) => (dispatch, getState) => {
  dispatch(updateState({ loading: true }));
  const state = getState();
  const { pageIndex } = getProjectsAppState(state);
  const data = { ps: PAGE_SIZE, p: pageIndex + 1 };
  const filter = convertToFilter(query, isFavorite);
  if (filter) {
    data.filter = filter;
  }
  return searchProjects(data).then(onReceiveMoreProjects(dispatch), onFail(dispatch));
};