merge_request.js 5.42 KB
Newer Older
1 2
import flash from '~/flash';
import { __ } from '~/locale';
Tim Zallmann's avatar
Tim Zallmann committed
3 4
import service from '../../services';
import * as types from '../mutation_types';
5
import { activityBarViews } from '../../constants';
Tim Zallmann's avatar
Tim Zallmann committed
6 7

export const getMergeRequestData = (
8
  { commit, dispatch, state },
9
  { projectId, mergeRequestId, targetProjectId = null, force = false } = {},
Tim Zallmann's avatar
Tim Zallmann committed
10 11 12 13
) =>
  new Promise((resolve, reject) => {
    if (!state.projects[projectId].mergeRequests[mergeRequestId] || force) {
      service
14 15 16
        .getProjectMergeRequestData(targetProjectId || projectId, mergeRequestId, {
          render_html: true,
        })
Phil Hughes's avatar
Phil Hughes committed
17
        .then(({ data }) => {
Tim Zallmann's avatar
Tim Zallmann committed
18 19 20 21 22
          commit(types.SET_MERGE_REQUEST, {
            projectPath: projectId,
            mergeRequestId,
            mergeRequest: data,
          });
23
          commit(types.SET_CURRENT_MERGE_REQUEST, mergeRequestId);
Tim Zallmann's avatar
Tim Zallmann committed
24 25 26
          resolve(data);
        })
        .catch(() => {
27 28 29 30 31 32 33 34 35
          dispatch('setErrorMessage', {
            text: __('An error occured whilst loading the merge request.'),
            action: payload =>
              dispatch('getMergeRequestData', payload).then(() =>
                dispatch('setErrorMessage', null),
              ),
            actionText: __('Please try again'),
            actionPayload: { projectId, mergeRequestId, force },
          });
Tim Zallmann's avatar
Tim Zallmann committed
36 37 38 39 40 41 42 43
          reject(new Error(`Merge Request not loaded ${projectId}`));
        });
    } else {
      resolve(state.projects[projectId].mergeRequests[mergeRequestId]);
    }
  });

export const getMergeRequestChanges = (
44
  { commit, dispatch, state },
45
  { projectId, mergeRequestId, targetProjectId = null, force = false } = {},
Tim Zallmann's avatar
Tim Zallmann committed
46 47
) =>
  new Promise((resolve, reject) => {
48
    if (!state.projects[projectId].mergeRequests[mergeRequestId].changes.length || force) {
Tim Zallmann's avatar
Tim Zallmann committed
49
      service
50
        .getProjectMergeRequestChanges(targetProjectId || projectId, mergeRequestId)
Phil Hughes's avatar
Phil Hughes committed
51
        .then(({ data }) => {
Tim Zallmann's avatar
Tim Zallmann committed
52 53 54 55 56 57 58 59
          commit(types.SET_MERGE_REQUEST_CHANGES, {
            projectPath: projectId,
            mergeRequestId,
            changes: data,
          });
          resolve(data);
        })
        .catch(() => {
60 61 62 63 64 65 66 67 68
          dispatch('setErrorMessage', {
            text: __('An error occured whilst loading the merge request changes.'),
            action: payload =>
              dispatch('getMergeRequestChanges', payload).then(() =>
                dispatch('setErrorMessage', null),
              ),
            actionText: __('Please try again'),
            actionPayload: { projectId, mergeRequestId, force },
          });
Tim Zallmann's avatar
Tim Zallmann committed
69 70 71 72 73 74 75
          reject(new Error(`Merge Request Changes not loaded ${projectId}`));
        });
    } else {
      resolve(state.projects[projectId].mergeRequests[mergeRequestId].changes);
    }
  });

Tim Zallmann's avatar
Tim Zallmann committed
76
export const getMergeRequestVersions = (
77
  { commit, dispatch, state },
78
  { projectId, mergeRequestId, targetProjectId = null, force = false } = {},
Tim Zallmann's avatar
Tim Zallmann committed
79 80
) =>
  new Promise((resolve, reject) => {
81
    if (!state.projects[projectId].mergeRequests[mergeRequestId].versions.length || force) {
Tim Zallmann's avatar
Tim Zallmann committed
82
      service
83
        .getProjectMergeRequestVersions(targetProjectId || projectId, mergeRequestId)
Tim Zallmann's avatar
Tim Zallmann committed
84 85 86 87 88 89 90 91 92 93
        .then(res => res.data)
        .then(data => {
          commit(types.SET_MERGE_REQUEST_VERSIONS, {
            projectPath: projectId,
            mergeRequestId,
            versions: data,
          });
          resolve(data);
        })
        .catch(() => {
94 95 96 97 98 99 100 101 102
          dispatch('setErrorMessage', {
            text: __('An error occured whilst loading the merge request version data.'),
            action: payload =>
              dispatch('getMergeRequestVersions', payload).then(() =>
                dispatch('setErrorMessage', null),
              ),
            actionText: __('Please try again'),
            actionPayload: { projectId, mergeRequestId, force },
          });
Tim Zallmann's avatar
Tim Zallmann committed
103 104 105 106 107 108
          reject(new Error(`Merge Request Versions not loaded ${projectId}`));
        });
    } else {
      resolve(state.projects[projectId].mergeRequests[mergeRequestId].versions);
    }
  });
109 110 111 112 113 114 115 116 117 118

export const openMergeRequest = (
  { dispatch, state },
  { projectId, targetProjectId, mergeRequestId } = {},
) =>
  dispatch('getMergeRequestData', {
    projectId,
    targetProjectId,
    mergeRequestId,
  })
Mike Greiling's avatar
Mike Greiling committed
119 120
    .then(mr => {
      dispatch('setCurrentBranchId', mr.source_branch);
121

Mike Greiling's avatar
Mike Greiling committed
122 123 124 125
      dispatch('getBranchData', {
        projectId,
        branchId: mr.source_branch,
      });
126

Mike Greiling's avatar
Mike Greiling committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
      return dispatch('getFiles', {
        projectId,
        branchId: mr.source_branch,
      });
    })
    .then(() =>
      dispatch('getMergeRequestVersions', {
        projectId,
        targetProjectId,
        mergeRequestId,
      }),
    )
    .then(() =>
      dispatch('getMergeRequestChanges', {
        projectId,
        targetProjectId,
        mergeRequestId,
      }),
    )
    .then(mrChanges => {
      if (mrChanges.changes.length) {
        dispatch('updateActivityBarView', activityBarViews.review);
      }
150

Mike Greiling's avatar
Mike Greiling committed
151 152
      mrChanges.changes.forEach((change, ind) => {
        const changeTreeEntry = state.entries[change.new_path];
153

Mike Greiling's avatar
Mike Greiling committed
154 155 156 157
        if (changeTreeEntry) {
          dispatch('setFileMrChange', {
            file: changeTreeEntry,
            mrChange: change,
158
          });
Mike Greiling's avatar
Mike Greiling committed
159 160 161 162 163

          if (ind < 10) {
            dispatch('getFileData', {
              path: change.new_path,
              makeFileActive: ind === 0,
164
              openFile: true,
Mike Greiling's avatar
Mike Greiling committed
165 166
            });
          }
167
        }
Mike Greiling's avatar
Mike Greiling committed
168 169 170 171 172
      });
    })
    .catch(e => {
      flash(__('Error while loading the merge request. Please try again.'));
      throw e;
173
    });