All files / app/assets/javascripts/import_entities/import_projects/store mutations.js

96.72% Statements 59/61
83.33% Branches 15/18
96.3% Functions 26/27
98.08% Lines 51/52

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        2x                   2x         1x 12x 12x 4x 4x   4x     4x     12x         2x 2x 2x       1x       8x   8x       6x           6x 6x           6x 6x 6x 1x 1x         6x   6x 3x     6x     2x 2x 2x         1x       1x 1x             2x 2x       1x 1x       1x 1x 1x 1x           1x       2x 2x       1x       2x   2x       1x   1x         1x      
import Vue from 'vue';
import { STATUSES } from '../../constants';
import * as types from './mutation_types';
 
const makeNewImportedProject = (importedProject) => ({
  importSource: {
    id: importedProject.id,
    fullName: importedProject.importSource,
    sanitizedName: importedProject.name,
    providerLink: importedProject.providerLink,
  },
  importedProject: { ...importedProject },
});
 
const makeNewIncompatibleProject = (project) => ({
  importSource: { ...project, incompatible: true },
  importedProject: null,
});
 
const processLegacyEntries = ({ newRepositories, existingRepositories, factory }) => {
  const newEntries = [];
  newRepositories.forEach((project) => {
    const existingProject = existingRepositories.find((p) => p.importSource.id === project.id);
    const importedProjectShape = factory(project);
 
    Iif (existingProject) {
      Object.assign(existingProject, importedProjectShape);
    } else {
      newEntries.push(importedProjectShape);
    }
  });
  return newEntries;
};
 
export default {
  [types.SET_FILTER](state, filter) {
    state.filter = filter;
    state.repositories = [];
    state.pageInfo.page = 0;
  },
 
  [types.REQUEST_REPOS](state) {
    state.isLoadingRepos = true;
  },
 
  [types.RECEIVE_REPOS_SUCCESS](state, repositories) {
    state.isLoadingRepos = false;
 
    if (!Array.isArray(repositories)) {
      // Legacy code path, will be removed when all importers will be switched to new pagination format
      // https://gitlab.com/gitlab-org/gitlab/-/issues/27370#note_379034091
 
      const newImportedProjects = processLegacyEntries({
        newRepositories: repositories.importedProjects,
        existingRepositories: state.repositories,
        factory: makeNewImportedProject,
      });
 
      const incompatibleRepos = repositories.incompatibleRepos ?? [];
      const newIncompatibleProjects = processLegacyEntries({
        newRepositories: incompatibleRepos,
        existingRepositories: state.repositories,
        factory: makeNewIncompatibleProject,
      });
 
      const existingProjects = [...newImportedProjects, ...state.repositories];
      const existingProjectNames = new Set(existingProjects.map((p) => p.importSource.fullName));
      const newProjects = repositories.providerRepos
        .filter((project) => !existingProjectNames.has(project.fullName))
        .map((project) => ({
          importSource: project,
          importedProject: null,
        }));
 
      state.repositories = [...existingProjects, ...newProjects, ...newIncompatibleProjects];
 
      if (incompatibleRepos.length === 0 && repositories.providerRepos.length === 0) {
        state.pageInfo.page -= 1;
      }
 
      return;
    }
 
    state.repositories = [...state.repositories, ...repositories];
    Eif (repositories.length === 0) {
      state.pageInfo.page -= 1;
    }
  },
 
  [types.RECEIVE_REPOS_ERROR](state) {
    state.isLoadingRepos = false;
  },
 
  [types.REQUEST_IMPORT](state, { repoId, importTarget }) {
    const existingRepo = state.repositories.find((r) => r.importSource.id === repoId);
    existingRepo.importedProject = {
      importStatus: STATUSES.SCHEDULING,
      fullPath: `/${importTarget.targetNamespace}/${importTarget.newName}`,
    };
  },
 
  [types.RECEIVE_IMPORT_SUCCESS](state, { importedProject, repoId }) {
    const existingRepo = state.repositories.find((r) => r.importSource.id === repoId);
    existingRepo.importedProject = importedProject;
  },
 
  [types.RECEIVE_IMPORT_ERROR](state, repoId) {
    const existingRepo = state.repositories.find((r) => r.importSource.id === repoId);
    existingRepo.importedProject = null;
  },
 
  [types.RECEIVE_JOBS_SUCCESS](state, updatedProjects) {
    updatedProjects.forEach((updatedProject) => {
      const repo = state.repositories.find((p) => p.importedProject?.id === updatedProject.id);
      Eif (repo?.importedProject) {
        repo.importedProject.importStatus = updatedProject.importStatus;
      }
    });
  },
 
  [types.REQUEST_NAMESPACES](state) {
    state.isLoadingNamespaces = true;
  },
 
  [types.RECEIVE_NAMESPACES_SUCCESS](state, namespaces) {
    state.isLoadingNamespaces = false;
    state.namespaces = namespaces;
  },
 
  [types.RECEIVE_NAMESPACES_ERROR](state) {
    state.isLoadingNamespaces = false;
  },
 
  [types.SET_IMPORT_TARGET](state, { repoId, importTarget }) {
    const existingRepo = state.repositories.find((r) => r.importSource.id === repoId);
 
    if (
      importTarget.targetNamespace === state.defaultTargetNamespace &&
      importTarget.newName === existingRepo.importSource.sanitizedName
    ) {
      Vue.delete(state.customImportTargets, repoId);
    } else {
      Vue.set(state.customImportTargets, repoId, importTarget);
    }
  },
 
  [types.SET_PAGE](state, page) {
    state.pageInfo.page = page;
  },
};