All files / app/assets/javascripts/ide/stores getters.js

91.78% Statements 134/146
98.36% Branches 60/61
85.71% Functions 60/70
92.72% Lines 102/110

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 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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263                                          47x 24x   24x 2x               22x 1x                   21x         122x   47x   47x   47x                                 47x 21x       19x   2x     343x   66x   47x 46x   47x 29x   47x   47x 2x   6x   6x 4x     6x   2x   47x 248x 47x   47x 25x   53x 47x 55x 47x     47x 50x   47x 9x 9x 3x     9x     47x     47x     47x 38x   38x     108x 109x   109x     47x 109x   47x   47x 19x   47x 29x     91x 111x 111x 111x       52x 60x 60x 60x 60x 60x 60x 60x   60x                 105x 110x   57x 58x   47x 19x   47x 27x         47x 56x 56x       56x 1x           55x 24x             31x             47x   47x 49x   47x 15x   15x 16x     15x     47x 57x   53x 64x 64x                        
import Api from '~/api';
import { addNumericSuffix } from '~/ide/utils';
import {
  leftSidebarViews,
  DEFAULT_PERMISSIONS,
  PERMISSION_READ_MR,
  PERMISSION_CREATE_MR,
  PERMISSION_PUSH_CODE,
  PUSH_RULE_REJECT_UNSIGNED_COMMITS,
} from '../constants';
import {
  MSG_CANNOT_PUSH_CODE,
  MSG_CANNOT_PUSH_CODE_SHOULD_FORK,
  MSG_CANNOT_PUSH_CODE_GO_TO_FORK,
  MSG_CANNOT_PUSH_UNSIGNED,
  MSG_CANNOT_PUSH_UNSIGNED_SHORT,
  MSG_FORK,
  MSG_GO_TO_FORK,
} from '../messages';
import { getChangesCountForFiles, filePathMatches } from './utils';
 
const getCannotPushCodeViewModel = (state) => {
  const { ide_path: idePath, fork_path: forkPath } = state.links.forkInfo || {};
 
  if (idePath) {
    return {
      message: MSG_CANNOT_PUSH_CODE_GO_TO_FORK,
      action: {
        href: idePath,
        text: MSG_GO_TO_FORK,
      },
    };
  }
  if (forkPath) {
    return {
      message: MSG_CANNOT_PUSH_CODE_SHOULD_FORK,
      action: {
        href: forkPath,
        isForm: true,
        text: MSG_FORK,
      },
    };
  }
 
  return {
    message: MSG_CANNOT_PUSH_CODE,
  };
};
 
export const activeFile = (state) => state.openFiles.find((file) => file.active) || null;
 
export const addedFiles = (state) => state.changedFiles.filter((f) => f.tempFile);
 
export const modifiedFiles = (state) => state.changedFiles.filter((f) => !f.tempFile);
 
export const projectsWithTrees = (state) =>
  Object.keys(state.projects).map((projectId) => {
    const project = state.projects[projectId];
 
    return {
      ...project,
      branches: Object.keys(project.branches).map((branchId) => {
        const branch = project.branches[branchId];
 
        return {
          ...branch,
          tree: state.trees[branch.treeId],
        };
      }),
    };
  });
 
export const currentMergeRequest = (state) => {
  if (
    state.projects[state.currentProjectId] &&
    state.projects[state.currentProjectId].mergeRequests
  ) {
    return state.projects[state.currentProjectId].mergeRequests[state.currentMergeRequestId];
  }
  return null;
};
 
export const findProject = (state) => (projectId) => state.projects[projectId];
 
export const currentProject = (state, getters) => getters.findProject(state.currentProjectId);
 
export const emptyRepo = (state) =>
  state.projects[state.currentProjectId] && state.projects[state.currentProjectId].empty_repo;
 
export const currentTree = (state) =>
  state.trees[`${state.currentProjectId}/${state.currentBranchId}`];
 
export const hasMergeRequest = (state) => Boolean(state.currentMergeRequestId);
 
export const allBlobs = (state) =>
  Object.keys(state.entries)
    .reduce((acc, key) => {
      const entry = state.entries[key];
 
      if (entry.type === 'blob') {
        acc.push(entry);
      }
 
      return acc;
    }, [])
    .sort((a, b) => b.lastOpenedAt - a.lastOpenedAt);
 
export const getChangedFile = (state) => (path) => state.changedFiles.find((f) => f.path === path);
export const getStagedFile = (state) => (path) => state.stagedFiles.find((f) => f.path === path);
export const getOpenFile = (state) => (path) => state.openFiles.find((f) => f.path === path);
 
export const lastOpenedFile = (state) =>
  [...state.changedFiles, ...state.stagedFiles].sort((a, b) => b.lastOpenedAt - a.lastOpenedAt)[0];
 
export const isEditModeActive = (state) => state.currentActivityView === leftSidebarViews.edit.name;
export const isCommitModeActive = (state) =>
  state.currentActivityView === leftSidebarViews.commit.name;
export const isReviewModeActive = (state) =>
  state.currentActivityView === leftSidebarViews.review.name;
 
export const someUncommittedChanges = (state) =>
  Boolean(state.changedFiles.length || state.stagedFiles.length);
 
