All files / app/assets/javascripts/merge_conflicts/store getters.js

100% Statements 65/65
87.87% Branches 29/33
100% Functions 13/13
100% Lines 58/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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      2x 15x 1x     14x 14x   14x 28x 26x 99x 39x       2x       14x     2x 14x 14x   14x     2x 17x 17x 17x   17x 28x   28x 25x 25x       25x 25x 96x 37x       25x 24x     3x     2x 2x   2x         17x     2x 13x 13x   13x     2x 1x   1x         1x 3x         3x   2x 1x 1x 1x   1x 1x     3x     1x     2x 14x     2x 3x    
import { s__ } from '~/locale';
import { CONFLICT_TYPES, EDIT_RESOLVE_MODE, INTERACTIVE_RESOLVE_MODE } from '../constants';
 
export const getConflictsCount = (state) => {
  if (!state.conflictsData.files.length) {
    return 0;
  }
 
  const { files } = state.conflictsData;
  let count = 0;
 
  files.forEach((file) => {
    if (file.type === CONFLICT_TYPES.TEXT) {
      file.sections.forEach((section) => {
        if (section.conflict) {
          count += 1;
        }
      });
    } else {
      count += 1;
    }
  });
 
  return count;
};
 
export const getConflictsCountText = (state, getters) => {
  const count = getters.getConflictsCount;
  const text = count > 1 ? s__('MergeConflict|conflicts') : s__('MergeConflict|conflict');
 
  return `${count} ${text}`;
};
 
export const isReadyToCommit = (state) => {
  const { files } = state.conflictsData;
  const hasCommitMessage = state.conflictsData.commitMessage.trim().length;
  let unresolved = 0;
 
  for (let i = 0, l = files.length; i < l; i += 1) {
    const file = files[i];
 
    if (file.resolveMode === INTERACTIVE_RESOLVE_MODE) {
      let numberConflicts = 0;
      const resolvedConflicts = Object.keys(file.resolutionData).length;
 
      // We only check for conflicts type 'text'
      // since conflicts `text_editor` canĀ“t be resolved in interactive mode
      Eif (file.type === CONFLICT_TYPES.TEXT) {
        for (let j = 0, k = file.sections.length; j < k; j += 1) {
          if (file.sections[j].conflict) {
            numberConflicts += 1;
          }
        }
 
        if (resolvedConflicts !== numberConflicts) {
          unresolved += 1;
        }
      }
    } else if (file.resolveMode === EDIT_RESOLVE_MODE) {
      // Unlikely to happen since switching to Edit mode saves content automatically.
      // Checking anyway in case the save strategy changes in the future
      Eif (!file.content) {
        unresolved += 1;
        // eslint-disable-next-line no-continue
        continue;
      }
    }
  }
 
  return Boolean(!state.isSubmitting && hasCommitMessage && !unresolved);
};
 
export const getCommitButtonText = (state) => {
  const initial = s__('MergeConflict|Commit to source branch');
  const inProgress = s__('MergeConflict|Committing...');
 
  return state.isSubmitting ? inProgress : initial;
};
 
export const getCommitData = (state) => {
  let commitData = {};
 
  commitData = {
    commit_message: state.conflictsData.commitMessage,
    files: [],
  };
 
  state.conflictsData.files.forEach((file) => {
    const addFile = {
      old_path: file.old_path,
      new_path: file.new_path,
    };
 
    if (file.type === CONFLICT_TYPES.TEXT) {
      // Submit only one data for type of editing
      if (file.resolveMode === INTERACTIVE_RESOLVE_MODE) {
        addFile.sections = file.resolutionData;
      } else Eif (file.resolveMode === EDIT_RESOLVE_MODE) {
        addFile.content = file.content;
      }
    } else Eif (file.type === CONFLICT_TYPES.TEXT_EDITOR) {
      addFile.content = file.content;
    }
 
    commitData.files.push(addFile);
  });
 
  return commitData;
};
 
export const fileTextTypePresent = (state) => {
  return state.conflictsData?.files.some((f) => f.type === CONFLICT_TYPES.TEXT);
};
 
export const getFileIndex = (state) => ({ blobPath }) => {
  return state.conflictsData.files.findIndex((f) => f.blobPath === blobPath);
};