merge_request.js 5.4 KB
Newer Older
1 2
import flash from '~/flash';
import { __ } from '~/locale';
T
Tim Zallmann 已提交
3 4
import service from '../../services';
import * as types from '../mutation_types';
5
import { activityBarViews } from '../../constants';
T
Tim Zallmann 已提交
6 7

export const getMergeRequestData = (
P
Phil Hughes 已提交
8
  { commit, dispatch, state },
9
  { projectId, mergeRequestId, targetProjectId = null, force = false } = {},
T
Tim Zallmann 已提交
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,
        })
P
Phil Hughes 已提交
17
        .then(({ data }) => {
T
Tim Zallmann 已提交
18 19 20 21 22
          commit(types.SET_MERGE_REQUEST, {
            projectPath: projectId,
            mergeRequestId,
            mergeRequest: data,
          });
P
Phil Hughes 已提交
23
          commit(types.SET_CURRENT_MERGE_REQUEST, mergeRequestId);
T
Tim Zallmann 已提交
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 },
          });
T
Tim Zallmann 已提交
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 = (
P
Phil Hughes 已提交
44
  { commit, dispatch, state },
45
  { projectId, mergeRequestId, targetProjectId = null, force = false } = {},
T
Tim Zallmann 已提交
46 47
) =>
  new Promise((resolve, reject) => {
T
Tim Zallmann 已提交
48
    if (!state.projects[projectId].mergeRequests[mergeRequestId].changes.length || force) {
T
Tim Zallmann 已提交
49
      service
50
        .getProjectMergeRequestChanges(targetProjectId || projectId, mergeRequestId)
P
Phil Hughes 已提交
51
        .then(({ data }) => {
T
Tim Zallmann 已提交
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 },
          });
T
Tim Zallmann 已提交
69 70 71 72 73 74 75
          reject(new Error(`Merge Request Changes not loaded ${projectId}`));
        });
    } else {
      resolve(state.projects[projectId].mergeRequests[mergeRequestId].changes);
    }
  });

T
Tim Zallmann 已提交
76
export const getMergeRequestVersions = (
P
Phil Hughes 已提交
77
  { commit, dispatch, state },
78
  { projectId, mergeRequestId, targetProjectId = null, force = false } = {},
T
Tim Zallmann 已提交
79 80
) =>
  new Promise((resolve, reject) => {
T
Tim Zallmann 已提交
81
    if (!state.projects[projectId].mergeRequests[mergeRequestId].versions.length || force) {
T
Tim Zallmann 已提交
82
      service
83
        .getProjectMergeRequestVersions(targetProjectId || projectId, mergeRequestId)
T
Tim Zallmann 已提交
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 },
          });
T
Tim Zallmann 已提交
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,
  })
M
Mike Greiling 已提交
119 120
    .then(mr => {
      dispatch('setCurrentBranchId', mr.source_branch);
121

M
Mike Greiling 已提交
122 123 124 125
      dispatch('getBranchData', {
        projectId,
        branchId: mr.source_branch,
      });
126

M
Mike Greiling 已提交
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

M
Mike Greiling 已提交
151 152
      mrChanges.changes.forEach((change, ind) => {
        const changeTreeEntry = state.entries[change.new_path];
153

M
Mike Greiling 已提交
154 155 156 157
        if (changeTreeEntry) {
          dispatch('setFileMrChange', {
            file: changeTreeEntry,
            mrChange: change,
158
          });
M
Mike Greiling 已提交
159 160 161 162 163 164 165

          if (ind < 10) {
            dispatch('getFileData', {
              path: change.new_path,
              makeFileActive: ind === 0,
            });
          }
166
        }
M
Mike Greiling 已提交
167 168 169 170 171
      });
    })
    .catch(e => {
      flash(__('Error while loading the merge request. Please try again.'));
      throw e;
172
    });