export const getChangesInFolder = (state) => (path) => {
  const changedFilesCount = state.changedFiles.filter((f) => filePathMatches(f.path, path)).length;
  const stagedFilesCount = state.stagedFiles.filter(
    (f) => filePathMatches(f.path, path) && !getChangedFile(state)(f.path),
  ).length;
 
  return changedFilesCount + stagedFilesCount;
};
 
export const getUnstagedFilesCountForPath = (state) => (path) =>
  getChangesCountForFiles(state.changedFiles, path);
 
export const getStagedFilesCountForPath = (state) => (path) =>
  getChangesCountForFiles(state.stagedFiles, path);
 
export const lastCommit = (state, getters) => {
  const branch = getters.currentProject && getters.currentBranch;
 
  return branch ? branch.commit : null;
};
 
export const findBranch = (state, getters) => (projectId, branchId) => {
  const project = getters.findProject(projectId);
 
  return project && project.branches[branchId];
};
 
export const currentBranch = (state, getters) =>
  getters.findBranch(state.currentProjectId, state.currentBranchId);
 
export const branchName = (_state, getters) => getters.currentBranch && getters.currentBranch.name;
 
export const isOnDefaultBranch = (_state, getters) =>
  getters.currentProject && getters.currentProject.default_branch === getters.branchName;
 
export const canPushToBranch = (_state, getters) => {
  return Boolean(getters.currentBranch ? getters.currentBranch.can_push : getters.canPushCode);
};
 
export const isFileDeletedAndReadded = (state, getters) => (path) => {
  const stagedFile = getters.getStagedFile(path);
  const file = state.entries[path];
  return Boolean(stagedFile && stagedFile.deleted && file.tempFile);
};
 
// checks if any diff exists in the staged or unstaged changes for this path
export const getDiffInfo = (state, getters) => (path) => {
  const stagedFile = getters.getStagedFile(path);
  const file = state.entries[path];
  const renamed = file.prevPath ? file.path !== file.prevPath : false;
  const deletedAndReadded = getters.isFileDeletedAndReadded(path);
  const deleted = deletedAndReadded ? false : file.deleted;
  const tempFile = deletedAndReadded ? false : file.tempFile;
  const changed = file.content !== (deletedAndReadded ? stagedFile.raw : file.raw);
 
  return {
    exists: changed || renamed || deleted || tempFile,
    changed,
    renamed,
    deleted,
    tempFile,
  };
};
 
export const findProjectPermissions = (state, getters) => (projectId) =>
  getters.findProject(projectId)?.userPermissions || DEFAULT_PERMISSIONS;
 
export const findPushRules = (state, getters) => (projectId) =>
  getters.findProject(projectId)?.pushRules || {};
 
export const canReadMergeRequests = (state, getters) =>
  Boolean(getters.findProjectPermissions(state.currentProjectId)[PERMISSION_READ_MR]);
 
export const canCreateMergeRequests = (state, getters) =>
  Boolean(getters.findProjectPermissions(state.currentProjectId)[PERMISSION_CREATE_MR]);
 
/**
 * Returns an object with `isAllowed` and `message` based on why the user cant push code
 */
export const canPushCodeStatus = (state, getters) => {
  const canPushCode = getters.findProjectPermissions(state.currentProjectId)[PERMISSION_PUSH_CODE];
  const rejectUnsignedCommits = getters.findPushRules(state.currentProjectId)[
    PUSH_RULE_REJECT_UNSIGNED_COMMITS
  ];
 
  if (window.gon?.features?.rejectUnsignedCommitsByGitlab && rejectUnsignedCommits) {
    return {
      isAllowed: false,
      message: MSG_CANNOT_PUSH_UNSIGNED,
      messageShort: MSG_CANNOT_PUSH_UNSIGNED_SHORT,
    };
  }
  if (!canPushCode) {
    return {
      isAllowed: false,
      messageShort: MSG_CANNOT_PUSH_CODE,
      ...getCannotPushCodeViewModel(state),
    };
  }
 
  return {
    isAllowed: true,
    message: '',
    messageShort: '',
  };
};
 
export const canPushCode = (state, getters) => getters.canPushCodeStatus.isAllowed;
 
export const entryExists = (state) => (path) =>
  Boolean(state.entries[path] && !state.entries[path].deleted);
 
export const getAvailableFileName = (state, getters) => (path) => {
  let newPath = path;
 
  while (getters.entryExists(newPath)) {
    newPath = addNumericSuffix(newPath);
  }
 
  return newPath;
};
 
export const getUrlForPath = (state) => (path) =>
  `/project/${state.currentProjectId}/tree/${state.currentBranchId}/-/${path}/`;
 
export const getJsonSchemaForPath = (state, getters) => (path) => {
  const [namespace, ...project] = state.currentProjectId.split('/');
  return {
    uri:
      // eslint-disable-next-line no-restricted-globals
      location.origin +
      Api.buildUrl(Api.projectFileSchemaPath)
        .replace(':namespace_path', namespace)
        .replace(':project_path', project.join('/'))
        .replace(':ref', getters.currentBranch?.commit.id || state.currentBranchId)
        .replace(':filename', path),
    fileMatch: [`*${path}`],
  };
};