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

95.31% Statements 61/64
80% Branches 16/20
92.85% Functions 26/28
98.18% Lines 54/55

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        2x         1x 6x 6x 2x 2x   2x     2x     6x         4x 4x 4x                 1x       8x   8x       6x 6x           6x 6x 6x 3x   3x 2x     3x           6x   6x 1x     6x     2x 2x 2x         1x       1x 1x             2x 2x       1x 1x       2x 2x 2x 2x                   1x 1x       2x   2x       1x 1x 1x   1x         1x       1x 1x       1x      
import Vue from 'vue';
import { STATUSES } from '../../constants';
import * as types from './mutation_types';
 
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, newFilter) {
    state.filter = { ...state.filter, ...newFilter };
    state.repositories = [];
    state.pageInfo = {
      page: 0,
      startCursor: null,
      endCursor: null,
      hasNextPage: true,
    };
  },
 
  [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 incompatibleRepos = repositories.incompatibleRepos ?? [];
      const newIncompatibleProjects = processLegacyEntries({
        newRepositories: incompatibleRepos,
        existingRepositories: state.repositories,
        factory: makeNewIncompatibleProject,
      });
 
      const existingProjectNames = new Set(state.repositories.map((p) => p.importSource.fullName));
      const importedProjects = [...(repositories.importedProjects ?? [])].reverse();
      const newProjects = repositories.providerRepos
        .filter((project) => !existingProjectNames.has(project.fullName))
        .map((project) => {
          const importedProject = importedProjects.find(
            (p) => p.providerLink === project.providerLink,
          );
 
          return {
            importSource: project,
            importedProject,
          };
        });
 
      state.repositories = [...state.repositories, ...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.importStatus = STATUSES.FAILED;
  },
 
  [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 = {
          ...repo.importedProject,
          stats: updatedProject.stats,
          importStatus: updatedProject.importStatus,
        };
      }
    });
  },
 
  [types.CANCEL_IMPORT_SUCCESS](state, { repoId }) {
    const existingRepo = state.repositories.find((r) => r.importSource.id === repoId);
    existingRepo.importedProject.importStatus = STATUSES.CANCELED;
  },
 
  [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
    ) {
      const importsCopy = { ...state.customImportTargets };
      delete importsCopy[repoId];
      state.customImportTargets = importsCopy;
    } else {
      Vue.set(state.customImportTargets, repoId, importTarget);
    }
  },
 
  [types.SET_PAGE](state, page) {
    state.pageInfo.page = page;
  },
 
  [types.SET_PAGE_CURSORS](state, pageInfo) {
    const { startCursor, endCursor, hasNextPage } = pageInfo;
    state.pageInfo = { ...state.pageInfo, startCursor, endCursor, hasNextPage };
  },
 
  [types.SET_HAS_NEXT_PAGE](state, hasNextPage) {
    state.pageInfo.hasNextPage = hasNextPage;
  },
};