diff options
Diffstat (limited to 'app/assets/javascripts/ide/stores/actions')
4 files changed, 93 insertions, 71 deletions
diff --git a/app/assets/javascripts/ide/stores/actions/file.js b/app/assets/javascripts/ide/stores/actions/file.js index 42668dec63a..d1e40920ebc 100644 --- a/app/assets/javascripts/ide/stores/actions/file.js +++ b/app/assets/javascripts/ide/stores/actions/file.js @@ -1,16 +1,16 @@ import { joinPaths, escapeFileUrl } from '~/lib/utils/url_utility'; import { __ } from '~/locale'; -import { performanceMarkAndMeasure } from '~/performance/utils'; import { WEBIDE_MARK_FETCH_FILE_DATA_START, WEBIDE_MARK_FETCH_FILE_DATA_FINISH, WEBIDE_MEASURE_FETCH_FILE_DATA, } from '~/performance/constants'; +import { performanceMarkAndMeasure } from '~/performance/utils'; +import { viewerTypes, stageKeys, commitActionTypes } from '../../constants'; import eventHub from '../../eventhub'; import service from '../../services'; import * as types from '../mutation_types'; import { setPageTitleForFile } from '../utils'; -import { viewerTypes, stageKeys, commitActionTypes } from '../../constants'; export const closeFile = ({ commit, state, dispatch, getters }, file) => { const { path } = file; @@ -120,10 +120,6 @@ export const getFileData = ( }); }; -export const setFileMrChange = ({ commit }, { file, mrChange }) => { - commit(types.SET_FILE_MERGE_REQUEST_CHANGE, { file, mrChange }); -}; - export const getRawFileData = ({ state, commit, dispatch, getters }, { path }) => { const file = state.entries[path]; const stagedFile = state.stagedFiles.find((f) => f.path === path); diff --git a/app/assets/javascripts/ide/stores/actions/merge_request.js b/app/assets/javascripts/ide/stores/actions/merge_request.js index 8215cba7ccf..753f6b9cd47 100644 --- a/app/assets/javascripts/ide/stores/actions/merge_request.js +++ b/app/assets/javascripts/ide/stores/actions/merge_request.js @@ -1,8 +1,8 @@ import { deprecatedCreateFlash as flash } from '~/flash'; import { __ } from '~/locale'; +import { leftSidebarViews, PERMISSION_READ_MR, MAX_MR_FILES_AUTO_OPEN } from '../../constants'; import service from '../../services'; import * as types from '../mutation_types'; -import { leftSidebarViews, PERMISSION_READ_MR } from '../../constants'; export const getMergeRequestsForBranch = ( { commit, state, getters }, @@ -147,70 +147,96 @@ export const getMergeRequestVersions = ( } }); -export const openMergeRequest = ( - { dispatch, state, getters }, - { projectId, targetProjectId, mergeRequestId } = {}, -) => - dispatch('getMergeRequestData', { - projectId, - targetProjectId, - mergeRequestId, - }) - .then((mr) => { - dispatch('setCurrentBranchId', mr.source_branch); - - return dispatch('getBranchData', { - projectId, - branchId: mr.source_branch, - }).then(() => { - const branch = getters.findBranch(projectId, mr.source_branch); - - return dispatch('getFiles', { - projectId, - branchId: mr.source_branch, - ref: branch.commit.id, +export const openMergeRequestChanges = async ({ dispatch, getters, state, commit }, changes) => { + const entryChanges = changes + .map((change) => ({ entry: state.entries[change.new_path], change })) + .filter((x) => x.entry); + + const pathsToOpen = entryChanges + .slice(0, MAX_MR_FILES_AUTO_OPEN) + .map(({ change }) => change.new_path); + + // If there are no changes with entries, do nothing. + if (!entryChanges.length) { + return; + } + + dispatch('updateActivityBarView', leftSidebarViews.review.name); + + entryChanges.forEach(({ change, entry }) => { + commit(types.SET_FILE_MERGE_REQUEST_CHANGE, { file: entry, mrChange: change }); + }); + + // Open paths in order as they appear in MR changes + pathsToOpen.forEach((path) => { + commit(types.TOGGLE_FILE_OPEN, path); + }); + + // Activate first path. + // We don't `getFileData` here since the editor component kicks that off. Otherwise, we'd fetch twice. + const [firstPath, ...remainingPaths] = pathsToOpen; + await dispatch('router/push', getters.getUrlForPath(firstPath)); + await dispatch('setFileActive', firstPath); + + // Lastly, eagerly fetch the remaining paths for improved user experience. + await Promise.all( + remainingPaths.map(async (path) => { + try { + await dispatch('getFileData', { + path, + makeFileActive: false, }); - }); - }) - .then(() => - dispatch('getMergeRequestVersions', { - projectId, - targetProjectId, - mergeRequestId, - }), - ) - .then(() => - dispatch('getMergeRequestChanges', { - projectId, - targetProjectId, - mergeRequestId, - }), - ) - .then((mrChanges) => { - if (mrChanges.changes.length) { - dispatch('updateActivityBarView', leftSidebarViews.review.name); + await dispatch('getRawFileData', { path }); + } catch (e) { + // If one of the file fetches fails, we dont want to blow up the rest of them. + // eslint-disable-next-line no-console + console.error('[gitlab] An unexpected error occurred fetching MR file data', e); } + }), + ); +}; - mrChanges.changes.forEach((change, ind) => { - const changeTreeEntry = state.entries[change.new_path]; +export const openMergeRequest = async ( + { dispatch, getters }, + { projectId, targetProjectId, mergeRequestId } = {}, +) => { + try { + const mr = await dispatch('getMergeRequestData', { + projectId, + targetProjectId, + mergeRequestId, + }); - if (changeTreeEntry) { - dispatch('setFileMrChange', { - file: changeTreeEntry, - mrChange: change, - }); + dispatch('setCurrentBranchId', mr.source_branch); - if (ind < 10) { - dispatch('getFileData', { - path: change.new_path, - makeFileActive: ind === 0, - openFile: true, - }); - } - } - }); - }) - .catch((e) => { - flash(__('Error while loading the merge request. Please try again.')); - throw e; + await dispatch('getBranchData', { + projectId, + branchId: mr.source_branch, + }); + + const branch = getters.findBranch(projectId, mr.source_branch); + + await dispatch('getFiles', { + projectId, + branchId: mr.source_branch, + ref: branch.commit.id, }); + + await dispatch('getMergeRequestVersions', { + projectId, + targetProjectId, + mergeRequestId, + }); + + const { changes } = await dispatch('getMergeRequestChanges', { + projectId, + targetProjectId, + mergeRequestId, + }); + + await dispatch('openMergeRequestChanges', changes); + } catch (e) { + flash(__('Error while loading the merge request. Please try again.')); + throw e; + } +}; diff --git a/app/assets/javascripts/ide/stores/actions/project.js b/app/assets/javascripts/ide/stores/actions/project.js index 27f6848f1d6..120a577d44a 100644 --- a/app/assets/javascripts/ide/stores/actions/project.js +++ b/app/assets/javascripts/ide/stores/actions/project.js @@ -1,8 +1,8 @@ import { escape } from 'lodash'; import { deprecatedCreateFlash as flash } from '~/flash'; import { __, sprintf } from '~/locale'; -import service from '../../services'; import api from '../../../api'; +import service from '../../services'; import * as types from '../mutation_types'; export const getProjectData = ({ commit, state }, { namespace, projectId, force = false } = {}) => diff --git a/app/assets/javascripts/ide/stores/actions/tree.js b/app/assets/javascripts/ide/stores/actions/tree.js index 150dfcb2726..f46d3cbe946 100644 --- a/app/assets/javascripts/ide/stores/actions/tree.js +++ b/app/assets/javascripts/ide/stores/actions/tree.js @@ -1,14 +1,14 @@ import { defer } from 'lodash'; -import { performanceMarkAndMeasure } from '~/performance/utils'; import { WEBIDE_MARK_FETCH_FILES_FINISH, WEBIDE_MEASURE_FETCH_FILES, WEBIDE_MARK_FETCH_FILES_START, } from '~/performance/constants'; +import { performanceMarkAndMeasure } from '~/performance/utils'; import { __ } from '../../../locale'; +import { decorateFiles } from '../../lib/files'; import service from '../../services'; import * as types from '../mutation_types'; -import { decorateFiles } from '../../lib/files'; export const toggleTreeOpen = ({ commit }, path) => { commit(types.TOGGLE_TREE_OPEN, path); |