summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ide/stores/actions/project.js
blob: 0ec808339fb4e6e0cd431c3a810c146f2f4c365a (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { escape } from 'lodash';
import createFlash from '~/flash';
import { __, sprintf } from '~/locale';
import { logError } from '~/lib/logger';
import api from '../../../api';
import service from '../../services';
import * as types from '../mutation_types';

const ERROR_LOADING_PROJECT = __('Error loading project data. Please try again.');

const errorFetchingData = (e) => {
  logError(ERROR_LOADING_PROJECT, e);

  createFlash({
    message: ERROR_LOADING_PROJECT,
    fadeTransition: false,
    addBodyClass: true,
  });
};

export const setProject = ({ commit }, { project } = {}) => {
  if (!project) {
    return;
  }
  const projectPath = project.path_with_namespace;
  commit(types.SET_PROJECT, { projectPath, project });
  commit(types.SET_CURRENT_PROJECT, projectPath);
};

export const fetchProjectPermissions = ({ commit, state }) => {
  const projectPath = state.currentProjectId;
  if (!projectPath) {
    return undefined;
  }
  return service
    .getProjectPermissionsData(projectPath)
    .then((permissions) => {
      commit(types.UPDATE_PROJECT, { projectPath, props: permissions });
    })
    .catch(errorFetchingData);
};

export const refreshLastCommitData = ({ commit }, { projectId, branchId } = {}) =>
  service
    .getBranchData(projectId, branchId)
    .then(({ data }) => {
      commit(types.SET_BRANCH_COMMIT, {
        projectId,
        branchId,
        commit: data.commit,
      });
    })
    .catch((e) => {
      createFlash({
        message: __('Error loading last commit.'),
        fadeTransition: false,
        addBodyClass: true,
      });
      throw e;
    });

export const createNewBranchFromDefault = ({ state, dispatch, getters }, branch) =>
  api
    .createBranch(state.currentProjectId, {
      ref: getters.currentProject.default_branch,
      branch,
    })
    .then(() => {
      dispatch('setErrorMessage', null);
      window.location.reload();
    })
    .catch(() => {
      dispatch('setErrorMessage', {
        text: __('An error occurred creating the new branch.'),
        action: (payload) => dispatch('createNewBranchFromDefault', payload),
        actionText: __('Please try again'),
        actionPayload: branch,
      });
    });

export const showBranchNotFoundError = ({ dispatch }, branchId) => {
  dispatch('setErrorMessage', {
    text: sprintf(
      __("Branch %{branchName} was not found in this project's repository."),
      {
        branchName: `<strong>${escape(branchId)}</strong>`,
      },
      false,
    ),
    action: (payload) => dispatch('createNewBranchFromDefault', payload),
    actionText: __('Create branch'),
    actionPayload: branchId,
  });
};

export const loadEmptyBranch = ({ commit, state }, { projectId, branchId }) => {
  const treePath = `${projectId}/${branchId}`;
  const currentTree = state.trees[`${projectId}/${branchId}`];

  // If we already have a tree, let's not recreate an empty one
  if (currentTree) {
    return;
  }

  commit(types.CREATE_TREE, { treePath });
  commit(types.TOGGLE_LOADING, {
    entry: state.trees[treePath],
    forceValue: false,
  });
};

export const loadFile = ({ dispatch, state }, { basePath }) => {
  if (basePath) {
    const path = basePath.slice(-1) === '/' ? basePath.slice(0, -1) : basePath;
    const treeEntryKey = Object.keys(state.entries).find(
      (key) => key === path && !state.entries[key].pending,
    );
    const treeEntry = state.entries[treeEntryKey];

    if (treeEntry) {
      dispatch('handleTreeEntryAction', treeEntry);
    } else {
      dispatch('createTempEntry', {
        name: path,
        type: 'blob',
      });
    }
  }
};

export const loadBranch = ({ dispatch, getters, state }, { projectId, branchId }) => {
  const currentProject = state.projects[projectId];

  if (currentProject?.branches?.[branchId]) {
    return Promise.resolve();
  } else if (getters.emptyRepo) {
    return dispatch('loadEmptyBranch', { projectId, branchId });
  }

  return dispatch('getBranchData', {
    projectId,
    branchId,
  })
    .then(() => {
      dispatch('getMergeRequestsForBranch', {
        projectId,
        branchId,
      });

      const branch = getters.findBranch(projectId, branchId);

      return dispatch('getFiles', {
        projectId,
        branchId,
        ref: branch.commit.id,
      });
    })
    .catch((err) => {
      dispatch('showBranchNotFoundError', branchId);
      throw err;
    });
};

export const openBranch = ({ dispatch }, { projectId, branchId, basePath }) => {
  dispatch('setCurrentBranchId', branchId);

  return dispatch('loadBranch', { projectId, branchId })
    .then(() => dispatch('loadFile', { basePath }))
    .catch(
      () =>
        new Error(
          sprintf(
            __('An error occurred while getting files for - %{branchId}'),
            {
              branchId: `<strong>${escape(projectId)}/${escape(branchId)}</strong>`,
            },
            false,
          ),
        ),
    );
};