debian-mirror-gitlab/app/assets/javascripts/diffs/store/mutations.js

366 lines
10 KiB
JavaScript
Raw Normal View History

2020-11-24 15:15:51 +05:30
import Vue from 'vue';
2018-11-08 19:23:39 +05:30
import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils';
2021-03-11 19:13:27 +05:30
import {
DIFF_FILE_MANUAL_COLLAPSE,
DIFF_FILE_AUTOMATIC_COLLAPSE,
INLINE_DIFF_LINES_KEY,
} from '../constants';
import * as types from './mutation_types';
2018-11-20 20:47:30 +05:30
import {
findDiffFile,
addLineReferences,
removeMatchLine,
addContextLines,
prepareDiffData,
isDiscussionApplicableToLine,
2019-09-30 21:07:59 +05:30
updateLineInFile,
2018-11-20 20:47:30 +05:30
} from './utils';
2018-11-08 19:23:39 +05:30
2020-04-22 19:07:51 +05:30
function updateDiffFilesInState(state, files) {
return Object.assign(state, { diffFiles: files });
}
2021-01-29 00:20:46 +05:30
function renderFile(file) {
Object.assign(file, {
renderIt: true,
});
}
2018-11-08 19:23:39 +05:30
export default {
[types.SET_BASE_CONFIG](state, options) {
2020-01-01 13:55:28 +05:30
const {
endpoint,
endpointMetadata,
endpointBatch,
2020-04-08 14:13:33 +05:30
endpointCoverage,
2021-04-29 21:17:54 +05:30
endpointUpdateUser,
2020-01-01 13:55:28 +05:30
projectPath,
dismissEndpoint,
showSuggestPopover,
2021-03-08 18:12:59 +05:30
defaultSuggestionCommitMessage,
2021-02-22 17:27:13 +05:30
viewDiffsFileByFile,
2021-03-08 18:12:59 +05:30
mrReviews,
2020-01-01 13:55:28 +05:30
} = options;
Object.assign(state, {
endpoint,
endpointMetadata,
endpointBatch,
2020-04-08 14:13:33 +05:30
endpointCoverage,
2021-04-29 21:17:54 +05:30
endpointUpdateUser,
2020-01-01 13:55:28 +05:30
projectPath,
dismissEndpoint,
showSuggestPopover,
2021-03-08 18:12:59 +05:30
defaultSuggestionCommitMessage,
2021-02-22 17:27:13 +05:30
viewDiffsFileByFile,
2021-03-08 18:12:59 +05:30
mrReviews,
2020-01-01 13:55:28 +05:30
});
2018-11-08 19:23:39 +05:30
},
[types.SET_LOADING](state, isLoading) {
Object.assign(state, { isLoading });
},
2020-01-01 13:55:28 +05:30
[types.SET_BATCH_LOADING](state, isBatchLoading) {
Object.assign(state, { isBatchLoading });
},
2020-03-13 15:44:24 +05:30
[types.SET_RETRIEVING_BATCHES](state, retrievingBatches) {
Object.assign(state, { retrievingBatches });
},
2020-04-22 19:07:51 +05:30
[types.SET_DIFF_FILES](state, files) {
updateDiffFilesInState(state, files);
},
2021-02-22 17:27:13 +05:30
[types.SET_DIFF_METADATA](state, data) {
2018-11-08 19:23:39 +05:30
Object.assign(state, {
2019-02-15 15:39:39 +05:30
...convertObjectPropsToCamelCase(data),
2018-11-20 20:47:30 +05:30
});
},
2020-01-01 13:55:28 +05:30
[types.SET_DIFF_DATA_BATCH](state, data) {
2021-04-29 21:17:54 +05:30
state.diffFiles = prepareDiffData({
2021-02-22 17:27:13 +05:30
diff: data,
priorFiles: state.diffFiles,
});
2020-01-01 13:55:28 +05:30
},
2020-04-08 14:13:33 +05:30
[types.SET_COVERAGE_DATA](state, coverageFiles) {
Object.assign(state, { coverageFiles });
},
2018-11-20 20:47:30 +05:30
[types.RENDER_FILE](state, file) {
2021-01-29 00:20:46 +05:30
renderFile(file);
2018-11-08 19:23:39 +05:30
},
[types.SET_MERGE_REQUEST_DIFFS](state, mergeRequestDiffs) {
Object.assign(state, {
2019-02-15 15:39:39 +05:30
mergeRequestDiffs,
2018-11-08 19:23:39 +05:30
});
},
[types.SET_DIFF_VIEW_TYPE](state, diffViewType) {
Object.assign(state, { diffViewType });
},
2019-02-15 15:39:39 +05:30
[types.TOGGLE_LINE_HAS_FORM](state, { lineCode, fileHash, hasForm }) {
2021-03-08 18:12:59 +05:30
const diffFile = state.diffFiles.find((f) => f.file_hash === fileHash);
2019-02-15 15:39:39 +05:30
if (!diffFile) return;
2021-03-08 18:12:59 +05:30
diffFile[INLINE_DIFF_LINES_KEY].find((l) => l.line_code === lineCode).hasForm = hasForm;
2018-11-08 19:23:39 +05:30
},
[types.ADD_CONTEXT_LINES](state, options) {
2019-10-12 21:52:04 +05:30
const { lineNumbers, contextLines, fileHash, isExpandDown, nextLineNumbers } = options;
2018-11-08 19:23:39 +05:30
const { bottom } = options.params;
const diffFile = findDiffFile(state.diffFiles, fileHash);
removeMatchLine(diffFile, lineNumbers, bottom);
2018-12-13 13:39:08 +05:30
2019-10-12 21:52:04 +05:30
const lines = addLineReferences(
contextLines,
lineNumbers,
bottom,
isExpandDown,
nextLineNumbers,
2021-03-08 18:12:59 +05:30
).map((line) => {
2019-10-12 21:52:04 +05:30
const lineCode =
line.type === 'match'
? `${fileHash}_${line.meta_data.old_pos}_${line.meta_data.new_pos}_match`
: line.line_code || `${fileHash}_${line.old_line}_${line.new_line}`;
return {
...line,
line_code: lineCode,
discussions: line.discussions || [],
hasForm: false,
};
});
2018-12-13 13:39:08 +05:30
2018-11-08 19:23:39 +05:30
addContextLines({
2021-02-22 17:27:13 +05:30
inlineLines: diffFile[INLINE_DIFF_LINES_KEY],
2018-11-08 19:23:39 +05:30
contextLines: lines,
bottom,
lineNumbers,
2019-10-12 21:52:04 +05:30
isExpandDown,
2018-11-08 19:23:39 +05:30
});
},
[types.ADD_COLLAPSED_DIFFS](state, { file, data }) {
2021-02-22 17:27:13 +05:30
const files = prepareDiffData({ diff: data });
2021-03-08 18:12:59 +05:30
const [newFileData] = files.filter((f) => f.file_hash === file.file_hash);
const selectedFile = state.diffFiles.find((f) => f.file_hash === file.file_hash);
2018-11-20 20:47:30 +05:30
Object.assign(selectedFile, { ...newFileData });
2018-11-08 19:23:39 +05:30
},
2019-09-30 21:07:59 +05:30
[types.SET_LINE_DISCUSSIONS_FOR_FILE](state, { discussion, diffPositionByLineCode, hash }) {
2018-12-13 13:39:08 +05:30
const { latestDiff } = state;
2021-03-11 19:13:27 +05:30
const originalStartLineCode = discussion.original_position?.line_range?.start?.line_code;
const discussionLineCodes = [
discussion.line_code,
originalStartLineCode,
...(discussion.line_codes || []),
];
2018-12-13 13:39:08 +05:30
const fileHash = discussion.diff_file.file_hash;
2021-03-08 18:12:59 +05:30
const lineCheck = (line) =>
2020-04-22 19:07:51 +05:30
discussionLineCodes.some(
2021-03-08 18:12:59 +05:30
(discussionLineCode) =>
2020-04-22 19:07:51 +05:30
line.line_code === discussionLineCode &&
isDiscussionApplicableToLine({
discussion,
diffPosition: diffPositionByLineCode[line.line_code],
latestDiff,
}),
);
2019-02-15 15:39:39 +05:30
const mapDiscussions = (line, extraCheck = () => true) => ({
...line,
discussions: extraCheck()
2020-03-13 15:44:24 +05:30
? line.discussions &&
line.discussions
2019-02-15 15:39:39 +05:30
.filter(() => !line.discussions.some(({ id }) => discussion.id === id))
.concat(lineCheck(line) ? discussion : line.discussions)
: [],
});
2018-12-13 13:39:08 +05:30
2021-03-08 18:12:59 +05:30
const setDiscussionsExpanded = (line) => {
2020-03-13 15:44:24 +05:30
const isLineNoteTargeted =
line.discussions &&
line.discussions.some(
2021-03-08 18:12:59 +05:30
(disc) => disc.notes && disc.notes.find((note) => hash === `note_${note.id}`),
2020-03-13 15:44:24 +05:30
);
2019-09-30 21:07:59 +05:30
return {
...line,
discussionsExpanded:
line.discussions && line.discussions.length
2021-03-08 18:12:59 +05:30
? line.discussions.some((disc) => !disc.resolved) || isLineNoteTargeted
2019-09-30 21:07:59 +05:30
: false,
};
};
2021-03-08 18:12:59 +05:30
state.diffFiles.forEach((file) => {
2020-03-13 15:44:24 +05:30
if (file.file_hash === fileHash) {
2021-02-22 17:27:13 +05:30
if (file[INLINE_DIFF_LINES_KEY].length) {
2021-03-08 18:12:59 +05:30
file[INLINE_DIFF_LINES_KEY].forEach((line) => {
2020-03-13 15:44:24 +05:30
Object.assign(
line,
setDiscussionsExpanded(lineCheck(line) ? mapDiscussions(line) : line),
);
});
2018-11-20 20:47:30 +05:30
}
2021-02-22 17:27:13 +05:30
if (!file[INLINE_DIFF_LINES_KEY].length) {
2020-03-13 15:44:24 +05:30
const newDiscussions = (file.discussions || [])
2021-03-08 18:12:59 +05:30
.filter((d) => d.id !== discussion.id)
2019-07-31 22:56:46 +05:30
.concat(discussion);
2018-12-13 13:39:08 +05:30
2020-03-13 15:44:24 +05:30
Object.assign(file, {
discussions: newDiscussions,
});
}
2018-11-20 20:47:30 +05:30
}
2018-12-13 13:39:08 +05:30
});
2018-11-20 20:47:30 +05:30
},
2019-02-15 15:39:39 +05:30
[types.REMOVE_LINE_DISCUSSIONS_FOR_FILE](state, { fileHash, lineCode }) {
2021-03-08 18:12:59 +05:30
const selectedFile = state.diffFiles.find((f) => f.file_hash === fileHash);
2018-11-20 20:47:30 +05:30
if (selectedFile) {
2021-03-08 18:12:59 +05:30
updateLineInFile(selectedFile, lineCode, (line) =>
2019-09-30 21:07:59 +05:30
Object.assign(line, {
2021-03-08 18:12:59 +05:30
discussions: line.discussions.filter((discussion) => discussion.notes.length),
2019-09-30 21:07:59 +05:30
}),
);
2018-12-13 13:39:08 +05:30
if (selectedFile.discussions && selectedFile.discussions.length) {
selectedFile.discussions = selectedFile.discussions.filter(
2021-03-08 18:12:59 +05:30
(discussion) => discussion.notes.length,
2018-12-13 13:39:08 +05:30
);
}
2018-11-20 20:47:30 +05:30
}
},
2019-09-30 21:07:59 +05:30
[types.TOGGLE_LINE_DISCUSSIONS](state, { fileHash, lineCode, expanded }) {
2021-03-08 18:12:59 +05:30
const selectedFile = state.diffFiles.find((f) => f.file_hash === fileHash);
2019-09-30 21:07:59 +05:30
2021-03-08 18:12:59 +05:30
updateLineInFile(selectedFile, lineCode, (line) => {
2020-03-13 15:44:24 +05:30
Object.assign(line, { discussionsExpanded: expanded });
});
2019-09-30 21:07:59 +05:30
},
2018-12-05 23:21:45 +05:30
[types.TOGGLE_FOLDER_OPEN](state, path) {
state.treeEntries[path].opened = !state.treeEntries[path].opened;
},
2021-02-22 17:27:13 +05:30
[types.SET_SHOW_TREE_LIST](state, showTreeList) {
state.showTreeList = showTreeList;
2018-12-05 23:21:45 +05:30
},
2020-11-24 15:15:51 +05:30
[types.VIEW_DIFF_FILE](state, fileId) {
2018-12-05 23:21:45 +05:30
state.currentDiffFileId = fileId;
2020-11-24 15:15:51 +05:30
Vue.set(state.viewedDiffFileIds, fileId, true);
2018-12-05 23:21:45 +05:30
},
2018-12-13 13:39:08 +05:30
[types.OPEN_DIFF_FILE_COMMENT_FORM](state, formData) {
state.commentForms.push({
...formData,
});
},
[types.UPDATE_DIFF_FILE_COMMENT_FORM](state, formData) {
const { fileHash } = formData;
2021-03-08 18:12:59 +05:30
state.commentForms = state.commentForms.map((form) => {
2018-12-13 13:39:08 +05:30
if (form.fileHash === fileHash) {
return {
...formData,
};
}
return form;
});
},
[types.CLOSE_DIFF_FILE_COMMENT_FORM](state, fileHash) {
2021-03-08 18:12:59 +05:30
state.commentForms = state.commentForms.filter((form) => form.fileHash !== fileHash);
2018-12-13 13:39:08 +05:30
},
2019-02-15 15:39:39 +05:30
[types.SET_HIGHLIGHTED_ROW](state, lineCode) {
state.highlightedRow = lineCode;
},
2019-03-02 22:35:43 +05:30
[types.SET_TREE_DATA](state, { treeEntries, tree }) {
state.treeEntries = treeEntries;
state.tree = tree;
2020-10-24 23:57:45 +05:30
state.isTreeLoaded = true;
2019-03-02 22:35:43 +05:30
},
[types.SET_RENDER_TREE_LIST](state, renderTreeList) {
state.renderTreeList = renderTreeList;
},
[types.SET_SHOW_WHITESPACE](state, showWhitespace) {
state.showWhitespace = showWhitespace;
2020-04-22 19:07:51 +05:30
state.diffFiles = [];
2019-03-02 22:35:43 +05:30
},
[types.TOGGLE_FILE_FINDER_VISIBLE](state, visible) {
state.fileFinderVisible = visible;
},
2019-07-07 11:18:12 +05:30
[types.REQUEST_FULL_DIFF](state, filePath) {
const file = findDiffFile(state.diffFiles, filePath, 'file_path');
file.isLoadingFullFile = true;
},
[types.RECEIVE_FULL_DIFF_ERROR](state, filePath) {
const file = findDiffFile(state.diffFiles, filePath, 'file_path');
file.isLoadingFullFile = false;
},
2019-07-31 22:56:46 +05:30
[types.RECEIVE_FULL_DIFF_SUCCESS](state, { filePath }) {
2019-07-07 11:18:12 +05:30
const file = findDiffFile(state.diffFiles, filePath, 'file_path');
file.isShowingFullFile = true;
file.isLoadingFullFile = false;
},
2021-01-29 00:20:46 +05:30
[types.SET_FILE_COLLAPSED](
state,
{ filePath, collapsed, trigger = DIFF_FILE_AUTOMATIC_COLLAPSE },
) {
2021-03-08 18:12:59 +05:30
const file = state.diffFiles.find((f) => f.file_path === filePath);
2019-07-07 11:18:12 +05:30
if (file && file.viewer) {
2021-01-29 00:20:46 +05:30
if (trigger === DIFF_FILE_MANUAL_COLLAPSE) {
file.viewer.automaticallyCollapsed = false;
file.viewer.manuallyCollapsed = collapsed;
} else if (trigger === DIFF_FILE_AUTOMATIC_COLLAPSE) {
file.viewer.automaticallyCollapsed = collapsed;
file.viewer.manuallyCollapsed = null;
}
}
if (file && !collapsed) {
renderFile(file);
2019-07-07 11:18:12 +05:30
}
},
2019-07-31 22:56:46 +05:30
[types.SET_CURRENT_VIEW_DIFF_FILE_LINES](state, { filePath, lines }) {
2021-03-08 18:12:59 +05:30
const file = state.diffFiles.find((f) => f.file_path === filePath);
2021-01-29 00:20:46 +05:30
2021-02-22 17:27:13 +05:30
file[INLINE_DIFF_LINES_KEY] = lines;
2019-07-31 22:56:46 +05:30
},
[types.ADD_CURRENT_VIEW_DIFF_FILE_LINES](state, { filePath, line }) {
2021-03-08 18:12:59 +05:30
const file = state.diffFiles.find((f) => f.file_path === filePath);
2019-07-31 22:56:46 +05:30
2021-02-22 17:27:13 +05:30
file[INLINE_DIFF_LINES_KEY].push(line);
2019-07-31 22:56:46 +05:30
},
[types.TOGGLE_DIFF_FILE_RENDERING_MORE](state, filePath) {
2021-03-08 18:12:59 +05:30
const file = state.diffFiles.find((f) => f.file_path === filePath);
2019-07-31 22:56:46 +05:30
file.renderingLines = !file.renderingLines;
},
2020-05-24 23:13:21 +05:30
[types.SET_DIFF_FILE_VIEWER](state, { filePath, viewer }) {
const file = findDiffFile(state.diffFiles, filePath, 'file_path');
file.viewer = viewer;
},
2019-09-04 21:01:54 +05:30
[types.SET_SHOW_SUGGEST_POPOVER](state) {
state.showSuggestPopover = false;
},
2021-02-22 17:27:13 +05:30
[types.SET_FILE_BY_FILE](state, fileByFile) {
state.viewDiffsFileByFile = fileByFile;
},
2021-03-08 18:12:59 +05:30
[types.SET_MR_FILE_REVIEWS](state, newReviews) {
state.mrReviews = newReviews;
},
2018-11-08 19:23:39 +05:30
};