project.js 3.8 KB
Newer Older
1
import _ from 'underscore';
P
Phil Hughes 已提交
2
import flash from '~/flash';
3
import { __, sprintf } from '~/locale';
P
Phil Hughes 已提交
4
import service from '../../services';
5
import api from '../../../api';
P
Phil Hughes 已提交
6
import * as types from '../mutation_types';
P
Phil Hughes 已提交
7
import router from '../../ide_router';
P
Phil Hughes 已提交
8

L
Lukas Eipert 已提交
9
export const getProjectData = ({ commit, state }, { namespace, projectId, force = false } = {}) =>
P
Phil Hughes 已提交
10 11
  new Promise((resolve, reject) => {
    if (!state.projects[`${namespace}/${projectId}`] || force) {
P
Phil Hughes 已提交
12
      commit(types.TOGGLE_LOADING, { entry: state });
P
Phil Hughes 已提交
13 14 15 16 17 18
      service
        .getProjectData(namespace, projectId)
        .then(res => res.data)
        .then(data => {
          commit(types.TOGGLE_LOADING, { entry: state });
          commit(types.SET_PROJECT, { projectPath: `${namespace}/${projectId}`, project: data });
P
Phil Hughes 已提交
19
          commit(types.SET_CURRENT_PROJECT, `${namespace}/${projectId}`);
P
Phil Hughes 已提交
20 21 22 23
          resolve(data);
        })
        .catch(() => {
          flash(
24
            __('Error loading project data. Please try again.'),
P
Phil Hughes 已提交
25 26 27 28 29 30 31 32 33 34 35 36
            'alert',
            document,
            null,
            false,
            true,
          );
          reject(new Error(`Project not loaded ${namespace}/${projectId}`));
        });
    } else {
      resolve(state.projects[`${namespace}/${projectId}`]);
    }
  });
P
Phil Hughes 已提交
37

38 39 40 41
export const getBranchData = (
  { commit, dispatch, state },
  { projectId, branchId, force = false } = {},
) =>
P
Phil Hughes 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  new Promise((resolve, reject) => {
    if (
      typeof state.projects[`${projectId}`] === 'undefined' ||
      !state.projects[`${projectId}`].branches[branchId] ||
      force
    ) {
      service
        .getBranchData(`${projectId}`, branchId)
        .then(({ data }) => {
          const { id } = data.commit;
          commit(types.SET_BRANCH, {
            projectPath: `${projectId}`,
            branchName: branchId,
            branch: data,
          });
          commit(types.SET_BRANCH_WORKING_REFERENCE, { projectId, branchId, reference: id });
          resolve(data);
        })
60 61 62 63 64 65 66 67 68 69 70 71 72
        .catch(e => {
          if (e.response.status === 404) {
            dispatch('showBranchNotFoundError', branchId);
          } else {
            flash(
              __('Error loading branch data. Please try again.'),
              'alert',
              document,
              null,
              false,
              true,
            );
          }
P
Phil Hughes 已提交
73 74 75 76 77 78
          reject(new Error(`Branch not loaded - ${projectId}/${branchId}`));
        });
    } else {
      resolve(state.projects[`${projectId}`].branches[branchId]);
    }
  });
79

L
Lukas Eipert 已提交
80
export const refreshLastCommitData = ({ commit }, { projectId, branchId } = {}) =>
81 82 83 84 85 86 87 88 89 90 91
  service
    .getBranchData(projectId, branchId)
    .then(({ data }) => {
      commit(types.SET_BRANCH_COMMIT, {
        projectId,
        branchId,
        commit: data.commit,
      });
    })
    .catch(() => {
      flash(__('Error loading last commit.'), 'alert', document, null, false, true);
92
    });
93

P
Phil Hughes 已提交
94
export const createNewBranchFromDefault = ({ state, dispatch, getters }, branch) =>
95 96 97 98 99 100
  api
    .createBranch(state.currentProjectId, {
      ref: getters.currentProject.default_branch,
      branch,
    })
    .then(() => {
P
Phil Hughes 已提交
101 102
      dispatch('setErrorMessage', null);
      router.push(`${router.currentRoute.path}?${Date.now()}`);
103
    })
P
Phil Hughes 已提交
104 105 106 107 108 109 110 111
    .catch(() => {
      dispatch('setErrorMessage', {
        text: __('An error occured creating the new branch.'),
        action: 'createNewBranchFromDefault',
        actionText: __('Please try again'),
        actionPayload: branch,
      });
    });
112 113

export const showBranchNotFoundError = ({ dispatch }, branchId) => {
114 115
  dispatch('setErrorMessage', {
    text: sprintf(
P
Phil Hughes 已提交
116
      __("Branch %{branchName} was not found in this project's repository."),
117 118
      {
        branchName: `<strong>${_.escape(branchId)}</strong>`,
119
      },
120 121 122
      false,
    ),
    action: 'createNewBranchFromDefault',
123
    actionText: __('Create branch'),
124 125
    actionPayload: branchId,
  });
126
};