2018-06-21 08:22:40 -04:00
|
|
|
import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils';
|
2018-10-03 05:05:43 -04:00
|
|
|
import { sortTree } from '~/ide/stores/utils';
|
2018-09-01 17:24:29 -04:00
|
|
|
import {
|
|
|
|
findDiffFile,
|
|
|
|
addLineReferences,
|
|
|
|
removeMatchLine,
|
|
|
|
addContextLines,
|
2018-09-05 04:28:49 -04:00
|
|
|
prepareDiffData,
|
2018-09-08 02:37:41 -04:00
|
|
|
isDiscussionApplicableToLine,
|
2018-10-03 05:05:43 -04:00
|
|
|
generateTreeList,
|
2018-09-01 17:24:29 -04:00
|
|
|
} from './utils';
|
2018-06-21 08:22:40 -04:00
|
|
|
import * as types from './mutation_types';
|
|
|
|
|
|
|
|
export default {
|
2018-06-26 14:49:22 -04:00
|
|
|
[types.SET_BASE_CONFIG](state, options) {
|
|
|
|
const { endpoint, projectPath } = options;
|
|
|
|
Object.assign(state, { endpoint, projectPath });
|
2018-06-21 08:22:40 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
[types.SET_LOADING](state, isLoading) {
|
|
|
|
Object.assign(state, { isLoading });
|
|
|
|
},
|
|
|
|
|
|
|
|
[types.SET_DIFF_DATA](state, data) {
|
2018-11-09 14:48:41 -05:00
|
|
|
prepareDiffData(data);
|
|
|
|
const { tree, treeEntries } = generateTreeList(data.diff_files);
|
2018-08-07 05:14:00 -04:00
|
|
|
|
2018-06-21 08:22:40 -04:00
|
|
|
Object.assign(state, {
|
2018-11-09 14:48:41 -05:00
|
|
|
...convertObjectPropsToCamelCase(data),
|
2018-10-03 05:05:43 -04:00
|
|
|
tree: sortTree(tree),
|
|
|
|
treeEntries,
|
2018-08-07 05:14:00 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
[types.RENDER_FILE](state, file) {
|
|
|
|
Object.assign(file, {
|
|
|
|
renderIt: true,
|
2018-06-21 08:22:40 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
[types.SET_MERGE_REQUEST_DIFFS](state, mergeRequestDiffs) {
|
|
|
|
Object.assign(state, {
|
2018-11-09 14:48:41 -05:00
|
|
|
mergeRequestDiffs,
|
2018-06-21 08:22:40 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
[types.SET_DIFF_VIEW_TYPE](state, diffViewType) {
|
|
|
|
Object.assign(state, { diffViewType });
|
|
|
|
},
|
|
|
|
|
2018-11-09 04:44:07 -05:00
|
|
|
[types.TOGGLE_LINE_HAS_FORM](state, { lineCode, fileHash, hasForm }) {
|
|
|
|
const diffFile = state.diffFiles.find(f => f.file_hash === fileHash);
|
|
|
|
|
|
|
|
if (!diffFile) return;
|
|
|
|
|
|
|
|
if (diffFile.highlighted_diff_lines) {
|
|
|
|
diffFile.highlighted_diff_lines.find(l => l.line_code === lineCode).hasForm = hasForm;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (diffFile.parallel_diff_lines) {
|
|
|
|
const line = diffFile.parallel_diff_lines.find(l => {
|
|
|
|
const { left, right } = l;
|
|
|
|
|
|
|
|
return (left && left.line_code === lineCode) || (right && right.line_code === lineCode);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (line.left && line.left.line_code === lineCode) {
|
|
|
|
line.left.hasForm = hasForm;
|
|
|
|
}
|
2018-06-21 08:22:40 -04:00
|
|
|
|
2018-11-09 04:44:07 -05:00
|
|
|
if (line.right && line.right.line_code === lineCode) {
|
|
|
|
line.right.hasForm = hasForm;
|
|
|
|
}
|
|
|
|
}
|
2018-06-21 08:22:40 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
[types.ADD_CONTEXT_LINES](state, options) {
|
|
|
|
const { lineNumbers, contextLines, fileHash } = options;
|
|
|
|
const { bottom } = options.params;
|
|
|
|
const diffFile = findDiffFile(state.diffFiles, fileHash);
|
|
|
|
|
|
|
|
removeMatchLine(diffFile, lineNumbers, bottom);
|
2018-10-16 12:21:16 -04:00
|
|
|
|
|
|
|
const lines = addLineReferences(contextLines, lineNumbers, bottom).map(line => ({
|
|
|
|
...line,
|
2018-11-09 14:48:41 -05:00
|
|
|
line_code: line.line_code || `${fileHash}_${line.old_line}_${line.new_line}`,
|
2018-10-16 12:21:16 -04:00
|
|
|
discussions: line.discussions || [],
|
2018-11-09 04:44:07 -05:00
|
|
|
hasForm: false,
|
2018-10-16 12:21:16 -04:00
|
|
|
}));
|
|
|
|
|
2018-06-21 08:22:40 -04:00
|
|
|
addContextLines({
|
2018-11-09 14:48:41 -05:00
|
|
|
inlineLines: diffFile.highlighted_diff_lines,
|
|
|
|
parallelLines: diffFile.parallel_diff_lines,
|
2018-06-21 08:22:40 -04:00
|
|
|
contextLines: lines,
|
|
|
|
bottom,
|
|
|
|
lineNumbers,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
[types.ADD_COLLAPSED_DIFFS](state, { file, data }) {
|
2018-11-09 14:48:41 -05:00
|
|
|
prepareDiffData(data);
|
|
|
|
const [newFileData] = data.diff_files.filter(f => f.file_hash === file.file_hash);
|
|
|
|
const selectedFile = state.diffFiles.find(f => f.file_hash === file.file_hash);
|
2018-09-07 10:20:57 -04:00
|
|
|
Object.assign(selectedFile, { ...newFileData });
|
2018-06-21 08:22:40 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
[types.EXPAND_ALL_FILES](state) {
|
2018-07-06 08:13:33 -04:00
|
|
|
state.diffFiles = state.diffFiles.map(file => ({
|
|
|
|
...file,
|
|
|
|
collapsed: false,
|
|
|
|
}));
|
2018-06-21 08:22:40 -04:00
|
|
|
},
|
2018-09-05 04:28:49 -04:00
|
|
|
|
2018-10-25 07:04:34 -04:00
|
|
|
[types.SET_LINE_DISCUSSIONS_FOR_FILE](state, { discussion, diffPositionByLineCode }) {
|
2018-10-19 09:09:16 -04:00
|
|
|
const { latestDiff } = state;
|
|
|
|
|
2018-10-25 07:04:34 -04:00
|
|
|
const discussionLineCode = discussion.line_code;
|
|
|
|
const fileHash = discussion.diff_file.file_hash;
|
2018-11-09 14:48:41 -05:00
|
|
|
const lineCheck = line =>
|
|
|
|
line.line_code === discussionLineCode &&
|
2018-10-25 07:04:34 -04:00
|
|
|
isDiscussionApplicableToLine({
|
|
|
|
discussion,
|
2018-11-09 14:48:41 -05:00
|
|
|
diffPosition: diffPositionByLineCode[line.line_code],
|
2018-10-25 07:04:34 -04:00
|
|
|
latestDiff,
|
|
|
|
});
|
2018-12-12 14:29:48 -05:00
|
|
|
const mapDiscussions = (line, extraCheck = () => true) => ({
|
|
|
|
...line,
|
|
|
|
discussions: extraCheck()
|
|
|
|
? line.discussions
|
|
|
|
.filter(() => !line.discussions.some(({ id }) => discussion.id === id))
|
|
|
|
.concat(lineCheck(line) ? discussion : line.discussions)
|
|
|
|
: [],
|
|
|
|
});
|
2018-10-25 07:04:34 -04:00
|
|
|
|
|
|
|
state.diffFiles = state.diffFiles.map(diffFile => {
|
2018-11-09 14:48:41 -05:00
|
|
|
if (diffFile.file_hash === fileHash) {
|
2018-10-25 07:04:34 -04:00
|
|
|
const file = { ...diffFile };
|
|
|
|
|
2018-11-09 14:48:41 -05:00
|
|
|
if (file.highlighted_diff_lines) {
|
2018-12-12 14:29:48 -05:00
|
|
|
file.highlighted_diff_lines = file.highlighted_diff_lines.map(line =>
|
|
|
|
mapDiscussions(line),
|
|
|
|
);
|
2018-09-07 18:24:47 -04:00
|
|
|
}
|
2018-09-07 11:13:11 -04:00
|
|
|
|
2018-11-09 14:48:41 -05:00
|
|
|
if (file.parallel_diff_lines) {
|
|
|
|
file.parallel_diff_lines = file.parallel_diff_lines.map(line => {
|
2018-10-25 07:04:34 -04:00
|
|
|
const left = line.left && lineCheck(line.left);
|
|
|
|
const right = line.right && lineCheck(line.right);
|
|
|
|
|
|
|
|
if (left || right) {
|
|
|
|
return {
|
2018-12-13 03:28:13 -05:00
|
|
|
left: line.left ? mapDiscussions(line.left) : null,
|
|
|
|
right: line.right ? mapDiscussions(line.right, () => !left) : null,
|
2018-10-25 07:04:34 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return line;
|
2018-09-07 18:24:47 -04:00
|
|
|
});
|
|
|
|
}
|
2018-10-19 09:09:16 -04:00
|
|
|
|
2018-11-09 14:48:41 -05:00
|
|
|
if (!file.parallel_diff_lines || !file.highlighted_diff_lines) {
|
2018-12-08 02:19:03 -05:00
|
|
|
file.discussions = (file.discussions || []).concat(discussion);
|
2018-10-25 07:04:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return file;
|
2018-10-19 09:09:16 -04:00
|
|
|
}
|
2018-10-25 07:04:34 -04:00
|
|
|
|
|
|
|
return diffFile;
|
2018-10-19 09:09:16 -04:00
|
|
|
});
|
2018-09-05 04:28:49 -04:00
|
|
|
},
|
|
|
|
|
2018-12-12 14:29:48 -05:00
|
|
|
[types.REMOVE_LINE_DISCUSSIONS_FOR_FILE](state, { fileHash, lineCode }) {
|
2018-11-09 14:48:41 -05:00
|
|
|
const selectedFile = state.diffFiles.find(f => f.file_hash === fileHash);
|
2018-09-07 18:24:47 -04:00
|
|
|
if (selectedFile) {
|
2018-11-09 14:48:41 -05:00
|
|
|
if (selectedFile.parallel_diff_lines) {
|
|
|
|
const targetLine = selectedFile.parallel_diff_lines.find(
|
2018-10-18 03:50:38 -04:00
|
|
|
line =>
|
2018-11-09 14:48:41 -05:00
|
|
|
(line.left && line.left.line_code === lineCode) ||
|
|
|
|
(line.right && line.right.line_code === lineCode),
|
2018-10-18 03:50:38 -04:00
|
|
|
);
|
|
|
|
if (targetLine) {
|
2018-11-09 14:48:41 -05:00
|
|
|
const side = targetLine.left && targetLine.left.line_code === lineCode ? 'left' : 'right';
|
2018-10-18 03:50:38 -04:00
|
|
|
|
|
|
|
Object.assign(targetLine[side], {
|
2018-12-12 14:29:48 -05:00
|
|
|
discussions: targetLine[side].discussions.filter(discussion => discussion.notes.length),
|
2018-10-18 03:50:38 -04:00
|
|
|
});
|
|
|
|
}
|
2018-09-07 11:13:11 -04:00
|
|
|
}
|
2018-09-07 18:24:47 -04:00
|
|
|
|
2018-11-09 14:48:41 -05:00
|
|
|
if (selectedFile.highlighted_diff_lines) {
|
|
|
|
const targetInlineLine = selectedFile.highlighted_diff_lines.find(
|
|
|
|
line => line.line_code === lineCode,
|
2018-09-07 18:24:47 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
if (targetInlineLine) {
|
|
|
|
Object.assign(targetInlineLine, {
|
2018-12-12 14:29:48 -05:00
|
|
|
discussions: targetInlineLine.discussions.filter(discussion => discussion.notes.length),
|
2018-09-07 18:24:47 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-10-18 03:50:38 -04:00
|
|
|
|
|
|
|
if (selectedFile.discussions && selectedFile.discussions.length) {
|
|
|
|
selectedFile.discussions = selectedFile.discussions.filter(
|
2018-12-12 14:29:48 -05:00
|
|
|
discussion => discussion.notes.length,
|
2018-10-18 03:50:38 -04:00
|
|
|
);
|
|
|
|
}
|
2018-09-07 11:13:11 -04:00
|
|
|
}
|
2018-09-05 04:28:49 -04:00
|
|
|
},
|
2018-10-03 05:05:43 -04:00
|
|
|
[types.TOGGLE_FOLDER_OPEN](state, path) {
|
|
|
|
state.treeEntries[path].opened = !state.treeEntries[path].opened;
|
|
|
|
},
|
|
|
|
[types.TOGGLE_SHOW_TREE_LIST](state) {
|
|
|
|
state.showTreeList = !state.showTreeList;
|
|
|
|
},
|
|
|
|
[types.UPDATE_CURRENT_DIFF_FILE_ID](state, fileId) {
|
|
|
|
state.currentDiffFileId = fileId;
|
|
|
|
},
|
2018-10-18 03:50:38 -04:00
|
|
|
[types.OPEN_DIFF_FILE_COMMENT_FORM](state, formData) {
|
|
|
|
state.commentForms.push({
|
|
|
|
...formData,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[types.UPDATE_DIFF_FILE_COMMENT_FORM](state, formData) {
|
|
|
|
const { fileHash } = formData;
|
|
|
|
|
|
|
|
state.commentForms = state.commentForms.map(form => {
|
|
|
|
if (form.fileHash === fileHash) {
|
|
|
|
return {
|
|
|
|
...formData,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return form;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[types.CLOSE_DIFF_FILE_COMMENT_FORM](state, fileHash) {
|
|
|
|
state.commentForms = state.commentForms.filter(form => form.fileHash !== fileHash);
|
|
|
|
},
|
2018-11-30 03:48:47 -05:00
|
|
|
[types.SET_HIGHLIGHTED_ROW](state, lineCode) {
|
|
|
|
state.highlightedRow = lineCode;
|
|
|
|
},
|
2018-06-21 08:22:40 -04:00
|
|
|
};
|