summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/reports/codequality_report/store/actions.js
blob: ddd1747899fdad6d28a2e45f4a6c97cfa51a5c37 (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
import axios from '~/lib/utils/axios_utils';
import * as types from './mutation_types';
import { parseCodeclimateMetrics, doCodeClimateComparison } from './utils/codequality_comparison';

export const setPaths = ({ commit }, paths) => commit(types.SET_PATHS, paths);

export const fetchReports = ({ state, dispatch, commit }, diffFeatureFlagEnabled) => {
  commit(types.REQUEST_REPORTS);

  if (diffFeatureFlagEnabled) {
    return axios
      .get(state.reportsPath)
      .then(({ data }) => {
        return dispatch('receiveReportsSuccess', {
          newIssues: parseCodeclimateMetrics(data.new_errors, state.headBlobPath),
          resolvedIssues: parseCodeclimateMetrics(data.resolved_errors, state.baseBlobPath),
        });
      })
      .catch((error) => dispatch('receiveReportsError', error));
  }
  if (!state.basePath) {
    return dispatch('receiveReportsError');
  }
  return Promise.all([axios.get(state.headPath), axios.get(state.basePath)])
    .then((results) =>
      doCodeClimateComparison(
        parseCodeclimateMetrics(results[0].data, state.headBlobPath),
        parseCodeclimateMetrics(results[1].data, state.baseBlobPath),
      ),
    )
    .then((data) => dispatch('receiveReportsSuccess', data))
    .catch((error) => dispatch('receiveReportsError', error));
};

export const receiveReportsSuccess = ({ commit }, data) => {
  commit(types.RECEIVE_REPORTS_SUCCESS, data);
};

export const receiveReportsError = ({ commit }, error) => {
  commit(types.RECEIVE_REPORTS_ERROR, error);
};